Skip to content

Commit ea0e40f

Browse files
authored
Revert "fix: evtrigs ownership" (#1500)
* Revert "fix: evtrigs ownership (#1489)" This reverts commit ef851d1. * chore: improve migration workflows
1 parent ef851d1 commit ea0e40f

16 files changed

+158
-252
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Release Migrations - Prod
2+
3+
on:
4+
workflow_dispatch:
5+
6+
jobs:
7+
build:
8+
runs-on: [self-hosted, linux]
9+
timeout-minutes: 15
10+
permissions:
11+
id-token: write
12+
contents: read
13+
14+
steps:
15+
- name: Guard
16+
run: |
17+
if [ $GITHUB_REF != 'refs/heads/develop' ]; then
18+
echo "This action can only be run on the develop branch"
19+
exit 1
20+
fi
21+
env:
22+
GITHUB_REF: ${{ github.ref }}
23+
24+
- name: Checkout Repo
25+
uses: actions/checkout@v2
26+
27+
- name: Merging migration files
28+
run: cat $(ls -1) > ../migration-output.sql
29+
working-directory: ${{ github.workspace }}/migrations/db/migrations
30+
31+
- name: configure aws credentials - prod
32+
uses: aws-actions/configure-aws-credentials@v1
33+
with:
34+
role-to-assume: ${{ secrets.PROD_AWS_ROLE }}
35+
aws-region: "ap-southeast-1"
36+
37+
- name: Deploy to S3 prod
38+
shell: bash
39+
run: aws s3 sync migrations/db s3://$AWS_S3_BUCKET/migrations/db --delete
40+
env:
41+
AWS_S3_BUCKET: ${{ secrets.PG_INIT_SCRIPT_S3_BUCKET_PROD }}

.github/workflows/publish-migrations.yml renamed to .github/workflows/publish-migrations-staging.yml

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Release Migrations
1+
name: Release Migrations - Staging
22

33
on:
44
push:
@@ -32,15 +32,3 @@ jobs:
3232
run: aws s3 sync migrations/db s3://$AWS_S3_BUCKET/migrations/db --delete
3333
env:
3434
AWS_S3_BUCKET: ${{ secrets.PG_INIT_SCRIPT_S3_BUCKET_STAGING }}
35-
36-
- name: configure aws credentials - prod
37-
uses: aws-actions/configure-aws-credentials@v1
38-
with:
39-
role-to-assume: ${{ secrets.PROD_AWS_ROLE }}
40-
aws-region: "ap-southeast-1"
41-
42-
- name: Deploy to S3 prod
43-
shell: bash
44-
run: aws s3 sync migrations/db s3://$AWS_S3_BUCKET/migrations/db --delete
45-
env:
46-
AWS_S3_BUCKET: ${{ secrets.PG_INIT_SCRIPT_S3_BUCKET_PROD }}

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;

0 commit comments

Comments
 (0)