From 67fd3ca33c9ea6b0bedba5727a5bba31f0d00290 Mon Sep 17 00:00:00 2001 From: Qiao Han Date: Mon, 9 Jun 2025 14:40:10 +0800 Subject: [PATCH 1/2] chore: remove unused scripts --- internal/db/dump/templates/dump_data.sh | 33 -------------- internal/db/dump/templates/dump_role.sh | 34 --------------- internal/db/dump/templates/dump_schema.sh | 53 ----------------------- internal/utils/misc.go | 2 +- 4 files changed, 1 insertion(+), 121 deletions(-) delete mode 100755 internal/db/dump/templates/dump_data.sh delete mode 100755 internal/db/dump/templates/dump_role.sh delete mode 100755 internal/db/dump/templates/dump_schema.sh diff --git a/internal/db/dump/templates/dump_data.sh b/internal/db/dump/templates/dump_data.sh deleted file mode 100755 index 7647665106..0000000000 --- a/internal/db/dump/templates/dump_data.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail - -export PGHOST="$PGHOST" -export PGPORT="$PGPORT" -export PGUSER="$PGUSER" -export PGPASSWORD="$PGPASSWORD" -export PGDATABASE="$PGDATABASE" - -# Disable triggers so that data dump can be restored exactly as it is -echo "SET session_replication_role = replica; -" - -# Explanation of pg_dump flags: -# -# --exclude-schema omit data from internal schemas as they are maintained by platform -# --exclude-table omit data from migration history tables as they are managed by platform -# --column-inserts only column insert syntax is supported, ie. no copy from stdin -# --schema '*' include all other schemas by default -# -# Never delete SQL comments because multiline records may begin with them. -pg_dump \ - --data-only \ - --quote-all-identifier \ - --exclude-schema "${EXCLUDED_SCHEMAS:-}" \ - --exclude-table "auth.schema_migrations" \ - --exclude-table "storage.migrations" \ - --exclude-table "supabase_functions.migrations" \ - --schema "$INCLUDED_SCHEMAS" \ - ${EXTRA_FLAGS:-} - -# Reset session config generated by pg_dump -echo "RESET ALL;" diff --git a/internal/db/dump/templates/dump_role.sh b/internal/db/dump/templates/dump_role.sh deleted file mode 100755 index e5c157ba30..0000000000 --- a/internal/db/dump/templates/dump_role.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail - -export PGHOST="$PGHOST" -export PGPORT="$PGPORT" -export PGUSER="$PGUSER" -export PGPASSWORD="$PGPASSWORD" -export PGDATABASE="$PGDATABASE" - -# Explanation of pg_dumpall flags: -# -# --roles-only only include create, alter, and grant role statements -# -# Explanation of sed substitutions: -# -# - do not create or alter reserved roles as they are blocked by supautils -# - explicitly allow altering safe attributes, ie. statement_timeout, pgrst.* -# - discard role attributes that require superuser, ie. nosuperuser, noreplication -# - do not alter membership grants by supabase_admin role -pg_dumpall \ - --roles-only \ - --quote-all-identifier \ - --no-role-passwords \ - --no-comments \ -| sed -E "s/^CREATE ROLE \"($RESERVED_ROLES)\"/-- &/" \ -| sed -E "s/^ALTER ROLE \"($RESERVED_ROLES)\"/-- &/" \ -| sed -E "s/ (NOSUPERUSER|NOREPLICATION)//g" \ -| sed -E "s/^-- (.* SET \"($ALLOWED_CONFIGS)\" .*)/\1/" \ -| sed -E "s/GRANT \".*\" TO \"($RESERVED_ROLES)\"/-- &/" \ -| sed -E "${EXTRA_SED:-}" \ -| uniq - -# Reset session config generated by pg_dump -echo "RESET ALL;" diff --git a/internal/db/dump/templates/dump_schema.sh b/internal/db/dump/templates/dump_schema.sh deleted file mode 100755 index 71aae48286..0000000000 --- a/internal/db/dump/templates/dump_schema.sh +++ /dev/null @@ -1,53 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail - -export PGHOST="$PGHOST" -export PGPORT="$PGPORT" -export PGUSER="$PGUSER" -export PGPASSWORD="$PGPASSWORD" -export PGDATABASE="$PGDATABASE" - -# Explanation of pg_dump flags: -# -# --schema-only omit data like migration history, pgsodium key, etc. -# --exclude-schema omit internal schemas as they are maintained by platform -# -# Explanation of sed substitutions: -# -# - do not alter superuser role "supabase_admin" -# - do not alter foreign data wrappers owner -# - do not include ACL changes on internal schemas -# - do not include RLS policies on cron extension schema -# - do not include event triggers -# - do not create pgtle schema and extension comments -# - do not create publication "supabase_realtime" -pg_dump \ - --schema-only \ - --quote-all-identifier \ - --exclude-schema "${EXCLUDED_SCHEMAS:-}" \ - ${EXTRA_FLAGS:-} \ -| sed -E 's/^CREATE SCHEMA "/CREATE SCHEMA IF NOT EXISTS "/' \ -| sed -E 's/^CREATE TABLE "/CREATE TABLE IF NOT EXISTS "/' \ -| sed -E 's/^CREATE SEQUENCE "/CREATE SEQUENCE IF NOT EXISTS "/' \ -| sed -E 's/^CREATE VIEW "/CREATE OR REPLACE VIEW "/' \ -| sed -E 's/^CREATE FUNCTION "/CREATE OR REPLACE FUNCTION "/' \ -| sed -E 's/^CREATE TRIGGER "/CREATE OR REPLACE TRIGGER "/' \ -| sed -E 's/^CREATE PUBLICATION "supabase_realtime/-- &/' \ -| sed -E 's/^CREATE EVENT TRIGGER /-- &/' \ -| sed -E 's/^ WHEN TAG IN /-- &/' \ -| sed -E 's/^ EXECUTE FUNCTION /-- &/' \ -| sed -E 's/^ALTER EVENT TRIGGER /-- &/' \ -| sed -E 's/^ALTER PUBLICATION "supabase_realtime_/-- &/' \ -| sed -E 's/^ALTER FOREIGN DATA WRAPPER (.+) OWNER TO /-- &/' \ -| sed -E 's/^ALTER DEFAULT PRIVILEGES FOR ROLE "supabase_admin"/-- &/' \ -| sed -E "s/^GRANT (.+) ON (.+) \"(${EXCLUDED_SCHEMAS:-})\"/-- &/" \ -| sed -E "s/^REVOKE (.+) ON (.+) \"(${EXCLUDED_SCHEMAS:-})\"/-- &/" \ -| sed -E 's/^(CREATE EXTENSION IF NOT EXISTS "pg_tle").+/\1;/' \ -| sed -E 's/^(CREATE EXTENSION IF NOT EXISTS "pgsodium").+/\1;/' \ -| sed -E 's/^COMMENT ON EXTENSION (.+)/-- &/' \ -| sed -E 's/^CREATE POLICY "cron_job_/-- &/' \ -| sed -E 's/^ALTER TABLE "cron"/-- &/' \ -| sed -E "${EXTRA_SED:-}" - -# Reset session config generated by pg_dump -echo "RESET ALL;" diff --git a/internal/utils/misc.go b/internal/utils/misc.go index 98705668c0..46a9d3d12b 100644 --- a/internal/utils/misc.go +++ b/internal/utils/misc.go @@ -90,7 +90,7 @@ var ( func GetCurrentTimestamp() string { // Magic number: https://stackoverflow.com/q/45160822. - return time.Now().UTC().Format("20060102150405") + return time.Now().UTC().Format(layoutVersion) } func GetCurrentBranchFS(fsys afero.Fs) (string, error) { From aaff2d345964668cdd24d8c313a3c3b00b76a768 Mon Sep 17 00:00:00 2001 From: Qiao Han Date: Mon, 9 Jun 2025 18:34:03 +0800 Subject: [PATCH 2/2] fix: allow skipping migrations so it can be managed by ORM --- internal/db/push/push.go | 6 ++++-- internal/migration/apply/apply.go | 15 +++++++++++---- pkg/config/config.go | 3 +++ pkg/config/db.go | 1 + pkg/config/templates/config.toml | 2 ++ pkg/config/testdata/config.toml | 2 ++ 6 files changed, 23 insertions(+), 6 deletions(-) diff --git a/internal/db/push/push.go b/internal/db/push/push.go index 4a1fdf6df5..6960702d17 100644 --- a/internal/db/push/push.go +++ b/internal/db/push/push.go @@ -26,8 +26,10 @@ func Run(ctx context.Context, dryRun, ignoreVersionMismatch bool, includeRoles, return err } defer conn.Close(context.Background()) - pending, err := up.GetPendingMigrations(ctx, ignoreVersionMismatch, conn, fsys) - if err != nil { + var pending []string + if !utils.Config.Db.Migrations.Enabled { + fmt.Fprintln(os.Stderr, "Skipping migrations because it is disabled in config.toml for project:", flags.ProjectRef) + } else if pending, err = up.GetPendingMigrations(ctx, ignoreVersionMismatch, conn, fsys); err != nil { return err } var seeds []migration.SeedFile diff --git a/internal/migration/apply/apply.go b/internal/migration/apply/apply.go index 224b342f76..f9c517ace5 100644 --- a/internal/migration/apply/apply.go +++ b/internal/migration/apply/apply.go @@ -11,14 +11,21 @@ import ( ) func MigrateAndSeed(ctx context.Context, version string, conn *pgx.Conn, fsys afero.Fs) error { - migrations, err := list.LoadPartialMigrations(version, fsys) - if err != nil { + if err := applyMigrationFiles(ctx, version, conn, fsys); err != nil { return err } - if err := migration.ApplyMigrations(ctx, migrations, conn, afero.NewIOFS(fsys)); err != nil { + return applySeedFiles(ctx, conn, fsys) +} + +func applyMigrationFiles(ctx context.Context, version string, conn *pgx.Conn, fsys afero.Fs) error { + if !utils.Config.Db.Migrations.Enabled { + return nil + } + migrations, err := list.LoadPartialMigrations(version, fsys) + if err != nil { return err } - return applySeedFiles(ctx, conn, fsys) + return migration.ApplyMigrations(ctx, migrations, conn, afero.NewIOFS(fsys)) } func applySeedFiles(ctx context.Context, conn *pgx.Conn, fsys afero.Fs) error { diff --git a/pkg/config/config.go b/pkg/config/config.go index 04142d7832..49dee97d61 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -344,6 +344,9 @@ func NewConfig(editors ...ConfigEditor) config { EncryptionKey: "12345678901234567890123456789032", SecretKeyBase: "EAx3IQ/wRG1v47ZD4NE4/9RzBI8Jmil3x0yhcW4V2NHBP6c2iPIzwjofi2Ep4HIG", }, + Migrations: migrations{ + Enabled: true, + }, Seed: seed{ Enabled: true, SqlPaths: []string{"seed.sql"}, diff --git a/pkg/config/db.go b/pkg/config/db.go index bdbc4da5dc..0f216a23f3 100644 --- a/pkg/config/db.go +++ b/pkg/config/db.go @@ -82,6 +82,7 @@ type ( } migrations struct { + Enabled bool `toml:"enabled"` SchemaPaths Glob `toml:"schema_paths"` } diff --git a/pkg/config/templates/config.toml b/pkg/config/templates/config.toml index b1dfdfe69f..d72ae50459 100644 --- a/pkg/config/templates/config.toml +++ b/pkg/config/templates/config.toml @@ -46,6 +46,8 @@ max_client_conn = 100 # secret_key = "env(SECRET_VALUE)" [db.migrations] +# If disabled, migrations will be skipped during a db push or reset. +enabled = true # Specifies an ordered list of schema files that describe your database. # Supports glob patterns relative to supabase directory: "./schemas/*.sql" schema_paths = [] diff --git a/pkg/config/testdata/config.toml b/pkg/config/testdata/config.toml index 6f1d814c7d..ed0d508fc5 100644 --- a/pkg/config/testdata/config.toml +++ b/pkg/config/testdata/config.toml @@ -31,6 +31,8 @@ shadow_port = 54320 major_version = 17 [db.migrations] +# If disabled, migrations will be skipped during a db push or reset. +enabled = true # Specifies an ordered list of schema files that describe your database. # Supports glob patterns relative to supabase directory: "./schemas/*.sql" schema_paths = ["./schemas/*.sql"]