cosmo/backend/scripts/fix_position_source_constra...

60 lines
1.9 KiB
Python

"""
Fix positions table CHECK constraint to include 'nasa_horizons_cron'
"""
import asyncio
import sys
from pathlib import Path
# Add backend to path
sys.path.insert(0, str(Path(__file__).parent.parent))
from sqlalchemy import text
from app.database import engine
async def fix_constraint():
"""Fix positions table source constraint"""
async with engine.begin() as conn:
print("🔍 Checking current constraint...")
# Check current constraint definition
result = await conn.execute(text("""
SELECT pg_get_constraintdef(oid)
FROM pg_constraint
WHERE conname = 'chk_source' AND conrelid = 'positions'::regclass;
"""))
current = result.fetchone()
if current:
print(f"📋 Current constraint: {current[0]}")
else:
print("⚠️ No constraint found!")
print("\n🔧 Dropping old constraint...")
await conn.execute(text("""
ALTER TABLE positions DROP CONSTRAINT IF EXISTS chk_source;
"""))
print("✅ Old constraint dropped")
print("\n🆕 Creating new constraint with 'nasa_horizons_cron'...")
await conn.execute(text("""
ALTER TABLE positions ADD CONSTRAINT chk_source
CHECK (source IN ('nasa_horizons', 'nasa_horizons_cron', 'calculated', 'user_defined', 'imported'));
"""))
print("✅ New constraint created")
# Verify new constraint
result = await conn.execute(text("""
SELECT pg_get_constraintdef(oid)
FROM pg_constraint
WHERE conname = 'chk_source' AND conrelid = 'positions'::regclass;
"""))
new_constraint = result.fetchone()
if new_constraint:
print(f"\n✅ New constraint: {new_constraint[0]}")
print("\n🎉 Constraint update completed successfully!")
if __name__ == "__main__":
asyncio.run(fix_constraint())