-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisallow_column_name_as_id.sql
69 lines (62 loc) · 2.37 KB
/
disallow_column_name_as_id.sql
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
-- disable any user on any schema create new table specify column name as id
-- alter table, alter materialzied view also not allow new column name as id.
CREATE OR REPLACE FUNCTION event_trigger_disable_column_name_id ()
RETURNS event_trigger
LANGUAGE plpgsql
AS $$
DECLARE
obj record;
_object_identity text;
command text;
BEGIN
FOR obj IN
SELECT
*
FROM
pg_event_trigger_ddl_commands ()
LOOP
command := obj.command_tag;
_object_identity := obj.object_identity;
IF command IN ('CREATE MATERIALIZED VIEW', 'CREATE TABLE') THEN
IF EXISTS (
SELECT
FROM
pg_attribute a
WHERE
a.attrelid = _object_identity::regclass
AND attname = 'id') THEN
RAISE EXCEPTION 'table or materialized view cannot have column name: "id"';
END IF;
END IF;
IF command IN ('ALTER MATERIALIZED VIEW', 'ALTER TABLE') THEN
IF (substring(_object_identity FROM (LENGTH(_object_identity) + 2 - STRPOS(REVERSE(_object_identity), '.'))) = 'id') THEN
RAISE EXCEPTION 'alter table/materialized view new column name cannot be: "id"';
END IF;
END IF;
END LOOP;
END;
$$;
DROP EVENT TRIGGER IF EXISTS event_trigger_disable_column_name_id_trg;
CREATE EVENT TRIGGER event_trigger_disable_column_name_id_trg ON ddl_command_end
WHEN TAG IN ('CREATE TABLE', 'CREATE MATERIALIZED VIEW', 'ALTER MATERIALIZED VIEW', 'ALTER TABLE')
EXECUTE FUNCTION event_trigger_disable_column_name_id ();
--------------------------------------------------------------------------
---test
BEGIN;
ALTER TABLE t1 RENAME col1 TO id;
-- ERROR: alter table/materialized view new column name cannot be: "id"
-- CONTEXT: PL/pgSQL function event_trigger_disable_column_name_id() line 33 at RAISE
BEGIN;
CREATE TABLE test_evetrg (id int);
-- ERROR: table or materialized view cannot have column name: "id"
-- CONTEXT: PL/pgSQL function test_event_trigger_rel_with_id() line 22 at RAISE
END;
BEGIN;
CREATE MATERIALIZED VIEW test_mv AS
SELECT
*
FROM
a; -- table a already have column name as id.
-- ERROR: table or materialized view cannot have column name: "id"
-- CONTEXT: PL/pgSQL function test_event_trigger_rel_with_id() line 22 at RAISE
END;