Skip to content

Commit

Permalink
fix: refactor for simplified migration code
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickAlphaC committed Nov 22, 2024
1 parent d431ba0 commit 3de8312
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
14 changes: 7 additions & 7 deletions boa/deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ def __init__(self, path=":memory:"):
self.db.execute(_CREATE_CMD)

# Migration for legacy DB without filename column
if not self._filename_is_in_db():
self.db.execute("ALTER TABLE deployments ADD COLUMN filename text;")
self.db.commit()
self._apply_filename_migration()

def __del__(self):
self.db.close()
Expand All @@ -133,12 +131,14 @@ def insert_deployment(self, deployment: Deployment):
self.db.execute(insert_cmd, tuple(values.values()))
self.db.commit()

def _filename_is_in_db(self) -> bool:
def _apply_filename_migration(self) -> None:
cursor = self.db.execute("PRAGMA table_info(deployments);")
columns = [col[1] for col in cursor.fetchall()]
if "filename" in columns:
return True
return False
is_in_db = "filename" in columns
if is_in_db:
return
self.db.execute("ALTER TABLE deployments ADD COLUMN filename text;")
self.db.commit()

def _get_deployments_from_sql(self, sql_query: str, parameters=(), /):
cur = self.db.execute(sql_query, parameters)
Expand Down
5 changes: 4 additions & 1 deletion tests/integration/network/anvil/test_network_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,8 @@ def test_deployments_db_migration(temp_legacy_db_path):
columns = [col[1] for col in cursor.fetchall()]
assert "filename" not in columns

# This next line is what does the migration (added the filename column)
db = DeploymentsDB(temp_legacy_db_path)
assert db._filename_is_in_db() is True
cursor = db.db.execute("PRAGMA table_info(deployments);")
columns = [col[1] for col in cursor.fetchall()]
assert "filename" in columns

0 comments on commit 3de8312

Please sign in to comment.