Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save pc-m/19a37f5e71b8965abddc9e7acc47931a to your computer and use it in GitHub Desktop.

Select an option

Save pc-m/19a37f5e71b8965abddc9e7acc47931a to your computer and use it in GitHub Desktop.
diff --git a/xivo_dao/alchemy/endpoint_sip_options_view.py b/xivo_dao/alchemy/endpoint_sip_options_view.py
index c4f9cc5e..9b358845 100644
--- a/xivo_dao/alchemy/endpoint_sip_options_view.py
+++ b/xivo_dao/alchemy/endpoint_sip_options_view.py
@@ -1,4 +1,4 @@
-# Copyright 2021-2022 The Wazo Authors (see the AUTHORS file)
+# Copyright 2021-2023 The Wazo Authors (see the AUTHORS file)
# SPDX-License-Identifier: GPL-3.0-or-later
from sqlalchemy import select, join, cast, literal, func, String, Index, text
@@ -85,7 +85,7 @@ class EndpointSIPOptionsView(MaterializedView):
__view__ = create_materialized_view(
'endpoint_sip_options_view',
_generate_selectable(),
- dependencies=[EndpointSIPSectionOption],
+ dependencies=(EndpointSIPSectionOption, EndpointSIP),
indexes=[
Index('endpoint_sip_options_view__idx_root', text('root'), unique=True),
],
diff --git a/xivo_dao/helpers/db_views.py b/xivo_dao/helpers/db_views.py
index 401f14a9..9c4bd90c 100644
--- a/xivo_dao/helpers/db_views.py
+++ b/xivo_dao/helpers/db_views.py
@@ -1,16 +1,17 @@
# Copyright 2021-2023 The Wazo Authors (see the AUTHORS file)
# SPDX-License-Identifier: GPL-3.0-or-later
-from sqlalchemy import Column, MetaData, PrimaryKeyConstraint, Table, Index
+from sqlalchemy import Column, MetaData, PrimaryKeyConstraint, Table, Index, text
from sqlalchemy.ext import compiler
from sqlalchemy.ext.declarative import DeclarativeMeta
from sqlalchemy.event import listens_for, listen, contains
from sqlalchemy.exc import InvalidRequestError, NoInspectionAvailable
from sqlalchemy.inspection import inspect
+from sqlalchemy.orm.unitofwork import UOWTransaction
from sqlalchemy.sql.ddl import DDLElement
from sqlalchemy.sql.selectable import Selectable
-from .db_manager import Base, Session, daosession
+from .db_manager import Base, Session
# Adapted from:
@@ -160,30 +161,30 @@ class _MaterializedViewMeta(DeclarativeMeta):
self._view_dependencies_handler = None
self._track_dependencies()
- @daosession
- def refresh(session, self, concurrently=True):
- _refresh_materialized_view(session, self.__table__.fullname, concurrently)
+ def refresh(self, concurrently=True):
+ _refresh_materialized_view(Session(), self.__table__.fullname, concurrently)
def _track_dependencies(self):
- if not hasattr(self, '_view_dependencies'):
+ if not (targets := getattr(self, '_view_dependencies', None)):
return
- targets = self._view_dependencies
- if targets:
-
- @listens_for(Session, 'before_commit')
- def _before_session_commit_handler(session):
- for obj in session:
- if isinstance(obj, tuple(targets)):
- self.refresh(concurrently=True)
- return
-
- self._view_dependencies_handler = staticmethod(
- _before_session_commit_handler
- )
+
+ @listens_for(Session, 'after_flush')
+ def _before_session_commit_handler(session: Session, flush_context: UOWTransaction) -> None:
+ for obj in session.dirty | session.new | session.deleted:
+ if isinstance(obj, tuple(targets)):
+ # Cannot call `refresh_materialized_view` as it will try to flush again.
+ session.execute(
+ text(f'REFRESH MATERIALIZED VIEW CONCURRENTLY {self.__table__.fullname}')
+ )
+ return
+
+ self._view_dependencies_handler = staticmethod(
+ _before_session_commit_handler
+ )
@property
def autorefresh(self):
- return contains(Session, 'before_commit', self._view_dependencies_handler)
+ return contains(Session, 'after_flush', self._view_dependencies_handler)
class MaterializedView(Base, metaclass=_MaterializedViewMeta):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment