Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ check the indexes on the table and delete any extra indexes and create any missi
is done through the index name, so if you use custom names for your indexes, it might happen that it won't get updated
on change of the content but not the name.

With data is also respected: if the view definition did not change, but data is not populated and the view class says data should be populated,
a refresh will happen to ensure the view is populated.

#### Refreshing dependencies / dependants

The `refresh` method on `MaterializedView` only refreshes the given view, not dependencies or dependants.
Expand Down
12 changes: 12 additions & 0 deletions django_pgviews/management/operations/create_materialized.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@ def create_materialized_view(

if definitions[0] == definitions[1]:
_ensure_indexes(connection, cursor, view_cls, schema_name_log)

if view_cls.with_data:
definitions_where, definitions_params = _make_where(schemaname=vschema, matviewname=[vname])
cursor.execute(
f"SELECT ispopulated FROM pg_catalog.pg_matviews WHERE {definitions_where}",
definitions_params,
)
has_data = cursor.fetchone()[0]

if not has_data:
view_cls.refresh(concurrently=False)

return "EXISTS"

if view_exists:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "django-pgviews-redux"
version = "1.0.1"
version = "1.1.0"
description = "Create and manage Postgres SQL Views in Django"
authors = ["Mikuláš Poul <mikulas.poul@xelix.com>"]
readme = "README.md"
Expand Down
25 changes: 25 additions & 0 deletions tests/test_project/viewtest/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,31 @@ def test_refresh_pgviews(self):

@pytest.mark.django_db
class TestMaterializedViewsCheckSQLSettings:
def test_migrate_materialized_views_check_sql_set_to_true_no_data(self, settings: SettingsWrapper) -> None:
settings.MATERIALIZED_VIEWS_CHECK_SQL_CHANGED = True

assert models.TestModel.objects.count() == 0
assert models.MaterializedRelatedView.objects.count() == 0

models.TestModel.objects.create(name="Test")
call_command("migrate")
assert models.MaterializedRelatedView.objects.count() == 0

# create it with no data
with connection.cursor() as cursor:
cursor.execute("DROP MATERIALIZED VIEW viewtest_materializedrelatedview CASCADE;")
cursor.execute(
"""
CREATE MATERIALIZED VIEW viewtest_materializedrelatedview as
SELECT id AS model_id, id FROM viewtest_testmodel
WITH NO DATA
"""
)

# view didn't change but it's not populated, but as it's configured to have data it got refreshed
call_command("migrate")
assert models.MaterializedRelatedView.objects.count() == 1

def test_migrate_materialized_views_check_sql_set_to_true(self, settings: SettingsWrapper) -> None:
settings.MATERIALIZED_VIEWS_CHECK_SQL_CHANGED = True

Expand Down