supabase-operator/internal/db/migrator.go
Peter Kurfer 647f602c79
Some checks failed
Lint / Run on Ubuntu (push) Failing after 2m58s
E2E Tests / Run on Ubuntu (push) Failing after 4m18s
Tests / Run on Ubuntu (push) Failing after 2m39s
feat: basic functionality implemented
- added Core CRD to manage DB migrations & configuration, PostgREST and
  GoTrue (auth)
- added APIGateway CRD to manage Envoy proxy
- added Dashboard CRD to manage (so far) pg-meta and (soon) studio
  deployments
- implemented basic Envoy control plane based on K8s watcher
2025-01-04 17:07:49 +01:00

51 lines
988 B
Go

package db
import (
"context"
"errors"
"iter"
"code.icb4dc0.de/prskr/supabase-operator/assets/migrations"
"github.com/jackc/pgx/v5"
supabasev1alpha1 "code.icb4dc0.de/prskr/supabase-operator/api/v1alpha1"
)
type Migrator struct {
Conn *pgx.Conn
}
func (m Migrator) ApplyAll(ctx context.Context, status supabasev1alpha1.MigrationStatus, seq iter.Seq2[migrations.Script, error]) (appliedSomething bool, err error) {
for s, err := range seq {
if err != nil {
return false, err
}
if status.IsApplied(s.FileName) {
continue
}
if err := m.Apply(ctx, s.Content); err != nil {
return false, err
}
appliedSomething = true
status.Record(s.FileName)
}
return appliedSomething, nil
}
func (m Migrator) Apply(ctx context.Context, script string) error {
tx, err := m.Conn.BeginTx(ctx, pgx.TxOptions{})
if err != nil {
return err
}
_, err = tx.Exec(ctx, script)
if err != nil {
return errors.Join(err, tx.Rollback(ctx))
}
return tx.Commit(ctx)
}