Skip to content

Commit

Permalink
Modified create_views to support materialized views (#538)
Browse files Browse the repository at this point in the history
Co-authored-by: Barry Daniels <[email protected]>
  • Loading branch information
barrydaniels-nl and gurustacks authored Jan 12, 2024
1 parent 065c44e commit 4cf51d9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
18 changes: 8 additions & 10 deletions src/schematools/contrib/django/management/commands/create_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}"

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 4cf51d9

Please sign in to comment.