Flask-Files/migrations/add_storage_name.py
2025-03-22 12:30:45 +01:00

52 lines
No EOL
1.5 KiB
Python

"""
Script to add storage_name column to the file table.
Run this once from the command line:
python migrations/add_storage_name.py
"""
import os
import sys
import sqlite3
# Add the parent directory to the path so we can import the app
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from config import Config
from app import create_app
def add_storage_name_column():
"""Add storage_name column to file table"""
# Get database path from config
app = create_app()
with app.app_context():
db_path = app.config['SQLALCHEMY_DATABASE_URI'].replace('sqlite:///', '')
print(f"Database path: {db_path}")
# Connect to the database
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
try:
# Check if column already exists
cursor.execute("PRAGMA table_info(file)")
columns = [column[1] for column in cursor.fetchall()]
if 'storage_name' not in columns:
print("Adding storage_name column to file table...")
cursor.execute("ALTER TABLE file ADD COLUMN storage_name TEXT")
# Update existing files to use name as storage_name
cursor.execute("UPDATE file SET storage_name = name WHERE is_folder = 0")
conn.commit()
print("Column added successfully!")
else:
print("Column already exists.")
except Exception as e:
print(f"Error: {e}")
finally:
conn.close()
if __name__ == "__main__":
add_storage_name_column()