Skip to content

Commit

Permalink
Nick/neos 304 fix missing migration table name from migrate up command (
Browse files Browse the repository at this point in the history
  • Loading branch information
nickzelei authored Nov 13, 2023
1 parent ce32d2d commit 3066587
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 14 deletions.
3 changes: 2 additions & 1 deletion backend/charts/api/templates/db-migrations-env-vars.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

2 changes: 2 additions & 0 deletions backend/charts/api/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ migrations:
password:
disableSsl: false
schemaDir: /app/migrations
migrationsTableName: neosync_api_schema_migrations
migrationsTableQuoted: true

istio:
enabled: false
Expand Down
26 changes: 20 additions & 6 deletions backend/internal/cmds/mgmt/migrate/up/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 8 additions & 6 deletions backend/internal/nucleusdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 13 additions & 1 deletion backend/internal/nucleusdb/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"database/sql"
"errors"
"fmt"
"net/url"
"strconv"
"time"

"github.com/jackc/pgx/v5"
Expand Down Expand Up @@ -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
}
Expand Down
17 changes: 17 additions & 0 deletions backend/internal/nucleusdb/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 3066587

Please sign in to comment.