@@ -19,31 +19,48 @@ def init_db() -> None:
1919def ensure_schema_compatibility ():
2020 """Ensure the database schema is compatible with current models, adding missing columns if needed."""
2121 with engine .connect () as conn :
22- # Check if permanent column exists in file table by querying table info
2322 try :
24- result = conn .execute (text ("PRAGMA table_info(file)" )).fetchall ()
25- column_names = [row [1 ] for row in result ] # Second column in PRAGMA is column name
26- if 'permanent' not in column_names :
27- # For SQLite, we need to recreate the table with the new column
28- # First, backup existing data
29- conn .execute (text ("ALTER TABLE file RENAME TO file_backup" ))
30-
31- # Create new table with permanent column
32- SQLModel .metadata .tables ['file' ].create (conn )
33-
34- # Copy data from backup to new table, setting permanent to false for existing records
35- conn .execute (text ("""
36- INSERT INTO file (id, original_name, stored_name, content_type, size_bytes, permanent, created_at)
37- SELECT id, original_name, stored_name, content_type, size_bytes, 0, created_at
38- FROM file_backup
39- """ ))
40-
41- # Drop backup table
42- conn .execute (text ("DROP TABLE file_backup" ))
43- conn .commit ()
44- logging .info ("Migrated 'file' table to include 'permanent' column" )
23+ # Check if database is SQLite or PostgreSQL and use appropriate approach
24+ if 'sqlite' in DB_URL .lower ():
25+ # SQLite-specific approach
26+ result = conn .execute (text ("PRAGMA table_info(file)" )).fetchall ()
27+ column_names = [row [1 ] for row in result ] # Second column in PRAGMA is column name
28+ if 'permanent' not in column_names :
29+ # For SQLite, we need to recreate the table with the new column
30+ # First, backup existing data
31+ conn .execute (text ("ALTER TABLE file RENAME TO file_backup" ))
32+
33+ # Create new table with permanent column
34+ SQLModel .metadata .tables ['file' ].create (conn )
35+
36+ # Copy data from backup to new table, setting permanent to false for existing records
37+ conn .execute (text ("""
38+ INSERT INTO file (id, original_name, stored_name, content_type, size_bytes, permanent, created_at)
39+ SELECT id, original_name, stored_name, content_type, size_bytes, 0, created_at
40+ FROM file_backup
41+ """ ))
42+
43+ # Drop backup table
44+ conn .execute (text ("DROP TABLE file_backup" ))
45+ conn .commit ()
46+ logging .info ("Migrated 'file' table to include 'permanent' column" )
47+ else :
48+ logging .info ("Database schema is up to date" )
4549 else :
46- logging .info ("Database schema is up to date" )
50+ # PostgreSQL-specific approach
51+ result = conn .execute (text ("""
52+ SELECT column_name
53+ FROM information_schema.columns
54+ WHERE table_name = 'file' AND column_name = 'permanent'
55+ """ )).fetchall ()
56+
57+ if not result :
58+ # Add the permanent column to the file table
59+ conn .execute (text ("ALTER TABLE file ADD COLUMN permanent BOOLEAN DEFAULT FALSE" ))
60+ conn .commit ()
61+ logging .info ("Migrated 'file' table to include 'permanent' column" )
62+ else :
63+ logging .info ("Database schema is up to date" )
4764 except OperationalError as e :
4865 logging .warning (f"Could not check or migrate database schema: { e } " )
4966
0 commit comments