Skip to content

Commit e501e36

Browse files
authored
Update migration query to make it work with existing view (#2308)
Signed-off-by: Minkyu Park <minkyu.park.200@gmail.com> Signed-off-by: Minkyu Park <minkyu.park.200@gmail.com>
1 parent 68fcb96 commit e501e36

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,28 @@
11
/* SPDX-License-Identifier: Apache-2.0 */
2-
ALTER TABLE dataset_symlinks ALTER COLUMN name TYPE VARCHAR;
2+
3+
DO
4+
$$
5+
DECLARE
6+
datasets_view_exists boolean;
7+
datasets_view_definition text;
8+
BEGIN
9+
SELECT EXISTS (
10+
SELECT * FROM information_schema.views
11+
WHERE table_schema='public' AND table_name='datasets_view'
12+
) INTO datasets_view_exists;
13+
14+
IF datasets_view_exists THEN
15+
-- Altering is not allowed when the column is being used from views. So here,
16+
-- we temporarily drop the view before altering and recreate it.
17+
SELECT view_definition FROM information_schema.views
18+
WHERE table_schema='public' AND table_name='datasets_view'
19+
INTO datasets_view_definition;
20+
21+
DROP VIEW datasets_view;
22+
ALTER TABLE dataset_symlinks ALTER COLUMN name TYPE VARCHAR;
23+
EXECUTE format('CREATE VIEW datasets_view AS %s', datasets_view_definition);
24+
ELSE
25+
ALTER TABLE dataset_symlinks ALTER COLUMN name TYPE VARCHAR;
26+
END IF;
27+
END
28+
$$ LANGUAGE plpgsql;

0 commit comments

Comments
 (0)