Skip to content

Commit 0176c9c

Browse files
committed
fix: update migration to support vault 0.2.8 and above
1 parent 4a7790c commit 0176c9c

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

migrations/db/migrations/20221207154255_create_pgsodium_and_vault.sql

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,44 @@ DECLARE
55
pgsodium_exists boolean;
66
vault_exists boolean;
77
BEGIN
8-
pgsodium_exists = (
9-
select count(*) = 1
10-
from pg_available_extensions
11-
where name = 'pgsodium'
12-
and default_version in ('3.1.6', '3.1.7', '3.1.8', '3.1.9')
13-
);
14-
15-
vault_exists = (
8+
IF EXISTS (SELECT FROM pg_available_extensions WHERE name = 'supabase_vault' AND default_version != '0.2.8') THEN
9+
CREATE EXTENSION IF NOT EXISTS supabase_vault;
10+
11+
-- for some reason extension custom scripts aren't run during AMI build, so
12+
-- we manually run it here
13+
GRANT USAGE ON SCHEMA vault TO postgres WITH GRANT OPTION;
14+
GRANT SELECT, DELETE ON vault.secrets, vault.decrypted_secrets TO postgres WITH GRANT OPTION;
15+
GRANT EXECUTE ON FUNCTION vault.create_secret, vault.update_secret, vault._crypto_aead_det_decrypt TO postgres WITH GRANT OPTION;
16+
ELSE
17+
pgsodium_exists = (
1618
select count(*) = 1
1719
from pg_available_extensions
18-
where name = 'supabase_vault'
19-
);
20-
21-
IF pgsodium_exists
22-
THEN
23-
create extension if not exists pgsodium;
24-
25-
grant pgsodium_keyiduser to postgres with admin option;
26-
grant pgsodium_keyholder to postgres with admin option;
27-
grant pgsodium_keymaker to postgres with admin option;
28-
29-
grant execute on function pgsodium.crypto_aead_det_decrypt(bytea, bytea, uuid, bytea) to service_role;
30-
grant execute on function pgsodium.crypto_aead_det_encrypt(bytea, bytea, uuid, bytea) to service_role;
31-
grant execute on function pgsodium.crypto_aead_det_keygen to service_role;
32-
33-
IF vault_exists
20+
where name = 'pgsodium'
21+
and default_version in ('3.1.6', '3.1.7', '3.1.8', '3.1.9')
22+
);
23+
24+
vault_exists = (
25+
select count(*) = 1
26+
from pg_available_extensions
27+
where name = 'supabase_vault'
28+
);
29+
30+
IF pgsodium_exists
3431
THEN
35-
create extension if not exists supabase_vault;
32+
create extension if not exists pgsodium;
33+
34+
grant pgsodium_keyiduser to postgres with admin option;
35+
grant pgsodium_keyholder to postgres with admin option;
36+
grant pgsodium_keymaker to postgres with admin option;
37+
38+
grant execute on function pgsodium.crypto_aead_det_decrypt(bytea, bytea, uuid, bytea) to service_role;
39+
grant execute on function pgsodium.crypto_aead_det_encrypt(bytea, bytea, uuid, bytea) to service_role;
40+
grant execute on function pgsodium.crypto_aead_det_keygen to service_role;
41+
42+
IF vault_exists
43+
THEN
44+
create extension if not exists supabase_vault;
45+
END IF;
3646
END IF;
3747
END IF;
3848
END $$;

0 commit comments

Comments
 (0)