Skip to content

Commit 1f2520e

Browse files
authored
Revert "fix: evtrigs ownership (#1489)"
This reverts commit ef851d1.
1 parent ef851d1 commit 1f2520e

14 files changed

+116
-239
lines changed

flake.nix

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,9 @@
807807
exit 1
808808
fi
809809
810+
echo "Running migrations tests"
811+
pg_prove -p 5435 -U supabase_admin -h localhost -d postgres -v ${./migrations/tests}/test.sql
812+
810813
mkdir -p $out/regression_output
811814
if ! pg_regress \
812815
--use-existing \
@@ -822,15 +825,12 @@
822825
exit 1
823826
fi
824827
825-
echo "Running migrations tests"
826-
pg_prove -p 5435 -U supabase_admin -h localhost -d postgres -v ${./migrations/tests}/test.sql
827-
828828
# Copy logs to output
829829
for logfile in $(find /tmp -name postgresql.log -type f); do
830830
cp "$logfile" $out/postgresql.log
831831
done
832832
exit 0
833-
'';
833+
'';
834834
in
835835
rec {
836836
# The list of all packages that can be built with 'nix build'. The list

migrations/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ nix run github:supabase/postgres/mybranch#dbmate-tool -- --version 15
4242

4343
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)
4444

45+
46+
4547
## How it was Created
4648

4749
Migrations were pulled (in order) from:
@@ -51,8 +53,9 @@ Migrations were pulled (in order) from:
5153

5254
For compatibility with hosted projects, we include [migrate.sh](migrate.sh) that executes migrations in the same order as ami build:
5355

54-
1. Run all `db/migrations` with `supabase_admin` superuser role.
55-
2. Finalize role passwords with `/etc/postgresql.schema.sql` if present.
56+
1. Run all `db/init-scripts` with `postgres` superuser role.
57+
2. Run all `db/migrations` with `supabase_admin` superuser role.
58+
3. Finalize role passwords with `/etc/postgresql.schema.sql` if present.
5659

5760
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.
5861

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
-- migrate:up
2+
3+
-- Set up realtime
4+
-- defaults to empty publication
5+
create publication supabase_realtime;
6+
7+
-- Supabase super admin
8+
alter user supabase_admin with superuser createdb createrole replication bypassrls;
9+
10+
-- Supabase replication user
11+
create user supabase_replication_admin with login replication;
12+
13+
-- Supabase read-only user
14+
create role supabase_read_only_user with login bypassrls;
15+
grant pg_read_all_data to supabase_read_only_user;
16+
17+
-- Extension namespacing
18+
create schema if not exists extensions;
19+
create extension if not exists "uuid-ossp" with schema extensions;
20+
create extension if not exists pgcrypto with schema extensions;
21+
create extension if not exists pgjwt with schema extensions;
22+
23+
-- Set up auth roles for the developer
24+
create role anon nologin noinherit;
25+
create role authenticated nologin noinherit; -- "logged in" user: web_user, app_user, etc
26+
create role service_role nologin noinherit bypassrls; -- allow developers to create JWT's that bypass their policies
27+
28+
create user authenticator noinherit;
29+
grant anon to authenticator;
30+
grant authenticated to authenticator;
31+
grant service_role to authenticator;
32+
grant supabase_admin to authenticator;
33+
34+
grant usage on schema public to postgres, anon, authenticated, service_role;
35+
alter default privileges in schema public grant all on tables to postgres, anon, authenticated, service_role;
36+
alter default privileges in schema public grant all on functions to postgres, anon, authenticated, service_role;
37+
alter default privileges in schema public grant all on sequences to postgres, anon, authenticated, service_role;
38+
39+
-- Allow Extensions to be used in the API
40+
grant usage on schema extensions to postgres, anon, authenticated, service_role;
41+
42+
-- Set up namespacing
43+
alter user supabase_admin SET search_path TO public, extensions; -- don't include the "auth" schema
44+
45+
-- These are required so that the users receive grants whenever "supabase_admin" creates tables/function
46+
alter default privileges for user supabase_admin in schema public grant all
47+
on sequences to postgres, anon, authenticated, service_role;
48+
alter default privileges for user supabase_admin in schema public grant all
49+
on tables to postgres, anon, authenticated, service_role;
50+
alter default privileges for user supabase_admin in schema public grant all
51+
on functions to postgres, anon, authenticated, service_role;
52+
53+
-- Set short statement/query timeouts for API roles
54+
alter role anon set statement_timeout = '3s';
55+
alter role authenticated set statement_timeout = '8s';
56+
57+
-- migrate:down

