Fix migration 002: psql does not interpolate :'var' inside DO $$ blocks
CREATE ROLE ... PASSWORD :'teecup_app_password' silently failed with "syntax error at or near ':'" because the whole DO $$ ... $$ body is a dollar-quoted string and psql skips variable substitution inside it. Replaced the idempotency check with \gset + \if/\else so the PASSWORD line is plain top-level SQL, where interpolation does work. Verified against a scratch database (see test_isolation.sql run).
This commit is contained in:
parent
4f90fb05dc
commit
576ae64fa3
1 changed files with 19 additions and 13 deletions
|
|
@ -19,9 +19,16 @@
|
|||
|
||||
-- --- Rolle (cluster-nivå: opprett bare hvis den ikke finnes) ----------
|
||||
-- Eksplisitt NOSUPERUSER / NOBYPASSRLS — dette er hele poenget.
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'teecup_app') THEN
|
||||
-- MERK: dette var opprinnelig en DO $$ ... $$-blokk med
|
||||
-- "PASSWORD :'teecup_app_password'" inni. psql interpolerer ikke :'var'
|
||||
-- inne i dollar-quoted strenger, så det feilet alltid med
|
||||
-- "syntax error at or near ':'". Erstattet med \if/\else slik at
|
||||
-- PASSWORD-linjen står som vanlig SQL utenfor $$ $$ (verifisert mot
|
||||
-- scratch-database, se test_isolation.sql-kjøringen).
|
||||
SELECT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'teecup_app') AS teecup_app_exists \gset
|
||||
\if :teecup_app_exists
|
||||
\echo 'teecup_app finnes allerede, hopper over CREATE ROLE'
|
||||
\else
|
||||
CREATE ROLE teecup_app
|
||||
LOGIN
|
||||
PASSWORD :'teecup_app_password'
|
||||
|
|
@ -30,8 +37,7 @@ BEGIN
|
|||
NOCREATEROLE
|
||||
NOBYPASSRLS
|
||||
INHERIT;
|
||||
END IF;
|
||||
END $$;
|
||||
\endif
|
||||
|
||||
-- --- Grants (kjøres mens du er koblet til teecup_db) ------------------
|
||||
GRANT CONNECT ON DATABASE teecup_db TO teecup_app;
|
||||
|
|
|
|||
Loading…
Reference in a new issue