self.path = path
if not self.path.is_file():
raise HandledException(f'no DB file at {self.path}')
-
if version_to_validate < 0:
return
- # ensure version
if (user_version := self._get_user_version()) != version_to_validate:
raise HandledException(
f'wrong DB version {user_version} (!= {version_to_validate})')
+ with DbConn(self) as conn:
+ self._validate_schema(conn)
- # ensure schema
- with sql_connect(self.path) as conn:
- schema_rows = [
- r[0] for r in
- conn.execute('SELECT sql FROM sqlite_master ORDER BY sql')
- if r[0]]
+ @staticmethod
+ def _validate_schema(conn: 'DbConn'):
+ schema_sql = SqlText('SELECT sql FROM sqlite_master ORDER BY sql')
schema_rows_normed = []
indent = ' '
- for row in schema_rows:
+ for row in [r[0] for r in conn.exec(schema_sql) if r[0]]:
row_normed = []
for subrow in row.split('\n'):
subrow = subrow.rstrip()
for migration in DbMigration.from_to_in_set(
start_version, EXPECTED_DB_VERSION, migrations):
migration.perform(conn)
+ self._validate_schema(conn)
conn.commit()