mirror of
https://github.com/maziggy/bambuddy.git
synced 2026-09-30 19:21:33 +02:00
103 lines
2.8 KiB
Python
103 lines
2.8 KiB
Python
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
|
|
from sqlalchemy.orm import DeclarativeBase
|
|
|
|
from backend.app.core.config import settings
|
|
|
|
|
|
engine = create_async_engine(
|
|
settings.database_url,
|
|
echo=settings.debug,
|
|
)
|
|
|
|
async_session = async_sessionmaker(
|
|
engine,
|
|
class_=AsyncSession,
|
|
expire_on_commit=False,
|
|
)
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
|
|
async def get_db() -> AsyncSession:
|
|
async with async_session() as session:
|
|
try:
|
|
yield session
|
|
await session.commit()
|
|
except Exception:
|
|
await session.rollback()
|
|
raise
|
|
finally:
|
|
await session.close()
|
|
|
|
|
|
async def init_db():
|
|
# Import models to register them with SQLAlchemy
|
|
from backend.app.models import printer, archive, filament, settings, smart_plug, print_queue, notification, maintenance # noqa: F401
|
|
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
|
|
# Run migrations for new columns (SQLite doesn't auto-add columns)
|
|
await run_migrations(conn)
|
|
|
|
|
|
async def run_migrations(conn):
|
|
"""Add new columns to existing tables if they don't exist."""
|
|
from sqlalchemy import text
|
|
|
|
# Migration: Add is_favorite column to print_archives
|
|
try:
|
|
await conn.execute(text(
|
|
"ALTER TABLE print_archives ADD COLUMN is_favorite BOOLEAN DEFAULT 0"
|
|
))
|
|
except Exception:
|
|
# Column already exists
|
|
pass
|
|
|
|
# Migration: Add content_hash column to print_archives for duplicate detection
|
|
try:
|
|
await conn.execute(text(
|
|
"ALTER TABLE print_archives ADD COLUMN content_hash VARCHAR(64)"
|
|
))
|
|
except Exception:
|
|
# Column already exists
|
|
pass
|
|
|
|
# Migration: Add auto_off_executed column to smart_plugs
|
|
try:
|
|
await conn.execute(text(
|
|
"ALTER TABLE smart_plugs ADD COLUMN auto_off_executed BOOLEAN DEFAULT 0"
|
|
))
|
|
except Exception:
|
|
# Column already exists
|
|
pass
|
|
|
|
# Migration: Add on_print_stopped column to notification_providers
|
|
try:
|
|
await conn.execute(text(
|
|
"ALTER TABLE notification_providers ADD COLUMN on_print_stopped BOOLEAN DEFAULT 1"
|
|
))
|
|
except Exception:
|
|
# Column already exists
|
|
pass
|
|
|
|
# Migration: Add source_3mf_path column to print_archives
|
|
try:
|
|
await conn.execute(text(
|
|
"ALTER TABLE print_archives ADD COLUMN source_3mf_path VARCHAR(500)"
|
|
))
|
|
except Exception:
|
|
# Column already exists
|
|
pass
|
|
|
|
# Migration: Add on_maintenance_due column to notification_providers
|
|
try:
|
|
await conn.execute(text(
|
|
"ALTER TABLE notification_providers ADD COLUMN on_maintenance_due BOOLEAN DEFAULT 0"
|
|
))
|
|
except Exception:
|
|
# Column already exists
|
|
pass
|