Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cli-definition.json
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@
"description": "Postgres schema to use for the migration",
"default": "public"
},
{
"name": "use-version-schema",
"description": "Create version schemas for each migration",
"default": "true"
},
{
"name": "verbose",
"description": "Enable verbose logging",
Expand Down
4 changes: 4 additions & 0 deletions cmd/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ func Role() string {
}

func Verbose() bool { return viper.GetBool("VERBOSE") }

func UseVersionSchema() bool {
return viper.GetBool("USE_VERSION_SCHEMA")
}
2 changes: 1 addition & 1 deletion cmd/latest_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func latestMigrationCmd() *cobra.Command {

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

fmt.Println(latestVersion)
Expand Down
2 changes: 1 addition & 1 deletion cmd/latest_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func latestSchemaCmd() *cobra.Command {

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

fmt.Println(latestVersion)
Expand Down
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func NewRoll(ctx context.Context) (*roll.Roll, error) {
role := flags.Role()
skipValidation := flags.SkipValidation()
verbose := flags.Verbose()
useVersionSchema := flags.UseVersionSchema()

state, err := state.New(ctx, pgURL, stateSchema, state.WithPgrollVersion(Version))
if err != nil {
Expand All @@ -35,6 +36,7 @@ func NewRoll(ctx context.Context) (*roll.Roll, error) {
roll.WithRole(role),
roll.WithSkipValidation(skipValidation),
roll.WithLogging(verbose),
roll.WithVersionSchema(useVersionSchema),
)
}

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

viper.BindPFlag("PG_URL", rootCmd.PersistentFlags().Lookup("postgres-url"))
viper.BindPFlag("SCHEMA", rootCmd.PersistentFlags().Lookup("schema"))
viper.BindPFlag("STATE_SCHEMA", rootCmd.PersistentFlags().Lookup("pgroll-schema"))
viper.BindPFlag("LOCK_TIMEOUT", rootCmd.PersistentFlags().Lookup("lock-timeout"))
viper.BindPFlag("ROLE", rootCmd.PersistentFlags().Lookup("role"))
viper.BindPFlag("USE_VERSION_SCHEMA", rootCmd.PersistentFlags().Lookup("use-version-schema"))
viper.BindPFlag("VERBOSE", rootCmd.PersistentFlags().Lookup("verbose"))

// register subcommands
Expand Down
10 changes: 8 additions & 2 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,14 @@ func runMigration(ctx context.Context, m *roll.Roll, migration *migrations.Migra
}
}

viewName := roll.VersionedSchemaName(flags.Schema(), migration.VersionSchemaName())
msg := fmt.Sprintf("New version of the schema available under the postgres %q schema", viewName)
var msg string
if m.UseVersionSchema() {
viewName := roll.VersionedSchemaName(flags.Schema(), migration.VersionSchemaName())
msg = fmt.Sprintf("New version of the schema available under the postgres %q schema", viewName)
} else {
msg = fmt.Sprintf("Migration %q started successfully", migration.Name)
}

sp.Success(msg)

return nil
Expand Down
14 changes: 14 additions & 0 deletions pkg/roll/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ func TestWithUseVersionSchemaOption(t *testing.T) {
require.False(t, exists)
})
})

t.Run("roll instance reports that it does not use version schema", func(t *testing.T) {
testutils.WithMigratorInSchemaAndConnectionToContainerWithOptions(t, "public", opts, func(mig *roll.Roll, db *sql.DB) {
// The roll instance correctly reports tht it does not use version schema
require.False(t, mig.UseVersionSchema())
})
})

t.Run("roll instance reports that it does use version schema", func(t *testing.T) {
testutils.WithMigratorAndConnectionToContainer(t, func(mig *roll.Roll, db *sql.DB) {
// The roll instance correctly reports that it uses version schema
require.True(t, mig.UseVersionSchema())
})
})
}

func TestPreviousVersionIsDroppedAfterMigrationCompletion(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/roll/roll.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ func (m *Roll) Schema() string {
return m.schema
}

func (m *Roll) UseVersionSchema() bool {
return !m.disableVersionSchemas
}

// Status returns the current migration status
func (m *Roll) Status(ctx context.Context, schema string) (*state.Status, error) {
return m.state.Status(ctx, schema)
Expand Down
Loading