sqlite: Add an upgrade script to database version 2

Right now it doesn't do any extra changes to the database besides
bumping the user_version to '2'. I'll fill it out over the next several
commits.

Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
This commit is contained in:
Anna Schumaker 2023-07-25 10:03:32 -04:00
parent 3f61adc941
commit 2082b904a0
3 changed files with 10 additions and 1 deletions

View File

@ -26,6 +26,8 @@ class TestConnection(unittest.TestCase):
pathlib.Path(xfstestsdb.__file__).parent / "scripts")
self.assertEqual(xfstestsdb.sqlite.SQL_V1_SCRIPT,
xfstestsdb.sqlite.SQL_SCRIPTS / "xfstestsdb.sql")
self.assertEqual(xfstestsdb.sqlite.SQL_V2_SCRIPT,
xfstestsdb.sqlite.SQL_SCRIPTS / "upgrade-v2.sql")
def test_foreign_keys(self):
"""Test that foreign key constraints are enabled."""
@ -35,7 +37,7 @@ class TestConnection(unittest.TestCase):
def test_version(self):
"""Test checking the database schema version."""
cur = self.sql("PRAGMA user_version")
self.assertEqual(cur.fetchone()["user_version"], 1)
self.assertEqual(cur.fetchone()["user_version"], 2)
def test_connection(self):
"""Check that the connection manager is initialized properly."""

View File

@ -0,0 +1,3 @@
/* Copyright 2023 (c) Anna Schumaker */
PRAGMA user_version = 2;

View File

@ -12,6 +12,7 @@ DATABASE = ":memory:" if "unittest" in sys.modules else DATA_FILE
SQL_SCRIPTS = pathlib.Path(__file__).parent / "scripts"
SQL_V1_SCRIPT = SQL_SCRIPTS / "xfstestsdb.sql"
SQL_V2_SCRIPT = SQL_SCRIPTS / "upgrade-v2.sql"
class Connection:
@ -28,6 +29,9 @@ class Connection:
match self("PRAGMA user_version").fetchone()["user_version"]:
case 0:
self.executescript(SQL_V1_SCRIPT)
self.executescript(SQL_V2_SCRIPT)
case 1:
self.executescript(SQL_V2_SCRIPT)
def __call__(self, statement: str,
*args, **kwargs) -> sqlite3.Cursor | None: