diff --git a/migrations/README.md b/migrations/README.md index ef3c7215b..e74fe4a53 100644 --- a/migrations/README.md +++ b/migrations/README.md @@ -42,8 +42,6 @@ nix run github:supabase/postgres/mybranch#dbmate-tool -- --version 15 aiming to provide a single source of truth for migrations on the platform that can be depended upon by those components. For more information on goals see [the RFC](https://www.notion.so/supabase/Centralize-SQL-Migrations-cd3847ae027d4f2bba9defb2cc82f69a) - - ## How it was Created Migrations were pulled (in order) from: @@ -53,9 +51,8 @@ Migrations were pulled (in order) from: For compatibility with hosted projects, we include [migrate.sh](migrate.sh) that executes migrations in the same order as ami build: -1. Run all `db/init-scripts` with `postgres` superuser role. -2. Run all `db/migrations` with `supabase_admin` superuser role. -3. Finalize role passwords with `/etc/postgresql.schema.sql` if present. +1. Run all `db/migrations` with `supabase_admin` superuser role. +2. Finalize role passwords with `/etc/postgresql.schema.sql` if present. Additionally, [supabase/postgres](https://github.com/supabase/postgres/blob/develop/ansible/playbook-docker.yml#L9) image contains several migration scripts to configure default extensions. These are run first by docker entrypoint and included in ami by ansible. diff --git a/migrations/db/migrate.sh b/migrations/db/migrate.sh index 0a84d1e6c..40b5cac6b 100755 --- a/migrations/db/migrate.sh +++ b/migrations/db/migrate.sh @@ -38,11 +38,6 @@ begin end if; end \$\$ EOSQL - # run init scripts as postgres user - for sql in "$db"/init-scripts/*.sql; do - echo "$0: running $sql" - psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -f "$sql" - done psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -c "ALTER USER supabase_admin WITH PASSWORD '$PGPASSWORD'" # run migrations as super user - postgres user demoted in post-setup for sql in "$db"/migrations/*.sql; do @@ -54,8 +49,6 @@ else create role postgres superuser login password '$PGPASSWORD'; alter database postgres owner to postgres; EOSQL - # run init scripts as postgres user - DBMATE_MIGRATIONS_DIR="$db/init-scripts" DATABASE_URL="postgres://postgres:$connect" dbmate --no-dump-schema migrate psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -c "ALTER USER supabase_admin WITH PASSWORD '$PGPASSWORD'" # run migrations as super user - postgres user demoted in post-setup DBMATE_MIGRATIONS_DIR="$db/migrations" DATABASE_URL="postgres://supabase_admin:$connect" dbmate --no-dump-schema migrate diff --git a/migrations/db/init-scripts/00000000000000-initial-schema.sql b/migrations/db/migrations/00000000000000-initial-schema.sql similarity index 100% rename from migrations/db/init-scripts/00000000000000-initial-schema.sql rename to migrations/db/migrations/00000000000000-initial-schema.sql diff --git a/migrations/db/init-scripts/00000000000001-auth-schema.sql b/migrations/db/migrations/00000000000001-auth-schema.sql similarity index 100% rename from migrations/db/init-scripts/00000000000001-auth-schema.sql rename to migrations/db/migrations/00000000000001-auth-schema.sql diff --git a/migrations/db/init-scripts/00000000000002-storage-schema.sql b/migrations/db/migrations/00000000000002-storage-schema.sql similarity index 100% rename from migrations/db/init-scripts/00000000000002-storage-schema.sql rename to migrations/db/migrations/00000000000002-storage-schema.sql diff --git a/migrations/db/init-scripts/00000000000003-post-setup.sql b/migrations/db/migrations/00000000000003-post-setup.sql similarity index 100% rename from migrations/db/init-scripts/00000000000003-post-setup.sql rename to migrations/db/migrations/00000000000003-post-setup.sql diff --git a/migrations/tests/extensions/04-pg_cron.sql b/migrations/tests/extensions/04-pg_cron.sql index 7f6686f13..111f28004 100644 --- a/migrations/tests/extensions/04-pg_cron.sql +++ b/migrations/tests/extensions/04-pg_cron.sql @@ -2,7 +2,6 @@ BEGIN; -- create cron extension as supabase_admin create extension if not exists pg_cron; --- \ir migrations/db/init-scripts/00000000000003-post-setup.sql grant usage on schema cron to postgres with grant option; alter default privileges in schema cron grant all on tables to postgres with grant option; alter default privileges in schema cron grant all on routines to postgres with grant option; diff --git a/migrations/tests/extensions/15-pg_net.sql b/migrations/tests/extensions/15-pg_net.sql index 121e11e14..db1d6b15b 100644 --- a/migrations/tests/extensions/15-pg_net.sql +++ b/migrations/tests/extensions/15-pg_net.sql @@ -2,7 +2,6 @@ BEGIN; -- create net extension as supabase_admin create extension if not exists pg_net with schema "extensions"; --- \ir migrations/db/init-scripts/00000000000003-post-setup.sql grant usage on schema net TO postgres, anon, authenticated, service_role; alter function net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) security definer; alter function net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) security definer; diff --git a/nix/tests/expected/evtrigs.out b/nix/tests/expected/evtrigs.out new file mode 100644 index 000000000..a6b561ffb --- /dev/null +++ b/nix/tests/expected/evtrigs.out @@ -0,0 +1,13 @@ +select proname, proowner::regrole from pg_proc where prorettype = 'event_trigger'::regtype; + proname | proowner +--------------------------+---------------- + event_trigger_in | supabase_admin + pgrst_drop_watch | supabase_admin + grant_pg_graphql_access | supabase_admin + set_graphql_placeholder | supabase_admin + pgrst_ddl_watch | supabase_admin + increment_schema_version | supabase_admin + grant_pg_cron_access | supabase_admin + grant_pg_net_access | supabase_admin +(8 rows) + diff --git a/nix/tests/sql/evtrigs.sql b/nix/tests/sql/evtrigs.sql new file mode 100644 index 000000000..b7463bf4a --- /dev/null +++ b/nix/tests/sql/evtrigs.sql @@ -0,0 +1 @@ +select proname, proowner::regrole from pg_proc where prorettype = 'event_trigger'::regtype; diff --git a/nix/tools/dbmate-tool.sh.in b/nix/tools/dbmate-tool.sh.in index 1197228af..ccac958ad 100644 --- a/nix/tools/dbmate-tool.sh.in +++ b/nix/tools/dbmate-tool.sh.in @@ -243,11 +243,6 @@ EOSQL export DATABASE_URL="postgres://$PGSQL_USER:$PGPASSWORD@localhost:$PORTNO/postgres?sslmode=disable" # Export path so dbmate can find correct psql and pg_dump export PATH="$PSQLBIN:$PATH" - # Run init scripts - if ! dbmate --migrations-dir "$MIGRATIONS_DIR/init-scripts" up; then - echo "Error: Initial migration failed" - exit 1 - fi # Password update command if ! "${PSQLBIN}/psql" -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p "$PORTNO" -h localhost -c "ALTER USER supabase_admin WITH PASSWORD '$PGPASSWORD'"; then diff --git a/nix/tools/run-server.sh.in b/nix/tools/run-server.sh.in index 0586e010b..a799da15c 100644 --- a/nix/tools/run-server.sh.in +++ b/nix/tools/run-server.sh.in @@ -309,14 +309,6 @@ EOSQL 'stop_postgres' 1 fi else - # Run default init scripts - for sql in "$MIGRATIONS_DIR"/init-scripts/*.sql; do - echo "Running $sql" - if ! psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U "$PSQL_USER" -p "$PORTNO" -h localhost -f "$sql" postgres; then - 'stop_postgres' 1 - fi - done - # Set superuser password if ! psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U "$PSQL_USER" -p "$PORTNO" -h localhost -c "ALTER USER supabase_admin WITH PASSWORD '$PGPASSWORD'"; then 'stop_postgres' 1