database.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
  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 ( # noqa: F401
  28. api_key,
  29. archive,
  30. external_link,
  31. filament,
  32. kprofile_note,
  33. maintenance,
  34. notification,
  35. notification_template,
  36. print_queue,
  37. printer,
  38. project,
  39. project_bom,
  40. settings,
  41. smart_plug,
  42. )
  43. async with engine.begin() as conn:
  44. await conn.run_sync(Base.metadata.create_all)
  45. # Run migrations for new columns (SQLite doesn't auto-add columns)
  46. await run_migrations(conn)
  47. # Seed default notification templates
  48. await seed_notification_templates()
  49. async def run_migrations(conn):
  50. """Add new columns to existing tables if they don't exist."""
  51. from sqlalchemy import text
  52. # Migration: Add is_favorite column to print_archives
  53. try:
  54. await conn.execute(text("ALTER TABLE print_archives ADD COLUMN is_favorite BOOLEAN DEFAULT 0"))
  55. except Exception:
  56. # Column already exists
  57. pass
  58. # Migration: Add content_hash column to print_archives for duplicate detection
  59. try:
  60. await conn.execute(text("ALTER TABLE print_archives ADD COLUMN content_hash VARCHAR(64)"))
  61. except Exception:
  62. # Column already exists
  63. pass
  64. # Migration: Add auto_off_executed column to smart_plugs
  65. try:
  66. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN auto_off_executed BOOLEAN DEFAULT 0"))
  67. except Exception:
  68. # Column already exists
  69. pass
  70. # Migration: Add on_print_stopped column to notification_providers
  71. try:
  72. await conn.execute(text("ALTER TABLE notification_providers ADD COLUMN on_print_stopped BOOLEAN DEFAULT 1"))
  73. except Exception:
  74. # Column already exists
  75. pass
  76. # Migration: Add source_3mf_path column to print_archives
  77. try:
  78. await conn.execute(text("ALTER TABLE print_archives ADD COLUMN source_3mf_path VARCHAR(500)"))
  79. except Exception:
  80. # Column already exists
  81. pass
  82. # Migration: Add f3d_path column to print_archives for Fusion 360 design files
  83. try:
  84. await conn.execute(text("ALTER TABLE print_archives ADD COLUMN f3d_path VARCHAR(500)"))
  85. except Exception:
  86. # Column already exists
  87. pass
  88. # Migration: Add on_maintenance_due column to notification_providers
  89. try:
  90. await conn.execute(text("ALTER TABLE notification_providers ADD COLUMN on_maintenance_due BOOLEAN DEFAULT 0"))
  91. except Exception:
  92. # Column already exists
  93. pass
  94. # Migration: Add location column to printers for grouping
  95. try:
  96. await conn.execute(text("ALTER TABLE printers ADD COLUMN location VARCHAR(100)"))
  97. except Exception:
  98. # Column already exists
  99. pass
  100. # Migration: Add interval_type column to maintenance_types
  101. try:
  102. await conn.execute(text("ALTER TABLE maintenance_types ADD COLUMN interval_type VARCHAR(20) DEFAULT 'hours'"))
  103. except Exception:
  104. # Column already exists
  105. pass
  106. # Migration: Add custom_interval_type column to printer_maintenance
  107. try:
  108. await conn.execute(text("ALTER TABLE printer_maintenance ADD COLUMN custom_interval_type VARCHAR(20)"))
  109. except Exception:
  110. # Column already exists
  111. pass
  112. # Migration: Add power alert columns to smart_plugs
  113. try:
  114. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN power_alert_enabled BOOLEAN DEFAULT 0"))
  115. except Exception:
  116. pass
  117. try:
  118. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN power_alert_high REAL"))
  119. except Exception:
  120. pass
  121. try:
  122. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN power_alert_low REAL"))
  123. except Exception:
  124. pass
  125. try:
  126. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN power_alert_last_triggered DATETIME"))
  127. except Exception:
  128. pass
  129. # Migration: Add schedule columns to smart_plugs
  130. try:
  131. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN schedule_enabled BOOLEAN DEFAULT 0"))
  132. except Exception:
  133. pass
  134. try:
  135. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN schedule_on_time VARCHAR(5)"))
  136. except Exception:
  137. pass
  138. try:
  139. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN schedule_off_time VARCHAR(5)"))
  140. except Exception:
  141. pass
  142. # Migration: Add daily digest columns to notification_providers
  143. try:
  144. await conn.execute(text("ALTER TABLE notification_providers ADD COLUMN daily_digest_enabled BOOLEAN DEFAULT 0"))
  145. except Exception:
  146. pass
  147. try:
  148. await conn.execute(text("ALTER TABLE notification_providers ADD COLUMN daily_digest_time VARCHAR(5)"))
  149. except Exception:
  150. pass
  151. # Migration: Add project_id column to print_archives
  152. try:
  153. await conn.execute(
  154. text("ALTER TABLE print_archives ADD COLUMN project_id INTEGER REFERENCES projects(id) ON DELETE SET NULL")
  155. )
  156. except Exception:
  157. pass
  158. # Migration: Add project_id column to print_queue
  159. try:
  160. await conn.execute(
  161. text("ALTER TABLE print_queue ADD COLUMN project_id INTEGER REFERENCES projects(id) ON DELETE SET NULL")
  162. )
  163. except Exception:
  164. pass
  165. # Migration: Create FTS5 virtual table for archive full-text search
  166. try:
  167. await conn.execute(
  168. text("""
  169. CREATE VIRTUAL TABLE IF NOT EXISTS archive_fts USING fts5(
  170. print_name,
  171. filename,
  172. tags,
  173. notes,
  174. designer,
  175. filament_type,
  176. content='print_archives',
  177. content_rowid='id'
  178. )
  179. """)
  180. )
  181. except Exception:
  182. pass
  183. # Migration: Create triggers to keep FTS index in sync
  184. try:
  185. await conn.execute(
  186. text("""
  187. CREATE TRIGGER IF NOT EXISTS archive_fts_insert AFTER INSERT ON print_archives BEGIN
  188. INSERT INTO archive_fts(rowid, print_name, filename, tags, notes, designer, filament_type)
  189. VALUES (new.id, new.print_name, new.filename, new.tags, new.notes, new.designer, new.filament_type);
  190. END
  191. """)
  192. )
  193. except Exception:
  194. pass
  195. try:
  196. await conn.execute(
  197. text("""
  198. CREATE TRIGGER IF NOT EXISTS archive_fts_delete AFTER DELETE ON print_archives BEGIN
  199. INSERT INTO archive_fts(archive_fts, rowid, print_name, filename, tags, notes, designer, filament_type)
  200. VALUES ('delete', old.id, old.print_name, old.filename, old.tags, old.notes, old.designer, old.filament_type);
  201. END
  202. """)
  203. )
  204. except Exception:
  205. pass
  206. try:
  207. await conn.execute(
  208. text("""
  209. CREATE TRIGGER IF NOT EXISTS archive_fts_update AFTER UPDATE ON print_archives BEGIN
  210. INSERT INTO archive_fts(archive_fts, rowid, print_name, filename, tags, notes, designer, filament_type)
  211. VALUES ('delete', old.id, old.print_name, old.filename, old.tags, old.notes, old.designer, old.filament_type);
  212. INSERT INTO archive_fts(rowid, print_name, filename, tags, notes, designer, filament_type)
  213. VALUES (new.id, new.print_name, new.filename, new.tags, new.notes, new.designer, new.filament_type);
  214. END
  215. """)
  216. )
  217. except Exception:
  218. pass
  219. # Migration: Add auto_off_pending columns to smart_plugs (for restart recovery)
  220. try:
  221. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN auto_off_pending BOOLEAN DEFAULT 0"))
  222. except Exception:
  223. pass
  224. try:
  225. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN auto_off_pending_since DATETIME"))
  226. except Exception:
  227. pass
  228. # Migration: Add AMS alarm notification columns to notification_providers
  229. try:
  230. await conn.execute(text("ALTER TABLE notification_providers ADD COLUMN on_ams_humidity_high BOOLEAN DEFAULT 0"))
  231. except Exception:
  232. pass
  233. try:
  234. await conn.execute(
  235. text("ALTER TABLE notification_providers ADD COLUMN on_ams_temperature_high BOOLEAN DEFAULT 0")
  236. )
  237. except Exception:
  238. pass
  239. # Migration: Add AMS-HT alarm notification columns to notification_providers
  240. try:
  241. await conn.execute(
  242. text("ALTER TABLE notification_providers ADD COLUMN on_ams_ht_humidity_high BOOLEAN DEFAULT 0")
  243. )
  244. except Exception:
  245. pass
  246. try:
  247. await conn.execute(
  248. text("ALTER TABLE notification_providers ADD COLUMN on_ams_ht_temperature_high BOOLEAN DEFAULT 0")
  249. )
  250. except Exception:
  251. pass
  252. # Migration: Add notes column to projects (Phase 2)
  253. try:
  254. await conn.execute(text("ALTER TABLE projects ADD COLUMN notes TEXT"))
  255. except Exception:
  256. pass
  257. # Migration: Add attachments column to projects (Phase 3)
  258. try:
  259. await conn.execute(text("ALTER TABLE projects ADD COLUMN attachments JSON"))
  260. except Exception:
  261. pass
  262. # Migration: Add tags column to projects (Phase 4)
  263. try:
  264. await conn.execute(text("ALTER TABLE projects ADD COLUMN tags TEXT"))
  265. except Exception:
  266. pass
  267. # Migration: Add due_date column to projects (Phase 5)
  268. try:
  269. await conn.execute(text("ALTER TABLE projects ADD COLUMN due_date DATETIME"))
  270. except Exception:
  271. pass
  272. # Migration: Add priority column to projects (Phase 5)
  273. try:
  274. await conn.execute(text("ALTER TABLE projects ADD COLUMN priority VARCHAR(20) DEFAULT 'normal'"))
  275. except Exception:
  276. pass
  277. # Migration: Add budget column to projects (Phase 6)
  278. try:
  279. await conn.execute(text("ALTER TABLE projects ADD COLUMN budget REAL"))
  280. except Exception:
  281. pass
  282. # Migration: Add is_template column to projects (Phase 8)
  283. try:
  284. await conn.execute(text("ALTER TABLE projects ADD COLUMN is_template BOOLEAN DEFAULT 0"))
  285. except Exception:
  286. pass
  287. # Migration: Add template_source_id column to projects (Phase 8)
  288. try:
  289. await conn.execute(text("ALTER TABLE projects ADD COLUMN template_source_id INTEGER"))
  290. except Exception:
  291. pass
  292. # Migration: Add parent_id column to projects (Phase 10)
  293. try:
  294. await conn.execute(
  295. text("ALTER TABLE projects ADD COLUMN parent_id INTEGER REFERENCES projects(id) ON DELETE SET NULL")
  296. )
  297. except Exception:
  298. pass
  299. # Migration: Rename quantity_printed to quantity_acquired in project_bom_items
  300. try:
  301. await conn.execute(text("ALTER TABLE project_bom_items RENAME COLUMN quantity_printed TO quantity_acquired"))
  302. except Exception:
  303. pass
  304. # Migration: Add unit_price column to project_bom_items
  305. try:
  306. await conn.execute(text("ALTER TABLE project_bom_items ADD COLUMN unit_price REAL"))
  307. except Exception:
  308. pass
  309. # Migration: Add sourcing_url column to project_bom_items
  310. try:
  311. await conn.execute(text("ALTER TABLE project_bom_items ADD COLUMN sourcing_url VARCHAR(512)"))
  312. except Exception:
  313. pass
  314. # Migration: Rename notes to remarks in project_bom_items
  315. try:
  316. await conn.execute(text("ALTER TABLE project_bom_items RENAME COLUMN notes TO remarks"))
  317. except Exception:
  318. pass
  319. # Migration: Add show_in_switchbar column to smart_plugs
  320. try:
  321. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN show_in_switchbar BOOLEAN DEFAULT 0"))
  322. except Exception:
  323. pass
  324. # Migration: Add runtime tracking columns to printers
  325. try:
  326. await conn.execute(text("ALTER TABLE printers ADD COLUMN runtime_seconds INTEGER DEFAULT 0"))
  327. except Exception:
  328. pass
  329. try:
  330. await conn.execute(text("ALTER TABLE printers ADD COLUMN last_runtime_update DATETIME"))
  331. except Exception:
  332. pass
  333. # Migration: Add quantity column to print_archives for tracking item count
  334. try:
  335. await conn.execute(text("ALTER TABLE print_archives ADD COLUMN quantity INTEGER DEFAULT 1"))
  336. except Exception:
  337. pass
  338. # Migration: Add manual_start column to print_queue for staged prints
  339. try:
  340. await conn.execute(text("ALTER TABLE print_queue ADD COLUMN manual_start BOOLEAN DEFAULT 0"))
  341. except Exception:
  342. pass
  343. # Migration: Add wiki_url column to maintenance_types for documentation links
  344. try:
  345. await conn.execute(text("ALTER TABLE maintenance_types ADD COLUMN wiki_url VARCHAR(500)"))
  346. except Exception:
  347. pass
  348. # Migration: Add ams_mapping column to print_queue for storing filament slot assignments
  349. try:
  350. await conn.execute(text("ALTER TABLE print_queue ADD COLUMN ams_mapping TEXT"))
  351. except Exception:
  352. pass
  353. # Migration: Add target_parts_count column to projects for tracking total parts needed
  354. try:
  355. await conn.execute(text("ALTER TABLE projects ADD COLUMN target_parts_count INTEGER"))
  356. except Exception:
  357. pass
  358. # Migration: Make printer_id nullable in print_queue for unassigned queue items
  359. # SQLite doesn't support ALTER COLUMN, so we need to recreate the table
  360. try:
  361. # Check if printer_id is already nullable by trying to insert NULL
  362. # This is a safe check that won't affect existing data
  363. result = await conn.execute(text("SELECT sql FROM sqlite_master WHERE type='table' AND name='print_queue'"))
  364. row = result.fetchone()
  365. if row and "printer_id INTEGER NOT NULL" in (row[0] or ""):
  366. # Need to migrate - printer_id is currently NOT NULL
  367. await conn.execute(
  368. text("""
  369. CREATE TABLE print_queue_new (
  370. id INTEGER PRIMARY KEY,
  371. printer_id INTEGER REFERENCES printers(id) ON DELETE CASCADE,
  372. archive_id INTEGER NOT NULL REFERENCES print_archives(id) ON DELETE CASCADE,
  373. project_id INTEGER REFERENCES projects(id) ON DELETE SET NULL,
  374. position INTEGER DEFAULT 0,
  375. scheduled_time DATETIME,
  376. manual_start BOOLEAN DEFAULT 0,
  377. require_previous_success BOOLEAN DEFAULT 0,
  378. auto_off_after BOOLEAN DEFAULT 0,
  379. ams_mapping TEXT,
  380. status VARCHAR(20) DEFAULT 'pending',
  381. started_at DATETIME,
  382. completed_at DATETIME,
  383. error_message TEXT,
  384. created_at DATETIME DEFAULT CURRENT_TIMESTAMP
  385. )
  386. """)
  387. )
  388. await conn.execute(
  389. text("""
  390. INSERT INTO print_queue_new
  391. SELECT id, printer_id, archive_id, project_id, position, scheduled_time,
  392. manual_start, require_previous_success, auto_off_after, ams_mapping,
  393. status, started_at, completed_at, error_message, created_at
  394. FROM print_queue
  395. """)
  396. )
  397. await conn.execute(text("DROP TABLE print_queue"))
  398. await conn.execute(text("ALTER TABLE print_queue_new RENAME TO print_queue"))
  399. except Exception:
  400. pass
  401. # Migration: Add plug_type column to smart_plugs for HA integration
  402. try:
  403. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN plug_type VARCHAR(20) DEFAULT 'tasmota'"))
  404. except Exception:
  405. pass
  406. # Migration: Add ha_entity_id column to smart_plugs for HA integration
  407. try:
  408. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN ha_entity_id VARCHAR(100)"))
  409. except Exception:
  410. pass
  411. # Migration: Make ip_address nullable for HA plugs (SQLite requires table recreation)
  412. try:
  413. # Check if ip_address is currently NOT NULL
  414. result = await conn.execute(text("SELECT sql FROM sqlite_master WHERE type='table' AND name='smart_plugs'"))
  415. row = result.fetchone()
  416. if row and "ip_address VARCHAR(45) NOT NULL" in (row[0] or ""):
  417. # Need to migrate - ip_address is currently NOT NULL
  418. await conn.execute(
  419. text("""
  420. CREATE TABLE smart_plugs_new (
  421. id INTEGER PRIMARY KEY,
  422. name VARCHAR(100) NOT NULL,
  423. ip_address VARCHAR(45),
  424. plug_type VARCHAR(20) DEFAULT 'tasmota',
  425. ha_entity_id VARCHAR(100),
  426. printer_id INTEGER UNIQUE REFERENCES printers(id) ON DELETE SET NULL,
  427. enabled BOOLEAN NOT NULL DEFAULT 1,
  428. auto_on BOOLEAN NOT NULL DEFAULT 1,
  429. auto_off BOOLEAN NOT NULL DEFAULT 1,
  430. off_delay_mode VARCHAR(20) NOT NULL DEFAULT 'time',
  431. off_delay_minutes INTEGER NOT NULL DEFAULT 5,
  432. off_temp_threshold INTEGER NOT NULL DEFAULT 70,
  433. username VARCHAR(50),
  434. password VARCHAR(100),
  435. power_alert_enabled BOOLEAN NOT NULL DEFAULT 0,
  436. power_alert_high FLOAT,
  437. power_alert_low FLOAT,
  438. power_alert_last_triggered DATETIME,
  439. schedule_enabled BOOLEAN NOT NULL DEFAULT 0,
  440. schedule_on_time VARCHAR(5),
  441. schedule_off_time VARCHAR(5),
  442. show_in_switchbar BOOLEAN DEFAULT 0,
  443. last_state VARCHAR(10),
  444. last_checked DATETIME,
  445. auto_off_executed BOOLEAN NOT NULL DEFAULT 0,
  446. auto_off_pending BOOLEAN DEFAULT 0,
  447. auto_off_pending_since DATETIME,
  448. created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
  449. updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
  450. )
  451. """)
  452. )
  453. await conn.execute(
  454. text("""
  455. INSERT INTO smart_plugs_new
  456. SELECT id, name, ip_address,
  457. COALESCE(plug_type, 'tasmota'), ha_entity_id, printer_id,
  458. enabled, auto_on, auto_off, off_delay_mode, off_delay_minutes, off_temp_threshold,
  459. username, password, power_alert_enabled, power_alert_high, power_alert_low,
  460. power_alert_last_triggered, schedule_enabled, schedule_on_time, schedule_off_time,
  461. COALESCE(show_in_switchbar, 0), last_state, last_checked, auto_off_executed,
  462. COALESCE(auto_off_pending, 0), auto_off_pending_since, created_at, updated_at
  463. FROM smart_plugs
  464. """)
  465. )
  466. await conn.execute(text("DROP TABLE smart_plugs"))
  467. await conn.execute(text("ALTER TABLE smart_plugs_new RENAME TO smart_plugs"))
  468. except Exception:
  469. pass
  470. async def seed_notification_templates():
  471. """Seed default notification templates if they don't exist."""
  472. from sqlalchemy import select
  473. from backend.app.models.notification_template import DEFAULT_TEMPLATES, NotificationTemplate
  474. async with async_session() as session:
  475. # Check if templates already exist
  476. result = await session.execute(select(NotificationTemplate).limit(1))
  477. if result.scalar_one_or_none() is not None:
  478. # Templates already seeded
  479. return
  480. # Insert default templates
  481. for template_data in DEFAULT_TEMPLATES:
  482. template = NotificationTemplate(
  483. event_type=template_data["event_type"],
  484. name=template_data["name"],
  485. title_template=template_data["title_template"],
  486. body_template=template_data["body_template"],
  487. is_default=True,
  488. )
  489. session.add(template)
  490. await session.commit()