-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpgsql.json
More file actions
130 lines (130 loc) · 4.99 KB
/
Copy pathpgsql.json
File metadata and controls
130 lines (130 loc) · 4.99 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
{
"create table": {
"prefix": "CTAB",
"body": [
"create table if not exists ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}.${1} (",
"$2",
");"
],
"description": "Create a table"
},
"create a function": {
"prefix": "CFUN",
"body": [
"create or replace function ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}.${1}($2) returns ${3}",
" language plpgsql",
" as $$",
"begin",
"${4}",
"end",
"$$;",
],
"description": "create a function"
},
"create table with row level security": {
"prefix": "CTRLS",
"body": [
"create table ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}.${TM_FILENAME_BASE} (",
" id UUID PRIMARY KEY DEFAULT uuid_generate_v1mc(),",
" $1 TEXT not null,",
" $2 INT",
" role TEXT REFERENCES ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}_private.users(role),",
" created_at TIMESTAMPTZ NOT NULL DEFAULT now()",
");",
"",
"ALTER TABLE ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}.${TM_FILENAME_BASE} ENABLE ROW LEVEL SECURITY;",
"",
"-- only show users own data",
"drop trigger if exists ensure_role_on_${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}_${TM_FILENAME_BASE} on ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}.${TM_FILENAME_BASE};",
"create trigger ensure_role_on_${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}_${TM_FILENAME_BASE}",
" before insert on ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}.${TM_FILENAME_BASE}",
" for each row",
" execute procedure ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}_private.set_role();",
"",
"drop policy if exists ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}_${TM_FILENAME_BASE}_policy on ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}.${TM_FILENAME_BASE};",
"CREATE POLICY ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}_${TM_FILENAME_BASE}_policy ON ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}.${TM_FILENAME_BASE}",
" for all",
" USING (role = current_user);"
],
"description": "Create a table with row level security and trigger to set owners"
},
"create after trigger function on table": {
"prefix": "TRGAFTFUN",
"body": [
"create or replace function ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}.${1}() returns trigger",
" language plpgsql",
" as $$",
"begin",
"${4}",
"end",
"$$;",
"",
"drop trigger if exists trigger_${1} on ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}.${2};",
"create constraint trigger trigger_${1}",
" after ${3} on ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}.${2}",
" for each row",
" execute procedure ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}.${1}();"
],
"description": "Create a function"
},
"create before trigger function on table": {
"prefix": "TRGBEFFUN",
"body": [
"create or replace function ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}.${1}() returns trigger",
" language plpgsql",
" as $$",
"begin",
"${4}",
"end",
"$$;",
"",
"drop trigger if exists trigger_${1} on ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}.${2};",
"create trigger trigger_${1}",
" before ${3} on ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}.${2}",
" for each row",
" execute procedure ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}.${1}();"
],
"description": "Create a function"
},
"Create function returning a trigger in plpgsql": {
"prefix": "CTFUN",
"body": [
"create or replace function ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}.${1}() returns trigger language plpgsql",
"as $$",
"begin",
" $2",
" return new;",
"end",
"$$;"
],
"description": "Create a plpgsql trigger"
},
"Create a schema": {
"prefix": "CSCH",
"body": ["create schema if not exists ${1};"],
"description": "Create a schema"
},
"Create role": {
"prefix": "CROL",
"body": ["create role ${1};"],
"description": "Create a role"
},
"Create index": {
"prefix": "ci",
"body": ["create index if not exists ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}_${TM_FILENAME_BASE}_${1}_index on ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}.${TM_FILENAME_BASE}(${1});"],
"description": "Create a role"
},
"quote identifier": {
"prefix": "qid",
"body": "quote_ident(${1})",
"description": "Quote identifier"
},
"create type": {
"prefix": "CTYP",
"body": [
"drop type if exists ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}.${1} cascade;",
"create type ${TM_DIRECTORY/.*\\/([^\\/]+)/$1/}.${1} AS (${2});"
],
"description": "create a new type"
}
}