Skip to content

Commit

Permalink
feat: override migration flag
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Aug 19, 2024
1 parent 5bcf16d commit 9960837
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
16 changes: 14 additions & 2 deletions api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 "<nil>"
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
19 changes: 15 additions & 4 deletions start.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,32 @@ 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")
flags.StringVar(&DefaultConfig.LogLevel, "db-log-level", "error", "Set gorm logging level. trace, debug & info")
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
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 9960837

Please sign in to comment.