From d0f113645b771fd62db6848a4289f7e1d5b6e99e Mon Sep 17 00:00:00 2001 From: Jacob Hinkle Date: Wed, 21 Sep 2022 13:50:46 -0400 Subject: [PATCH] Create schema submodule holding versioned schemas This is meant to hold schemas and migrations. It contains an `__init__.py` due to a limitation of `importlib.resources` outlined in detail here: https://github.com/python/importlib_resources/issues/58 Eventually the schema submodule will handle automatic migration of older databases when importing into new stores. --- src/nancy/db.py | 17 +++++++---------- src/nancy/schema/__init__.py | 0 src/nancy/{schema.sql => schema/version0.sql} | 0 src/nancy/store.py | 3 +-- tests/test_db.py | 18 +++++++----------- 5 files changed, 15 insertions(+), 23 deletions(-) create mode 100644 src/nancy/schema/__init__.py rename src/nancy/{schema.sql => schema/version0.sql} (100%) diff --git a/src/nancy/db.py b/src/nancy/db.py index 3524527..6ad0c48 100644 --- a/src/nancy/db.py +++ b/src/nancy/db.py @@ -8,16 +8,13 @@ import sqlite3 schema_version = 0 -def init(path): - """ - Initialize a data store directory. - """ - if os.path.exists(path): - raise FileExistsError(f"Refusing to overwrite existing database {path}") - schema = importlib.resources.open_text("nancy", "schema.sql").read() - conn = sqlite3.connect(path) - conn.cursor().executescript(schema) - return conn +def init_schema(cur): + """Initialize a database following the current schema.""" + schema = importlib.resources.read_text( + "nancy.schema", + f"version{schema_version}.sql", + ) + cur.executescript(schema) def connect(path): diff --git a/src/nancy/schema/__init__.py b/src/nancy/schema/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/nancy/schema.sql b/src/nancy/schema/version0.sql similarity index 100% rename from src/nancy/schema.sql rename to src/nancy/schema/version0.sql diff --git a/src/nancy/store.py b/src/nancy/store.py index 200e5f2..8bf9108 100644 --- a/src/nancy/store.py +++ b/src/nancy/store.py @@ -59,9 +59,8 @@ class Store: f"File {db_path} exists. Refusing to re-initialize", ) # initialize a database in the target directory - schema = importlib.resources.open_text("nancy", "schema.sql").read() conn = sqlite3.connect(db_path) - conn.cursor().executescript(schema) + db.init_schema(conn.cursor()) new_store = cls(directory, conn) new_store.record_machine_description() diff --git a/tests/test_db.py b/tests/test_db.py index 7bcd11e..48ccedd 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -10,17 +10,13 @@ import sqlite3 @pytest.fixture def temp_db(): """Create an in-memory database that follow's the nancy schema""" - conn = sqlite3.connect(':memory:') - cur = conn.cursor() - #import importlib - #schema = importlib.resources.open_text("nancy", "schema.sql").read() - schema = open(os.path.join( - os.path.dirname(__file__), - '../src/nancy/schema.sql', - ), 'r').read() - cur.executescript(schema) - yield cur - conn.close() + with sqlite3.connect(':memory:') as conn: + cur = conn.cursor() + + from nancy import db + db.init_schema(cur) + + yield cur @pytest.fixture