Skip to content

Commit

Permalink
Merge branch 'feat/database-version-control' into feat/metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
NanamiNakano committed Jan 5, 2025
2 parents 0a8c663 + 8d5adb0 commit cd3dbb0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
6 changes: 3 additions & 3 deletions database.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ def __init__(self, mercury_db_path: str, user_db_path: str):
print("Open db at ", mercury_db_path)
version = mercury_db.execute("SELECT value FROM config WHERE key = 'version'").fetchone()
if version is None:
mercury_db.execute("INSERT INTO config (key, value) VALUES ('version', ?)", (__version__,))
else:
if version[0] != __version__:
print("Can not determine database version.")
exit(1)
elif version[0] != __version__:
print("Database version mismatch. Please migrate the database.")
exit(1)
mercury_db.execute("CREATE TABLE IF NOT EXISTS annotations (\
Expand Down
2 changes: 1 addition & 1 deletion ingester.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def prepare_db(self):
f"CREATE VIRTUAL TABLE IF NOT EXISTS chunks USING vec0(chunk_id INTEGER PRIMARY KEY, text TEXT, text_type TEXT, sample_id INTEGER, char_offset INTEGER, chunk_offset INTEGER, embedding float[{self.embedding_dimension}])"
)
self.db.execute(
"CREATE TABLE IF NOT EXISTS config (key TEXT PRIMARY KEY, value TEXT)"
"CREATE TABLE IF NOT EXISTS config (key TEXT PRIMARY KEY UNIQUE , value TEXT)"
)
self.db.execute(
"CREATE TABLE IF NOT EXISTS sample_meta (sample_id INTEGER PRIMARY KEY, json_meta TEXT)"
Expand Down
27 changes: 27 additions & 0 deletions migration/database_version_control.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import argparse
import sqlite3


class Migrator:
def __init__(self, db_path):
conn = sqlite3.connect(db_path)
self.conn = conn
version = self.conn.execute("SELECT count(*) FROM config WHERE key = 'version'").fetchone()
if version[0] != 0:
print("Can not migrate database with existing version")
exit(0)

def migrate(self):
self.conn.execute("ALTER TABLE config RENAME TO config_old")
self.conn.execute("CREATE TABLE config(key TEXT PRIMARY KEY UNIQUE , value TEXT)")
self.conn.execute("INSERT INTO config SELECT key, value FROM config_old")
self.conn.execute("INSERT INTO config VALUES ('version', '0.1.0')")
self.conn.commit()
print("Migration completed")

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Migrate the database to 0.1.0")
parser.add_argument("--db_path", help="Path to the database", default="../mercury.sqlite")
args = parser.parse_args()
migrator = Migrator(args.db_path)
migrator.migrate()

0 comments on commit cd3dbb0

Please sign in to comment.