I have a .sql script like this:
DO $$
DECLARE
prev_count := (SELECT count(*) FROM ...);
END$$;
UPDATE [...]
DO $$
DECLARE
cur_count := (SELECT count(*) FROM ...);
BEGIN
ASSERT cur_count = prev_count, 'Mismatch';
END$$;
In which I get some value, modify the database, and expect a new value to match an old value. However, I get errors like this:
psql:migration.sql:163: ERROR: column "prev_count" does not exist
LINE 1: SELECT cur_count = prev_count
^
QUERY: SELECT cur_count = prev_count
CONTEXT: PL/pgSQL function inline_code_block line 4 at ASSERT
I can't tell if this is a scoping issue because of the anonymous block, and why it's attempting to treat my variables like columns. Any ideas?
prev_countis a column because the variable nameprev_countno longer exists in the current scope. If this is a repeated task with a static update function I would probably just create a stored proc