Skip to content

Commit 60e188d

Browse files
Fix missing log directory and attached DB table creation
1 parent 5dffb7b commit 60e188d

3 files changed

Lines changed: 39 additions & 19 deletions

File tree

CONTRIBUTORS

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,10 @@ Copyright (C) 2024-2025 Calibre-Web Automated contributors
303303
- zhiyue (1 commits)
304304
# Fork Contributors (crocodilestick/calibre-web-automated)
305305

306-
- crocodilestick (776 commits)
306+
- crocodilestick (778 commits)
307307
- jmarmstrong1207 (73 commits)
308308
- demitrix (30 commits)
309-
- sirwolfgang (22 commits)
309+
- sirwolfgang (29 commits)
310310
- Domoel (21 commits)
311311
- wolffshots (14 commits)
312312
- angelicadvocate (9 commits)

cps/progress_syncing/models.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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}")

scripts/kindle_epub_fixer.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,22 @@
4040
# Define the logger
4141
logger = logging.getLogger(__name__)
4242
logger.setLevel(logging.INFO) # Set the logging level
43-
# Create a FileHandler
44-
file_handler = logging.FileHandler(epub_fixer_log_file, mode='w', encoding='utf-8')
45-
# Create a Formatter and set it for the handler
46-
LOG_FORMAT = '%(message)s'
47-
formatter = logging.Formatter(LOG_FORMAT)
48-
file_handler.setFormatter(formatter)
49-
# Add the handler to the logger
50-
logger.addHandler(file_handler)
43+
# Create a FileHandler if possible, otherwise use StreamHandler
44+
try:
45+
file_handler = logging.FileHandler(epub_fixer_log_file, mode='w', encoding='utf-8')
46+
# Create a Formatter and set it for the handler
47+
LOG_FORMAT = '%(message)s'
48+
formatter = logging.Formatter(LOG_FORMAT)
49+
file_handler.setFormatter(formatter)
50+
# Add the handler to the logger
51+
logger.addHandler(file_handler)
52+
except FileNotFoundError:
53+
# Fallback for test environments where /config might not exist
54+
stream_handler = logging.StreamHandler()
55+
LOG_FORMAT = '%(message)s'
56+
formatter = logging.Formatter(LOG_FORMAT)
57+
stream_handler.setFormatter(formatter)
58+
logger.addHandler(stream_handler)
5159

5260
# Define user and group
5361
USER_NAME = "abc"

0 commit comments

Comments
 (0)