database.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
  2. from sqlalchemy.orm import DeclarativeBase
  3. from backend.app.core.config import settings
  4. engine = create_async_engine(
  5. settings.database_url,
  6. echo=settings.debug,
  7. )
  8. async_session = async_sessionmaker(
  9. engine,
  10. class_=AsyncSession,
  11. expire_on_commit=False,
  12. )
  13. class Base(DeclarativeBase):
  14. pass
  15. async def get_db() -> AsyncSession:
  16. async with async_session() as session:
  17. try:
  18. yield session
  19. await session.commit()
  20. except Exception:
  21. await session.rollback()
  22. raise
  23. finally:
  24. await session.close()
  25. async def init_db():
  26. # Import models to register them with SQLAlchemy
  27. from backend.app.models import printer, archive, filament, settings, smart_plug, print_queue, notification, maintenance # noqa: F401
  28. async with engine.begin() as conn:
  29. await conn.run_sync(Base.metadata.create_all)
  30. # Run migrations for new columns (SQLite doesn't auto-add columns)
  31. await run_migrations(conn)
  32. async def run_migrations(conn):
  33. """Add new columns to existing tables if they don't exist."""
  34. from sqlalchemy import text
  35. # Migration: Add is_favorite column to print_archives
  36. try:
  37. await conn.execute(text(
  38. "ALTER TABLE print_archives ADD COLUMN is_favorite BOOLEAN DEFAULT 0"
  39. ))
  40. except Exception:
  41. # Column already exists
  42. pass
  43. # Migration: Add content_hash column to print_archives for duplicate detection
  44. try:
  45. await conn.execute(text(
  46. "ALTER TABLE print_archives ADD COLUMN content_hash VARCHAR(64)"
  47. ))
  48. except Exception:
  49. # Column already exists
  50. pass
  51. # Migration: Add auto_off_executed column to smart_plugs
  52. try:
  53. await conn.execute(text(
  54. "ALTER TABLE smart_plugs ADD COLUMN auto_off_executed BOOLEAN DEFAULT 0"
  55. ))
  56. except Exception:
  57. # Column already exists
  58. pass
  59. # Migration: Add on_print_stopped column to notification_providers
  60. try:
  61. await conn.execute(text(
  62. "ALTER TABLE notification_providers ADD COLUMN on_print_stopped BOOLEAN DEFAULT 1"
  63. ))
  64. except Exception:
  65. # Column already exists
  66. pass
  67. # Migration: Add source_3mf_path column to print_archives
  68. try:
  69. await conn.execute(text(
  70. "ALTER TABLE print_archives ADD COLUMN source_3mf_path VARCHAR(500)"
  71. ))
  72. except Exception:
  73. # Column already exists
  74. pass
  75. # Migration: Add on_maintenance_due column to notification_providers
  76. try:
  77. await conn.execute(text(
  78. "ALTER TABLE notification_providers ADD COLUMN on_maintenance_due BOOLEAN DEFAULT 0"
  79. ))
  80. except Exception:
  81. # Column already exists
  82. pass
  83. # Migration: Add location column to printers for grouping
  84. try:
  85. await conn.execute(text(
  86. "ALTER TABLE printers ADD COLUMN location VARCHAR(100)"
  87. ))
  88. except Exception:
  89. # Column already exists
  90. pass