From bfb50b29007a277dc3c43a3368d3f17101ae16a2 Mon Sep 17 00:00:00 2001 From: Kamran Ali Date: Tue, 4 Jun 2024 17:39:25 +0200 Subject: [PATCH 1/3] write db purge script --- mex/scripts/purge.py | 27 +++++++++++++++++++++++++++ pyproject.toml | 1 + 2 files changed, 28 insertions(+) create mode 100644 mex/scripts/purge.py diff --git a/mex/scripts/purge.py b/mex/scripts/purge.py new file mode 100644 index 0000000..e327f28 --- /dev/null +++ b/mex/scripts/purge.py @@ -0,0 +1,27 @@ +import click +from neo4j import GraphDatabase + +from mex.backend.settings import BackendSettings +from mex.common.cli import entrypoint + + +@entrypoint(BackendSettings) +def purge_db() -> None: + """Purge graph db.""" + settings = BackendSettings.get() + if click.confirm(f'Purging graph db "{settings.graph_db}", Continue?:'): + click.echo("Purging...") + with GraphDatabase.driver( + settings.graph_url, + auth=( + settings.graph_user.get_secret_value(), + settings.graph_password.get_secret_value(), + ), + database=settings.graph_db, + ) as driver: + driver.execute_query("MATCH (n) DETACH DELETE n;") + for row in driver.execute_query("SHOW ALL CONSTRAINTS;").records: + driver.execute_query(f"DROP CONSTRAINT {row['name']};") + for row in driver.execute_query("SHOW ALL INDEXES;").records: + driver.execute_query(f"DROP INDEX {row['name']};") + click.echo("Done") diff --git a/pyproject.toml b/pyproject.toml index b75c5c0..2f366cf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,6 +29,7 @@ optional-dependencies.dev = [ [project.scripts] backend = "mex.backend.main:main" +purge-db = "mex.scripts.purge:purge_db" [tool.cruft] template = "https://github.com/robert-koch-institut/mex-template" From cd0593b9e6febaa856b99798a445b89f43dbc2c9 Mon Sep 17 00:00:00 2001 From: Kamran Ali Date: Tue, 4 Jun 2024 17:39:49 +0200 Subject: [PATCH 2/3] update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e2deba5..6de7fbc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- db purge script + ### Changes ### Deprecated From 32c4e7128ea563e8665ace4fd4ded3b3c4b4a224 Mon Sep 17 00:00:00 2001 From: Kamran Ali Date: Wed, 5 Jun 2024 16:55:37 +0200 Subject: [PATCH 3/3] move purge script to scripts --- pyproject.toml | 2 +- {mex/scripts => scripts}/purge.py | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) rename {mex/scripts => scripts}/purge.py (93%) diff --git a/pyproject.toml b/pyproject.toml index 2f366cf..2515088 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,7 @@ optional-dependencies.dev = [ [project.scripts] backend = "mex.backend.main:main" -purge-db = "mex.scripts.purge:purge_db" +purge-db = "scripts.purge:purge_db" [tool.cruft] template = "https://github.com/robert-koch-institut/mex-template" diff --git a/mex/scripts/purge.py b/scripts/purge.py similarity index 93% rename from mex/scripts/purge.py rename to scripts/purge.py index e327f28..1a8fe2a 100644 --- a/mex/scripts/purge.py +++ b/scripts/purge.py @@ -2,10 +2,8 @@ from neo4j import GraphDatabase from mex.backend.settings import BackendSettings -from mex.common.cli import entrypoint -@entrypoint(BackendSettings) def purge_db() -> None: """Purge graph db.""" settings = BackendSettings.get()