Fix: Preserve new INSTALLED_APPS during upgrade by not overwriting settings.py

The issue was that restoreCriticalFiles was restoring the OLD settings.py from backup
which didn't have new apps like 'aiScanner' in INSTALLED_APPS.

Solution:
- Modified restoreCriticalFiles to skip settings.py restoration
- Keep the NEW settings.py from the fresh clone (which has aiScanner in INSTALLED_APPS)
- Only update the DATABASES section with saved credentials from backup
- This preserves all new app registrations while maintaining database connectivity

This properly fixes the RuntimeError about aiScanner.status_models.ScanStatusUpdate
not being in INSTALLED_APPS after upgrades.
This commit is contained in:
usmannasir 2025-08-08 22:54:44 +05:00
parent 63371be011
commit 753f4f0606
2 changed files with 13 additions and 10 deletions

View File

@ -4,9 +4,7 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="5251c5c9-f2a1-41f2-bc76-10b517091df1" name="Changes" comment=""> <list default="true" id="5251c5c9-f2a1-41f2-bc76-10b517091df1" name="Changes" comment="" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
@ -117,7 +115,7 @@
<workItem from="1754429757112" duration="3503000" /> <workItem from="1754429757112" duration="3503000" />
<workItem from="1754433799097" duration="517000" /> <workItem from="1754433799097" duration="517000" />
<workItem from="1754448353513" duration="2970000" /> <workItem from="1754448353513" duration="2970000" />
<workItem from="1754511414251" duration="24525000" /> <workItem from="1754511414251" duration="25159000" />
</task> </task>
<servers /> <servers />
</component> </component>

View File

@ -2360,6 +2360,11 @@ CREATE TABLE `websiteFunctions_backupsv2` (`id` integer AUTO_INCREMENT NOT NULL
def restoreCriticalFiles(backup_dir, backed_up_files): def restoreCriticalFiles(backup_dir, backed_up_files):
"""Restore critical configuration files after upgrade""" """Restore critical configuration files after upgrade"""
for original_path, backup_path in backed_up_files.items(): for original_path, backup_path in backed_up_files.items():
# Skip settings.py - we'll handle it separately to preserve INSTALLED_APPS
if 'settings.py' in original_path:
Upgrade.stdOut(f"Skipping {original_path} - will be handled separately")
continue
try: try:
if os.path.isdir(backup_path): if os.path.isdir(backup_path):
if os.path.exists(original_path): if os.path.exists(original_path):
@ -2463,23 +2468,23 @@ CREATE TABLE `websiteFunctions_backupsv2` (`id` integer AUTO_INCREMENT NOT NULL
if not Upgrade.executioner(command, command, 1): if not Upgrade.executioner(command, command, 1):
Upgrade.stdOut(f"Warning: Failed to checkout branch {branch}, continuing with default branch") Upgrade.stdOut(f"Warning: Failed to checkout branch {branch}, continuing with default branch")
# Restore all backed up configuration files # Restore all backed up configuration files (except settings.py)
Upgrade.stdOut("Restoring configuration files...") Upgrade.stdOut("Restoring configuration files...")
Upgrade.restoreCriticalFiles(backup_dir, backed_up_files) Upgrade.restoreCriticalFiles(backup_dir, backed_up_files)
## Update settings file with database credentials while preserving other settings ## Handle settings.py separately to preserve NEW INSTALLED_APPS while keeping old database credentials
# Read the current settings file (which was just restored from backup) # Read the NEW settings file from the fresh clone (has new INSTALLED_APPS like 'aiScanner')
settingsData = open(settingsFile, 'r').read() settingsData = open(settingsFile, 'r').read()
# Replace only the DATABASES section while keeping everything else (including INSTALLED_APPS) # Replace only the DATABASES section with our saved credentials
import re import re
# More precise pattern to match the entire DATABASES dictionary including nested dictionaries # More precise pattern to match the entire DATABASES dictionary including nested dictionaries
# This pattern looks for DATABASES = { ... } including the 'default' and 'rootdb' nested dicts # This pattern looks for DATABASES = { ... } including the 'default' and 'rootdb' nested dicts
database_pattern = r'DATABASES\s*=\s*\{[^}]*\{[^}]*\}[^}]*\{[^}]*\}[^}]*\}' database_pattern = r'DATABASES\s*=\s*\{[^}]*\{[^}]*\}[^}]*\{[^}]*\}[^}]*\}'
# Replace the DATABASES section with our saved credentials # Replace the DATABASES section with our saved credentials from before upgrade
settingsData = re.sub(database_pattern, completDBString.strip(), settingsData, flags=re.DOTALL) settingsData = re.sub(database_pattern, completDBString.strip(), settingsData, flags=re.DOTALL)
# Write back the updated settings # Write back the updated settings
@ -2487,7 +2492,7 @@ CREATE TABLE `websiteFunctions_backupsv2` (`id` integer AUTO_INCREMENT NOT NULL
writeToFile.write(settingsData) writeToFile.write(settingsData)
writeToFile.close() writeToFile.close()
Upgrade.stdOut('Settings file restored with database credentials!') Upgrade.stdOut('Settings file updated with database credentials while preserving new INSTALLED_APPS!')
Upgrade.staticContent() Upgrade.staticContent()