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
8 changes: 7 additions & 1 deletion .github/workflows/tests_mergin_db_sync.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,10 @@ jobs:

- name: Run tests
run: |
pytest test --cov=. --cov-report=term-missing:skip-covered -vv
pytest test --cov=. --cov-report=term-missing:skip-covered -vv

- name: Check files using the black formatter
uses: rickstaa/action-black@v1
id: action_black
with:
black_args: "."
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
repos:
- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
86 changes: 67 additions & 19 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
License: MIT
"""

from dynaconf import Dynaconf
from dynaconf import (
Dynaconf,
)
import platform
import tempfile
import pathlib
Expand All @@ -16,7 +18,7 @@
envvar_prefix=False,
settings_files=[],
geodiff_exe="geodiff.exe" if platform.system() == "Windows" else "geodiff",
working_dir=(pathlib.Path(tempfile.gettempdir()) / "dbsync").as_posix()
working_dir=(pathlib.Path(tempfile.gettempdir()) / "dbsync").as_posix(),
)


Expand All @@ -25,13 +27,22 @@ class ConfigError(Exception):


def validate_config(config):
""" Validate config - make sure values are consistent """
"""Validate config - make sure values are consistent"""

# validate that geodiff can be found, otherwise it does not make sense to run DB Sync
try:
subprocess.run([config.geodiff_exe, "help"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
subprocess.run(
[
config.geodiff_exe,
"help",
],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
except FileNotFoundError:
raise ConfigError("Config error: Geodiff executable not found. Is it installed and available in `PATH` environment variable?")
raise ConfigError(
"Config error: Geodiff executable not found. Is it installed and available in `PATH` environment variable?"
)

if not (config.mergin.url and config.mergin.username and config.mergin.password):
raise ConfigError("Config error: Incorrect mergin settings")
Expand All @@ -42,48 +53,85 @@ def validate_config(config):
if "init_from" not in config:
raise ConfigError("Config error: Missing parameter `init_from` in the configuration.")

if config.init_from not in ["gpkg", "db"]:
raise ConfigError(f"Config error: `init_from` parameter must be either `gpkg` or `db`. Current value is `{config.init_from}`.")
if config.init_from not in [
"gpkg",
"db",
]:
raise ConfigError(
f"Config error: `init_from` parameter must be either `gpkg` or `db`. Current value is `{config.init_from}`."
)

for conn in config.connections:
for attr in ["driver", "conn_info", "modified", "base", "mergin_project", "sync_file"]:
if not hasattr(conn, attr):
raise ConfigError(f"Config error: Incorrect connection settings. Required parameter `{attr}` is missing.")
for attr in [
"driver",
"conn_info",
"modified",
"base",
"mergin_project",
"sync_file",
]:
if not hasattr(
conn,
attr,
):
raise ConfigError(
f"Config error: Incorrect connection settings. Required parameter `{attr}` is missing."
)

if conn.driver != "postgres":
raise ConfigError("Config error: Only 'postgres' driver is currently supported.")

if "/" not in conn.mergin_project:
raise ConfigError("Config error: Name of the Mergin Maps project should be provided in the namespace/name format.")
raise ConfigError(
"Config error: Name of the Mergin Maps project should be provided in the namespace/name format."
)

if "skip_tables" in conn:
if conn.skip_tables is None:
continue
elif isinstance(conn.skip_tables, str):
elif isinstance(
conn.skip_tables,
str,
):
continue
elif not isinstance(conn.skip_tables, list):
elif not isinstance(
conn.skip_tables,
list,
):
raise ConfigError("Config error: Ignored tables parameter should be a list")


def get_ignored_tables(connection):
def get_ignored_tables(
connection,
):
if "skip_tables" in connection:
if connection.skip_tables is None:
return []
elif isinstance(connection.skip_tables, str):
elif isinstance(
connection.skip_tables,
str,
):
return [connection.skip_tables]
elif isinstance(connection.skip_tables, list):
elif isinstance(
connection.skip_tables,
list,
):
return connection.skip_tables
else:
return []


def update_config_path(path_param: str) -> None:
def update_config_path(
path_param: str,
) -> None:
config_file_path = pathlib.Path(path_param)

if config_file_path.exists():
print(f"Using config file: {path_param}")
user_file_config = Dynaconf(envvar_prefix=False,
settings_files=[config_file_path])
user_file_config = Dynaconf(
envvar_prefix=False,
settings_files=[config_file_path],
)
config.update(user_file_config)
else:
raise IOError(f"Config file {config_file_path} does not exist.")
Loading