Add trigger and function for updated_date and created_date column in gepafin_schema

This commit is contained in:
harish
2024-09-23 18:19:14 +05:30
parent 2e2ad1c9e1
commit b28f5b71fc
3 changed files with 83 additions and 2 deletions

View File

@@ -0,0 +1,51 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-4.0.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.23.xsd">
<changeSet id="23-09-2024_3" author="Harish Bagora" runAlways="true">
<sql splitStatements="false">
DO $$
DECLARE
r RECORD;
BEGIN
-- Loop through all tables in the schema that have the 'updated_date' column
FOR r IN (
SELECT table_name
FROM information_schema.columns
WHERE column_name = 'updated_date'
AND table_schema = 'gepafin_schema'
)
LOOP
EXECUTE format(
'CREATE OR REPLACE TRIGGER tg_gepafin_schema_updated_at_%I
BEFORE UPDATE ON gepafin_schema.%I
FOR EACH ROW
EXECUTE FUNCTION gepafin_schema.clock_timestamp_updated_date_column()',
r.table_name, r.table_name
);
END LOOP;
-- Loop through all tables in the schema that have the 'created_date' column
FOR r IN (
SELECT table_name
FROM information_schema.columns
WHERE column_name = 'created_date'
AND table_schema = 'gepafin_schema'
)
LOOP
EXECUTE format(
'CREATE OR REPLACE TRIGGER tg_gepafin_schema_created_at_%I
BEFORE INSERT ON gepafin_schema.%I
FOR EACH ROW
EXECUTE FUNCTION gepafin_schema.clock_timestamp_created_date_column()',
r.table_name, r.table_name
);
END LOOP;
END;
$$ LANGUAGE plpgsql;
</sql>
</changeSet>
</databaseChangeLog>