Skip to content

Commit

Permalink
Schema-qualify
Browse files Browse the repository at this point in the history
 * Schema-qualifies everything.
 * Removes `search_path` assignments.
 * Moves all functions to the `artwork_indexer` schema. This is more
   convenient than having them in separate schemas.
  • Loading branch information
mwiencek committed Feb 23, 2024
1 parent b8bdf5f commit 85a8a99
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 80 deletions.
35 changes: 16 additions & 19 deletions generate_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
q_im_table = f'{im_schema}.{im_table}'

for tg_op in im['tg_ops']:
extra_functions_source += f'\nCREATE OR REPLACE FUNCTION artwork_indexer_a_{tg_op}_{im_table}() RETURNS trigger AS $$\n'
extra_functions_source += f'\nCREATE OR REPLACE FUNCTION artwork_indexer.a_{tg_op}_{im_table}() RETURNS trigger AS $$\n'
extra_functions_source += 'BEGIN\n'

tg_rowvar = 'OLD' if tg_op == 'del' else 'NEW'
Expand Down Expand Up @@ -128,15 +128,15 @@
extra_functions_source += 'END;\n'
extra_functions_source += '$$ LANGUAGE plpgsql;\n'

tg_fn_name = f'artwork_indexer_a_{tg_op}_{im_table}'
tg_name = f'{tg_fn_name}'
tg_fn_name = f'a_{tg_op}_{im_table}'
tg_name = f'artwork_indexer_{tg_fn_name}'

extra_triggers_source += dedent(f'''
DROP TRIGGER IF EXISTS {tg_name} ON {q_im_table};
CREATE TRIGGER {tg_name} AFTER {TG_OP_FULLNAMES[tg_op]}
ON {q_im_table} FOR EACH ROW
EXECUTE PROCEDURE {art_schema}.{tg_fn_name}();
EXECUTE PROCEDURE artwork_indexer.{tg_fn_name}();
''')

