84 lines
3.0 KiB
Python
84 lines
3.0 KiB
Python
"""
|
|
Migration: Fix custom_hostname for existing S3 sites
|
|
|
|
Updates existing S3 site deployments that have s3_custom_domain set
|
|
but custom_hostname=None. This fixes sites registered before the bug fix
|
|
in discover_s3_buckets.py.
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
from pathlib import Path
|
|
|
|
# Add parent directory to path for imports
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
from src.database.session import db_manager
|
|
from src.database.repositories import SiteDeploymentRepository
|
|
|
|
def migrate():
|
|
"""Update custom_hostname from s3_custom_domain for S3 sites"""
|
|
print("Fixing custom_hostname for existing S3 sites...")
|
|
|
|
try:
|
|
db_manager.initialize()
|
|
session = db_manager.get_session()
|
|
site_repo = SiteDeploymentRepository(session)
|
|
|
|
# Find all S3 sites with s3_custom_domain but no custom_hostname
|
|
all_sites = site_repo.get_all()
|
|
s3_sites_to_fix = [
|
|
site for site in all_sites
|
|
if site.storage_provider in ('s3', 's3_compatible')
|
|
and site.s3_custom_domain
|
|
and not site.custom_hostname
|
|
]
|
|
|
|
if not s3_sites_to_fix:
|
|
print("[OK] No S3 sites need fixing (all have custom_hostname set or no s3_custom_domain)")
|
|
return
|
|
|
|
print(f"\nFound {len(s3_sites_to_fix)} S3 site(s) to fix:")
|
|
for site in s3_sites_to_fix:
|
|
print(f" Site {site.id}: {site.site_name}")
|
|
print(f" s3_custom_domain: {site.s3_custom_domain}")
|
|
print(f" custom_hostname: {site.custom_hostname} (will be set to {site.s3_custom_domain})")
|
|
|
|
# Update each site
|
|
updated_count = 0
|
|
for site in s3_sites_to_fix:
|
|
try:
|
|
# Check if custom_hostname already exists (unique constraint)
|
|
existing = site_repo.get_by_hostname(site.s3_custom_domain)
|
|
if existing and existing.id != site.id:
|
|
print(f"\n[WARNING] Site {site.id} ({site.site_name})")
|
|
print(f" Cannot set custom_hostname='{site.s3_custom_domain}' - already used by site {existing.id}")
|
|
continue
|
|
|
|
# Update custom_hostname
|
|
site.custom_hostname = site.s3_custom_domain
|
|
session.add(site)
|
|
session.commit()
|
|
updated_count += 1
|
|
print(f"\n[OK] Updated site {site.id}: custom_hostname = '{site.s3_custom_domain}'")
|
|
|
|
except Exception as e:
|
|
session.rollback()
|
|
print(f"\n[ERROR] Failed to update site {site.id}: {e}")
|
|
raise
|
|
|
|
print(f"\n{'=' * 60}")
|
|
print(f"Migration complete: {updated_count}/{len(s3_sites_to_fix)} site(s) updated")
|
|
print("=" * 60)
|
|
|
|
except Exception as e:
|
|
print(f"\n[ERROR] Migration failed: {e}")
|
|
raise
|
|
finally:
|
|
if 'session' in locals():
|
|
session.close()
|
|
|
|
if __name__ == "__main__":
|
|
migrate()
|
|
|