Skip to content

Commit

Permalink
chore: create seed table separately
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Oct 7, 2024
1 parent b3efd48 commit c805fc6
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 13 deletions.
3 changes: 2 additions & 1 deletion internal/db/push/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ func TestPushAll(t *testing.T) {
Reply("SELECT 0")
helper.MockMigrationHistory(conn).
Query(migration.INSERT_MIGRATION_VERSION, "0", "test", nil).
Reply("INSERT 0 1").
Reply("INSERT 0 1")
helper.MockSeedHistory(conn).
Query(migration.UPSERT_SEED_FILE, seedPath, digest).
ReplyError(pgerrcode.NotNullViolation, `null value in column "hash" of relation "seed_files"`)
// Run test
Expand Down
5 changes: 4 additions & 1 deletion internal/link/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,10 @@ func linkDatabase(ctx context.Context, config pgconn.Config, options ...func(*pg
defer conn.Close(context.Background())
updatePostgresConfig(conn)
// If `schema_migrations` doesn't exist on the remote database, create it.
return migration.CreateMigrationTable(ctx, conn)
if err := migration.CreateMigrationTable(ctx, conn); err != nil {
return err
}
return migration.CreateSeedTable(ctx, conn)
}

func linkDatabaseVersion(ctx context.Context, projectRef string, fsys afero.Fs) error {
Expand Down
6 changes: 4 additions & 2 deletions internal/link/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestLinkCommand(t *testing.T) {
conn := pgtest.NewConn()
defer conn.Close(t)
helper.MockMigrationHistory(conn)
helper.MockSeedHistory(conn)
// Flush pending mocks after test execution
defer gock.OffAll()
gock.New(utils.DefaultApiHost).
Expand Down Expand Up @@ -279,6 +280,7 @@ func TestLinkDatabase(t *testing.T) {
})
defer conn.Close(t)
helper.MockMigrationHistory(conn)
helper.MockSeedHistory(conn)
// Run test
err := linkDatabase(context.Background(), dbConfig, conn.Intercept)
// Check error
Expand All @@ -294,6 +296,7 @@ func TestLinkDatabase(t *testing.T) {
})
defer conn.Close(t)
helper.MockMigrationHistory(conn)
helper.MockSeedHistory(conn)
// Run test
err := linkDatabase(context.Background(), dbConfig, conn.Intercept)
// Check error
Expand All @@ -313,8 +316,7 @@ func TestLinkDatabase(t *testing.T) {
Query(migration.CREATE_VERSION_TABLE).
ReplyError(pgerrcode.InsufficientPrivilege, "permission denied for relation supabase_migrations").
Query(migration.ADD_STATEMENTS_COLUMN).
Query(migration.ADD_NAME_COLUMN).
Query(migration.CREATE_SEED_TABLE)
Query(migration.ADD_NAME_COLUMN)
// Run test
err := linkDatabase(context.Background(), dbConfig, conn.Intercept)
// Check error
Expand Down
9 changes: 8 additions & 1 deletion internal/testing/helper/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ func MockMigrationHistory(conn *pgtest.MockConn) *pgtest.MockConn {
Query(migration.ADD_STATEMENTS_COLUMN).
Reply("ALTER TABLE").
Query(migration.ADD_NAME_COLUMN).
Reply("ALTER TABLE").
Reply("ALTER TABLE")
return conn
}

func MockSeedHistory(conn *pgtest.MockConn) *pgtest.MockConn {
conn.Query(migration.SET_LOCK_TIMEOUT).
Query(migration.CREATE_VERSION_SCHEMA).
Reply("CREATE SCHEMA").
Query(migration.CREATE_SEED_TABLE).
Reply("CREATE TABLE")
return conn
Expand Down
7 changes: 2 additions & 5 deletions pkg/migration/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ func TestApplyMigrations(t *testing.T) {
Query(CREATE_VERSION_TABLE).
ReplyError(pgerrcode.InsufficientPrivilege, "permission denied for relation supabase_migrations").
Query(ADD_STATEMENTS_COLUMN).
Query(ADD_NAME_COLUMN).
Query(CREATE_SEED_TABLE)
Query(ADD_NAME_COLUMN)
// Run test
err := ApplyMigrations(context.Background(), pending, conn.MockClient(t), fsys)
// Check error
Expand Down Expand Up @@ -176,8 +175,6 @@ func mockMigrationHistory(conn *pgtest.MockConn) *pgtest.MockConn {
Query(ADD_STATEMENTS_COLUMN).
Reply("ALTER TABLE").
Query(ADD_NAME_COLUMN).
Reply("ALTER TABLE").
Query(CREATE_SEED_TABLE).
Reply("CREATE TABLE")
Reply("ALTER TABLE")
return conn
}
14 changes: 13 additions & 1 deletion pkg/migration/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func CreateMigrationTable(ctx context.Context, conn *pgx.Conn) error {
batch.ExecParams(CREATE_VERSION_TABLE, nil, nil, nil, nil)
batch.ExecParams(ADD_STATEMENTS_COLUMN, nil, nil, nil, nil)
batch.ExecParams(ADD_NAME_COLUMN, nil, nil, nil, nil)
batch.ExecParams(CREATE_SEED_TABLE, nil, nil, nil, nil)
if _, err := conn.PgConn().ExecBatch(ctx, &batch).ReadAll(); err != nil {
return errors.Errorf("failed to create migration table: %w", err)
}
Expand All @@ -51,6 +50,19 @@ func ReadMigrationTable(ctx context.Context, conn *pgx.Conn) ([]MigrationFile, e
return pgxv5.CollectRows[MigrationFile](rows)
}

func CreateSeedTable(ctx context.Context, conn *pgx.Conn) error {
// This must be run without prepared statements because each statement in the batch depends on
// the previous schema change. The lock timeout will be reset when implicit transaction ends.
batch := pgconn.Batch{}
batch.ExecParams(SET_LOCK_TIMEOUT, nil, nil, nil, nil)
batch.ExecParams(CREATE_VERSION_SCHEMA, nil, nil, nil, nil)
batch.ExecParams(CREATE_SEED_TABLE, nil, nil, nil, nil)
if _, err := conn.PgConn().ExecBatch(ctx, &batch).ReadAll(); err != nil {
return errors.Errorf("failed to create migration table: %w", err)
}
return nil
}

func ReadSeedTable(ctx context.Context, conn *pgx.Conn) ([]SeedFile, error) {
rows, err := conn.Query(ctx, SELECT_SEED_TABLE)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions pkg/migration/seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ func GetPendingSeeds(ctx context.Context, locals []string, conn *pgx.Conn, fsys
}

func SeedData(ctx context.Context, pending []SeedFile, conn *pgx.Conn, fsys fs.FS) error {
if len(pending) > 0 {
if err := CreateSeedTable(ctx, conn); err != nil {
return err
}
}
for _, seed := range pending {
if seed.Dirty {
fmt.Fprintf(os.Stderr, "Updating seed hash to %s...\n", seed.Path)
Expand Down
15 changes: 13 additions & 2 deletions pkg/migration/seed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ func TestSeedData(t *testing.T) {
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
conn.Query(UPSERT_SEED_FILE, seed.Path, seed.Hash).
mockSeedHistory(conn).
Query(UPSERT_SEED_FILE, seed.Path, seed.Hash).
Reply("INSERT 0 1")
// Run test
err := SeedData(context.Background(), []SeedFile{seed}, conn.MockClient(t), testMigrations)
Expand All @@ -118,7 +119,8 @@ func TestSeedData(t *testing.T) {
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
conn.Query(testSeed+`;INSERT INTO supabase_migrations.seed_files(path, hash) VALUES( 'testdata/seed.sql' , '61868484fc0ddca2a2022217629a9fd9a4cf1ca479432046290797d6d40ffcc3' ) ON CONFLICT (path) DO UPDATE SET hash = EXCLUDED.hash`).
mockSeedHistory(conn).
Query(testSeed+`;INSERT INTO supabase_migrations.seed_files(path, hash) VALUES( 'testdata/seed.sql' , '61868484fc0ddca2a2022217629a9fd9a4cf1ca479432046290797d6d40ffcc3' ) ON CONFLICT (path) DO UPDATE SET hash = EXCLUDED.hash`).
ReplyError(pgerrcode.NotNullViolation, `null value in column "age" of relation "employees"`)
// Run test
err := SeedData(context.Background(), []SeedFile{seed}, conn.MockClient(t, func(cc *pgx.ConnConfig) {
Expand All @@ -129,6 +131,15 @@ func TestSeedData(t *testing.T) {
})
}

func mockSeedHistory(conn *pgtest.MockConn) *pgtest.MockConn {
conn.Query(SET_LOCK_TIMEOUT).
Query(CREATE_VERSION_SCHEMA).
Reply("CREATE SCHEMA").
Query(CREATE_SEED_TABLE).
Reply("CREATE TABLE")
return conn
}

//go:embed testdata/1_globals.sql
var testGlobals string

Expand Down

0 comments on commit c805fc6

Please sign in to comment.