Skip to content

Commit 542166f

Browse files
Construct version schema name in Roll.StartDDLOperations without DB lookup (#912)
Change how the `Roll.StartDDLOperations` constructs the name of the version schema that will be created by the migration so that it doesn't require a lookup in the `pgroll.migrations` table. There is no need to look it up in the state db; it's determied by the`VersionSchema` of the migration being started and the schema name it's being applied to. Related to #872
1 parent 1fe1af8 commit 542166f

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

pkg/roll/execute.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,11 @@ func (m *Roll) StartDDLOperations(ctx context.Context, migration *migrations.Mig
8585
defer m.migrationHooks.AfterStartDDL(m)
8686
}
8787

88-
// Get the name of the latest version schema
89-
// This is created after ops have started but ops need to know what it will
90-
// be called in order to set up any up/down triggers
91-
latestVersion, err := m.state.LatestVersion(ctx, m.schema)
92-
if err != nil {
93-
return nil, fmt.Errorf("unable to get name of latest version: %w", err)
94-
}
95-
latestSchema := VersionedSchemaName(m.schema, *latestVersion)
88+
// Construct the full name of the version schema that will be created by this
89+
// migration. The version schema is created after operations have completed
90+
// but ops need to know the name in advance in order to construct backfill
91+
// triggers.
92+
versionSchemaName := VersionedSchemaName(m.schema, migration.VersionSchemaName())
9693

9794
// Reread the latest schema as validation may have updated the schema object
9895
// in memory.
@@ -104,7 +101,7 @@ func (m *Roll) StartDDLOperations(ctx context.Context, migration *migrations.Mig
104101
// execute operations
105102
var tablesToBackfill []*schema.Table
106103
for _, op := range migration.Operations {
107-
table, err := op.Start(ctx, m.logger, m.pgConn, latestSchema, newSchema)
104+
table, err := op.Start(ctx, m.logger, m.pgConn, versionSchemaName, newSchema)
108105
if err != nil {
109106
errRollback := m.Rollback(ctx)
110107
if errRollback != nil {

0 commit comments

Comments
 (0)