from sqlalchemy import create_engine, text
from config.database import DATABASE_URL

engine = create_engine(DATABASE_URL, future=True)
sqls = [
    "CREATE SEQUENCE IF NOT EXISTS users_id_seq;",
    "ALTER TABLE users ALTER COLUMN id SET DEFAULT nextval('users_id_seq');",
    "SELECT setval('users_id_seq', COALESCE((SELECT MAX(id) FROM users), 1));",
    "ALTER SEQUENCE users_id_seq OWNED BY users.id;",
]
with engine.begin() as conn:
    for s in sqls:
        print('EXECUTING:', s)
        conn.execute(text(s))
print('SEQUENCE fix applied')
