Flask-Files/app/migrations/add_folder_id_to_files.py
2025-03-23 03:29:05 +01:00

29 lines
No EOL
1 KiB
Python

"""
Migration script to add folder_id column to files table
"""
from flask import Flask
from app import create_app, db
from app.models import File, Folder
def run_migration():
"""Add folder_id column to files table if it doesn't exist"""
app = create_app()
with app.app_context():
# Check if the column exists
from sqlalchemy import inspect
inspector = inspect(db.engine)
columns = [col['name'] for col in inspector.get_columns('files')]
if 'folder_id' not in columns:
print("Adding folder_id column to files table...")
# Add the column
db.engine.execute('ALTER TABLE files ADD COLUMN folder_id INTEGER;')
# Add foreign key constraint
db.engine.execute('ALTER TABLE files ADD CONSTRAINT fk_files_folder_id FOREIGN KEY (folder_id) REFERENCES folders (id);')
print("Column added successfully!")
else:
print("folder_id column already exists")
if __name__ == "__main__":
run_migration()