Skip to content

Commit

Permalink
fix: check db schema changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jungs1 committed Aug 3, 2024
1 parent ee907df commit d2ba43b
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/mutahunter/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class MutationDatabase:
def __init__(self, db_path: str = "mutahunter.db"):
self.db_path = db_path
self.conn = None
if os.path.exists(self.db_path):
if not self.check_schema():
print(
f"Schema mismatch detected. Removing old database: {self.db_path}"
)
os.remove(self.db_path)
self.create_tables()

@contextmanager
Expand All @@ -24,6 +30,51 @@ def get_connection(self):
finally:
conn.close()

def check_schema(self):
expected_tables = {
"SourcseFiles": ["id", "file_path", "last_modified"],
"FileVersions": [
"id",
"sourc_file_id",
"version_hash",
"content",
"created_at",
],
"Runs": [
"id",
"command_line",
"start_time",
"end_time",
"execution_time",
"mutation_score",
"line_coverage",
],
"Mutants": [
"id",
"file_version_id",
"run_id",
"status",
"type",
"line_number",
"mutant_path",
"original_code",
"mutated_code",
"description",
"error_msg",
"created_at",
],
}

with self.get_connection() as conn:
cursor = conn.cursor()
for table, expected_columns in expected_tables.items():
cursor.execute(f"PRAGMA table_info({table})")
actual_columns = [row[1] for row in cursor.fetchall()]
if set(actual_columns) != set(expected_columns):
return False

return True

def create_tables(self):
with self.get_connection() as conn:
cursor = conn.cursor()
Expand Down

0 comments on commit d2ba43b

Please sign in to comment.