78 lines
1.9 KiB
Python
78 lines
1.9 KiB
Python
"""
|
|
Migration script to add anchor_text field to article_links table
|
|
Story 4.5: Create URL and Link Reporting Script (Enhancement)
|
|
"""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
from src.database.session import db_manager
|
|
from sqlalchemy import text
|
|
|
|
|
|
def migrate():
|
|
"""Add anchor_text field to article_links table"""
|
|
|
|
session = db_manager.get_session()
|
|
|
|
try:
|
|
print("Starting migration: Add anchor_text field to article_links...")
|
|
|
|
print(" Adding anchor_text column...")
|
|
session.execute(text("""
|
|
ALTER TABLE article_links
|
|
ADD COLUMN anchor_text TEXT NULL
|
|
"""))
|
|
|
|
session.commit()
|
|
|
|
print("Migration completed successfully!")
|
|
print("\nNew field added:")
|
|
print(" - anchor_text (TEXT, nullable)")
|
|
print("\nNote: Existing links will have NULL anchor_text.")
|
|
print(" Re-run content injection to populate this field for existing content.")
|
|
|
|
except Exception as e:
|
|
session.rollback()
|
|
print(f"Migration failed: {e}")
|
|
raise
|
|
|
|
finally:
|
|
session.close()
|
|
|
|
|
|
def rollback():
|
|
"""Rollback migration (remove anchor_text field)"""
|
|
|
|
session = db_manager.get_session()
|
|
|
|
try:
|
|
print("Rolling back migration: Remove anchor_text field from article_links...")
|
|
|
|
print(" Removing anchor_text column...")
|
|
session.execute(text("""
|
|
ALTER TABLE article_links
|
|
DROP COLUMN anchor_text
|
|
"""))
|
|
|
|
session.commit()
|
|
|
|
print("Rollback completed successfully!")
|
|
|
|
except Exception as e:
|
|
session.rollback()
|
|
print(f"Rollback failed: {e}")
|
|
raise
|
|
|
|
finally:
|
|
session.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) > 1 and sys.argv[1] == "rollback":
|
|
rollback()
|
|
else:
|
|
migrate()
|
|
|