migrations/db/migrations/00000000000001-auth-schema.sql renamed to migrations/db/init-scripts/00000000000001-auth-schema.sql

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CREATE SCHEMA IF NOT EXISTS auth AUTHORIZATION supabase_admin;
44

55
-- auth.users definition
66

7-
CREATE TABLE IF NOT EXISTS auth.users (
7+
CREATE TABLE auth.users (
88
instance_id uuid NULL,
99
id uuid NOT NULL UNIQUE,
1010
aud varchar(255) NULL,
@@ -28,13 +28,13 @@ CREATE TABLE IF NOT EXISTS auth.users (
2828
updated_at timestamptz NULL,
2929
CONSTRAINT users_pkey PRIMARY KEY (id)
3030
);
31-
CREATE INDEX IF NOT EXISTS users_instance_id_email_idx ON auth.users USING btree (instance_id, email);
32-
CREATE INDEX IF NOT EXISTS users_instance_id_idx ON auth.users USING btree (instance_id);
31+
CREATE INDEX users_instance_id_email_idx ON auth.users USING btree (instance_id, email);
32+
CREATE INDEX users_instance_id_idx ON auth.users USING btree (instance_id);
3333
comment on table auth.users is 'Auth: Stores user login data within a secure schema.';
3434

3535
-- auth.refresh_tokens definition
3636

37-
CREATE TABLE IF NOT EXISTS auth.refresh_tokens (
37+
CREATE TABLE auth.refresh_tokens (
3838
instance_id uuid NULL,
3939
id bigserial NOT NULL,
4040
"token" varchar(255) NULL,
@@ -44,14 +44,14 @@ CREATE TABLE IF NOT EXISTS auth.refresh_tokens (
4444
updated_at timestamptz NULL,
4545
CONSTRAINT refresh_tokens_pkey PRIMARY KEY (id)
4646
);
47-
CREATE INDEX IF NOT EXISTS refresh_tokens_instance_id_idx ON auth.refresh_tokens USING btree (instance_id);
48-
CREATE INDEX IF NOT EXISTS refresh_tokens_instance_id_user_id_idx ON auth.refresh_tokens USING btree (instance_id, user_id);
49-
CREATE INDEX IF NOT EXISTS refresh_tokens_token_idx ON auth.refresh_tokens USING btree (token);
47+
CREATE INDEX refresh_tokens_instance_id_idx ON auth.refresh_tokens USING btree (instance_id);
48+
CREATE INDEX refresh_tokens_instance_id_user_id_idx ON auth.refresh_tokens USING btree (instance_id, user_id);
49+
CREATE INDEX refresh_tokens_token_idx ON auth.refresh_tokens USING btree (token);
5050
comment on table auth.refresh_tokens is 'Auth: Store of tokens used to refresh JWT tokens once they expire.';
5151

5252
-- auth.instances definition
5353

54-
CREATE TABLE IF NOT EXISTS auth.instances (
54+
CREATE TABLE auth.instances (
5555
id uuid NOT NULL,
5656
uuid uuid NULL,
5757
raw_base_config text NULL,
@@ -63,34 +63,32 @@ comment on table auth.instances is 'Auth: Manages users across multiple sites.';
6363

6464
-- auth.audit_log_entries definition
6565

66-
CREATE TABLE IF NOT EXISTS auth.audit_log_entries (
66+
CREATE TABLE auth.audit_log_entries (
6767
instance_id uuid NULL,
6868
id uuid NOT NULL,
6969
payload json NULL,
7070
created_at timestamptz NULL,
7171
CONSTRAINT audit_log_entries_pkey PRIMARY KEY (id)
7272
);
73-
CREATE INDEX IF NOT EXISTS audit_logs_instance_id_idx ON auth.audit_log_entries USING btree (instance_id);
73+
CREATE INDEX audit_logs_instance_id_idx ON auth.audit_log_entries USING btree (instance_id);
7474
comment on table auth.audit_log_entries is 'Auth: Audit trail for user actions.';
7575

7676
-- auth.schema_migrations definition
7777

78-
CREATE TABLE IF NOT EXISTS auth.schema_migrations (
78+
CREATE TABLE auth.schema_migrations (
7979
"version" varchar(255) NOT NULL,
8080
CONSTRAINT schema_migrations_pkey PRIMARY KEY ("version")
8181
);
8282
comment on table auth.schema_migrations is 'Auth: Manages updates to the auth system.';
8383

84-
-- insert migrations if they do not yet exist
8584
INSERT INTO auth.schema_migrations (version)
8685
VALUES ('20171026211738'),
8786
('20171026211808'),
8887
('20171026211834'),
8988
('20180103212743'),
9089
('20180108183307'),
9190
('20180119214651'),
92-
('20180125194653')
93-
ON CONFLICT DO NOTHING;
91+
('20180125194653');
9492

9593
-- Gets the User ID from the request cookie
9694
create or replace function auth.uid() returns uuid as $$
@@ -111,18 +109,8 @@ $$ language sql stable;
111109
GRANT USAGE ON SCHEMA auth TO anon, authenticated, service_role;
112110

113111
-- Supabase super admin
114-
do $$
115-
begin
116-
if not exists (
117-
select 1 from pg_roles
118-
where rolname = 'supabase_auth_admin'
119-
)
120-
then
121-
CREATE USER supabase_auth_admin NOINHERIT CREATEROLE LOGIN NOREPLICATION;
122-
end if;
123-
end
124-
$$;
125-
112+
CREATE USER supabase_auth_admin NOINHERIT CREATEROLE LOGIN NOREPLICATION;
113+
GRANT ALL PRIVILEGES ON SCHEMA auth TO supabase_auth_admin;
126114
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA auth TO supabase_auth_admin;
127115
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA auth TO supabase_auth_admin;
128116
ALTER USER supabase_auth_admin SET search_path = "auth";

migrations/db/migrations/00000000000002-storage-schema.sql renamed to migrations/db/init-scripts/00000000000002-storage-schema.sql

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ alter default privileges in schema storage grant all on tables to postgres, anon
77
alter default privileges in schema storage grant all on functions to postgres, anon, authenticated, service_role;
88
alter default privileges in schema storage grant all on sequences to postgres, anon, authenticated, service_role;
99

10-
CREATE TABLE IF NOT EXISTS "storage"."buckets" (
10+
CREATE TABLE "storage"."buckets" (
1111
"id" text not NULL,
1212
"name" text NOT NULL,
1313
"owner" uuid,
@@ -16,9 +16,9 @@ CREATE TABLE IF NOT EXISTS "storage"."buckets" (
1616
CONSTRAINT "buckets_owner_fkey" FOREIGN KEY ("owner") REFERENCES "auth"."users"("id"),
1717
PRIMARY KEY ("id")
1818
);
19-
CREATE UNIQUE INDEX IF NOT EXISTS "bname" ON "storage"."buckets" USING BTREE ("name");
19+
CREATE UNIQUE INDEX "bname" ON "storage"."buckets" USING BTREE ("name");
2020

21-
CREATE TABLE IF NOT EXISTS "storage"."objects" (
21+
CREATE TABLE "storage"."objects" (
2222
"id" uuid NOT NULL DEFAULT extensions.uuid_generate_v4(),
2323
"bucket_id" text,
2424
"name" text,
@@ -31,12 +31,12 @@ CREATE TABLE IF NOT EXISTS "storage"."objects" (
3131
CONSTRAINT "objects_owner_fkey" FOREIGN KEY ("owner") REFERENCES "auth"."users"("id"),
3232
PRIMARY KEY ("id")
3333
);
34-
CREATE UNIQUE INDEX IF NOT EXISTS "bucketid_objname" ON "storage"."objects" USING BTREE ("bucket_id","name");
35-
CREATE INDEX IF NOT EXISTS name_prefix_search ON storage.objects(name text_pattern_ops);
34+
CREATE UNIQUE INDEX "bucketid_objname" ON "storage"."objects" USING BTREE ("bucket_id","name");
35+
CREATE INDEX name_prefix_search ON storage.objects(name text_pattern_ops);
3636

3737
ALTER TABLE storage.objects ENABLE ROW LEVEL SECURITY;
3838

39-
CREATE OR REPLACE FUNCTION storage.foldername(name text)
39+
CREATE FUNCTION storage.foldername(name text)
4040
RETURNS text[]
4141
LANGUAGE plpgsql
4242
AS $function$
@@ -48,7 +48,7 @@ BEGIN
4848
END
4949
$function$;
5050

51-
CREATE OR REPLACE FUNCTION storage.filename(name text)
51+
CREATE FUNCTION storage.filename(name text)
5252
RETURNS text
5353
LANGUAGE plpgsql
5454
AS $function$
@@ -60,7 +60,7 @@ BEGIN
6060
END
6161
$function$;
6262

63-
CREATE OR REPLACE FUNCTION storage.extension(name text)
63+
CREATE FUNCTION storage.extension(name text)
6464
RETURNS text
6565
LANGUAGE plpgsql
6666
AS $function$
@@ -75,7 +75,7 @@ BEGIN
7575
END
7676
$function$;
7777

78-
CREATE OR REPLACE FUNCTION storage.search(prefix text, bucketname text, limits int DEFAULT 100, levels int DEFAULT 1, offsets int DEFAULT 0)
78+
CREATE FUNCTION storage.search(prefix text, bucketname text, limits int DEFAULT 100, levels int DEFAULT 1, offsets int DEFAULT 0)
7979
RETURNS TABLE (
8080
name text,
8181
id uuid,
@@ -104,17 +104,7 @@ CREATE TABLE IF NOT EXISTS storage.migrations (
104104
executed_at timestamp DEFAULT current_timestamp
105105
);
106106

107-
do $$
108-
begin
109-
if not exists (
110-
select 1 from pg_roles
111-
where rolname = 'supabase_storage_admin'
112-
)
113-
then
114-
CREATE USER supabase_storage_admin NOINHERIT CREATEROLE LOGIN NOREPLICATION;
115-
end if;
116-
end
117-
$$;
107+
CREATE USER supabase_storage_admin NOINHERIT CREATEROLE LOGIN NOREPLICATION;
118108
GRANT ALL PRIVILEGES ON SCHEMA storage TO supabase_storage_admin;
119109
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA storage TO supabase_storage_admin;
120110
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA storage TO supabase_storage_admin;

migrations/db/migrations/00000000000003-post-setup.sql renamed to migrations/db/init-scripts/00000000000003-post-setup.sql

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,8 @@ BEGIN
3939

4040
END;
4141
$$;
42-
DO $$
43-
44-
BEGIN
45-
IF NOT EXISTS (
46-
SELECT 1
47-
FROM pg_catalog.pg_event_trigger
48-
WHERE evtname = 'issue_pg_cron_access'
49-
)
50-
THEN
51-
CREATE EVENT TRIGGER issue_pg_cron_access ON ddl_command_end WHEN TAG in ('CREATE SCHEMA')
52-
EXECUTE PROCEDURE extensions.grant_pg_cron_access();
53-
END IF;
54-
END
55-
$$;
42+
CREATE EVENT TRIGGER issue_pg_cron_access ON ddl_command_end WHEN TAG in ('CREATE SCHEMA')
43+
EXECUTE PROCEDURE extensions.grant_pg_cron_access();
5644
COMMENT ON FUNCTION extensions.grant_pg_cron_access IS 'Grants access to pg_cron';
5745

5846
-- Event trigger for pg_net
@@ -94,6 +82,7 @@ BEGIN
9482
END IF;
9583
END;
9684
$$;
85+
COMMENT ON FUNCTION extensions.grant_pg_net_access IS 'Grants access to pg_net';
9786

9887
DO
9988
$$
@@ -110,21 +99,9 @@ BEGIN
11099
END IF;
111100
END
112101
$$;
113-
COMMENT ON FUNCTION extensions.grant_pg_net_access IS 'Grants access to pg_net';
114102

115103
-- Supabase dashboard user
116-
do $$
117-
begin
118-
if not exists (
119-
select 1 from pg_roles
120-
where rolname = 'dashboard_user'
121-
)
122-
then
123-
CREATE ROLE dashboard_user NOSUPERUSER CREATEDB CREATEROLE REPLICATION;
124-
end if;
125-
end
126-
$$;
127-
104+
CREATE ROLE dashboard_user NOSUPERUSER CREATEDB CREATEROLE REPLICATION;
128105
GRANT ALL ON DATABASE postgres TO dashboard_user;
129106
GRANT ALL ON SCHEMA auth TO dashboard_user;
130107
GRANT ALL ON SCHEMA extensions TO dashboard_user;

migrations/db/migrate.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ begin
3838
end if;
3939
end \$\$
4040
EOSQL
41+
# run init scripts as postgres user
42+
for sql in "$db"/init-scripts/*.sql; do
43+
echo "$0: running $sql"
44+
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -f "$sql"
45+
done
4146
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -c "ALTER USER supabase_admin WITH PASSWORD '$PGPASSWORD'"
4247
# run migrations as super user - postgres user demoted in post-setup
4348
for sql in "$db"/migrations/*.sql; do
@@ -49,6 +54,8 @@ else
4954
create role postgres superuser login password '$PGPASSWORD';
5055
alter database postgres owner to postgres;
5156
EOSQL
57+
# run init scripts as postgres user
58+
DBMATE_MIGRATIONS_DIR="$db/init-scripts" DATABASE_URL="postgres://postgres:$connect" dbmate --no-dump-schema migrate
5259
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -c "ALTER USER supabase_admin WITH PASSWORD '$PGPASSWORD'"
5360
# run migrations as super user - postgres user demoted in post-setup
5461
DBMATE_MIGRATIONS_DIR="$db/migrations" DATABASE_URL="postgres://supabase_admin:$connect" dbmate --no-dump-schema migrate

0 commit comments

Comments
 (0)