Skip to content

Commit 1fb32c8

Browse files
Add a global --use-version-schema flag to the pgroll CLI (#919)
Add a global `--use-version-schema` option to the `pgroll` CLI to enable/disable the creation and maintenance of version schema by `pgroll` operations. ## Usage Apply a `migrations/` directory without creating any version schema: ``` $ pgroll migrate migrations/ --complete --use-version-schema=false ``` Start a migration without creating a version schema: ``` $ pgroll start migrations/01_some_migration.yaml --use-version-schema=false ``` The option can also be set by specifying: ```env PGROLL_USE_VERSION_SCHEMA=false ``` as an environment variable. --- Fixes #872
1 parent c74a242 commit 1fb32c8

File tree

8 files changed

+41
-4
lines changed

8 files changed

+41
-4
lines changed

cli-definition.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,11 @@
302302
"description": "Postgres schema to use for the migration",
303303
"default": "public"
304304
},
305+
{
306+
"name": "use-version-schema",
307+
"description": "Create version schemas for each migration",
308+
"default": "true"
309+
},
305310
{
306311
"name": "verbose",
307312
"description": "Enable verbose logging",

cmd/flags/flags.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ func Role() string {
2929
}
3030

3131
func Verbose() bool { return viper.GetBool("VERBOSE") }
32+
33+
func UseVersionSchema() bool {
34+
return viper.GetBool("USE_VERSION_SCHEMA")
35+
}

cmd/latest_migration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func latestMigrationCmd() *cobra.Command {
2424

2525
latestVersion, err := latestMigrationName(ctx, migrationsDir)
2626
if err != nil {
27-
return fmt.Errorf("failed to get latest migration from database: %w", err)
27+
return fmt.Errorf("failed to get latest migration: %w", err)
2828
}
2929

3030
fmt.Println(latestVersion)

cmd/latest_schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func latestSchemaCmd() *cobra.Command {
2525

2626
latestVersion, err := latestVersion(ctx, migrationsDir)
2727
if err != nil {
28-
return fmt.Errorf("failed to get latest migration from database: %w", err)
28+
return fmt.Errorf("failed to get latest version: %w", err)
2929
}
3030

3131
fmt.Println(latestVersion)

cmd/root.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func NewRoll(ctx context.Context) (*roll.Roll, error) {
2424
role := flags.Role()
2525
skipValidation := flags.SkipValidation()
2626
verbose := flags.Verbose()
27+
useVersionSchema := flags.UseVersionSchema()
2728

2829
state, err := state.New(ctx, pgURL, stateSchema, state.WithPgrollVersion(Version))
2930
if err != nil {
@@ -35,6 +36,7 @@ func NewRoll(ctx context.Context) (*roll.Roll, error) {
3536
roll.WithRole(role),
3637
roll.WithSkipValidation(skipValidation),
3738
roll.WithLogging(verbose),
39+
roll.WithVersionSchema(useVersionSchema),
3840
)
3941
}
4042

@@ -85,13 +87,15 @@ func Prepare() *cobra.Command {
8587
rootCmd.PersistentFlags().String("pgroll-schema", "pgroll", "Postgres schema to use for pgroll internal state")
8688
rootCmd.PersistentFlags().Int("lock-timeout", 500, "Postgres lock timeout in milliseconds for pgroll DDL operations")
8789
rootCmd.PersistentFlags().String("role", "", "Optional postgres role to set when executing migrations")
90+
rootCmd.PersistentFlags().Bool("use-version-schema", true, "Create version schemas for each migration")
8891
rootCmd.PersistentFlags().Bool("verbose", false, "Enable verbose logging")
8992

9093
viper.BindPFlag("PG_URL", rootCmd.PersistentFlags().Lookup("postgres-url"))
9194
viper.BindPFlag("SCHEMA", rootCmd.PersistentFlags().Lookup("schema"))
9295
viper.BindPFlag("STATE_SCHEMA", rootCmd.PersistentFlags().Lookup("pgroll-schema"))
9396
viper.BindPFlag("LOCK_TIMEOUT", rootCmd.PersistentFlags().Lookup("lock-timeout"))
9497
viper.BindPFlag("ROLE", rootCmd.PersistentFlags().Lookup("role"))
98+
viper.BindPFlag("USE_VERSION_SCHEMA", rootCmd.PersistentFlags().Lookup("use-version-schema"))
9599
viper.BindPFlag("VERBOSE", rootCmd.PersistentFlags().Lookup("verbose"))
96100

97101
// register subcommands

cmd/start.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,14 @@ func runMigration(ctx context.Context, m *roll.Roll, migration *migrations.Migra
105105
}
106106
}
107107

108-
viewName := roll.VersionedSchemaName(flags.Schema(), migration.VersionSchemaName())
109-
msg := fmt.Sprintf("New version of the schema available under the postgres %q schema", viewName)
108+
var msg string
109+
if m.UseVersionSchema() {
110+
viewName := roll.VersionedSchemaName(flags.Schema(), migration.VersionSchemaName())
111+
msg = fmt.Sprintf("New version of the schema available under the postgres %q schema", viewName)
112+
} else {
113+
msg = fmt.Sprintf("Migration %q started successfully", migration.Name)
114+
}
115+
110116
sp.Success(msg)
111117

112118
return nil

pkg/roll/execute_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ func TestWithUseVersionSchemaOption(t *testing.T) {
8484
require.False(t, exists)
8585
})
8686
})
87+
88+
t.Run("roll instance reports that it does not use version schema", func(t *testing.T) {
89+
testutils.WithMigratorInSchemaAndConnectionToContainerWithOptions(t, "public", opts, func(mig *roll.Roll, db *sql.DB) {
90+
// The roll instance correctly reports tht it does not use version schema
91+
require.False(t, mig.UseVersionSchema())
92+
})
93+
})
94+
95+
t.Run("roll instance reports that it does use version schema", func(t *testing.T) {
96+
testutils.WithMigratorAndConnectionToContainer(t, func(mig *roll.Roll, db *sql.DB) {
97+
// The roll instance correctly reports that it uses version schema
98+
require.True(t, mig.UseVersionSchema())
99+
})
100+
})
87101
}
88102

89103
func TestPreviousVersionIsDroppedAfterMigrationCompletion(t *testing.T) {

pkg/roll/roll.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ func (m *Roll) Schema() string {
148148
return m.schema
149149
}
150150

151+
func (m *Roll) UseVersionSchema() bool {
152+
return !m.disableVersionSchemas
153+
}
154+
151155
// Status returns the current migration status
152156
func (m *Roll) Status(ctx context.Context, schema string) (*state.Status, error) {
153157
return m.state.Status(ctx, schema)

0 commit comments

Comments
 (0)