forked from ovh/cds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path000_create_all.sql
More file actions
80 lines (72 loc) · 2.7 KB
/
000_create_all.sql
File metadata and controls
80 lines (72 loc) · 2.7 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
-- +migrate Up
-- +migrate StatementBegin
CREATE OR REPLACE FUNCTION create_index(table_name text, index_name text, column_name text) RETURNS void AS $$
declare
l_count integer;
begin
select count(*)
into l_count
from pg_indexes
where schemaname = 'public'
and tablename = lower(table_name)
and indexname = lower(index_name);
if l_count = 0 then
execute 'create index ' || index_name || ' on "' || table_name || '"(' || column_name || ')';
end if;
end;
$$ LANGUAGE plpgsql;
-- +migrate StatementEnd
-- +migrate StatementBegin
CREATE OR REPLACE FUNCTION create_unique_index(table_name text, index_name text, column_names text) RETURNS void AS $$
declare
l_count integer;
begin
select count(*)
into l_count
from pg_indexes
where schemaname = 'public'
and tablename = lower(table_name)
and indexname = lower(index_name);
if l_count = 0 then
execute 'create unique index ' || index_name || ' on "' || table_name || '"(' || array_to_string(string_to_array(column_names, ',') , ',') || ')';
end if;
end;
$$ LANGUAGE plpgsql;
-- +migrate StatementEnd
-- +migrate StatementBegin
CREATE OR REPLACE FUNCTION create_foreign_key(fk_name text, table_name_child text, table_name_parent text, column_name_child text, column_name_parent text) RETURNS void AS $$
declare
l_count integer;
begin
select count(*)
into l_count
from information_schema.table_constraints as tc
where constraint_type = 'FOREIGN KEY'
and tc.table_name = lower(table_name_child)
and tc.constraint_name = lower(fk_name);
if l_count = 0 then
execute 'alter table "' || table_name_child || '" ADD CONSTRAINT ' || fk_name || ' FOREIGN KEY(' || column_name_child || ') REFERENCES "' || table_name_parent || '"(' || column_name_parent || ')';
end if;
end;
$$ LANGUAGE plpgsql;
-- +migrate StatementEnd
-- +migrate StatementBegin
CREATE OR REPLACE FUNCTION create_foreign_key_idx_cascade(fk_name text, table_name_child text, table_name_parent text, column_name_child text, column_name_parent text) RETURNS void AS $$
declare
l_count integer;
begin
select count(*)
into l_count
from information_schema.table_constraints as tc
where constraint_type = 'FOREIGN KEY'
and tc.table_name = lower(table_name_child)
and tc.constraint_name = lower(fk_name);
if l_count = 0 then
execute 'alter table "' || table_name_child || '" ADD CONSTRAINT ' || fk_name || ' FOREIGN KEY(' || column_name_child || ') REFERENCES "' || table_name_parent || '"(' || column_name_parent || ') ON DELETE CASCADE';
execute create_index(table_name_child, 'IDX_' || fk_name, column_name_child);
end if;
end;
$$ LANGUAGE plpgsql;
-- +migrate StatementEnd
-- +migrate Down
-- nothing to downgrade, it's a creation !