database.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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, kprofile_note # 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
  91. # Migration: Add interval_type column to maintenance_types
  92. try:
  93. await conn.execute(text(
  94. "ALTER TABLE maintenance_types ADD COLUMN interval_type VARCHAR(20) DEFAULT 'hours'"
  95. ))
  96. except Exception:
  97. # Column already exists
  98. pass
  99. # Migration: Add custom_interval_type column to printer_maintenance
  100. try:
  101. await conn.execute(text(
  102. "ALTER TABLE printer_maintenance ADD COLUMN custom_interval_type VARCHAR(20)"
  103. ))
  104. except Exception:
  105. # Column already exists
  106. pass