-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimestamp-triggers.py
77 lines (66 loc) · 2.72 KB
/
timestamp-triggers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Generate SQL for automatically updating timestamp columns.
def __name(table, column):
return "{table}_{column}".format(table=table, column=column)
def create_function_sql(name, definition, returns='trigger'):
"""Generate SQL for creating a function."""
return """
CREATE OR REPLACE FUNCTION {name} ()
RETURNS {returns}
LANGUAGE 'plpgsql'
AS $${definition}$$;
""".format(name=name, definition=definition, returns=returns)
def drop_function_sql(name):
"""Generate SQL for dropping a function."""
return "DROP FUNCTION IF EXISTS {name}();".format(name=name)
def create_trigger_sql(table, trigger_name, function_name, events=['INSERT', 'UPDATE']):
"""Generate SQL for creating a trigger."""
return """
CREATE TRIGGER {name} BEFORE {events} on {table}
FOR EACH ROW EXECUTE PROCEDURE {function}();
""".format(
name=trigger_name,
events=' OR '.join(events),
table=table,
function=function_name
)
def drop_trigger_sql(table, name):
"""Generate SQL for dropping a trigger."""
return "DROP TRIGGER IF EXISTS {name} on {table};".format(name=name, table=table)
def create_timestamp_trigger(table, column, definition):
"""Generate SQL for udpating a timestamp column."""
name = __name(table, column)
create_function = create_function_sql(name, definition)
create_trigger = create_trigger_sql(table, name, name)
return "\n".join([create_function, create_trigger])
def drop_timestamp_trigger(table, column):
"""Generate SQL for dropping a timestamp trigger on a column."""
name = __name(table, column)
drop_function = drop_function_sql(name)
drop_trigger = drop_trigger_sql(table, name)
return "\n".join([drop_trigger, drop_function])
def create_timestamp_trigger_for_creation(table, column='created'):
"""Generate SQL for updating a timestamp column on creation."""
return create_timestamp_trigger(table, column, """
BEGIN
IF (TG_OP = 'UPDATE') THEN
NEW.{column} := OLD.{column};
ELSIF (TG_OP = 'INSERT') THEN
NEW.{column} := CURRENT_TIMESTAMP;
END IF;
RETURN NEW;
END;
""".format(column=column))
def drop_timestamp_trigger_for_creation(table, column='created'):
"""Generate SQL to stop updating a timestamp column on creation."""
return drop_timestamp_trigger(table, column)
def create_timestamp_trigger_for_modification(table, column='modified'):
"""Generate SQL for updating a timestamp column on modification."""
return create_timestamp_trigger(table, column, """
BEGIN
NEW.{column} := CURRENT_TIMESTAMP;
RETURN NEW;
END;
""".format(column=column))
def drop_timestamp_trigger_for_modification(table, column='modified'):
"""Generate SQL to stop updating a timestamp column on modification."""
return drop_timestamp_trigger(table, column)