Skip to content

Commit

Permalink
Attempted to fix DB error that occurs when first updating from V2.1.2…
Browse files Browse the repository at this point in the history
… to V3.0.0. Also made improvments to the convert-library user experiance
  • Loading branch information
crocodilestick committed Dec 31, 2024
1 parent 2940f4a commit 992fc93
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 25 deletions.
3 changes: 2 additions & 1 deletion root/app/calibre-web/cps/cwa_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import queue
import os
import tempfile
from datetime import datetime

import sys
sys.path.insert(1, '/app/calibre-web-automated/scripts/')
Expand Down Expand Up @@ -282,7 +283,7 @@ def kill_convert_library(queue):
except FileNotFoundError:
...
with open("/config/convert-library.log", 'a') as f:
f.write("\nCONVERT LIBRARY PROCESS TERMINATED BY USER")
f.write(f"\nCONVERT LIBRARY PROCESS TERMINATED BY USER AT {datetime.now()}")
break

@convert_library.route('/cwa-convert-library-overview', methods=["GET"])
Expand Down
2 changes: 1 addition & 1 deletion root/app/calibre-web/cps/templates/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ <h2>{{_('CWA Admin Functions⚡')}}</h2>
<a class="btn btn-default" id="check_mon_srvs" href="{{url_for('cwa_check_status.cwa_flash_status')}}">{{_('Check CWA Status')}}</a>
</div>
<div class="row form-group">
<a class="btn btn-default" id="library_convert" href="{{url_for('convert_library.show_convert_library_page')}}">{{_('Convert Library to Target Format')}}</a>
<a class="btn btn-default" id="library_convert" href="{{url_for('convert_library.show_convert_library_page')}}">{{_('CWA Library Conversion Service')}}</a>
<a class="btn btn-default" id="cwa_history" href="{{url_for('cwa_history.cwa_history_show')}}">{{_('Show CWA History')}}</a>
</div>
<div class="row form-group">
Expand Down
12 changes: 6 additions & 6 deletions root/app/calibre-web/cps/templates/cwa_convert_library.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ <h3 style="color: whitesmoke;">CWA Library Convertor - Current Target Format - {
justify-content: space-between;
gap: 20px;">
<p style="font-size: xx-small;max-width: 70%;">
The Library Conversion Process automatically starts upon loading this page and therefore has already started,
you can follow it's progress below. Leaving this page will not interrupt the process but you will be unable to
return to check on it's process here in the Web UI after leaving, you would then need to check it's progress
in the container's logs.</p>
Upon loading this page, if you have previously started a run of the CWA Convert Library service either here in the Web UI or through the CLI, you will see the output of the most recent previous run below.
Once you start a run, you are free to leave the page and return whenever you want to check on the run's progress. If you wish to cancel a run that is still in progress, simply press the Cancel button above
and the run will terminate ASAP. If you wish to change the service's target format, please change your target format in the CWA Settings panel as desired.
</p>
<div>
<a class="btn btn-default" href="{{ url_for('convert_library.cancel_convert_library') }}" style="vertical-align: top; float: right; width: 100px; margin-left: 10px;">{{_('Cancel')}}</a>
<a class="btn btn-default" href="{{ url_for('convert_library.start_conversion') }}" style="vertical-align: top; float: right; width: 100px;">{{_('Start')}}</a>
Expand All @@ -24,7 +24,7 @@ <h3 style="color: whitesmoke;">CWA Library Convertor - Current Target Format - {
background: #0000003d;
padding-top: 11px;
padding-bottom: 1px;">
<p id="innerStatus" style="color: whitesmoke; font-size: small;">Loading...</h2>
<p id="innerStatus" style="color: whitesmoke; font-size: small;">No current or previous run to display. Press the Start button above to initiate a run.</h2>
</div>
</div>
</div>
Expand All @@ -48,7 +48,7 @@ <h3 style="color: whitesmoke;">CWA Library Convertor - Current Target Format - {

document.getElementById("innerStatus").innerHTML = get.status.replace(/\n/g, "<br>"); // * 10 + "&percnt;"

if (get.status.includes("Done!") || get.status.includes("CONVERT LIBRARY PROCESS TERMINATED BY USER")){
if (get.status.includes("CWA Convert Library Service - Run Ended:") || get.status.includes("CONVERT LIBRARY PROCESS TERMINATED BY USER")){
document.getElementById("innerStatus").innerHTML;
clearTimeout(timeout);
return false;
Expand Down
6 changes: 4 additions & 2 deletions scripts/convert_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import subprocess
import tempfile
import atexit
from datetime import datetime

from cwa_db import CWA_DB
from kindle_epub_fixer import EPUBFixer
Expand Down Expand Up @@ -320,16 +321,17 @@ def main():
parser.add_argument('--verbose', '-v', action='store_true', required=False, dest='verbose', help='When passed, the output from the ebook-convert command will be included in what is shown to the user in the Web UI', default=False)
args = parser.parse_args()

logging.info(f"CWA Convert Library Service - Run Started: {datetime.now()}\n")
converter = LibraryConverter(args)
if len(converter.to_convert) > 0:
converter.convert_library()
else:
print_and_log("[convert-library]: No books found in library without a copy in the target format. Exiting now...")
logging.info("\nDone!")
logging.info(f"\nCWA Convert Library Service - Run Ended: {datetime.now()}")
sys.exit(0)

print_and_log(f"\n[convert-library]: Library conversion complete! {len(converter.to_convert)} books converted! Exiting now...")
logging.info("\nDone!")
logging.info(f"\nCWA Convert Library Service - Run Ended: {datetime.now()}")
sys.exit(0)


Expand Down
45 changes: 30 additions & 15 deletions scripts/cwa_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,29 +97,44 @@ def make_tables(self) -> tuple[list[str], list[str]]:
def ensure_settings_schema_match(self) -> None:
self.cur.execute("SELECT * FROM cwa_settings")
cwa_setting_names = [header[0] for header in self.cur.description]
print(f"[cwa-db] DEBUG: Current available cwa_settings: {cwa_setting_names}")

# Add any settings present in the schema file but not in the db
for setting in self.cwa_default_settings.keys():
if setting not in cwa_setting_names:
for line in self.schema:
matches = re.findall(setting, line)
if matches:
command = line.replace('\n', '').strip()
command = command.replace(',', ';')
with open('/config/debug', 'w') as f:
f.write(command)
self.cur.execute(f"ALTER TABLE cwa_settings ADD {command}")
self.con.commit()
else:
print("[cwa_db] Error adding new setting to cwa.db: Matching setting could not be found in schema file")
success = self.add_missing_setting(setting)
if success:
print(f"[cwa-db] Setting '{setting}' successfully added to cwa.db!")

# Delete any settings in the db but not in the schema file
for setting in cwa_setting_names:
if setting not in self.cwa_default_settings.keys():
self.cur.execute(f"ALTER TABLE cwa_settings DROP COLUMN {setting}")
self.con.commit()
print(f"[cwa_db] Deprecated setting found from previous version of CWA, deleting setting '{setting}' from cwa.db...")

try:
print(f"[cwa-db] Deprecated setting found from previous version of CWA, removing setting '{setting}' from cwa.db...")
self.cur.execute(f"ALTER TABLE cwa_settings DROP COLUMN {setting}")
self.con.commit()
print(f"[cwa-db] Deprecated setting {setting} successfully removed from cwa.db!")
except Exception as e:
print(f"[cwa-db] The following error occurred when trying to remove {setting} from cwa.db:\n{e}")


def add_missing_setting(self, setting) -> bool:
for line in self.schema:
match = re.findall(setting, line)
if match:
try:
command = line.replace('\n', '').strip()
command = command.replace(',', ';')
with open('/config/.cwa_db_debug', 'a') as f:
f.write(command)
self.cur.execute(f"ALTER TABLE cwa_settings ADD {command}")
self.con.commit()
return True
except Exception as e:
print(f"[cwa-db] The following error occurred when trying to add {setting} to cwa.db:\n{e}")
return False
print(f"[cwa-db] Error adding new setting to cwa.db: {setting}: Matching setting could not be found in schema file")
return False

def match_stat_table_columns_with_schema(self) -> None:
""" Used to rename columns whose names have been changed in later versions and add columns added in later versions """
Expand Down

0 comments on commit 992fc93

Please sign in to comment.