Fix settings.py preservation during upgrade to maintain INSTALLED_APPS

During the upgrade process, settings.py was being overwritten with only the DATABASES
section preserved, causing loss of INSTALLED_APPS and other configurations. This resulted
in the 'aiScanner' app not being recognized after upgrade.

Fixed by:
- Improving the regex pattern to more accurately match only the DATABASES dictionary
- Adding re.DOTALL flag to handle multi-line DATABASES configuration
- Ensuring all other settings including INSTALLED_APPS are preserved during upgrade

This resolves the RuntimeError about aiScanner.status_models.ScanStatusUpdate not having
an explicit app_label.
This commit is contained in:
usmannasir 2025-08-08 22:44:42 +05:00
parent 00885d24a8
commit 63371be011
2 changed files with 19 additions and 21 deletions

View File

@ -6,7 +6,6 @@
<component name="ChangeListManager">
<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" />
<change beforePath="$PROJECT_DIR$/plogical/upgrade.py" beforeDir="false" afterPath="$PROJECT_DIR$/plogical/upgrade.py" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -118,7 +117,7 @@
<workItem from="1754429757112" duration="3503000" />
<workItem from="1754433799097" duration="517000" />
<workItem from="1754448353513" duration="2970000" />
<workItem from="1754511414251" duration="14901000" />
<workItem from="1754511414251" duration="24525000" />
</task>
<servers />
</component>

View File

@ -2467,28 +2467,27 @@ CREATE TABLE `websiteFunctions_backupsv2` (`id` integer AUTO_INCREMENT NOT NULL
Upgrade.stdOut("Restoring configuration files...")
Upgrade.restoreCriticalFiles(backup_dir, backed_up_files)
## Copy settings file
settingsData = open(settingsFile, 'r').readlines()
DATABASESCHECK = 0
## Update settings file with database credentials while preserving other settings
# Read the current settings file (which was just restored from backup)
settingsData = open(settingsFile, 'r').read()
# Replace only the DATABASES section while keeping everything else (including INSTALLED_APPS)
import re
# More precise pattern to match the entire DATABASES dictionary including nested dictionaries
# This pattern looks for DATABASES = { ... } including the 'default' and 'rootdb' nested dicts
database_pattern = r'DATABASES\s*=\s*\{[^}]*\{[^}]*\}[^}]*\{[^}]*\}[^}]*\}'
# Replace the DATABASES section with our saved credentials
settingsData = re.sub(database_pattern, completDBString.strip(), settingsData, flags=re.DOTALL)
# Write back the updated settings
writeToFile = open(settingsFile, 'w')
for items in settingsData:
if items.find('DATABASES = {') > -1:
DATABASESCHECK = 1
if DATABASESCHECK == 0:
writeToFile.write(items)
if items.find('DATABASE_ROUTERS = [') > -1:
DATABASESCHECK = 0
writeToFile.write(completDBString)
writeToFile.write(items)
writeToFile.write(settingsData)
writeToFile.close()
Upgrade.stdOut('Settings file restored!')
Upgrade.stdOut('Settings file restored with database credentials!')
Upgrade.staticContent()