Skip to content

Commit

Permalink
Add modification_last_modified functions
Browse files Browse the repository at this point in the history
Add modification_last_modified functions
  • Loading branch information
ponceta committed Oct 25, 2024
1 parent c2a5d60 commit 2f9f03e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
3 changes: 3 additions & 0 deletions datamodel/app/create_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ def create_app(

run_sql("CREATE SCHEMA tdh_app;", pg_service)

# Create application functions

run_sql_file("functions/oid_functions.sql", pg_service, variables)
run_sql_file("functions/modification_functions.sql", pg_service)
run_sql_file("functions/organisation_functions.sql", pg_service, variables)

# to do add symbology_function and geometry_funcions for TEKSI Distance heating
Expand Down
72 changes: 72 additions & 0 deletions datamodel/app/functions/modification_functions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
------ LAST MODIFIED -----
CREATE FUNCTION tdh_app.modification_last_modified() RETURNS trigger AS $$
BEGIN
NEW.last_modification := TIMEOFDAY();

RETURN NEW;
END;
$$ LANGUAGE PLPGSQL;

CREATE FUNCTION tdh_app.modification_last_modified_parent() RETURNS trigger AS $$
DECLARE
table_name TEXT;
BEGIN
table_name = TG_ARGV[0];

EXECUTE '
UPDATE ' || table_name || '
SET last_modification = TIMEOFDAY()::timestamp
WHERE obj_id = ''' || NEW.obj_id || '''
';
RETURN NEW;
END;
$$ LANGUAGE PLPGSQL;

CREATE OR REPLACE FUNCTION tdh_app.alter_modification_triggers(action_name text) RETURNS VOID AS
$DO$
DECLARE
schdf text;
tbldf text;
trig text;
BEGIN
IF NOT (action_name ILIKE ANY(ARRAY['ENABLE','DISABLE'])) THEN
RAISE NOTICE '% not a valid input',action_name;
RETURN;
ELSE
FOR schdf,tbldf, trig IN
SELECT
c.relnamespace ::regnamespace::text,
c.relname,
t.tgname
FROM pg_trigger t
INNER JOIN pg_class c on t.tgrelid=c.oid
INNER JOIN pg_proc p on t.tgfoid=p.oid
WHERE p.proname LIKE 'modification_%'
AND p.pronamespace::regnamespace::text = 'tdh_app'
LOOP
EXECUTE FORMAT('ALTER TABLE %I.%I %s TRIGGER %I',schdf,tbldf,upper(action_name),trig);
END LOOP;

RETURN;
END IF;
END;
$DO$
LANGUAGE plpgsql SECURITY DEFINER;

CREATE OR REPLACE FUNCTION tdh_app.check_modification_enabled() RETURNS BOOL AS
$DO$
DECLARE _disabled_count numeric;
BEGIN
SELECT count(*) into _disabled_count
FROM pg_trigger t
INNER JOIN pg_class c on t.tgrelid=c.oid
INNER JOIN pg_proc p on t.tgfoid=p.oid
WHERE
p.proname LIKE 'modification_%'
AND p.pronamespace::regnamespace::text = 'tdh_app'

AND t.tgenabled = 'D';
RETURN _disabled_count=0;
END;
$DO$
LANGUAGE plpgsql SECURITY DEFINER;

0 comments on commit 2f9f03e

Please sign in to comment.