Fix latest migration not applying

This commit is contained in:
Justin 2023-05-19 12:06:19 -05:00
parent 9c2c4d5f6f
commit a546e969cc

View file

@ -24,7 +24,7 @@ def get_version(db: DuckDBPyConnection) -> int:
try: try:
return int(db.execute("SELECT MAX(id) FROM migrations").fetchone()[0]) return int(db.execute("SELECT MAX(id) FROM migrations").fetchone()[0])
except: except:
return 0 return 1
def migrate(db: DuckDBPyConnection, from_version: Optional[int] = None, to_version: Optional[int] = None) -> None: def migrate(db: DuckDBPyConnection, from_version: Optional[int] = None, to_version: Optional[int] = None) -> None:
"""Migrate the database to a specific version. """Migrate the database to a specific version.
@ -36,7 +36,7 @@ def migrate(db: DuckDBPyConnection, from_version: Optional[int] = None, to_versi
""" """
if from_version is None: if from_version is None:
from_version = get_version(db) + 1 from_version = get_version(db)
if to_version is None: if to_version is None:
to_version = max(MIGRATIONS.keys()) to_version = max(MIGRATIONS.keys())
@ -44,8 +44,12 @@ def migrate(db: DuckDBPyConnection, from_version: Optional[int] = None, to_versi
if from_version > to_version: if from_version > to_version:
raise ValueError("Cannot migrate from a higher version to a lower version.") raise ValueError("Cannot migrate from a higher version to a lower version.")
# dont migrate if already at the target version
if from_version == to_version:
return from_version, to_version
print(f"Migrating from version {from_version} to {to_version}...") print(f"Migrating from version {from_version} to {to_version}...")
for version in range(from_version, to_version): for version in range(from_version, to_version + 1):
if version in MIGRATIONS: if version in MIGRATIONS:
print(f"Running migration {version}...") print(f"Running migration {version}...")
MIGRATIONS[version](db) MIGRATIONS[version](db)