Spaces:
Sleeping
Sleeping
File size: 1,847 Bytes
bbeedce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | """
Script to migrate tasks table user_id from INTEGER to TEXT
"""
import psycopg2
import os
from dotenv import load_dotenv
load_dotenv()
def run_migration():
"""Run the user_id conversion migration"""
database_url = os.getenv('DATABASE_URL')
if not database_url:
print("ERROR: DATABASE_URL not found in environment variables")
return False
try:
# Read the SQL file
with open('migrations/convert-user-id-to-text.sql', 'r') as f:
sql = f.read()
# Connect to database
print("Connecting to database...")
conn = psycopg2.connect(database_url)
cursor = conn.cursor()
# Execute migration
print("Running user_id conversion migration...")
print("WARNING: This will delete all existing tasks!")
print("Press Ctrl+C to cancel, or Enter to continue...")
input()
cursor.execute(sql)
conn.commit()
print("SUCCESS: Migration completed successfully!")
print("\nVerifying migration...")
# Verify the change
cursor.execute("""
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = 'tasks' AND column_name = 'user_id';
""")
result = cursor.fetchone()
if result:
print(f"Column: {result[0]}, Type: {result[1]}, Nullable: {result[2]}")
else:
print("ERROR: user_id column not found!")
# Close connection
cursor.close()
conn.close()
return True
except Exception as e:
print(f"ERROR: Migration failed: {e}")
return False
if __name__ == "__main__":
print("=" * 50)
print("Tasks Table Migration")
print("Convert user_id from INTEGER to TEXT")
print("=" * 50)
run_migration()
|