42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
from sqlalchemy import inspect, text
|
|
from database import db
|
|
|
|
|
|
def run_migrations():
|
|
db.create_all()
|
|
print("Таблицы созданы (или уже существуют)")
|
|
_add_profile_columns()
|
|
print("Все миграции применены!")
|
|
|
|
|
|
def _add_profile_columns():
|
|
inspector = inspect(db.engine)
|
|
existing_columns = [col['name'] for col in inspector.get_columns('users')]
|
|
|
|
columns_to_add = {
|
|
'email': 'VARCHAR(255) NULL',
|
|
'jabber': 'VARCHAR(255) NULL',
|
|
'updated_at': 'DATETIME NULL',
|
|
'notify_email': 'BOOLEAN DEFAULT 0',
|
|
'notify_jabber': 'BOOLEAN DEFAULT 0',
|
|
}
|
|
|
|
added = []
|
|
for col_name, col_type in columns_to_add.items():
|
|
if col_name not in existing_columns:
|
|
print(f"Добавляем колонку: {col_name}")
|
|
db.session.execute(text(f"ALTER TABLE users ADD COLUMN {col_name} {col_type}"))
|
|
added.append(col_name)
|
|
|
|
if added:
|
|
db.session.commit()
|
|
print(f"Добавлены колонки: {', '.join(added)}")
|
|
else:
|
|
print("ℹВсе колонки профиля уже существуют")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
from main import app
|
|
|
|
with app.app_context():
|
|
run_migrations() |