Skip to content

Commit 146d740

Browse files
Ignore all DDL executed by pgroll (#890)
Set a session-level setting `pgroll.internal` to `TRUE` on connections used by `pgroll` (`pgroll` uses two connections, one to execute migrations and one for its internal state). Use the `pgroll.internal` setting to prevent schema changes made by `pgroll` itself being captured by the `raw_migration()` event trigger function. DDL changes made in schema `s` are already ignored by `raw_migration()` if there is an active migration period in progress for schema `s`. The change in this commit ensures that *all* DDL changes in *all* schema at *all* times made by `pgroll` are ignored by `raw_migration()`, including version schema creation and the creation of views within those schema. This reverts PR #823 which removed a `pgroll.internal` setting that was set using `SET LOCAL` outside a transaction block. That PR assumed that setting `pgroll.internal` with `SET LOCAL` outside a transaction had no effect, but it did have an effect (the setting was set to an empty string, which was sufficient given how it was used in`raw_migration()`). This PR uses the same setting but sets it with `SET` (instead of `SET LOCAL`) to make it more obvious what is happening. Add a test to verify that version schema creation does not create inferred migrations.
1 parent f672053 commit 146d740

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

pkg/roll/execute_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,37 @@ func TestStartFailsWithExistingSchemaWithoutHistory(t *testing.T) {
880880
})
881881
}
882882

883+
func TestVersionSchemaCreationIsNotCapturedAsAnInferredMigration(t *testing.T) {
884+
t.Parallel()
885+
886+
testutils.WithMigratorAndConnectionToContainer(t, func(m *roll.Roll, db *sql.DB) {
887+
ctx := context.Background()
888+
889+
// Apply a migration
890+
err := m.Start(ctx, &migrations.Migration{
891+
Name: "01_create_table",
892+
Operations: migrations.Operations{createTableOp("new_table")},
893+
}, backfill.NewConfig())
894+
require.NoError(t, err)
895+
err = m.Complete(ctx)
896+
require.NoError(t, err)
897+
898+
// Ensure that the version schema has been created
899+
versionSchema := roll.VersionedSchemaName("public", "01_create_table")
900+
require.True(t, schemaExists(t, db, versionSchema))
901+
902+
// Get the schema history **for the version schema**
903+
hist, err := m.State().SchemaHistory(ctx, versionSchema)
904+
require.NoError(t, err)
905+
906+
// Ensure that there are no inferred migrations recorded for the version
907+
// schema; the DDL statements that `pgroll` executes to create the version
908+
// schema and the views inside it should be ignored by the event trigger
909+
// that captures inferred migrations.
910+
require.Len(t, hist, 0)
911+
})
912+
}
913+
883914
func addColumnOp(tableName string) *migrations.OpAddColumn {
884915
return &migrations.OpAddColumn{
885916
Table: tableName,

pkg/roll/roll.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ func setupConn(ctx context.Context, pgURL, schema string, options options) (*sql
105105
return nil, err
106106
}
107107

108+
_, err = conn.ExecContext(ctx, "SET pgroll.internal TO 'TRUE'")
109+
if err != nil {
110+
return nil, fmt.Errorf("unable to set pgroll.internal to true: %w", err)
111+
}
112+
108113
if options.lockTimeoutMs > 0 {
109114
_, err = conn.ExecContext(ctx, fmt.Sprintf("SET lock_timeout to '%dms'", options.lockTimeoutMs))
110115
if err != nil {

pkg/state/init.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,10 @@ DECLARE
331331
schemaname text;
332332
migration_id text;
333333
BEGIN
334+
-- Ignore schema changes made by pgroll
335+
IF (pg_catalog.current_setting('pgroll.internal', TRUE) = 'TRUE') THEN
336+
RETURN;
337+
END IF;
334338
IF tg_event = 'sql_drop' AND tg_tag = 'DROP SCHEMA' THEN
335339
-- Take the schema name from the drop schema command
336340
SELECT

pkg/state/state.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ func New(ctx context.Context, pgURL, stateSchema string, opts ...StateOpt) (*Sta
4747
return nil, err
4848
}
4949

50+
_, err = conn.ExecContext(ctx, "SET pgroll.internal TO 'TRUE'")
51+
if err != nil {
52+
return nil, fmt.Errorf("unable to set pgroll.internal to true: %w", err)
53+
}
54+
5055
st := &State{
5156
pgConn: conn,
5257
pgrollVersion: "development",

0 commit comments

Comments
 (0)