diff --git a/backend/charts/api/templates/db-migrations-env-vars.yaml b/backend/charts/api/templates/db-migrations-env-vars.yaml index 9055d1d355..c6857dc349 100644 --- a/backend/charts/api/templates/db-migrations-env-vars.yaml +++ b/backend/charts/api/templates/db-migrations-env-vars.yaml @@ -15,5 +15,6 @@ stringData: DB_SSL_DISABLE: {{ .Values.migrations.db.disableSsl | quote}} DB_SCHEMA_DIR: {{ .Values.migrations.db.schemaDir }} - + DB_MIGRATIONS_TABLE: {{ .Values.migrations.db.migrationsTableName }} + DB_MIGRATIONS_TABLE_QUOTED: {{ .Values.migrations.db.migrationsTableQuoted | quote }} diff --git a/backend/charts/api/values.yaml b/backend/charts/api/values.yaml index 1caa1f1138..2d5346e160 100644 --- a/backend/charts/api/values.yaml +++ b/backend/charts/api/values.yaml @@ -36,6 +36,8 @@ migrations: password: disableSsl: false schemaDir: /app/migrations + migrationsTableName: neosync_api_schema_migrations + migrationsTableQuoted: true istio: enabled: false diff --git a/backend/internal/cmds/mgmt/migrate/up/up.go b/backend/internal/cmds/mgmt/migrate/up/up.go index bcf558a741..f6d4644c09 100644 --- a/backend/internal/cmds/mgmt/migrate/up/up.go +++ b/backend/internal/cmds/mgmt/migrate/up/up.go @@ -171,12 +171,26 @@ func getDbUrl() (string, error) { sslMode = "disable" } + var migrationsTable *string + if viper.IsSet("DB_MIGRATIONS_TABLE") { + table := viper.GetString("DB_MIGRATIONS_TABLE") + migrationsTable = &table + } + + var tableQuoted *bool + if viper.IsSet("DB_MIGRATIONS_TABLE_QUOTED") { + isQuoted := viper.GetBool("DB_MIGRATIONS_TABLE_QUOTED") + tableQuoted = &isQuoted + } + return nucleusdb.GetDbUrl(&nucleusdb.ConnectConfig{ - Host: dbHost, - Port: dbPort, - Database: dbName, - User: dbUser, - Pass: dbPass, - SslMode: &sslMode, + Host: dbHost, + Port: dbPort, + Database: dbName, + User: dbUser, + Pass: dbPass, + SslMode: &sslMode, + MigrationsTableName: migrationsTable, + MigrationsTableQuoted: tableQuoted, }), nil } diff --git a/backend/internal/nucleusdb/db.go b/backend/internal/nucleusdb/db.go index 873914e8b3..0dd57754fd 100644 --- a/backend/internal/nucleusdb/db.go +++ b/backend/internal/nucleusdb/db.go @@ -31,12 +31,14 @@ type NucleusDb struct { } type ConnectConfig struct { - Host string - Port int - Database string - User string - Pass string - SslMode *string + Host string + Port int + Database string + User string + Pass string + SslMode *string + MigrationsTableName *string + MigrationsTableQuoted *bool } func New(db DBTX, q db_queries.Querier) *NucleusDb { diff --git a/backend/internal/nucleusdb/util.go b/backend/internal/nucleusdb/util.go index 922677185e..2c4dfd50ba 100644 --- a/backend/internal/nucleusdb/util.go +++ b/backend/internal/nucleusdb/util.go @@ -4,6 +4,8 @@ import ( "database/sql" "errors" "fmt" + "net/url" + "strconv" "time" "github.com/jackc/pgx/v5" @@ -44,8 +46,18 @@ func GetDbUrl(cfg *ConnectConfig) string { cfg.Port, cfg.Database, ) + pgOpts := url.Values{} if cfg.SslMode != nil && *cfg.SslMode != "" { - dburl = fmt.Sprintf("%s?sslmode=%s", dburl, *cfg.SslMode) + pgOpts["sslmode"] = []string{*cfg.SslMode} + } + if cfg.MigrationsTableName != nil && *cfg.MigrationsTableName != "" { + pgOpts["x-migrations-table"] = []string{*cfg.MigrationsTableName} + } + if cfg.MigrationsTableQuoted != nil { + pgOpts["x-migrations-table-quoted"] = []string{strconv.FormatBool(*cfg.MigrationsTableQuoted)} + } + if len(pgOpts) > 0 { + return fmt.Sprintf("%s?%s", dburl, pgOpts.Encode()) } return dburl } diff --git a/backend/internal/nucleusdb/util_test.go b/backend/internal/nucleusdb/util_test.go index 63eb4a14fc..54e9497d25 100644 --- a/backend/internal/nucleusdb/util_test.go +++ b/backend/internal/nucleusdb/util_test.go @@ -57,6 +57,23 @@ func Test_GetDbUrl(t *testing.T) { }), "postgres://myuser:mypass@localhost:5432/neosync?sslmode=disable", ) + + migrationsTableName := "test-table-name" + migrationsTableQuoted := true + assert.Equal( + t, + GetDbUrl(&ConnectConfig{ + User: "myuser", + Pass: "mypass", + Host: "localhost", + Port: 5432, + Database: "neosync", + SslMode: &sslmode, + MigrationsTableName: &migrationsTableName, + MigrationsTableQuoted: &migrationsTableQuoted, + }), + "postgres://myuser:mypass@localhost:5432/neosync?sslmode=disable&x-migrations-table=test-table-name&x-migrations-table-quoted=true", + ) } func Test_ToUuid(t *testing.T) { diff --git a/compose.yml b/compose.yml index c98f9a22f1..15f03e769d 100644 --- a/compose.yml +++ b/compose.yml @@ -67,6 +67,8 @@ services: - DB_USER=postgres - DB_PASS=foofar - DB_SSL_DISABLE=true + - DB_MIGRATIONS_TABLE=neosync_api_schema_migrations + - DB_MIGRATIONS_TABLE_QUOTED=true - AUTH_ENABLED=false