@@ -82,13 +82,25 @@ def execute_sql(sql):
8282 else :
8383 return conn .execute (sql )
8484
85+ # Check if 'calibre' database is attached (Calibre-Web architecture)
86+ try :
87+ db_list = execute_sql ("PRAGMA database_list" ).fetchall ()
88+ # Row format: (seq, name, file)
89+ is_calibre_attached = any (str (row [1 ]) == 'calibre' for row in db_list )
90+ except Exception :
91+ is_calibre_attached = False
92+
93+ table_prefix = "calibre." if is_calibre_attached else ""
94+ table_name = f"{ table_prefix } book_format_checksums"
95+ master_table = f"{ table_prefix } sqlite_master"
96+
8597 # Check if table exists
86- result = execute_sql ("SELECT name FROM sqlite_master WHERE type='table' AND name='book_format_checksums'" )
98+ result = execute_sql (f "SELECT name FROM { master_table } WHERE type='table' AND name='book_format_checksums'" )
8799 table_exists = result .fetchone () is not None
88100
89101 if table_exists :
90102 # Check if table has the expected schema
91- pragma_result = execute_sql ("PRAGMA table_info(book_format_checksums)" )
103+ pragma_result = execute_sql (f "PRAGMA { table_prefix } table_info(book_format_checksums)" )
92104 columns = [row [1 ] for row in pragma_result .fetchall ()]
93105
94106 expected_columns = {'id' , 'book' , 'format' , 'checksum' , 'version' , 'created' }
@@ -108,8 +120,8 @@ def execute_sql(sql):
108120
109121 if not table_exists :
110122 # Create table for book format checksums
111- execute_sql ("""
112- CREATE TABLE book_format_checksums (
123+ execute_sql (f """
124+ CREATE TABLE { table_name } (
113125 id INTEGER PRIMARY KEY AUTOINCREMENT,
114126 book INTEGER NOT NULL,
115127 format TEXT NOT NULL COLLATE NOCASE,
@@ -119,12 +131,12 @@ def execute_sql(sql):
119131 FOREIGN KEY (book) REFERENCES books(id)
120132 )
121133 """ )
122- execute_sql ("CREATE INDEX idx_checksum ON book_format_checksums(checksum)" )
123- execute_sql ("CREATE INDEX idx_checksum_version ON book_format_checksums(checksum, version)" )
124- execute_sql ("CREATE INDEX idx_book_format ON book_format_checksums(book, format)" )
125- execute_sql ("CREATE INDEX idx_created ON book_format_checksums(created)" )
134+ execute_sql (f "CREATE INDEX { table_prefix } idx_checksum ON book_format_checksums(checksum)" )
135+ execute_sql (f "CREATE INDEX { table_prefix } idx_checksum_version ON book_format_checksums(checksum, version)" )
136+ execute_sql (f "CREATE INDEX { table_prefix } idx_book_format ON book_format_checksums(book, format)" )
137+ execute_sql (f "CREATE INDEX { table_prefix } idx_created ON book_format_checksums(created)" )
126138 conn .commit ()
127- log .info ("Created book_format_checksums table with indexes" )
139+ log .info (f "Created { table_name } table with indexes" )
128140
129141 except Exception as e :
130142 log .error (f"Could not create book_format_checksums table: { e } " )
0 commit comments