diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2e955d778d..cd139e4bf3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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. diff --git a/Makefile b/Makefile index 995d5a93e9..8fadefd83d 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/cmd/migrate_cmd.go b/cmd/migrate_cmd.go index 511f53b27d..2790cef84f 100644 --- a/cmd/migrate_cmd.go +++ b/cmd/migrate_cmd.go @@ -2,6 +2,7 @@ package cmd import ( "embed" + "fmt" "net/url" "os" @@ -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) @@ -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() diff --git a/cmd/root_cmd.go b/cmd/root_cmd.go index 1b8607328e..66128086ef 100644 --- a/cmd/root_cmd.go +++ b/cmd/root_cmd.go @@ -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 } diff --git a/hack/migrate.sh b/hack/migrate.sh index 2d1f0e5e84..42ca171d5e 100755 --- a/hack/migrate.sh +++ b/hack/migrate.sh @@ -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}"