From 4cf51d95fb1752aad0bfe8a3d9a42707d809f49f Mon Sep 17 00:00:00 2001 From: Barry Daniels <101455655+barrydaniels-nl@users.noreply.github.com> Date: Fri, 12 Jan 2024 12:59:28 +0100 Subject: [PATCH] Modified create_views to support materialized views (#538) Co-authored-by: Barry Daniels --- CHANGES.md | 4 ++++ setup.cfg | 2 +- .../django/management/commands/create_views.py | 18 ++++++++---------- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 4c15eacd..2943de56 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,7 @@ +# 2024-01-12 (5.23.0) + +* Modified create_views functions to support materialized views + # 2024-01-10 (5.22.0) * Add `enable_export` column to the `dataset` model to be able diff --git a/setup.cfg b/setup.cfg index f5a5df42..7d82b260 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = amsterdam-schema-tools -version = 5.22.0 +version = 5.23.0 url = https://github.com/amsterdam/schema-tools license = Mozilla Public 2.0 author = Team Data Diensten, van het Dataplatform onder de Directie Digitale Voorzieningen (Gemeente Amsterdam) diff --git a/src/schematools/contrib/django/management/commands/create_views.py b/src/schematools/contrib/django/management/commands/create_views.py index c5bb26bf..d9ccd1ac 100644 --- a/src/schematools/contrib/django/management/commands/create_views.py +++ b/src/schematools/contrib/django/management/commands/create_views.py @@ -34,11 +34,6 @@ def _is_valid_sql(view_sql: str, view_name: str, write_role_name: str) -> bool: ) ) - cursor.execute( - sql.SQL("DROP VIEW IF EXISTS {view_name} CASCADE").format( - view_name=sql.Identifier(view_name) - ) - ) cursor.execute(view_sql) transaction.savepoint_rollback(sid) except Exception as e: # noqa: F841 @@ -135,7 +130,6 @@ def create_views( for table in dataset.schema.tables: if table.is_view: command.stdout.write(f"* Creating view {table.db_name}") - # Generate the write role name write_role_name = f"write_{table._parent_schema.db_name}" @@ -200,11 +194,15 @@ def create_views( ) # Remove the view if it exists - cursor.execute( - sql.SQL("DROP VIEW IF EXISTS {view_name} CASCADE").format( - view_name=sql.Identifier(table.db_name) + # Due to the large costs of recreating materialized views, we only create + # and not drop them. When changes are made to the materialized view the view + # must be droped manually. + if "materialized" not in view_sql.lower(): + cursor.execute( + sql.SQL("DROP VIEW IF EXISTS {view_name} CASCADE").format( + view_name=sql.Identifier(table.db_name) + ) ) - ) # Create the view cursor.execute(view_sql)