From 47ff33afb8523ae4af260d35e3c6a7268f96fdd4 Mon Sep 17 00:00:00 2001 From: Marcus Pasell <3690498+rickyrombo@users.noreply.github.com> Date: Mon, 29 Sep 2025 13:56:49 -0700 Subject: [PATCH 1/3] Run migrations by default when using the 'migrate' command, default to test db for 'make test-schema' --- Makefile | 8 +++++--- ddl/run_migrations.go | 4 ++-- main.go | 8 ++++---- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index dd222029..0fdc9159 100644 --- a/Makefile +++ b/Makefile @@ -41,8 +41,10 @@ test-schema:: @set -a; \ . .env; \ if [ -z "$$writeDbUrl" ]; then \ - echo "writeDbUrl is not set in .env"; \ - exit 1; \ + echo "writeDbUrl is not set in .env - using test db"; \ + writeDbUrl=postgresql://postgres:example@localhost:21300/postgres; \ fi; \ + make migrate; \ adjustedUrl=$$(echo "$$writeDbUrl" | sed 's/localhost/host.docker.internal/g'); \ - docker compose exec db bash -c "pg_dump '$$adjustedUrl' --schema-only --no-owner --no-acl > ./sql/01_schema.sql" + docker compose exec db bash -c "pg_dump '$$adjustedUrl' --schema-only --no-owner --no-acl > ./sql/01_schema.sql"; \ + echo "schema dumped to ./sql/01_schema.sql" diff --git a/ddl/run_migrations.go b/ddl/run_migrations.go index 7125df0b..e8710724 100644 --- a/ddl/run_migrations.go +++ b/ddl/run_migrations.go @@ -8,9 +8,9 @@ import ( "api.audius.co/config" ) -func RunMigrations() error { +func RunMigrations(forceMigration bool) error { fmt.Println("Running migrations...") - if !config.Cfg.RunMigrations { + if !config.Cfg.RunMigrations && !forceMigration { fmt.Println("Skipping migrations. Set env runMigrations=true to run.") return nil } diff --git a/main.go b/main.go index 725ab8b9..50dd6d10 100644 --- a/main.go +++ b/main.go @@ -26,7 +26,7 @@ func main() { switch command { case "server": { - ddl.RunMigrations() + ddl.RunMigrations(false) fmt.Println("Running server...") as := api.NewApiServer(config.Cfg) @@ -34,7 +34,7 @@ func main() { } case "indexer": { - ddl.RunMigrations() + ddl.RunMigrations(false) fmt.Println("Running indexer...") _, err := indexer.NewIndexer(indexer.CoreIndexerConfig{ DbUrl: config.Cfg.WriteDbUrl, @@ -53,7 +53,7 @@ func main() { } case "solana-indexer": { - ddl.RunMigrations() + ddl.RunMigrations(false) fmt.Println("Running solana-indexer...") solanaIndexer := solana_indexer.New(config.Cfg) defer solanaIndexer.Close() @@ -70,7 +70,7 @@ func main() { } case "migrate": { - ddl.RunMigrations() + ddl.RunMigrations(true) os.Exit(0) } default: From 398a039c7b88903b0cdb2caa890b0b7d9964dbf7 Mon Sep 17 00:00:00 2001 From: Marcus Pasell <3690498+rickyrombo@users.noreply.github.com> Date: Mon, 29 Sep 2025 13:58:17 -0700 Subject: [PATCH 2/3] only run migrations for test db --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 0fdc9159..ba988f73 100644 --- a/Makefile +++ b/Makefile @@ -41,10 +41,10 @@ test-schema:: @set -a; \ . .env; \ if [ -z "$$writeDbUrl" ]; then \ - echo "writeDbUrl is not set in .env - using test db"; \ + echo "writeDbUrl is not set in .env - using test db and running migrations"; \ writeDbUrl=postgresql://postgres:example@localhost:21300/postgres; \ + make migrate; \ fi; \ - make migrate; \ adjustedUrl=$$(echo "$$writeDbUrl" | sed 's/localhost/host.docker.internal/g'); \ docker compose exec db bash -c "pg_dump '$$adjustedUrl' --schema-only --no-owner --no-acl > ./sql/01_schema.sql"; \ echo "schema dumped to ./sql/01_schema.sql" From db8b9651b0ac2714301b179130d9981634e61162 Mon Sep 17 00:00:00 2001 From: Marcus Pasell <3690498+rickyrombo@users.noreply.github.com> Date: Mon, 29 Sep 2025 14:06:33 -0700 Subject: [PATCH 3/3] Better migrations runner --- ddl/run_migrations.go | 8 +------- main.go | 13 ++++++++----- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/ddl/run_migrations.go b/ddl/run_migrations.go index e8710724..68c20233 100644 --- a/ddl/run_migrations.go +++ b/ddl/run_migrations.go @@ -8,13 +8,7 @@ import ( "api.audius.co/config" ) -func RunMigrations(forceMigration bool) error { - fmt.Println("Running migrations...") - if !config.Cfg.RunMigrations && !forceMigration { - fmt.Println("Skipping migrations. Set env runMigrations=true to run.") - return nil - } - +func RunMigrations() error { cmd := exec.Command("bash", "pg_migrate.sh") cmd.Dir = "ddl" diff --git a/main.go b/main.go index 50dd6d10..d74b7274 100644 --- a/main.go +++ b/main.go @@ -23,18 +23,22 @@ func main() { command = os.Args[1] } + if !config.Cfg.RunMigrations && command != "migrate" { + fmt.Println("Skipping migrations. Set env runMigrations=true to run.") + } else { + fmt.Println("Running migrations...") + ddl.RunMigrations() + } + switch command { case "server": { - ddl.RunMigrations(false) - fmt.Println("Running server...") as := api.NewApiServer(config.Cfg) as.Serve() } case "indexer": { - ddl.RunMigrations(false) fmt.Println("Running indexer...") _, err := indexer.NewIndexer(indexer.CoreIndexerConfig{ DbUrl: config.Cfg.WriteDbUrl, @@ -53,7 +57,6 @@ func main() { } case "solana-indexer": { - ddl.RunMigrations(false) fmt.Println("Running solana-indexer...") solanaIndexer := solana_indexer.New(config.Cfg) defer solanaIndexer.Close() @@ -70,7 +73,7 @@ func main() { } case "migrate": { - ddl.RunMigrations(true) + // no-op, handled prior to switch/case os.Exit(0) } default: