DecisionTree/migrations/add_oauth_support.py

42 lines
1.7 KiB
Python

from app import app, db
from models import User, OAuth
def upgrade_database():
"""Add OAuth support to the database"""
with app.app_context():
# Check if columns already exist
with db.engine.connect() as conn:
# Check if is_oauth_user column exists in User table
result = conn.execute(db.text("PRAGMA table_info(user)"))
columns = result.fetchall()
column_names = [col[1] for col in columns]
if 'is_oauth_user' not in column_names:
conn.execute(db.text("ALTER TABLE user ADD COLUMN is_oauth_user BOOLEAN DEFAULT 0"))
print("Added is_oauth_user column to User table")
# Check if OAuth table exists
result = conn.execute(db.text("SELECT name FROM sqlite_master WHERE type='table' AND name='oauth'"))
table_exists = result.fetchone() is not None
if not table_exists:
# Create the OAuth table
conn.execute(db.text("""
CREATE TABLE oauth (
id INTEGER PRIMARY KEY AUTOINCREMENT,
provider VARCHAR(50) NOT NULL,
provider_user_id VARCHAR(256) NOT NULL,
token TEXT NOT NULL,
user_id INTEGER,
FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE,
UNIQUE (provider, provider_user_id)
)
"""))
print("Created OAuth table")
conn.commit()
print("Database migration for OAuth support completed successfully")
if __name__ == "__main__":
upgrade_database()