From 1b8494c577b2dd65786d6f7b3c4a28ff3a106dcf Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Mon, 19 Aug 2024 12:55:52 +0545 Subject: [PATCH] fix: run migrations by default --- api/config.go | 4 ++-- db.go | 2 +- start.go | 9 +++++---- tests/setup/common.go | 6 ++++-- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/api/config.go b/api/config.go index 07bd0a51..b8dfa2af 100644 --- a/api/config.go +++ b/api/config.go @@ -27,8 +27,8 @@ func NewConfig(connection string) Config { type Config struct { Metrics bool ConnectionString, Schema string - RunMigrations bool - SkipMigrations bool // Deprecated + RunMigrations bool // Deprecated + SkipMigrations bool SkipMigrationFiles []string DisableKubernetes bool Namespace string diff --git a/db.go b/db.go index 191668ad..83d7b441 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.RunMigrations { + if !config.SkipMigrations { if err = Migrate(config); err != nil { return } diff --git a/start.go b/start.go index b7305167..f58dabd9 100644 --- a/start.go +++ b/start.go @@ -27,7 +27,7 @@ func BindPFlags(flags *pflag.FlagSet) { 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", false, "Run database migrations") + 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") @@ -36,7 +36,7 @@ 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("skip-migrations", "migrations are skipped by default. Use --db-migrations to run migrations.") + _ = flags.MarkDeprecated("db-migrations", "migrations are run by default. Use --skip-migrations to skip migrations.") } type StartOption func(config Config) Config @@ -63,13 +63,14 @@ var EnableMetrics = func(config Config) Config { return config } -var RunMigrations = func(config Config) Config { - config.RunMigrations = true +var SkipMigrations = func(config Config) Config { + config.SkipMigrations = true return config } var ClientOnly = func(config Config) Config { config.Postgrest.Disable = true + config.SkipMigrations = true return config } diff --git a/tests/setup/common.go b/tests/setup/common.go index 079dc9da..bd4021ff 100644 --- a/tests/setup/common.go +++ b/tests/setup/common.go @@ -154,7 +154,7 @@ func BeforeSuiteFn(args ...interface{}) context.Context { }) } - ctx, _, err := duty.Start("test", duty.DisablePostgrest, duty.RunMigrations, duty.WithUrl(PgUrl)) + ctx, _, err := duty.Start("test", duty.DisablePostgrest, duty.SkipMigrations, duty.WithUrl(PgUrl)) if err != nil { panic(err.Error()) } @@ -220,7 +220,9 @@ func NewDB(ctx context.Context, name string) (*context.Context, func(), error) { return nil, nil, err } - newCtx, err := duty.InitDB(api.NewConfig(strings.ReplaceAll(pgUrl, pgDbName, newName))) + newCtx, err := duty.InitDB(api.Config{ + ConnectionString: strings.ReplaceAll(pgUrl, pgDbName, newName), + }) if err != nil { return nil, nil, err }