-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoconnor-26.000-26.001.sql
More file actions
65 lines (60 loc) · 1.86 KB
/
oconnor-26.000-26.001.sql
File metadata and controls
65 lines (60 loc) · 1.86 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
DO $$
DECLARE
rec RECORD;
BEGIN
FOR rec IN
SELECT c.table_name, c.column_name
FROM information_schema.columns c
JOIN information_schema.tables t ON t.table_schema = c.table_schema AND t.table_name = c.table_name
WHERE c.table_schema = 'oconnor'
AND t.table_type = 'BASE TABLE'
AND lower(c.column_name) IN ('createdby',
'modifiedby')
AND c.data_type = 'smallint'
LOOP
EXECUTE format(
'ALTER TABLE oconnor.%I ALTER COLUMN %I
TYPE userid',
rec.table_name, rec.column_name
);
END LOOP;
END $$;
DO $$
DECLARE
rec RECORD;
view_rec RECORD;
BEGIN
-- Save view definitions and drop all views in oconnor schema to clear dependencies
CREATE TEMP TABLE _oconnor_view_defs (viewname text, definition text);
FOR view_rec IN
SELECT viewname, definition
FROM pg_views
WHERE schemaname = 'oconnor'
LOOP
INSERT INTO _oconnor_view_defs VALUES (view_rec.viewname, view_rec.definition);
EXECUTE format('DROP VIEW IF EXISTS oconnor.%I CASCADE', view_rec.viewname);
END LOOP;
-- Alter container columns on base tables
FOR rec IN
SELECT c.table_name, c.column_name
FROM information_schema.columns c
JOIN information_schema.tables t ON t.table_schema = c.table_schema AND t.table_name = c.table_name
WHERE c.table_schema = 'oconnor'
AND t.table_type = 'BASE TABLE'
AND lower(c.column_name) = 'container'
AND c.data_type = 'character varying'
LOOP
EXECUTE format(
'ALTER TABLE oconnor.%I ALTER COLUMN %I
TYPE entityid',
rec.table_name, rec.column_name
);
END LOOP;
-- Recreate views from saved definitions
FOR view_rec IN
SELECT viewname, definition FROM _oconnor_view_defs
LOOP
EXECUTE format('CREATE OR REPLACE VIEW oconnor.%I AS %s', view_rec.viewname, view_rec.definition);
END LOOP;
DROP TABLE _oconnor_view_defs;
END $$;