def index_artwork_stmt(gids, parent):
Expand Down Expand Up @@ -179,9 +179,7 @@ def delete_artwork_stmt(artwork_id, gid, suffix, parent, return_var):
functions_source = dedent(f'''\
-- Automatically generated, do not edit.
SET LOCAL search_path = {art_schema};
CREATE OR REPLACE FUNCTION artwork_indexer_a_ins_{art_table}() RETURNS trigger AS $$
CREATE OR REPLACE FUNCTION artwork_indexer.a_ins_{art_table}() RETURNS trigger AS $$
DECLARE
{entity_type}_gid UUID;
BEGIN
Expand All @@ -196,7 +194,7 @@ def delete_artwork_stmt(artwork_id, gid, suffix, parent, return_var):
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION artwork_indexer_a_upd_{art_table}() RETURNS trigger AS $$
CREATE OR REPLACE FUNCTION artwork_indexer.a_upd_{art_table}() RETURNS trigger AS $$
DECLARE
suffix TEXT;
old_{entity_type}_gid UUID;
Expand Down Expand Up @@ -243,7 +241,7 @@ def delete_artwork_stmt(artwork_id, gid, suffix, parent, return_var):
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION artwork_indexer_a_del_{art_table}()
CREATE OR REPLACE FUNCTION artwork_indexer.a_del_{art_table}()
RETURNS trigger AS $$
DECLARE
suffix TEXT;
Expand All @@ -263,7 +261,7 @@ def delete_artwork_stmt(artwork_id, gid, suffix, parent, return_var):
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION artwork_indexer_a_ins_{art_table}_type() RETURNS trigger AS $$
CREATE OR REPLACE FUNCTION artwork_indexer.a_ins_{art_table}_type() RETURNS trigger AS $$
DECLARE
{entity_type}_gid UUID;
BEGIN
Expand All @@ -279,7 +277,7 @@ def delete_artwork_stmt(artwork_id, gid, suffix, parent, return_var):
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION artwork_indexer_a_del_{art_table}_type() RETURNS trigger AS $$
CREATE OR REPLACE FUNCTION artwork_indexer.a_del_{art_table}_type() RETURNS trigger AS $$
DECLARE
{entity_type}_gid UUID;
BEGIN
Expand All @@ -299,7 +297,7 @@ def delete_artwork_stmt(artwork_id, gid, suffix, parent, return_var):
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION artwork_indexer_a_del_{entity_table}() RETURNS trigger AS $$
CREATE OR REPLACE FUNCTION artwork_indexer.a_del_{entity_table}() RETURNS trigger AS $$
BEGIN
INSERT INTO artwork_indexer.event_queue (entity_type, action, message)
VALUES ('{entity_type}', 'deindex', jsonb_build_object('gid', OLD.gid))
Expand Down Expand Up @@ -327,7 +325,6 @@ def delete_artwork_stmt(artwork_id, gid, suffix, parent, return_var):
triggers_source = dedent(f'''\
-- Automatically generated, do not edit.
SET LOCAL search_path = '{art_schema}';
SET LOCAL client_min_messages = warning;
-- We drop the triggers first to simulate "CREATE OR REPLACE,"
Expand All @@ -337,37 +334,37 @@ def delete_artwork_stmt(artwork_id, gid, suffix, parent, return_var):
CREATE TRIGGER artwork_indexer_a_ins_{art_table} AFTER INSERT
ON {art_schema}.{art_table} FOR EACH ROW
EXECUTE PROCEDURE {art_schema}.artwork_indexer_a_ins_{art_table}();
EXECUTE PROCEDURE artwork_indexer.a_ins_{art_table}();
DROP TRIGGER IF EXISTS artwork_indexer_a_upd_{art_table} ON {art_schema}.{art_table};
CREATE TRIGGER artwork_indexer_a_upd_{art_table} AFTER UPDATE
ON {art_schema}.{art_table} FOR EACH ROW
EXECUTE PROCEDURE {art_schema}.artwork_indexer_a_upd_{art_table}();
EXECUTE PROCEDURE artwork_indexer.a_upd_{art_table}();
DROP TRIGGER IF EXISTS artwork_indexer_a_del_{art_table} ON {art_schema}.{art_table};
CREATE TRIGGER artwork_indexer_a_del_{art_table} AFTER DELETE
ON {art_schema}.{art_table} FOR EACH ROW
EXECUTE PROCEDURE {art_schema}.artwork_indexer_a_del_{art_table}();
EXECUTE PROCEDURE artwork_indexer.a_del_{art_table}();
DROP TRIGGER IF EXISTS artwork_indexer_a_ins_{art_table}_type ON {art_schema}.{art_table}_type;
CREATE TRIGGER artwork_indexer_a_ins_{art_table}_type AFTER INSERT
ON {art_schema}.{art_table}_type FOR EACH ROW
EXECUTE PROCEDURE {art_schema}.artwork_indexer_a_ins_{art_table}_type();
EXECUTE PROCEDURE artwork_indexer.a_ins_{art_table}_type();
DROP TRIGGER IF EXISTS artwork_indexer_a_del_{art_table}_type ON {art_schema}.{art_table}_type;
CREATE TRIGGER artwork_indexer_a_del_{art_table}_type AFTER DELETE
ON {art_schema}.{art_table}_type FOR EACH ROW
EXECUTE PROCEDURE {art_schema}.artwork_indexer_a_del_{art_table}_type();
EXECUTE PROCEDURE artwork_indexer.a_del_{art_table}_type();
DROP TRIGGER IF EXISTS artwork_indexer_a_del_{entity_table} ON {entity_schema}.{entity_table};
CREATE TRIGGER artwork_indexer_a_del_{entity_table} AFTER DELETE
ON {entity_schema}.{entity_table} FOR EACH ROW
EXECUTE PROCEDURE {art_schema}.artwork_indexer_a_del_{entity_table}();
EXECUTE PROCEDURE artwork_indexer.a_del_{entity_table}();
''')

triggers_source += extra_triggers_source + '\n'
Expand Down
24 changes: 11 additions & 13 deletions sql/caa_functions.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
-- Automatically generated, do not edit.

SET LOCAL search_path = cover_art_archive;

CREATE OR REPLACE FUNCTION artwork_indexer_a_ins_cover_art() RETURNS trigger AS $$
CREATE OR REPLACE FUNCTION artwork_indexer.a_ins_cover_art() RETURNS trigger AS $$
DECLARE
release_gid UUID;
BEGIN
Expand All @@ -17,7 +15,7 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION artwork_indexer_a_upd_cover_art() RETURNS trigger AS $$
CREATE OR REPLACE FUNCTION artwork_indexer.a_upd_cover_art() RETURNS trigger AS $$
DECLARE
suffix TEXT;
old_release_gid UUID;
Expand Down Expand Up @@ -64,7 +62,7 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION artwork_indexer_a_del_cover_art()
CREATE OR REPLACE FUNCTION artwork_indexer.a_del_cover_art()
RETURNS trigger AS $$
DECLARE
suffix TEXT;
Expand All @@ -84,7 +82,7 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION artwork_indexer_a_ins_cover_art_type() RETURNS trigger AS $$
CREATE OR REPLACE FUNCTION artwork_indexer.a_ins_cover_art_type() RETURNS trigger AS $$
DECLARE
release_gid UUID;
BEGIN
Expand All @@ -100,7 +98,7 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION artwork_indexer_a_del_cover_art_type() RETURNS trigger AS $$
CREATE OR REPLACE FUNCTION artwork_indexer.a_del_cover_art_type() RETURNS trigger AS $$
DECLARE
release_gid UUID;
BEGIN
Expand All @@ -120,7 +118,7 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION artwork_indexer_a_del_release() RETURNS trigger AS $$
CREATE OR REPLACE FUNCTION artwork_indexer.a_del_release() RETURNS trigger AS $$
BEGIN
INSERT INTO artwork_indexer.event_queue (entity_type, action, message)
VALUES ('release', 'deindex', jsonb_build_object('gid', OLD.gid))
Expand All @@ -138,7 +136,7 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION artwork_indexer_a_upd_artist() RETURNS trigger AS $$
CREATE OR REPLACE FUNCTION artwork_indexer.a_upd_artist() RETURNS trigger AS $$
BEGIN
IF (OLD.name != NEW.name OR OLD.sort_name != NEW.sort_name) THEN
INSERT INTO artwork_indexer.event_queue (entity_type, action, message) (
Expand All @@ -158,7 +156,7 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION artwork_indexer_a_upd_release() RETURNS trigger AS $$
CREATE OR REPLACE FUNCTION artwork_indexer.a_upd_release() RETURNS trigger AS $$
BEGIN
IF (OLD.name != NEW.name OR OLD.artist_credit != NEW.artist_credit OR OLD.language IS DISTINCT FROM NEW.language OR OLD.barcode IS DISTINCT FROM NEW.barcode) THEN
INSERT INTO artwork_indexer.event_queue (entity_type, action, message) (
Expand All @@ -177,7 +175,7 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION artwork_indexer_a_upd_release_meta() RETURNS trigger AS $$
CREATE OR REPLACE FUNCTION artwork_indexer.a_upd_release_meta() RETURNS trigger AS $$
BEGIN
IF (OLD.amazon_asin IS DISTINCT FROM NEW.amazon_asin) THEN
INSERT INTO artwork_indexer.event_queue (entity_type, action, message) (
Expand All @@ -196,7 +194,7 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION artwork_indexer_a_ins_release_first_release_date() RETURNS trigger AS $$
CREATE OR REPLACE FUNCTION artwork_indexer.a_ins_release_first_release_date() RETURNS trigger AS $$
BEGIN
INSERT INTO artwork_indexer.event_queue (entity_type, action, message) (
SELECT 'release', 'index', jsonb_build_object('gid', musicbrainz.release.gid)
Expand All @@ -213,7 +211,7 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION artwork_indexer_a_del_release_first_release_date() RETURNS trigger AS $$
CREATE OR REPLACE FUNCTION artwork_indexer.a_del_release_first_release_date() RETURNS trigger AS $$
BEGIN
INSERT INTO artwork_indexer.event_queue (entity_type, action, message) (
SELECT 'release', 'index', jsonb_build_object('gid', musicbrainz.release.gid)
Expand Down
23 changes: 11 additions & 12 deletions sql/caa_triggers.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
-- Automatically generated, do not edit.

SET LOCAL search_path = 'cover_art_archive';
SET LOCAL client_min_messages = warning;

-- We drop the triggers first to simulate "CREATE OR REPLACE,"
Expand All @@ -10,65 +9,65 @@ DROP TRIGGER IF EXISTS artwork_indexer_a_ins_cover_art ON cover_art_archive.cove

CREATE TRIGGER artwork_indexer_a_ins_cover_art AFTER INSERT
ON cover_art_archive.cover_art FOR EACH ROW
EXECUTE PROCEDURE cover_art_archive.artwork_indexer_a_ins_cover_art();
EXECUTE PROCEDURE artwork_indexer.a_ins_cover_art();

DROP TRIGGER IF EXISTS artwork_indexer_a_upd_cover_art ON cover_art_archive.cover_art;

CREATE TRIGGER artwork_indexer_a_upd_cover_art AFTER UPDATE
ON cover_art_archive.cover_art FOR EACH ROW
EXECUTE PROCEDURE cover_art_archive.artwork_indexer_a_upd_cover_art();
EXECUTE PROCEDURE artwork_indexer.a_upd_cover_art();

DROP TRIGGER IF EXISTS artwork_indexer_a_del_cover_art ON cover_art_archive.cover_art;

CREATE TRIGGER artwork_indexer_a_del_cover_art AFTER DELETE
ON cover_art_archive.cover_art FOR EACH ROW
EXECUTE PROCEDURE cover_art_archive.artwork_indexer_a_del_cover_art();
EXECUTE PROCEDURE artwork_indexer.a_del_cover_art();

DROP TRIGGER IF EXISTS artwork_indexer_a_ins_cover_art_type ON cover_art_archive.cover_art_type;

CREATE TRIGGER artwork_indexer_a_ins_cover_art_type AFTER INSERT
ON cover_art_archive.cover_art_type FOR EACH ROW
EXECUTE PROCEDURE cover_art_archive.artwork_indexer_a_ins_cover_art_type();
EXECUTE PROCEDURE artwork_indexer.a_ins_cover_art_type();

DROP TRIGGER IF EXISTS artwork_indexer_a_del_cover_art_type ON cover_art_archive.cover_art_type;

CREATE TRIGGER artwork_indexer_a_del_cover_art_type AFTER DELETE
ON cover_art_archive.cover_art_type FOR EACH ROW
EXECUTE PROCEDURE cover_art_archive.artwork_indexer_a_del_cover_art_type();
EXECUTE PROCEDURE artwork_indexer.a_del_cover_art_type();

DROP TRIGGER IF EXISTS artwork_indexer_a_del_release ON musicbrainz.release;

CREATE TRIGGER artwork_indexer_a_del_release AFTER DELETE
ON musicbrainz.release FOR EACH ROW
EXECUTE PROCEDURE cover_art_archive.artwork_indexer_a_del_release();
EXECUTE PROCEDURE artwork_indexer.a_del_release();

DROP TRIGGER IF EXISTS artwork_indexer_a_upd_artist ON musicbrainz.artist;

CREATE TRIGGER artwork_indexer_a_upd_artist AFTER UPDATE
ON musicbrainz.artist FOR EACH ROW
EXECUTE PROCEDURE cover_art_archive.artwork_indexer_a_upd_artist();
EXECUTE PROCEDURE artwork_indexer.a_upd_artist();

DROP TRIGGER IF EXISTS artwork_indexer_a_upd_release ON musicbrainz.release;

CREATE TRIGGER artwork_indexer_a_upd_release AFTER UPDATE
ON musicbrainz.release FOR EACH ROW
EXECUTE PROCEDURE cover_art_archive.artwork_indexer_a_upd_release();
EXECUTE PROCEDURE artwork_indexer.a_upd_release();

DROP TRIGGER IF EXISTS artwork_indexer_a_upd_release_meta ON musicbrainz.release_meta;

CREATE TRIGGER artwork_indexer_a_upd_release_meta AFTER UPDATE
ON musicbrainz.release_meta FOR EACH ROW
EXECUTE PROCEDURE cover_art_archive.artwork_indexer_a_upd_release_meta();
EXECUTE PROCEDURE artwork_indexer.a_upd_release_meta();

DROP TRIGGER IF EXISTS artwork_indexer_a_ins_release_first_release_date ON musicbrainz.release_first_release_date;

CREATE TRIGGER artwork_indexer_a_ins_release_first_release_date AFTER INSERT
ON musicbrainz.release_first_release_date FOR EACH ROW
EXECUTE PROCEDURE cover_art_archive.artwork_indexer_a_ins_release_first_release_date();
EXECUTE PROCEDURE artwork_indexer.a_ins_release_first_release_date();

DROP TRIGGER IF EXISTS artwork_indexer_a_del_release_first_release_date ON musicbrainz.release_first_release_date;

CREATE TRIGGER artwork_indexer_a_del_release_first_release_date AFTER DELETE
ON musicbrainz.release_first_release_date FOR EACH ROW
EXECUTE PROCEDURE cover_art_archive.artwork_indexer_a_del_release_first_release_date();
EXECUTE PROCEDURE artwork_indexer.a_del_release_first_release_date();

Loading

0 comments on commit 85a8a99

Please sign in to comment.