Skip to content

Commit a75c3f6

Browse files
Remove State.LastSchema method (#909)
Remove the `State.LastSchema` method and replace its usages with `state.ReadSchema` instead. For validating migrations, we always want to validate against the current state of the db schema.
1 parent e7786f5 commit a75c3f6

File tree

3 files changed

+2
-23
lines changed

3 files changed

+2
-23
lines changed

pkg/roll/execute.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func (m *Roll) Validate(ctx context.Context, migration *migrations.Migration) er
1919
if m.skipValidation {
2020
return nil
2121
}
22-
lastSchema, err := m.state.LastSchema(ctx, m.schema)
22+
lastSchema, err := m.state.ReadSchema(ctx, m.schema)
2323
if err != nil {
2424
return err
2525
}

pkg/state/state.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -328,27 +328,6 @@ func (s *State) Start(ctx context.Context, schemaname string, migration *migrati
328328
return err
329329
}
330330

331-
func (s *State) LastSchema(ctx context.Context, schemaname string) (*schema.Schema, error) {
332-
stmt := fmt.Sprintf(`SELECT COALESCE(
333-
(SELECT resulting_schema FROM %[1]s.migrations WHERE schema=$1 AND name=%[1]s.latest_version($1)),
334-
%[1]s.read_schema($1))`,
335-
pq.QuoteIdentifier(s.schema))
336-
337-
var rawSchema string
338-
err := s.pgConn.QueryRowContext(ctx, stmt, schemaname).Scan(&rawSchema)
339-
if err != nil {
340-
return nil, err
341-
}
342-
343-
var unmarshalledSchema schema.Schema
344-
err = json.Unmarshal([]byte(rawSchema), &unmarshalledSchema)
345-
if err != nil {
346-
return nil, fmt.Errorf("unable to unmarshal schema: %w", err)
347-
}
348-
349-
return &unmarshalledSchema, nil
350-
}
351-
352331
// Complete marks a migration as completed
353332
func (s *State) Complete(ctx context.Context, schema, name string) error {
354333
res, err := s.pgConn.ExecContext(ctx, fmt.Sprintf("UPDATE %[1]s.migrations SET done=$1, resulting_schema=(SELECT %[1]s.read_schema($2)) WHERE schema=$2 AND name=$3 AND done=$4", pq.QuoteIdentifier(s.schema)), true, schema, name, false)

pkg/state/state_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestSchemaOptionIsRespected(t *testing.T) {
3838
}
3939

4040
// check that we can retrieve the already existing table
41-
currentSchema, err := state.LastSchema(ctx, "public")
41+
currentSchema, err := state.ReadSchema(ctx, "public")
4242
assert.NoError(t, err)
4343

4444
assert.Equal(t, 1, len(currentSchema.Tables))

0 commit comments

Comments
 (0)