supabase-operator/magefiles/migrate.go
Peter Kurfer 12090c913a
Some checks failed
E2E Tests / Run on Ubuntu (push) Failing after 20s
Lint / Run on Ubuntu (push) Failing after 3m45s
Tests / Run on Ubuntu (push) Failing after 16m8s
feat(db): prepare migrations and core CRD
2024-12-13 09:09:14 +01:00

54 lines
879 B
Go

package main
import (
"context"
"errors"
"log/slog"
"os"
"github.com/jackc/pgx/v5"
"code.icb4dc0.de/prskr/supabase-operator/assets/migrations"
)
func Migrate(ctx context.Context) error {
dsn := os.Getenv("DATABASE_URL")
if dsn == "" {
return errors.New("DATABASE_URL is required")
}
conn, err := pgx.Connect(ctx, dsn)
if err != nil {
return err
}
defer conn.Close(ctx)
for s, err := range migrations.InitScripts() {
if err != nil {
return err
}
slog.Info("Running init script", slog.String("file", s.FileName))
_, err = conn.Exec(ctx, s.Content)
if err != nil {
return err
}
}
for s, err := range migrations.MigrationScripts() {
if err != nil {
return err
}
slog.Info("Running migration script", slog.String("file", s.FileName))
_, err = conn.Exec(ctx, s.Content)
if err != nil {
return err
}
}
return nil
}