Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Re-Enable of triggers too early #211

Merged
merged 1 commit into from
Apr 18, 2024
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
35 changes: 35 additions & 0 deletions plugin/teksi_wastewater/interlis/interlis_importer_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ def interlis_import(self, xtf_file_input, show_selection_dialog=False, logs_next
self._progress_done(30, "Importing XTF data...")
self._import_xtf_file(xtf_file_input=xtf_file_input)

# Disable symbology triggers
self._progress_done(35, "Disable symbolgy triggers...")
self._import_disable_symbology_triggers()

# Import from the temporary ili2pg model
self._progress_done(40, "Converting to Teksi Wastewater...")
tww_session = self._import_from_intermediate_schema(import_model)
Expand All @@ -123,6 +127,7 @@ def interlis_import(self, xtf_file_input, show_selection_dialog=False, logs_next
if import_dialog.exec_() == import_dialog.Rejected:
tww_session.rollback()
tww_session.close()
self._import_enable_symbology_triggers()
raise InterlisImporterExporterStopped()
QApplication.setOverrideCursor(Qt.WaitCursor)
else:
Expand All @@ -134,7 +139,12 @@ def interlis_import(self, xtf_file_input, show_selection_dialog=False, logs_next
self._progress_done(95, "Update main cover and refresh materialized views...")
self._import_update_main_cover_and_refresh_mat_views()

# Reenable symbology triggers
self._progress_done(95, "Reenable symbology triggers...")
self._import_enable_symbology_triggers()

self._progress_done(100)
logger.info("Interlis import finished.")

def interlis_export(
self,
Expand Down Expand Up @@ -190,6 +200,7 @@ def interlis_export(
self._export_xtf_files(file_name_base, export_models)

self._progress_done(100)
logger.info("Interlis export finished.")

def _import_validate_xtf_file(self, xtf_file_input):
log_path = make_log_path(self.base_log_path, "ilivalidator")
Expand Down Expand Up @@ -262,6 +273,30 @@ def _import_update_main_cover_and_refresh_mat_views(self):
connection.commit()
connection.close()

def _import_disable_symbology_triggers(self):
connection = psycopg.connect(get_pgconf_as_psycopg_dsn(), **DEFAULTS_CONN_ARG)
if PSYCOPG_VERSION == 2:
connection.set_session(autocommit=True)
cursor = connection.cursor()

logger.info("Disable symbology triggers")
cursor.execute("SELECT tww_sys.disable_symbology_triggers();")

connection.commit()
connection.close()

def _import_enable_symbology_triggers(self):
connection = psycopg.connect(get_pgconf_as_psycopg_dsn(), **DEFAULTS_CONN_ARG)
if PSYCOPG_VERSION == 2:
connection.set_session(autocommit=True)
cursor = connection.cursor()

logger.info("Enable symbology triggers")
cursor.execute("SELECT tww_sys.enable_symbology_triggers();")

connection.commit()
connection.close()

def _export_labels_file(
self, limit_to_selection, selected_labels_scales_indices, labels_file_path
):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,6 @@ def _tww_import(self, skip_closing_tww_session):
commit or rollback and close the session.
"""

pre_session = Session(
utils.tww_sqlalchemy.create_engine(), autocommit=False, autoflush=False
)

# We also drop symbology triggers as they badly affect performance. This must be done in a separate session as it
# would deadlock other sessions.
pre_session.execute(text("SELECT tww_sys.disable_symbology_triggers();"))
pre_session.commit()
pre_session.close()

# We use two different sessions for reading and writing so it's easier to
# review imports and to keep the door open to getting data from another
# connection / database type.
Expand Down Expand Up @@ -381,17 +371,6 @@ def close_sessions(self, skip_closing_tww_session=False):
self.session_tww.close()
self.session_interlis.close()

try:
post_session = Session(
utils.tww_sqlalchemy.create_engine(), autocommit=False, autoflush=False
)
post_session.execute(text("SELECT tww_sys.enable_symbology_triggers();"))
post_session.commit()
post_session.close()

except Exception as exception:
logger.error(f"Could not re-enable symbology triggers: '{exception}'")

def get_vl_instance(self, vl_table, value_de):
"""
Gets a value list instance from the value_de name. Returns None and a warning if not found.
Expand Down