Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,9 @@ If you need to run any new migrations:
make migrate_test
```

This passes `--verbose` to the `migrate` command, which logs the name of each
migration as it is applied.

## Testing

Currently, we don't use a separate test database, so the same database created when installing Auth to run locally is used.
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ migrate_dev: ## Run database migrations for development.
hack/migrate.sh postgres

migrate_test: ## Run database migrations for test.
hack/migrate.sh postgres
hack/migrate.sh postgres --verbose

test: auth ## Run tests.
go test -failfast $(CHECK_FILES) -coverprofile=coverage.out -coverpkg ./... -p 1 -race -v -count=1
Expand Down
34 changes: 24 additions & 10 deletions cmd/migrate_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"embed"
"fmt"
"net/url"
"os"

Expand All @@ -14,12 +15,26 @@ import (

var EmbeddedMigrations embed.FS

var migrateVerbose bool

var migrateCmd = cobra.Command{
Use: "migrate",
Long: "Migrate database strucutures. This will create new tables and add missing columns and indexes.",
Run: migrate,
}

func popProgressLogger(lvl logging.Level, s string, args ...interface{}) {
if lvl == logging.SQL || lvl == logging.Debug {
return
}
if len(args) > 0 {
s = fmt.Sprintf(s, args...)
}
fmt.Println(s)
}

func popNoopLogger(logging.Level, string, ...interface{}) {}

func migrate(cmd *cobra.Command, args []string) {
globalConfig := loadGlobalConfig(cmd.Context())
u, err := url.Parse(globalConfig.DB.URL)
Expand All @@ -40,16 +55,15 @@ func migrate(cmd *cobra.Command, args []string) {
log.Fatalf("Failed to parse log level: %+v", err)
}
log.SetLevel(level)
if level == logrus.DebugLevel {
// Set to true to display query info
pop.Debug = true
}
if level != logrus.DebugLevel {
var noopLogger = func(lvl logging.Level, s string, args ...interface{}) {
}
// Hide pop migration logging
pop.SetLogger(noopLogger)
}
}

switch {
case log.Level == logrus.DebugLevel:
pop.Debug = true
case migrateVerbose:
pop.SetLogger(popProgressLogger)
case globalConfig.Logging.Level != "":
pop.SetLogger(popNoopLogger)
}

q := u.Query()
Expand Down
1 change: 1 addition & 0 deletions cmd/root_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func RootCommand() *cobra.Command {
rootCmd.AddCommand(&serveCmd, &migrateCmd, &versionCmd, adminCmd())
rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "", "base configuration file to load")
rootCmd.PersistentFlags().StringVarP(&watchDir, "config-dir", "d", "", "directory containing a sorted list of config files to watch for changes")
migrateCmd.Flags().BoolVarP(&migrateVerbose, "verbose", "v", false, "print the name of each migration as it is applied")
return &rootCmd
}

Expand Down
3 changes: 1 addition & 2 deletions hack/migrate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
DB_ENV=$1

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
DATABASE="$DIR/database.yml"

export GOTRUE_DB_DRIVER="postgres"
export GOTRUE_DB_DATABASE_URL="postgres://supabase_auth_admin:root@localhost:5432/$DB_ENV"
export GOTRUE_DB_MIGRATIONS_PATH=$DIR/../migrations

go run main.go migrate -c $DIR/test.env
go run main.go migrate -c "$DIR/test.env" "${@:2}"
Loading