@@ -41,9 +41,9 @@ def _fetchone_scalar(conn: duckdb.DuckDBPyConnection, sql: str) -> int:
4141
4242def apply_fixes (db_path : str , * , dry_run : bool ) -> None :
4343 label = "DRY RUN" if dry_run else "APPLY"
44- print (f"\n { '=' * 60 } " )
44+ print (f"\n { '=' * 60 } " )
4545 print (f"[{ label } ] Database: { db_path } " )
46- print (f"{ '=' * 60 } " )
46+ print (f"{ '=' * 60 } " )
4747
4848 conn = duckdb .connect (str (db_path ), read_only = dry_run )
4949
@@ -53,46 +53,36 @@ def apply_fixes(db_path: str, *, dry_run: bool) -> None:
5353 if dry_run :
5454 for old_name , new_name , new_year in FIXES :
5555 if new_year is not None :
56- sql = ("UPDATE images SET event_name = ?, "
57- "event_year = ? WHERE event_name = ?" )
56+ sql = "UPDATE images SET event_name = ?, event_year = ? WHERE event_name = ?"
5857 params = [new_name , new_year , old_name ]
5958 else :
60- sql = ("UPDATE images SET event_name = ? "
61- "WHERE event_name = ?" )
59+ sql = "UPDATE images SET event_name = ? WHERE event_name = ?"
6260 params = [new_name , old_name ]
6361 print (f" [DRY RUN] { sql } params={ params } " )
6462 else :
6563 # Temporarily drop FK constraint by backing up
6664 # and recreating image_embeddings
67- has_embeddings = _fetchone_scalar (
68- conn , "SELECT COUNT(*) FROM image_embeddings"
69- ) > 0
65+ has_embeddings = _fetchone_scalar (conn , "SELECT COUNT(*) FROM image_embeddings" ) > 0
7066
7167 print (" Backing up image_embeddings..." )
72- conn .execute (
73- "CREATE TEMPORARY TABLE _emb_backup "
74- "AS SELECT * FROM image_embeddings"
75- )
68+ conn .execute ("CREATE TEMPORARY TABLE _emb_backup AS SELECT * FROM image_embeddings" )
7669 conn .execute ("DROP TABLE image_embeddings" )
7770
7871 # Now we can UPDATE images freely
7972 for old_name , new_name , new_year in FIXES :
8073 if new_year is not None :
8174 row = conn .execute (
82- "UPDATE images SET event_name = ?, "
83- "event_year = ? WHERE event_name = ?" ,
75+ "UPDATE images SET event_name = ?, event_year = ? WHERE event_name = ?" ,
8476 [new_name , new_year , old_name ],
8577 ).fetchone ()
8678 else :
8779 row = conn .execute (
88- "UPDATE images SET event_name = ? "
89- "WHERE event_name = ?" ,
80+ "UPDATE images SET event_name = ? WHERE event_name = ?" ,
9081 [new_name , old_name ],
9182 ).fetchone ()
9283 cnt = int (row [0 ]) if row else 0
9384 suffix = f" (year={ new_year } )" if new_year is not None else ""
94- print (f" Updated { cnt } rows: '{ old_name } ' -> '{ new_name } '"
95- + suffix )
85+ print (f" Updated { cnt } rows: '{ old_name } ' -> '{ new_name } '" + suffix )
9686
9787 # Restore image_embeddings with FK
9888 print (" Restoring image_embeddings..." )
@@ -106,21 +96,14 @@ def apply_fixes(db_path: str, *, dry_run: bool) -> None:
10696 FOREIGN KEY (image_id) REFERENCES images(id)
10797 )
10898 """ )
99+ conn .execute ("INSERT INTO image_embeddings SELECT * FROM _emb_backup" )
109100 conn .execute (
110- "INSERT INTO image_embeddings "
111- "SELECT * FROM _emb_backup"
112- )
113- conn .execute (
114- "CREATE INDEX IF NOT EXISTS idx_embeddings_model "
115- "ON image_embeddings(model_name)"
101+ "CREATE INDEX IF NOT EXISTS idx_embeddings_model ON image_embeddings(model_name)"
116102 )
117103 conn .execute ("DROP TABLE _emb_backup" )
118104
119- restored = _fetchone_scalar (
120- conn , "SELECT COUNT(*) FROM image_embeddings"
121- )
122- print (f" Restored { restored } embeddings"
123- f" (had_data={ has_embeddings } )" )
105+ restored = _fetchone_scalar (conn , "SELECT COUNT(*) FROM image_embeddings" )
106+ print (f" Restored { restored } embeddings (had_data={ has_embeddings } )" )
124107
125108 print ("\n --- After ---" )
126109 show_events (conn )
0 commit comments