diff --git a/api/config.go b/api/config.go index d0e15604..b10438ea 100644 --- a/api/config.go +++ b/api/config.go @@ -27,7 +27,7 @@ func NewConfig(connection string) Config { type Config struct { Metrics bool ConnectionString, Schema string - RunMigrations bool // Deprecated + RunMigrations bool SkipMigrations bool SkipMigrationFiles []string DisableKubernetes bool @@ -37,6 +37,18 @@ type Config struct { LogName string } +func (t *Config) Migrate() bool { + // We have two flags dictating whether to run migration or not + // depending on whether it's being used on mission-control or (config-db/canary-checker). + // Prioritize SkipMigrations over RunMigrations. + + if t.SkipMigrations { + return false + } + + return t.RunMigrations +} + func PrintableSecret(secret string) string { if len(secret) == 0 { return "" @@ -72,7 +84,7 @@ func (c Config) ReadEnv() Config { } func (c Config) String() string { - s := fmt.Sprintf("migrate=%v log=%v postgrest=(%s)", !c.SkipMigrations, c.LogLevel, c.Postgrest.String()) + s := fmt.Sprintf("migrate=%v log=%v postgrest=(%s)", c.Migrate(), c.LogLevel, c.Postgrest.String()) if pgUrl, err := url.Parse(c.ConnectionString); err == nil { s = fmt.Sprintf("url=%s ", pgUrl.Redacted()) + s } diff --git a/db.go b/db.go index 83d7b441..43eb7cc4 100644 --- a/db.go +++ b/db.go @@ -213,7 +213,7 @@ func SetupDB(config api.Config) (gormDB *gorm.DB, pgxpool *pgxpool.Pool, err err return } - if !config.SkipMigrations { + if config.Migrate() { if err = Migrate(config); err != nil { return } diff --git a/start.go b/start.go index f58dabd9..e2b88667 100644 --- a/start.go +++ b/start.go @@ -20,15 +20,17 @@ import ( "gorm.io/plugin/prometheus" ) -func BindPFlags(flags *pflag.FlagSet) { +type BindFlagOption struct { + SkipMigrationByDefault bool +} + +func BindPFlags(flags *pflag.FlagSet, opt BindFlagOption) { _ = flags.MarkDeprecated("postgrest-anon-role", "Use postgrest-role instead") flags.StringVar(&DefaultConfig.ConnectionString, "db", "DB_URL", "Connection string for the postgres database") flags.StringVar(&DefaultConfig.Schema, "db-schema", "public", "Postgres schema") flags.StringVar(&DefaultConfig.Postgrest.URL, "postgrest-uri", "http://localhost:3000", "URL for the PostgREST instance to use. If localhost is supplied, a PostgREST instance will be started") flags.StringVar(&DefaultConfig.Postgrest.LogLevel, "postgrest-log-level", "info", "PostgREST log level") flags.StringVar(&DefaultConfig.Postgrest.JWTSecret, "postgrest-jwt-secret", "PGRST_JWT_SECRET", "JWT Secret Token for PostgREST") - flags.BoolVar(&DefaultConfig.RunMigrations, "db-migrations", true, "Run database migrations") - flags.BoolVar(&DefaultConfig.SkipMigrations, "skip-migrations", false, "Skip database migrations") flags.BoolVar(&DefaultConfig.Postgrest.Disable, "disable-postgrest", false, "Disable PostgREST. Deprecated (Use --postgrest-uri '' to disable PostgREST)") flags.StringVar(&DefaultConfig.Postgrest.DBRole, "postgrest-role", "postgrest_api", "PostgREST role for authentication connections") flags.IntVar(&DefaultConfig.Postgrest.MaxRows, "postgrest-max-rows", 2000, "A hard limit to the number of rows PostgREST will fetch") @@ -36,7 +38,14 @@ func BindPFlags(flags *pflag.FlagSet) { flags.BoolVar(&DefaultConfig.DisableKubernetes, "disable-kubernetes", false, "Disable Kubernetes integration") flags.BoolVar(&DefaultConfig.Metrics, "db-metrics", false, "Expose db metrics") - _ = flags.MarkDeprecated("db-migrations", "migrations are run by default. Use --skip-migrations to skip migrations.") + if opt.SkipMigrationByDefault { + flags.BoolVar(&DefaultConfig.RunMigrations, "db-migrations", false, "Run database migrations") + DefaultConfig.SkipMigrations = false // pratically disable this flag so RunMigrations dictates migration + } else { + flags.BoolVar(&DefaultConfig.SkipMigrations, "skip-migrations", false, "Skip database migrations") + flags.BoolVar(&DefaultConfig.RunMigrations, "db-migrations", true, "Run database migrations") + _ = flags.MarkDeprecated("db-migrations", "migrations are run by default. Use --skip-migrations to skip migrations.") + } } type StartOption func(config Config) Config @@ -65,12 +74,14 @@ var EnableMetrics = func(config Config) Config { var SkipMigrations = func(config Config) Config { config.SkipMigrations = true + config.RunMigrations = false return config } var ClientOnly = func(config Config) Config { config.Postgrest.Disable = true config.SkipMigrations = true + config.RunMigrations = false return config }