diff --git a/migrations/__init__.py b/migrations/__init__.py index 02a7168..9724944 100644 --- a/migrations/__init__.py +++ b/migrations/__init__.py @@ -24,7 +24,7 @@ def get_version(db: DuckDBPyConnection) -> int: try: return int(db.execute("SELECT MAX(id) FROM migrations").fetchone()[0]) except: - return 0 + return 1 def migrate(db: DuckDBPyConnection, from_version: Optional[int] = None, to_version: Optional[int] = None) -> None: """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: - from_version = get_version(db) + 1 + from_version = get_version(db) if to_version is None: 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: 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}...") - for version in range(from_version, to_version): + for version in range(from_version, to_version + 1): if version in MIGRATIONS: print(f"Running migration {version}...") MIGRATIONS[version](db)