From 18efeae9e89638be7ca4af89fec3918a557112cc Mon Sep 17 00:00:00 2001 From: Minkyu Park Date: Fri, 9 Dec 2022 14:35:02 -0800 Subject: [PATCH] Update migration query to make it work with existing view Signed-off-by: Minkyu Park --- .../migration/V52__alter_dataset_symlinks.sql | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/api/src/main/resources/marquez/db/migration/V52__alter_dataset_symlinks.sql b/api/src/main/resources/marquez/db/migration/V52__alter_dataset_symlinks.sql index 8ce804db8d..37d26ca070 100644 --- a/api/src/main/resources/marquez/db/migration/V52__alter_dataset_symlinks.sql +++ b/api/src/main/resources/marquez/db/migration/V52__alter_dataset_symlinks.sql @@ -1,2 +1,28 @@ /* SPDX-License-Identifier: Apache-2.0 */ -ALTER TABLE dataset_symlinks ALTER COLUMN name TYPE VARCHAR; \ No newline at end of file + +DO +$$ +DECLARE + datasets_view_exists boolean; + datasets_view_definition text; +BEGIN + SELECT EXISTS ( + SELECT * FROM information_schema.views + WHERE table_schema='public' AND table_name='datasets_view' + ) INTO datasets_view_exists; + + IF datasets_view_exists THEN + -- Altering is not allowed when the column is being used from views. So here, + -- we temporarily drop the view before altering and recreate it. + SELECT view_definition FROM information_schema.views + WHERE table_schema='public' AND table_name='datasets_view' + INTO datasets_view_definition; + + DROP VIEW datasets_view; + ALTER TABLE dataset_symlinks ALTER COLUMN name TYPE VARCHAR; + EXECUTE format('CREATE VIEW datasets_view AS %s', datasets_view_definition); + ELSE + ALTER TABLE dataset_symlinks ALTER COLUMN name TYPE VARCHAR; + END IF; +END +$$ LANGUAGE plpgsql;