96 lines
3.3 KiB
Python
96 lines
3.3 KiB
Python
"""
|
|
Database migration script to add template-related fields to generated_content table
|
|
|
|
Adds:
|
|
- formatted_html (Text, nullable)
|
|
- template_used (String(50), nullable)
|
|
- site_deployment_id (Integer, FK to site_deployments, nullable, indexed)
|
|
|
|
Usage:
|
|
python scripts/migrate_add_template_fields.py
|
|
"""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
project_root = Path(__file__).parent.parent
|
|
sys.path.insert(0, str(project_root))
|
|
|
|
from sqlalchemy import text
|
|
from src.database.session import db_manager
|
|
from src.core.config import get_config
|
|
|
|
|
|
def migrate():
|
|
"""Add template-related fields to generated_content table"""
|
|
print("Starting migration: add template fields to generated_content...")
|
|
|
|
try:
|
|
config = get_config()
|
|
print(f"Database URL: {config.database.url}")
|
|
except Exception as e:
|
|
print(f"Error loading configuration: {e}")
|
|
sys.exit(1)
|
|
|
|
try:
|
|
db_manager.initialize()
|
|
engine = db_manager.get_engine()
|
|
|
|
with engine.connect() as conn:
|
|
print("Checking for existing columns...")
|
|
|
|
result = conn.execute(text("PRAGMA table_info(generated_content)"))
|
|
existing_columns = [row[1] for row in result]
|
|
print(f"Existing columns: {', '.join(existing_columns)}")
|
|
|
|
migrations_applied = []
|
|
|
|
if "formatted_html" not in existing_columns:
|
|
print("Adding formatted_html column...")
|
|
conn.execute(text("ALTER TABLE generated_content ADD COLUMN formatted_html TEXT"))
|
|
migrations_applied.append("formatted_html")
|
|
conn.commit()
|
|
else:
|
|
print("formatted_html column already exists, skipping")
|
|
|
|
if "template_used" not in existing_columns:
|
|
print("Adding template_used column...")
|
|
conn.execute(text("ALTER TABLE generated_content ADD COLUMN template_used VARCHAR(50)"))
|
|
migrations_applied.append("template_used")
|
|
conn.commit()
|
|
else:
|
|
print("template_used column already exists, skipping")
|
|
|
|
if "site_deployment_id" not in existing_columns:
|
|
print("Adding site_deployment_id column...")
|
|
conn.execute(text("ALTER TABLE generated_content ADD COLUMN site_deployment_id INTEGER"))
|
|
migrations_applied.append("site_deployment_id")
|
|
conn.commit()
|
|
|
|
print("Creating index on site_deployment_id...")
|
|
conn.execute(text(
|
|
"CREATE INDEX IF NOT EXISTS ix_generated_content_site_deployment_id "
|
|
"ON generated_content(site_deployment_id)"
|
|
))
|
|
conn.commit()
|
|
else:
|
|
print("site_deployment_id column already exists, skipping")
|
|
|
|
if migrations_applied:
|
|
print(f"\nMigration complete! Added columns: {', '.join(migrations_applied)}")
|
|
else:
|
|
print("\nNo migrations needed - all columns already exist")
|
|
|
|
except Exception as e:
|
|
print(f"Error during migration: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
sys.exit(1)
|
|
finally:
|
|
db_manager.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
migrate()
|
|
|