Skip to content

Commit aa24037

Browse files
committed
Alter 'container' column type to entityid.
1 parent 746859a commit aa24037

File tree

1 file changed

+52
-6
lines changed

1 file changed

+52
-6
lines changed

OConnor/resources/schemas/dbscripts/postgresql/oconnor-26.000-26.001.sql

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,63 @@ DO $$
33
rec RECORD;
44
BEGIN
55
FOR rec IN
6-
SELECT table_name, column_name
7-
FROM information_schema.columns
8-
WHERE table_schema = 'oconnor'
9-
AND lower(column_name) IN ('createdby',
10-
'modifiedby')
11-
AND data_type = 'smallint'
6+
SELECT c.table_name, c.column_name
7+
FROM information_schema.columns c
8+
JOIN information_schema.tables t ON t.table_schema = c.table_schema AND t.table_name = c.table_name
9+
WHERE c.table_schema = 'oconnor'
10+
AND t.table_type = 'BASE TABLE'
11+
AND lower(c.column_name) IN ('createdby',
12+
'modifiedby')
13+
AND c.data_type = 'smallint'
1214
LOOP
1315
EXECUTE format(
1416
'ALTER TABLE oconnor.%I ALTER COLUMN %I
1517
TYPE userid',
1618
rec.table_name, rec.column_name
1719
);
1820
END LOOP;
21+
END $$;
22+
23+
DO $$
24+
DECLARE
25+
rec RECORD;
26+
view_rec RECORD;
27+
BEGIN
28+
-- Save view definitions and drop all views in oconnor schema to clear dependencies
29+
CREATE TEMP TABLE _oconnor_view_defs (viewname text, definition text);
30+
31+
FOR view_rec IN
32+
SELECT viewname, definition
33+
FROM pg_views
34+
WHERE schemaname = 'oconnor'
35+
LOOP
36+
INSERT INTO _oconnor_view_defs VALUES (view_rec.viewname, view_rec.definition);
37+
EXECUTE format('DROP VIEW IF EXISTS oconnor.%I CASCADE', view_rec.viewname);
38+
END LOOP;
39+
40+
-- Alter container columns on base tables
41+
FOR rec IN
42+
SELECT c.table_name, c.column_name
43+
FROM information_schema.columns c
44+
JOIN information_schema.tables t ON t.table_schema = c.table_schema AND t.table_name = c.table_name
45+
WHERE c.table_schema = 'oconnor'
46+
AND t.table_type = 'BASE TABLE'
47+
AND lower(c.column_name) = 'container'
48+
AND c.data_type = 'character varying'
49+
LOOP
50+
EXECUTE format(
51+
'ALTER TABLE oconnor.%I ALTER COLUMN %I
52+
TYPE entityid',
53+
rec.table_name, rec.column_name
54+
);
55+
END LOOP;
56+
57+
-- Recreate views from saved definitions
58+
FOR view_rec IN
59+
SELECT viewname, definition FROM _oconnor_view_defs
60+
LOOP
61+
EXECUTE format('CREATE OR REPLACE VIEW oconnor.%I AS %s', view_rec.viewname, view_rec.definition);
62+
END LOOP;
63+
64+
DROP TABLE _oconnor_view_defs;
1965
END $$;

0 commit comments

Comments
 (0)