36 lines
945 B
Python
36 lines
945 B
Python
from sqlalchemy import create_engine, text
|
|
from sqlalchemy.orm import sessionmaker, DeclarativeBase
|
|
from app.config import settings
|
|
|
|
engine = create_engine(
|
|
settings.DATABASE_URL,
|
|
connect_args={"check_same_thread": False},
|
|
)
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
def apply_migrations():
|
|
"""Aplica migraciones incrementales compatibles con SQLite."""
|
|
with engine.connect() as conn:
|
|
for sql in [
|
|
"ALTER TABLE playlists ADD COLUMN spotify_type VARCHAR NOT NULL DEFAULT 'playlist'",
|
|
"ALTER TABLE playlists ADD COLUMN emoji VARCHAR NOT NULL DEFAULT ''",
|
|
]:
|
|
try:
|
|
conn.execute(text(sql))
|
|
conn.commit()
|
|
except Exception:
|
|
pass # columna ya existe
|