Skip to content

Commit

Permalink
fix #53: add a check for the datamodel version
Browse files Browse the repository at this point in the history
  • Loading branch information
olivierdalang committed Mar 9, 2023
1 parent 8d2ad41 commit 6354e44
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
14 changes: 13 additions & 1 deletion qgepqwat2ili/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@
from .qgep.mapping import get_qgep_mapping
from .qgep.model_abwasser import Base as BaseAbwasser
from .qgep.model_qgep import Base as BaseQgep
from .qgep.version import QGEP_PUM_TABLE, QGEP_SUPPORTED_VERSION
from .qwat.export import qwat_export
from .qwat.import_ import qwat_import
from .qwat.mapping import get_qwat_mapping
from .qwat.model_qwat import Base as BaseQwat
from .qwat.model_wasser import Base as BaseWasser
from .utils.various import make_log_path
from .qwat.version import QWAT_PUM_TABLE, QWAT_SUPPORTED_VERSION
from .utils.various import check_version, make_log_path


def main(args):

parser = argparse.ArgumentParser(
description="ili2QWAT / ili2QGEP entrypoint", formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument("--skip_version_check", action="store_true", help="do not check the datamodel version")

subparsers = parser.add_subparsers(title="subcommands", dest="parser")
# subparsers.required = True
Expand Down Expand Up @@ -130,6 +133,10 @@ def main(args):
SCHEMA = config.ABWASSER_SCHEMA
ILI_MODEL = config.ABWASSER_ILI_MODEL
ILI_MODEL_NAME = config.ABWASSER_ILI_MODEL_NAME

if not args.skip_version_check:
check_version(QGEP_PUM_TABLE, QGEP_SUPPORTED_VERSION)

if args.direction == "export":
utils.ili2db.create_ili_schema(
SCHEMA, ILI_MODEL, make_log_path(log_path, "ilicreate"), recreate_schema=args.recreate_schema
Expand Down Expand Up @@ -164,6 +171,11 @@ def main(args):
SCHEMA = config.WASSER_SCHEMA
ILI_MODEL = config.WASSER_ILI_MODEL
ILI_MODEL_NAME = config.WASSER_ILI_MODEL_NAME

if not args.skip_version_check:
# note: there are two tables, info and upgrades, not sure which one is used
check_version(QWAT_PUM_TABLE, QWAT_SUPPORTED_VERSION)

if args.direction == "export":
utils.ili2db.create_ili_schema(
SCHEMA, ILI_MODEL, make_log_path(log_path, "ilicreate"), recreate_schema=args.recreate_schema
Expand Down
15 changes: 14 additions & 1 deletion qgepqwat2ili/gui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@
from .. import config
from ..qgep.export import qgep_export
from ..qgep.import_ import qgep_import
from ..qgep.version import QGEP_PUM_TABLE, QGEP_SUPPORTED_VERSION
from ..utils.ili2db import (
create_ili_schema,
export_xtf_data,
import_xtf_data,
validate_xtf_data,
)
from ..utils.various import CmdException, LoggingHandlerContext, logger, make_log_path
from ..utils.various import (
CmdException,
LoggingHandlerContext,
check_version,
logger,
make_log_path,
)
from .gui_export import GuiExport
from .gui_import import GuiImport

Expand Down Expand Up @@ -55,6 +62,9 @@ def action_import(plugin):
if not configure_from_modelbaker(plugin.iface):
return

# check version
check_version(QGEP_PUM_TABLE, QGEP_SUPPORTED_VERSION)

default_folder = QgsSettings().value("qgep_pluging/last_interlis_path", QgsProject.instance().absolutePath())
file_name, _ = QFileDialog.getOpenFileName(
None, plugin.tr("Import file"), default_folder, plugin.tr("Interlis transfer files (*.xtf)")
Expand Down Expand Up @@ -159,6 +169,9 @@ def action_export(plugin):
if not configure_from_modelbaker(plugin.iface):
return

# check version
check_version(QGEP_PUM_TABLE, QGEP_SUPPORTED_VERSION)

export_dialog = GuiExport(plugin.iface.mainWindow())

def action_do_export():
Expand Down
2 changes: 2 additions & 0 deletions qgepqwat2ili/qgep/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
QGEP_PUM_TABLE = "qgep_sys.pum_info"
QGEP_SUPPORTED_VERSION = ">=1.5.7" # see https://peps.python.org/pep-0440/#version-specifiers
2 changes: 2 additions & 0 deletions qgepqwat2ili/qwat/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
QWAT_PUM_TABLE = "qwat_sys.info" # note: in the demo data, there's also an "upgrades" table ?!
QWAT_SUPPORTED_VERSION = ">=1.3.6" # see https://peps.python.org/pep-0440/#version-specifiers
24 changes: 24 additions & 0 deletions qgepqwat2ili/utils/various.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import time
from typing import List

import psycopg2
from packaging import version
from packaging.specifiers import SpecifierSet

from .. import config


Expand Down Expand Up @@ -269,6 +273,26 @@ def make_log_path(next_to_path, step_name):
return os.path.join(temp_path, f"{now}.{step_name}.log")


def check_version(qualified_pum_info_table, version_specifier):
# get the current version
connection = psycopg2.connect(get_pgconf_as_psycopg2_dsn())
connection.set_session(autocommit=True)
cursor = connection.cursor()
cursor.execute(f'SELECT "version" FROM {qualified_pum_info_table};')
# we get the max version by iterating in python, since no easy/reliable way to get it in SQL
current_version = max([version.parse("0"), *[version.parse(v_str) for (v_str,) in cursor.fetchall()]])

# get the specified version
# see https://peps.python.org/pep-0440/#version-specifiers
supported_version_spec = SpecifierSet(version_specifier)

if not supported_version_spec.contains(current_version):
raise RuntimeError(
f"Version mismatch ! Your datamodel is in version {current_version} which is not supported (supported versions: {version_specifier})."
)
logger.info(f"Running a supported datamodel version: {current_version}")


class LoggingHandlerContext:
"""Temporarily sets a log handler, then removes it"""

Expand Down

0 comments on commit 6354e44

Please sign in to comment.