database.py 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  1. from sqlalchemy import event
  2. from sqlalchemy.exc import OperationalError
  3. from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
  4. from sqlalchemy.orm import DeclarativeBase
  5. from backend.app.core.config import settings
  6. def _set_sqlite_pragmas(dbapi_conn, connection_record):
  7. """Set SQLite pragmas on each new connection for concurrency and performance."""
  8. cursor = dbapi_conn.cursor()
  9. # WAL mode allows concurrent readers + one writer (vs default DELETE mode which locks entirely)
  10. cursor.execute("PRAGMA journal_mode = WAL")
  11. # Wait up to 5 seconds when the database is locked instead of failing immediately
  12. cursor.execute("PRAGMA busy_timeout = 5000")
  13. cursor.execute("PRAGMA synchronous = NORMAL")
  14. cursor.close()
  15. engine = create_async_engine(
  16. settings.database_url,
  17. echo=settings.debug,
  18. )
  19. # Register the pragma listener on the underlying sync engine
  20. event.listen(engine.sync_engine, "connect", _set_sqlite_pragmas)
  21. async_session = async_sessionmaker(
  22. engine,
  23. class_=AsyncSession,
  24. expire_on_commit=False,
  25. )
  26. async def close_all_connections():
  27. """Close all database connections for backup/restore operations."""
  28. global engine
  29. await engine.dispose()
  30. async def reinitialize_database():
  31. """Reinitialize database connection after restore."""
  32. global engine, async_session
  33. engine = create_async_engine(
  34. settings.database_url,
  35. echo=settings.debug,
  36. )
  37. event.listen(engine.sync_engine, "connect", _set_sqlite_pragmas)
  38. async_session = async_sessionmaker(
  39. engine,
  40. class_=AsyncSession,
  41. expire_on_commit=False,
  42. )
  43. class Base(DeclarativeBase):
  44. pass
  45. async def get_db() -> AsyncSession:
  46. async with async_session() as session:
  47. try:
  48. yield session
  49. await session.commit()
  50. except Exception:
  51. await session.rollback()
  52. raise
  53. finally:
  54. await session.close()
  55. async def init_db():
  56. # Import models to register them with SQLAlchemy
  57. from backend.app.models import ( # noqa: F401
  58. active_print_spoolman,
  59. ams_history,
  60. api_key,
  61. archive,
  62. color_catalog,
  63. external_link,
  64. filament,
  65. github_backup,
  66. group,
  67. kprofile_note,
  68. library,
  69. local_preset,
  70. maintenance,
  71. notification,
  72. notification_template,
  73. orca_base_cache,
  74. pending_upload,
  75. print_log,
  76. print_queue,
  77. printer,
  78. project,
  79. project_bom,
  80. settings,
  81. slot_preset,
  82. smart_plug,
  83. spool,
  84. spool_assignment,
  85. spool_catalog,
  86. spool_k_profile,
  87. spool_usage_history,
  88. user,
  89. )
  90. async with engine.begin() as conn:
  91. await conn.run_sync(Base.metadata.create_all)
  92. # Run migrations for new columns (SQLite doesn't auto-add columns)
  93. await run_migrations(conn)
  94. # Seed default notification templates
  95. await seed_notification_templates()
  96. # Seed default groups and migrate existing users
  97. await seed_default_groups()
  98. # Seed default catalog entries
  99. await seed_spool_catalog()
  100. await seed_color_catalog()
  101. async def run_migrations(conn):
  102. """Add new columns to existing tables if they don't exist."""
  103. from sqlalchemy import text
  104. # Migration: Add is_favorite column to print_archives
  105. try:
  106. await conn.execute(text("ALTER TABLE print_archives ADD COLUMN is_favorite BOOLEAN DEFAULT 0"))
  107. except OperationalError:
  108. # Column already exists
  109. pass
  110. # Migration: Add content_hash column to print_archives for duplicate detection
  111. try:
  112. await conn.execute(text("ALTER TABLE print_archives ADD COLUMN content_hash VARCHAR(64)"))
  113. except OperationalError:
  114. # Column already exists
  115. pass
  116. # Migration: Add auto_off_executed column to smart_plugs
  117. try:
  118. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN auto_off_executed BOOLEAN DEFAULT 0"))
  119. except OperationalError:
  120. # Column already exists
  121. pass
  122. # Migration: Add on_print_stopped column to notification_providers
  123. try:
  124. await conn.execute(text("ALTER TABLE notification_providers ADD COLUMN on_print_stopped BOOLEAN DEFAULT 1"))
  125. except OperationalError:
  126. # Column already exists
  127. pass
  128. # Migration: Add source_3mf_path column to print_archives
  129. try:
  130. await conn.execute(text("ALTER TABLE print_archives ADD COLUMN source_3mf_path VARCHAR(500)"))
  131. except OperationalError:
  132. # Column already exists
  133. pass
  134. # Migration: Add f3d_path column to print_archives for Fusion 360 design files
  135. try:
  136. await conn.execute(text("ALTER TABLE print_archives ADD COLUMN f3d_path VARCHAR(500)"))
  137. except OperationalError:
  138. # Column already exists
  139. pass
  140. # Migration: Add on_maintenance_due column to notification_providers
  141. try:
  142. await conn.execute(text("ALTER TABLE notification_providers ADD COLUMN on_maintenance_due BOOLEAN DEFAULT 0"))
  143. except OperationalError:
  144. # Column already exists
  145. pass
  146. # Migration: Add location column to printers for grouping
  147. try:
  148. await conn.execute(text("ALTER TABLE printers ADD COLUMN location VARCHAR(100)"))
  149. except OperationalError:
  150. # Column already exists
  151. pass
  152. # Migration: Add interval_type column to maintenance_types
  153. try:
  154. await conn.execute(text("ALTER TABLE maintenance_types ADD COLUMN interval_type VARCHAR(20) DEFAULT 'hours'"))
  155. except OperationalError:
  156. # Column already exists
  157. pass
  158. # Migration: Add is_deleted column to maintenance_types for soft-deletes
  159. try:
  160. await conn.execute(text("ALTER TABLE maintenance_types ADD COLUMN is_deleted BOOLEAN DEFAULT 0"))
  161. except OperationalError:
  162. # Column already exists
  163. pass
  164. # Migration: Add custom_interval_type column to printer_maintenance
  165. try:
  166. await conn.execute(text("ALTER TABLE printer_maintenance ADD COLUMN custom_interval_type VARCHAR(20)"))
  167. except OperationalError:
  168. # Column already exists
  169. pass
  170. # Migration: Add power alert columns to smart_plugs
  171. try:
  172. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN power_alert_enabled BOOLEAN DEFAULT 0"))
  173. except OperationalError:
  174. pass # Already applied
  175. try:
  176. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN power_alert_high REAL"))
  177. except OperationalError:
  178. pass # Already applied
  179. try:
  180. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN power_alert_low REAL"))
  181. except OperationalError:
  182. pass # Already applied
  183. try:
  184. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN power_alert_last_triggered DATETIME"))
  185. except OperationalError:
  186. pass # Already applied
  187. # Migration: Add schedule columns to smart_plugs
  188. try:
  189. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN schedule_enabled BOOLEAN DEFAULT 0"))
  190. except OperationalError:
  191. pass # Already applied
  192. try:
  193. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN schedule_on_time VARCHAR(5)"))
  194. except OperationalError:
  195. pass # Already applied
  196. try:
  197. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN schedule_off_time VARCHAR(5)"))
  198. except OperationalError:
  199. pass # Already applied
  200. # Migration: Add daily digest columns to notification_providers
  201. try:
  202. await conn.execute(text("ALTER TABLE notification_providers ADD COLUMN daily_digest_enabled BOOLEAN DEFAULT 0"))
  203. except OperationalError:
  204. pass # Already applied
  205. try:
  206. await conn.execute(text("ALTER TABLE notification_providers ADD COLUMN daily_digest_time VARCHAR(5)"))
  207. except OperationalError:
  208. pass # Already applied
  209. # Migration: Add project_id column to print_archives
  210. try:
  211. await conn.execute(
  212. text("ALTER TABLE print_archives ADD COLUMN project_id INTEGER REFERENCES projects(id) ON DELETE SET NULL")
  213. )
  214. except OperationalError:
  215. pass # Already applied
  216. # Migration: Add project_id column to print_queue
  217. try:
  218. await conn.execute(
  219. text("ALTER TABLE print_queue ADD COLUMN project_id INTEGER REFERENCES projects(id) ON DELETE SET NULL")
  220. )
  221. except OperationalError:
  222. pass # Already applied
  223. # Migration: Create FTS5 virtual table for archive full-text search
  224. try:
  225. await conn.execute(
  226. text("""
  227. CREATE VIRTUAL TABLE IF NOT EXISTS archive_fts USING fts5(
  228. print_name,
  229. filename,
  230. tags,
  231. notes,
  232. designer,
  233. filament_type,
  234. content='print_archives',
  235. content_rowid='id'
  236. )
  237. """)
  238. )
  239. except OperationalError:
  240. pass # Already applied
  241. # Migration: Create triggers to keep FTS index in sync
  242. try:
  243. await conn.execute(
  244. text("""
  245. CREATE TRIGGER IF NOT EXISTS archive_fts_insert AFTER INSERT ON print_archives BEGIN
  246. INSERT INTO archive_fts(rowid, print_name, filename, tags, notes, designer, filament_type)
  247. VALUES (new.id, new.print_name, new.filename, new.tags, new.notes, new.designer, new.filament_type);
  248. END
  249. """)
  250. )
  251. except OperationalError:
  252. pass # Already applied
  253. try:
  254. await conn.execute(
  255. text("""
  256. CREATE TRIGGER IF NOT EXISTS archive_fts_delete AFTER DELETE ON print_archives BEGIN
  257. INSERT INTO archive_fts(archive_fts, rowid, print_name, filename, tags, notes, designer, filament_type)
  258. VALUES ('delete', old.id, old.print_name, old.filename, old.tags, old.notes, old.designer, old.filament_type);
  259. END
  260. """)
  261. )
  262. except OperationalError:
  263. pass # Already applied
  264. try:
  265. await conn.execute(
  266. text("""
  267. CREATE TRIGGER IF NOT EXISTS archive_fts_update AFTER UPDATE ON print_archives BEGIN
  268. INSERT INTO archive_fts(archive_fts, rowid, print_name, filename, tags, notes, designer, filament_type)
  269. VALUES ('delete', old.id, old.print_name, old.filename, old.tags, old.notes, old.designer, old.filament_type);
  270. INSERT INTO archive_fts(rowid, print_name, filename, tags, notes, designer, filament_type)
  271. VALUES (new.id, new.print_name, new.filename, new.tags, new.notes, new.designer, new.filament_type);
  272. END
  273. """)
  274. )
  275. except OperationalError:
  276. pass # Already applied
  277. # Migration: Add auto_off_pending columns to smart_plugs (for restart recovery)
  278. try:
  279. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN auto_off_pending BOOLEAN DEFAULT 0"))
  280. except OperationalError:
  281. pass # Already applied
  282. try:
  283. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN auto_off_pending_since DATETIME"))
  284. except OperationalError:
  285. pass # Already applied
  286. # Migration: Add AMS alarm notification columns to notification_providers
  287. try:
  288. await conn.execute(text("ALTER TABLE notification_providers ADD COLUMN on_ams_humidity_high BOOLEAN DEFAULT 0"))
  289. except OperationalError:
  290. pass # Already applied
  291. try:
  292. await conn.execute(
  293. text("ALTER TABLE notification_providers ADD COLUMN on_ams_temperature_high BOOLEAN DEFAULT 0")
  294. )
  295. except OperationalError:
  296. pass # Already applied
  297. # Migration: Add AMS-HT alarm notification columns to notification_providers
  298. try:
  299. await conn.execute(
  300. text("ALTER TABLE notification_providers ADD COLUMN on_ams_ht_humidity_high BOOLEAN DEFAULT 0")
  301. )
  302. except OperationalError:
  303. pass # Already applied
  304. try:
  305. await conn.execute(
  306. text("ALTER TABLE notification_providers ADD COLUMN on_ams_ht_temperature_high BOOLEAN DEFAULT 0")
  307. )
  308. except OperationalError:
  309. pass # Already applied
  310. # Migration: Add plate not empty notification column to notification_providers
  311. try:
  312. await conn.execute(text("ALTER TABLE notification_providers ADD COLUMN on_plate_not_empty BOOLEAN DEFAULT 1"))
  313. except OperationalError:
  314. pass # Already applied
  315. # Migration: Add notes column to projects (Phase 2)
  316. try:
  317. await conn.execute(text("ALTER TABLE projects ADD COLUMN notes TEXT"))
  318. except OperationalError:
  319. pass # Already applied
  320. # Migration: Add attachments column to projects (Phase 3)
  321. try:
  322. await conn.execute(text("ALTER TABLE projects ADD COLUMN attachments JSON"))
  323. except OperationalError:
  324. pass # Already applied
  325. # Migration: Add tags column to projects (Phase 4)
  326. try:
  327. await conn.execute(text("ALTER TABLE projects ADD COLUMN tags TEXT"))
  328. except OperationalError:
  329. pass # Already applied
  330. # Migration: Add due_date column to projects (Phase 5)
  331. try:
  332. await conn.execute(text("ALTER TABLE projects ADD COLUMN due_date DATETIME"))
  333. except OperationalError:
  334. pass # Already applied
  335. # Migration: Add priority column to projects (Phase 5)
  336. try:
  337. await conn.execute(text("ALTER TABLE projects ADD COLUMN priority VARCHAR(20) DEFAULT 'normal'"))
  338. except OperationalError:
  339. pass # Already applied
  340. # Migration: Add budget column to projects (Phase 6)
  341. try:
  342. await conn.execute(text("ALTER TABLE projects ADD COLUMN budget REAL"))
  343. except OperationalError:
  344. pass # Already applied
  345. # Migration: Add is_template column to projects (Phase 8)
  346. try:
  347. await conn.execute(text("ALTER TABLE projects ADD COLUMN is_template BOOLEAN DEFAULT 0"))
  348. except OperationalError:
  349. pass # Already applied
  350. # Migration: Add template_source_id column to projects (Phase 8)
  351. try:
  352. await conn.execute(text("ALTER TABLE projects ADD COLUMN template_source_id INTEGER"))
  353. except OperationalError:
  354. pass # Already applied
  355. # Migration: Add parent_id column to projects (Phase 10)
  356. try:
  357. await conn.execute(
  358. text("ALTER TABLE projects ADD COLUMN parent_id INTEGER REFERENCES projects(id) ON DELETE SET NULL")
  359. )
  360. except OperationalError:
  361. pass # Already applied
  362. # Migration: Rename quantity_printed to quantity_acquired in project_bom_items
  363. try:
  364. await conn.execute(text("ALTER TABLE project_bom_items RENAME COLUMN quantity_printed TO quantity_acquired"))
  365. except OperationalError:
  366. pass # Already applied
  367. # Migration: Add unit_price column to project_bom_items
  368. try:
  369. await conn.execute(text("ALTER TABLE project_bom_items ADD COLUMN unit_price REAL"))
  370. except OperationalError:
  371. pass # Already applied
  372. # Migration: Add sourcing_url column to project_bom_items
  373. try:
  374. await conn.execute(text("ALTER TABLE project_bom_items ADD COLUMN sourcing_url VARCHAR(512)"))
  375. except OperationalError:
  376. pass # Already applied
  377. # Migration: Rename notes to remarks in project_bom_items
  378. try:
  379. await conn.execute(text("ALTER TABLE project_bom_items RENAME COLUMN notes TO remarks"))
  380. except OperationalError:
  381. pass # Already applied
  382. # Migration: Add show_in_switchbar column to smart_plugs
  383. try:
  384. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN show_in_switchbar BOOLEAN DEFAULT 0"))
  385. except OperationalError:
  386. pass # Already applied
  387. # Migration: Add runtime tracking columns to printers
  388. try:
  389. await conn.execute(text("ALTER TABLE printers ADD COLUMN runtime_seconds INTEGER DEFAULT 0"))
  390. except OperationalError:
  391. pass # Already applied
  392. try:
  393. await conn.execute(text("ALTER TABLE printers ADD COLUMN last_runtime_update DATETIME"))
  394. except OperationalError:
  395. pass # Already applied
  396. # Migration: Add quantity column to print_archives for tracking item count
  397. try:
  398. await conn.execute(text("ALTER TABLE print_archives ADD COLUMN quantity INTEGER DEFAULT 1"))
  399. except OperationalError:
  400. pass # Already applied
  401. # Migration: Add manual_start column to print_queue for staged prints
  402. try:
  403. await conn.execute(text("ALTER TABLE print_queue ADD COLUMN manual_start BOOLEAN DEFAULT 0"))
  404. except OperationalError:
  405. pass # Already applied
  406. # Migration: Add wiki_url column to maintenance_types for documentation links
  407. try:
  408. await conn.execute(text("ALTER TABLE maintenance_types ADD COLUMN wiki_url VARCHAR(500)"))
  409. except OperationalError:
  410. pass # Already applied
  411. # Migration: Add ams_mapping column to print_queue for storing filament slot assignments
  412. try:
  413. await conn.execute(text("ALTER TABLE print_queue ADD COLUMN ams_mapping TEXT"))
  414. except OperationalError:
  415. pass # Already applied
  416. # Migration: Add target_parts_count column to projects for tracking total parts needed
  417. try:
  418. await conn.execute(text("ALTER TABLE projects ADD COLUMN target_parts_count INTEGER"))
  419. except OperationalError:
  420. pass # Already applied
  421. # Migration: Make printer_id nullable in print_queue for unassigned queue items
  422. # SQLite doesn't support ALTER COLUMN, so we need to recreate the table
  423. try:
  424. # Check if printer_id is already nullable by trying to insert NULL
  425. # This is a safe check that won't affect existing data
  426. result = await conn.execute(text("SELECT sql FROM sqlite_master WHERE type='table' AND name='print_queue'"))
  427. row = result.fetchone()
  428. if row and "printer_id INTEGER NOT NULL" in (row[0] or ""):
  429. # Need to migrate - printer_id is currently NOT NULL
  430. await conn.execute(
  431. text("""
  432. CREATE TABLE print_queue_new (
  433. id INTEGER PRIMARY KEY,
  434. printer_id INTEGER REFERENCES printers(id) ON DELETE CASCADE,
  435. archive_id INTEGER NOT NULL REFERENCES print_archives(id) ON DELETE CASCADE,
  436. project_id INTEGER REFERENCES projects(id) ON DELETE SET NULL,
  437. position INTEGER DEFAULT 0,
  438. scheduled_time DATETIME,
  439. manual_start BOOLEAN DEFAULT 0,
  440. require_previous_success BOOLEAN DEFAULT 0,
  441. auto_off_after BOOLEAN DEFAULT 0,
  442. ams_mapping TEXT,
  443. status VARCHAR(20) DEFAULT 'pending',
  444. started_at DATETIME,
  445. completed_at DATETIME,
  446. error_message TEXT,
  447. created_at DATETIME DEFAULT CURRENT_TIMESTAMP
  448. )
  449. """)
  450. )
  451. await conn.execute(
  452. text("""
  453. INSERT INTO print_queue_new
  454. SELECT id, printer_id, archive_id, project_id, position, scheduled_time,
  455. manual_start, require_previous_success, auto_off_after, ams_mapping,
  456. status, started_at, completed_at, error_message, created_at
  457. FROM print_queue
  458. """)
  459. )
  460. await conn.execute(text("DROP TABLE print_queue"))
  461. await conn.execute(text("ALTER TABLE print_queue_new RENAME TO print_queue"))
  462. except OperationalError:
  463. pass # Already applied
  464. # Migration: Add plug_type column to smart_plugs for HA integration
  465. try:
  466. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN plug_type VARCHAR(20) DEFAULT 'tasmota'"))
  467. except OperationalError:
  468. pass # Already applied
  469. # Migration: Add ha_entity_id column to smart_plugs for HA integration
  470. try:
  471. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN ha_entity_id VARCHAR(100)"))
  472. except OperationalError:
  473. pass # Already applied
  474. # Migration: Add project_id column to library_folders for linking folders to projects
  475. try:
  476. await conn.execute(
  477. text("ALTER TABLE library_folders ADD COLUMN project_id INTEGER REFERENCES projects(id) ON DELETE SET NULL")
  478. )
  479. except OperationalError:
  480. pass # Already applied
  481. # Migration: Add archive_id column to library_folders for linking folders to archives
  482. try:
  483. await conn.execute(
  484. text(
  485. "ALTER TABLE library_folders ADD COLUMN archive_id INTEGER REFERENCES print_archives(id) ON DELETE SET NULL"
  486. )
  487. )
  488. except OperationalError:
  489. pass # Already applied
  490. # Migration: Make ip_address nullable for HA plugs (SQLite requires table recreation)
  491. try:
  492. # Check if ip_address is currently NOT NULL
  493. result = await conn.execute(text("SELECT sql FROM sqlite_master WHERE type='table' AND name='smart_plugs'"))
  494. row = result.fetchone()
  495. if row and "ip_address VARCHAR(45) NOT NULL" in (row[0] or ""):
  496. # Need to migrate - ip_address is currently NOT NULL
  497. await conn.execute(
  498. text("""
  499. CREATE TABLE smart_plugs_new (
  500. id INTEGER PRIMARY KEY,
  501. name VARCHAR(100) NOT NULL,
  502. ip_address VARCHAR(45),
  503. plug_type VARCHAR(20) DEFAULT 'tasmota',
  504. ha_entity_id VARCHAR(100),
  505. printer_id INTEGER UNIQUE REFERENCES printers(id) ON DELETE SET NULL,
  506. enabled BOOLEAN NOT NULL DEFAULT 1,
  507. auto_on BOOLEAN NOT NULL DEFAULT 1,
  508. auto_off BOOLEAN NOT NULL DEFAULT 1,
  509. off_delay_mode VARCHAR(20) NOT NULL DEFAULT 'time',
  510. off_delay_minutes INTEGER NOT NULL DEFAULT 5,
  511. off_temp_threshold INTEGER NOT NULL DEFAULT 70,
  512. username VARCHAR(50),
  513. password VARCHAR(100),
  514. power_alert_enabled BOOLEAN NOT NULL DEFAULT 0,
  515. power_alert_high FLOAT,
  516. power_alert_low FLOAT,
  517. power_alert_last_triggered DATETIME,
  518. schedule_enabled BOOLEAN NOT NULL DEFAULT 0,
  519. schedule_on_time VARCHAR(5),
  520. schedule_off_time VARCHAR(5),
  521. show_in_switchbar BOOLEAN DEFAULT 0,
  522. last_state VARCHAR(10),
  523. last_checked DATETIME,
  524. auto_off_executed BOOLEAN NOT NULL DEFAULT 0,
  525. auto_off_pending BOOLEAN DEFAULT 0,
  526. auto_off_pending_since DATETIME,
  527. created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
  528. updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
  529. )
  530. """)
  531. )
  532. await conn.execute(
  533. text("""
  534. INSERT INTO smart_plugs_new
  535. SELECT id, name, ip_address,
  536. COALESCE(plug_type, 'tasmota'), ha_entity_id, printer_id,
  537. enabled, auto_on, auto_off, off_delay_mode, off_delay_minutes, off_temp_threshold,
  538. username, password, power_alert_enabled, power_alert_high, power_alert_low,
  539. power_alert_last_triggered, schedule_enabled, schedule_on_time, schedule_off_time,
  540. COALESCE(show_in_switchbar, 0), last_state, last_checked, auto_off_executed,
  541. COALESCE(auto_off_pending, 0), auto_off_pending_since, created_at, updated_at
  542. FROM smart_plugs
  543. """)
  544. )
  545. await conn.execute(text("DROP TABLE smart_plugs"))
  546. await conn.execute(text("ALTER TABLE smart_plugs_new RENAME TO smart_plugs"))
  547. except OperationalError:
  548. pass # Already applied
  549. # Migration: Add plate_id column to print_queue for multi-plate 3MF support
  550. try:
  551. await conn.execute(text("ALTER TABLE print_queue ADD COLUMN plate_id INTEGER"))
  552. except OperationalError:
  553. pass # Already applied
  554. # Migration: Add print options columns to print_queue
  555. try:
  556. await conn.execute(text("ALTER TABLE print_queue ADD COLUMN bed_levelling BOOLEAN DEFAULT 1"))
  557. except OperationalError:
  558. pass # Already applied
  559. try:
  560. await conn.execute(text("ALTER TABLE print_queue ADD COLUMN flow_cali BOOLEAN DEFAULT 0"))
  561. except OperationalError:
  562. pass # Already applied
  563. try:
  564. await conn.execute(text("ALTER TABLE print_queue ADD COLUMN vibration_cali BOOLEAN DEFAULT 1"))
  565. except OperationalError:
  566. pass # Already applied
  567. try:
  568. await conn.execute(text("ALTER TABLE print_queue ADD COLUMN layer_inspect BOOLEAN DEFAULT 0"))
  569. except OperationalError:
  570. pass # Already applied
  571. try:
  572. await conn.execute(text("ALTER TABLE print_queue ADD COLUMN timelapse BOOLEAN DEFAULT 0"))
  573. except OperationalError:
  574. pass # Already applied
  575. try:
  576. await conn.execute(text("ALTER TABLE print_queue ADD COLUMN use_ams BOOLEAN DEFAULT 1"))
  577. except OperationalError:
  578. pass # Already applied
  579. # Migration: Add library_file_id column to print_queue and make archive_id nullable
  580. # This allows queue items to reference library files directly (archive created at print start)
  581. try:
  582. await conn.execute(
  583. text(
  584. "ALTER TABLE print_queue ADD COLUMN library_file_id INTEGER REFERENCES library_files(id) ON DELETE CASCADE"
  585. )
  586. )
  587. except OperationalError:
  588. pass # Already applied
  589. # Check if archive_id needs to be made nullable (requires table recreation in SQLite)
  590. try:
  591. result = await conn.execute(text("SELECT sql FROM sqlite_master WHERE type='table' AND name='print_queue'"))
  592. row = result.fetchone()
  593. if row and "archive_id INTEGER NOT NULL" in (row[0] or ""):
  594. # Need to migrate - archive_id is currently NOT NULL
  595. await conn.execute(
  596. text("""
  597. CREATE TABLE print_queue_new2 (
  598. id INTEGER PRIMARY KEY,
  599. printer_id INTEGER REFERENCES printers(id) ON DELETE CASCADE,
  600. archive_id INTEGER REFERENCES print_archives(id) ON DELETE CASCADE,
  601. library_file_id INTEGER REFERENCES library_files(id) ON DELETE CASCADE,
  602. project_id INTEGER REFERENCES projects(id) ON DELETE SET NULL,
  603. position INTEGER DEFAULT 0,
  604. scheduled_time DATETIME,
  605. manual_start BOOLEAN DEFAULT 0,
  606. require_previous_success BOOLEAN DEFAULT 0,
  607. auto_off_after BOOLEAN DEFAULT 0,
  608. ams_mapping TEXT,
  609. plate_id INTEGER,
  610. bed_levelling BOOLEAN DEFAULT 1,
  611. flow_cali BOOLEAN DEFAULT 0,
  612. vibration_cali BOOLEAN DEFAULT 1,
  613. layer_inspect BOOLEAN DEFAULT 0,
  614. timelapse BOOLEAN DEFAULT 0,
  615. use_ams BOOLEAN DEFAULT 1,
  616. status VARCHAR(20) DEFAULT 'pending',
  617. started_at DATETIME,
  618. completed_at DATETIME,
  619. error_message TEXT,
  620. created_at DATETIME DEFAULT CURRENT_TIMESTAMP
  621. )
  622. """)
  623. )
  624. await conn.execute(
  625. text("""
  626. INSERT INTO print_queue_new2
  627. SELECT id, printer_id, archive_id, NULL, project_id, position, scheduled_time,
  628. manual_start, require_previous_success, auto_off_after, ams_mapping, plate_id,
  629. COALESCE(bed_levelling, 1), COALESCE(flow_cali, 0), COALESCE(vibration_cali, 1),
  630. COALESCE(layer_inspect, 0), COALESCE(timelapse, 0), COALESCE(use_ams, 1),
  631. status, started_at, completed_at, error_message, created_at
  632. FROM print_queue
  633. """)
  634. )
  635. await conn.execute(text("DROP TABLE print_queue"))
  636. await conn.execute(text("ALTER TABLE print_queue_new2 RENAME TO print_queue"))
  637. except OperationalError:
  638. pass # Already applied
  639. # Migration: Add HA energy sensor entity columns to smart_plugs
  640. try:
  641. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN ha_power_entity VARCHAR(100)"))
  642. except OperationalError:
  643. pass # Already applied
  644. try:
  645. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN ha_energy_today_entity VARCHAR(100)"))
  646. except OperationalError:
  647. pass # Already applied
  648. try:
  649. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN ha_energy_total_entity VARCHAR(100)"))
  650. except OperationalError:
  651. pass # Already applied
  652. # Migration: Create users table for authentication
  653. try:
  654. await conn.execute(
  655. text("""
  656. CREATE TABLE IF NOT EXISTS users (
  657. id INTEGER PRIMARY KEY,
  658. username VARCHAR(100) NOT NULL UNIQUE,
  659. password_hash VARCHAR(255) NOT NULL,
  660. role VARCHAR(20) NOT NULL DEFAULT 'user',
  661. is_active BOOLEAN NOT NULL DEFAULT 1,
  662. created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  663. updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
  664. )
  665. """)
  666. )
  667. await conn.execute(text("CREATE INDEX IF NOT EXISTS ix_users_username ON users(username)"))
  668. except OperationalError:
  669. pass # Already applied
  670. # Migration: Add external camera columns to printers
  671. try:
  672. await conn.execute(text("ALTER TABLE printers ADD COLUMN external_camera_url VARCHAR(500)"))
  673. except OperationalError:
  674. pass # Already applied
  675. try:
  676. await conn.execute(text("ALTER TABLE printers ADD COLUMN external_camera_type VARCHAR(20)"))
  677. except OperationalError:
  678. pass # Already applied
  679. try:
  680. await conn.execute(text("ALTER TABLE printers ADD COLUMN external_camera_enabled BOOLEAN DEFAULT 0"))
  681. except OperationalError:
  682. pass # Already applied
  683. # Migration: Add external_url column to print_archives for user-defined links (Printables, etc.)
  684. try:
  685. await conn.execute(text("ALTER TABLE print_archives ADD COLUMN external_url VARCHAR(500)"))
  686. except OperationalError:
  687. pass # Already applied
  688. # Migration: Add sliced_for_model column to print_archives for model-based queue assignment
  689. try:
  690. await conn.execute(text("ALTER TABLE print_archives ADD COLUMN sliced_for_model VARCHAR(50)"))
  691. except OperationalError:
  692. pass # Already applied
  693. # Migration: Add is_external column to library_files for external cloud files
  694. try:
  695. await conn.execute(text("ALTER TABLE library_files ADD COLUMN is_external BOOLEAN DEFAULT 0"))
  696. except OperationalError:
  697. pass # Already applied
  698. # Migration: Add project_id column to library_files
  699. try:
  700. await conn.execute(
  701. text("ALTER TABLE library_files ADD COLUMN project_id INTEGER REFERENCES projects(id) ON DELETE SET NULL")
  702. )
  703. except OperationalError:
  704. pass # Already applied
  705. # Migration: Add is_external column to library_folders for external cloud folders
  706. try:
  707. await conn.execute(text("ALTER TABLE library_folders ADD COLUMN is_external BOOLEAN DEFAULT 0"))
  708. except OperationalError:
  709. pass # Already applied
  710. # Migration: Add external folder settings columns to library_folders
  711. try:
  712. await conn.execute(text("ALTER TABLE library_folders ADD COLUMN external_readonly BOOLEAN DEFAULT 0"))
  713. except OperationalError:
  714. pass # Already applied
  715. try:
  716. await conn.execute(text("ALTER TABLE library_folders ADD COLUMN external_show_hidden BOOLEAN DEFAULT 0"))
  717. except OperationalError:
  718. pass # Already applied
  719. try:
  720. await conn.execute(text("ALTER TABLE library_folders ADD COLUMN external_path VARCHAR(500)"))
  721. except OperationalError:
  722. pass # Already applied
  723. # Migration: Add plate_detection_enabled column to printers
  724. try:
  725. await conn.execute(text("ALTER TABLE printers ADD COLUMN plate_detection_enabled BOOLEAN DEFAULT 0"))
  726. except OperationalError:
  727. pass # Already applied
  728. # Migration: Add plate detection ROI columns to printers
  729. try:
  730. await conn.execute(text("ALTER TABLE printers ADD COLUMN plate_detection_roi_x REAL"))
  731. except OperationalError:
  732. pass # Already applied
  733. try:
  734. await conn.execute(text("ALTER TABLE printers ADD COLUMN plate_detection_roi_y REAL"))
  735. except OperationalError:
  736. pass # Already applied
  737. try:
  738. await conn.execute(text("ALTER TABLE printers ADD COLUMN plate_detection_roi_w REAL"))
  739. except OperationalError:
  740. pass # Already applied
  741. try:
  742. await conn.execute(text("ALTER TABLE printers ADD COLUMN plate_detection_roi_h REAL"))
  743. except OperationalError:
  744. pass # Already applied
  745. # Migration: Remove UNIQUE constraint from smart_plugs.printer_id
  746. # This allows HA scripts to coexist with regular plugs (scripts are for multi-device control)
  747. # SQLite requires table recreation to drop constraints
  748. try:
  749. # Check if we need to migrate (if UNIQUE constraint exists)
  750. result = await conn.execute(text("SELECT sql FROM sqlite_master WHERE type='table' AND name='smart_plugs'"))
  751. row = result.fetchone()
  752. if row and "printer_id INTEGER UNIQUE" in (row[0] or ""):
  753. # Create new table without UNIQUE constraint on printer_id
  754. await conn.execute(
  755. text("""
  756. CREATE TABLE smart_plugs_temp (
  757. id INTEGER PRIMARY KEY,
  758. name VARCHAR(100) NOT NULL,
  759. ip_address VARCHAR(45),
  760. plug_type VARCHAR(20) DEFAULT 'tasmota',
  761. ha_entity_id VARCHAR(100),
  762. ha_power_entity VARCHAR(100),
  763. ha_energy_today_entity VARCHAR(100),
  764. ha_energy_total_entity VARCHAR(100),
  765. printer_id INTEGER REFERENCES printers(id) ON DELETE SET NULL,
  766. enabled BOOLEAN NOT NULL DEFAULT 1,
  767. auto_on BOOLEAN NOT NULL DEFAULT 1,
  768. auto_off BOOLEAN NOT NULL DEFAULT 1,
  769. off_delay_mode VARCHAR(20) NOT NULL DEFAULT 'time',
  770. off_delay_minutes INTEGER NOT NULL DEFAULT 5,
  771. off_temp_threshold INTEGER NOT NULL DEFAULT 70,
  772. username VARCHAR(50),
  773. password VARCHAR(100),
  774. power_alert_enabled BOOLEAN NOT NULL DEFAULT 0,
  775. power_alert_high FLOAT,
  776. power_alert_low FLOAT,
  777. power_alert_last_triggered DATETIME,
  778. schedule_enabled BOOLEAN NOT NULL DEFAULT 0,
  779. schedule_on_time VARCHAR(5),
  780. schedule_off_time VARCHAR(5),
  781. show_in_switchbar BOOLEAN DEFAULT 0,
  782. last_state VARCHAR(10),
  783. last_checked DATETIME,
  784. auto_off_executed BOOLEAN NOT NULL DEFAULT 0,
  785. auto_off_pending BOOLEAN DEFAULT 0,
  786. auto_off_pending_since DATETIME,
  787. created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
  788. updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
  789. )
  790. """)
  791. )
  792. # Copy data
  793. await conn.execute(
  794. text("""
  795. INSERT INTO smart_plugs_temp
  796. SELECT id, name, ip_address, plug_type, ha_entity_id, ha_power_entity,
  797. ha_energy_today_entity, ha_energy_total_entity, printer_id, enabled,
  798. auto_on, auto_off, off_delay_mode, off_delay_minutes, off_temp_threshold,
  799. username, password, power_alert_enabled, power_alert_high, power_alert_low,
  800. power_alert_last_triggered, schedule_enabled, schedule_on_time, schedule_off_time,
  801. show_in_switchbar, last_state, last_checked, auto_off_executed,
  802. auto_off_pending, auto_off_pending_since, created_at, updated_at
  803. FROM smart_plugs
  804. """)
  805. )
  806. # Drop old table and rename new one
  807. await conn.execute(text("DROP TABLE smart_plugs"))
  808. await conn.execute(text("ALTER TABLE smart_plugs_temp RENAME TO smart_plugs"))
  809. except OperationalError:
  810. pass # Already applied
  811. # Migration: Add show_on_printer_card column to smart_plugs
  812. try:
  813. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN show_on_printer_card BOOLEAN DEFAULT 1"))
  814. except OperationalError:
  815. pass # Already applied
  816. # Migration: Add MQTT smart plug fields (legacy)
  817. try:
  818. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN mqtt_topic VARCHAR(200)"))
  819. except OperationalError:
  820. pass # Already applied
  821. try:
  822. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN mqtt_power_path VARCHAR(100)"))
  823. except OperationalError:
  824. pass # Already applied
  825. try:
  826. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN mqtt_energy_path VARCHAR(100)"))
  827. except OperationalError:
  828. pass # Already applied
  829. try:
  830. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN mqtt_state_path VARCHAR(100)"))
  831. except OperationalError:
  832. pass # Already applied
  833. try:
  834. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN mqtt_multiplier REAL DEFAULT 1.0"))
  835. except OperationalError:
  836. pass # Already applied
  837. # Migration: Add enhanced MQTT smart plug fields (separate topics and multipliers)
  838. try:
  839. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN mqtt_power_topic VARCHAR(200)"))
  840. except OperationalError:
  841. pass # Already applied
  842. try:
  843. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN mqtt_power_multiplier REAL DEFAULT 1.0"))
  844. except OperationalError:
  845. pass # Already applied
  846. try:
  847. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN mqtt_energy_topic VARCHAR(200)"))
  848. except OperationalError:
  849. pass # Already applied
  850. try:
  851. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN mqtt_energy_multiplier REAL DEFAULT 1.0"))
  852. except OperationalError:
  853. pass # Already applied
  854. try:
  855. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN mqtt_state_topic VARCHAR(200)"))
  856. except OperationalError:
  857. pass # Already applied
  858. try:
  859. await conn.execute(text("ALTER TABLE smart_plugs ADD COLUMN mqtt_state_on_value VARCHAR(50)"))
  860. except OperationalError:
  861. pass # Already applied
  862. # Migration: Copy existing mqtt_topic to mqtt_power_topic for backward compatibility
  863. try:
  864. await conn.execute(
  865. text("""
  866. UPDATE smart_plugs
  867. SET mqtt_power_topic = mqtt_topic,
  868. mqtt_power_multiplier = mqtt_multiplier
  869. WHERE mqtt_topic IS NOT NULL AND mqtt_power_topic IS NULL
  870. """)
  871. )
  872. except OperationalError:
  873. pass # Already applied
  874. # Migration: Create groups table for permission-based access control
  875. try:
  876. await conn.execute(
  877. text("""
  878. CREATE TABLE IF NOT EXISTS groups (
  879. id INTEGER PRIMARY KEY,
  880. name VARCHAR(100) NOT NULL UNIQUE,
  881. description VARCHAR(500),
  882. permissions JSON,
  883. is_system BOOLEAN NOT NULL DEFAULT 0,
  884. created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  885. updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
  886. )
  887. """)
  888. )
  889. await conn.execute(text("CREATE INDEX IF NOT EXISTS ix_groups_name ON groups(name)"))
  890. except OperationalError:
  891. pass # Already applied
  892. # Migration: Create user_groups association table
  893. try:
  894. await conn.execute(
  895. text("""
  896. CREATE TABLE IF NOT EXISTS user_groups (
  897. user_id INTEGER NOT NULL,
  898. group_id INTEGER NOT NULL,
  899. PRIMARY KEY (user_id, group_id),
  900. FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
  901. FOREIGN KEY (group_id) REFERENCES groups(id) ON DELETE CASCADE
  902. )
  903. """)
  904. )
  905. except OperationalError:
  906. pass # Already applied
  907. # Migration: Add model-based queue assignment columns to print_queue
  908. try:
  909. await conn.execute(text("ALTER TABLE print_queue ADD COLUMN target_model VARCHAR(50)"))
  910. except OperationalError:
  911. pass # Already applied
  912. try:
  913. await conn.execute(text("ALTER TABLE print_queue ADD COLUMN required_filament_types TEXT"))
  914. except OperationalError:
  915. pass # Already applied
  916. try:
  917. await conn.execute(text("ALTER TABLE print_queue ADD COLUMN waiting_reason TEXT"))
  918. except OperationalError:
  919. pass # Already applied
  920. # Migration: Add nozzle_count column to printers (for dual-extruder detection)
  921. try:
  922. await conn.execute(text("ALTER TABLE printers ADD COLUMN nozzle_count INTEGER DEFAULT 1"))
  923. except OperationalError:
  924. pass # Already applied
  925. # Migration: Add print_hours_offset column to printers (baseline hours adjustment)
  926. try:
  927. await conn.execute(text("ALTER TABLE printers ADD COLUMN print_hours_offset REAL DEFAULT 0.0"))
  928. except OperationalError:
  929. pass # Already applied
  930. # Migration: Add queue notification event columns to notification_providers
  931. try:
  932. await conn.execute(text("ALTER TABLE notification_providers ADD COLUMN on_queue_job_added BOOLEAN DEFAULT 0"))
  933. except OperationalError:
  934. pass # Already applied
  935. try:
  936. await conn.execute(
  937. text("ALTER TABLE notification_providers ADD COLUMN on_queue_job_assigned BOOLEAN DEFAULT 0")
  938. )
  939. except OperationalError:
  940. pass # Already applied
  941. try:
  942. await conn.execute(text("ALTER TABLE notification_providers ADD COLUMN on_queue_job_started BOOLEAN DEFAULT 0"))
  943. except OperationalError:
  944. pass # Already applied
  945. try:
  946. await conn.execute(text("ALTER TABLE notification_providers ADD COLUMN on_queue_job_waiting BOOLEAN DEFAULT 1"))
  947. except OperationalError:
  948. pass # Already applied
  949. try:
  950. await conn.execute(text("ALTER TABLE notification_providers ADD COLUMN on_queue_job_skipped BOOLEAN DEFAULT 1"))
  951. except OperationalError:
  952. pass # Already applied
  953. try:
  954. await conn.execute(text("ALTER TABLE notification_providers ADD COLUMN on_queue_job_failed BOOLEAN DEFAULT 1"))
  955. except OperationalError:
  956. pass # Already applied
  957. try:
  958. await conn.execute(text("ALTER TABLE notification_providers ADD COLUMN on_queue_completed BOOLEAN DEFAULT 0"))
  959. except OperationalError:
  960. pass # Already applied
  961. # Migration: Add created_by_id column to print_archives for user tracking (Issue #206)
  962. try:
  963. await conn.execute(
  964. text("ALTER TABLE print_archives ADD COLUMN created_by_id INTEGER REFERENCES users(id) ON DELETE SET NULL")
  965. )
  966. except OperationalError:
  967. pass # Already applied
  968. # Migration: Add created_by_id column to print_queue for user tracking (Issue #206)
  969. try:
  970. await conn.execute(
  971. text("ALTER TABLE print_queue ADD COLUMN created_by_id INTEGER REFERENCES users(id) ON DELETE SET NULL")
  972. )
  973. except OperationalError:
  974. pass # Already applied
  975. # Migration: Add created_by_id column to library_files for user tracking (Issue #206)
  976. try:
  977. await conn.execute(
  978. text("ALTER TABLE library_files ADD COLUMN created_by_id INTEGER REFERENCES users(id) ON DELETE SET NULL")
  979. )
  980. except OperationalError:
  981. pass # Already applied
  982. # Migration: Add target_location column to print_queue for location-based filtering (Issue #220)
  983. try:
  984. await conn.execute(text("ALTER TABLE print_queue ADD COLUMN target_location VARCHAR(100)"))
  985. except OperationalError:
  986. pass # Already applied
  987. # Migration: Convert absolute paths to relative paths in library_files table
  988. # This ensures backup/restore portability across different installations
  989. try:
  990. base_dir_str = str(settings.base_dir)
  991. # Ensure we have a trailing slash for clean replacement
  992. if not base_dir_str.endswith("/"):
  993. base_dir_str += "/"
  994. # Update file_path - remove base_dir prefix from absolute paths
  995. await conn.execute(
  996. text("""
  997. UPDATE library_files
  998. SET file_path = SUBSTR(file_path, LENGTH(:base_dir) + 1)
  999. WHERE file_path LIKE :pattern
  1000. """),
  1001. {"base_dir": base_dir_str, "pattern": base_dir_str + "%"},
  1002. )
  1003. # Update thumbnail_path - remove base_dir prefix from absolute paths
  1004. await conn.execute(
  1005. text("""
  1006. UPDATE library_files
  1007. SET thumbnail_path = SUBSTR(thumbnail_path, LENGTH(:base_dir) + 1)
  1008. WHERE thumbnail_path LIKE :pattern
  1009. """),
  1010. {"base_dir": base_dir_str, "pattern": base_dir_str + "%"},
  1011. )
  1012. except OperationalError:
  1013. pass # Already applied
  1014. # Create active_print_spoolman table for Spoolman per-filament tracking
  1015. try:
  1016. await conn.execute(
  1017. text("""
  1018. CREATE TABLE IF NOT EXISTS active_print_spoolman (
  1019. id INTEGER PRIMARY KEY AUTOINCREMENT,
  1020. printer_id INTEGER NOT NULL REFERENCES printers(id) ON DELETE CASCADE,
  1021. archive_id INTEGER NOT NULL REFERENCES print_archives(id) ON DELETE CASCADE,
  1022. filament_usage TEXT NOT NULL,
  1023. ams_trays TEXT NOT NULL,
  1024. slot_to_tray TEXT,
  1025. layer_usage TEXT,
  1026. filament_properties TEXT,
  1027. UNIQUE(printer_id, archive_id)
  1028. )
  1029. """)
  1030. )
  1031. except OperationalError:
  1032. pass # Already applied
  1033. # Migration: Add preset_source column to slot_preset_mappings for local preset support
  1034. try:
  1035. await conn.execute(
  1036. text("ALTER TABLE slot_preset_mappings ADD COLUMN preset_source VARCHAR(20) DEFAULT 'cloud'")
  1037. )
  1038. except OperationalError:
  1039. pass # Already applied
  1040. # Migration: Add email column to users for Advanced Auth (PR #322)
  1041. try:
  1042. await conn.execute(text("ALTER TABLE users ADD COLUMN email VARCHAR(255)"))
  1043. except OperationalError:
  1044. pass # Already applied
  1045. # Migration: Add inventory spool tracking columns
  1046. try:
  1047. await conn.execute(text("ALTER TABLE spool ADD COLUMN added_full BOOLEAN"))
  1048. except OperationalError:
  1049. pass # Already applied
  1050. try:
  1051. await conn.execute(text("ALTER TABLE spool ADD COLUMN last_used DATETIME"))
  1052. except OperationalError:
  1053. pass # Already applied
  1054. try:
  1055. await conn.execute(text("ALTER TABLE spool ADD COLUMN encode_time DATETIME"))
  1056. except OperationalError:
  1057. pass # Already applied
  1058. # Migration: Add RFID tag matching columns to spool
  1059. try:
  1060. await conn.execute(text("ALTER TABLE spool ADD COLUMN tag_uid VARCHAR(16)"))
  1061. except OperationalError:
  1062. pass # Already applied
  1063. try:
  1064. await conn.execute(text("ALTER TABLE spool ADD COLUMN tray_uuid VARCHAR(32)"))
  1065. except OperationalError:
  1066. pass # Already applied
  1067. try:
  1068. await conn.execute(text("ALTER TABLE spool ADD COLUMN data_origin VARCHAR(20)"))
  1069. except OperationalError:
  1070. pass # Already applied
  1071. try:
  1072. await conn.execute(text("ALTER TABLE spool ADD COLUMN tag_type VARCHAR(20)"))
  1073. except OperationalError:
  1074. pass # Already applied
  1075. # Migration: Add core_weight_catalog_id to track which catalog entry was used for empty spool weight
  1076. try:
  1077. await conn.execute(text("ALTER TABLE spool ADD COLUMN core_weight_catalog_id INTEGER"))
  1078. except OperationalError:
  1079. pass # Already applied
  1080. # Migration: Create spool_usage_history table for filament consumption tracking
  1081. try:
  1082. await conn.execute(
  1083. text("""
  1084. CREATE TABLE IF NOT EXISTS spool_usage_history (
  1085. id INTEGER PRIMARY KEY AUTOINCREMENT,
  1086. spool_id INTEGER NOT NULL REFERENCES spool(id) ON DELETE CASCADE,
  1087. printer_id INTEGER REFERENCES printers(id) ON DELETE SET NULL,
  1088. print_name VARCHAR(500),
  1089. weight_used REAL NOT NULL DEFAULT 0,
  1090. percent_used INTEGER NOT NULL DEFAULT 0,
  1091. status VARCHAR(20) NOT NULL DEFAULT 'completed',
  1092. created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
  1093. )
  1094. """)
  1095. )
  1096. except OperationalError:
  1097. pass # Already applied
  1098. # Migration: Add open_in_new_tab column to external_links
  1099. try:
  1100. await conn.execute(text("ALTER TABLE external_links ADD COLUMN open_in_new_tab BOOLEAN DEFAULT 0"))
  1101. except OperationalError:
  1102. pass # Already applied
  1103. # Migration: Add bed cooled notification column to notification_providers
  1104. try:
  1105. await conn.execute(text("ALTER TABLE notification_providers ADD COLUMN on_bed_cooled BOOLEAN DEFAULT 0"))
  1106. except OperationalError:
  1107. pass # Already applied
  1108. async def seed_notification_templates():
  1109. """Seed default notification templates if they don't exist."""
  1110. from sqlalchemy import select
  1111. from backend.app.models.notification_template import DEFAULT_TEMPLATES, NotificationTemplate
  1112. async with async_session() as session:
  1113. # Get existing template event types
  1114. result = await session.execute(select(NotificationTemplate.event_type))
  1115. existing_types = {row[0] for row in result.fetchall()}
  1116. if not existing_types:
  1117. # No templates exist - insert all defaults
  1118. for template_data in DEFAULT_TEMPLATES:
  1119. template = NotificationTemplate(
  1120. event_type=template_data["event_type"],
  1121. name=template_data["name"],
  1122. title_template=template_data["title_template"],
  1123. body_template=template_data["body_template"],
  1124. is_default=True,
  1125. )
  1126. session.add(template)
  1127. else:
  1128. # Templates exist - only add missing ones
  1129. for template_data in DEFAULT_TEMPLATES:
  1130. if template_data["event_type"] not in existing_types:
  1131. template = NotificationTemplate(
  1132. event_type=template_data["event_type"],
  1133. name=template_data["name"],
  1134. title_template=template_data["title_template"],
  1135. body_template=template_data["body_template"],
  1136. is_default=True,
  1137. )
  1138. session.add(template)
  1139. await session.commit()
  1140. async def seed_default_groups():
  1141. """Seed default groups and migrate existing users to appropriate groups.
  1142. Creates the default system groups (Administrators, Operators, Viewers) if they
  1143. don't exist, then migrates existing users:
  1144. - Users with role='admin' -> Administrators group
  1145. - Users with role='user' -> Operators group
  1146. Also migrates old permissions to new ownership-based permissions (Issue #205).
  1147. """
  1148. import logging
  1149. from sqlalchemy import select
  1150. from backend.app.core.permissions import DEFAULT_GROUPS
  1151. from backend.app.models.group import Group
  1152. from backend.app.models.user import User
  1153. logger = logging.getLogger(__name__)
  1154. # Map old permissions to new ones for migration
  1155. # Administrators get *_all permissions, Operators get *_own permissions
  1156. PERMISSION_MIGRATION_ALL = {
  1157. "queue:update": "queue:update_all",
  1158. "queue:delete": "queue:delete_all",
  1159. "archives:update": "archives:update_all",
  1160. "archives:delete": "archives:delete_all",
  1161. "archives:reprint": "archives:reprint_all",
  1162. "library:update": "library:update_all",
  1163. "library:delete": "library:delete_all",
  1164. }
  1165. PERMISSION_MIGRATION_OWN = {
  1166. "queue:update": "queue:update_own",
  1167. "queue:delete": "queue:delete_own",
  1168. "archives:update": "archives:update_own",
  1169. "archives:delete": "archives:delete_own",
  1170. "archives:reprint": "archives:reprint_own",
  1171. "library:update": "library:update_own",
  1172. "library:delete": "library:delete_own",
  1173. }
  1174. async with async_session() as session:
  1175. # Get existing groups
  1176. result = await session.execute(select(Group))
  1177. existing_groups = {group.name: group for group in result.scalars().all()}
  1178. # Create default groups if they don't exist
  1179. groups_created = []
  1180. for group_name, group_config in DEFAULT_GROUPS.items():
  1181. if group_name not in existing_groups:
  1182. group = Group(
  1183. name=group_name,
  1184. description=group_config["description"],
  1185. permissions=group_config["permissions"],
  1186. is_system=group_config["is_system"],
  1187. )
  1188. session.add(group)
  1189. groups_created.append(group_name)
  1190. logger.info("Created default group: %s", group_name)
  1191. else:
  1192. # Migrate existing group's permissions from old to new format
  1193. group = existing_groups[group_name]
  1194. if group.permissions:
  1195. updated = False
  1196. new_permissions = list(group.permissions)
  1197. # Determine which migration map to use based on group
  1198. migration_map = (
  1199. PERMISSION_MIGRATION_ALL if group_name == "Administrators" else PERMISSION_MIGRATION_OWN
  1200. )
  1201. for old_perm, new_perm in migration_map.items():
  1202. if old_perm in new_permissions:
  1203. new_permissions.remove(old_perm)
  1204. if new_perm not in new_permissions:
  1205. new_permissions.append(new_perm)
  1206. updated = True
  1207. logger.info(
  1208. "Migrated permission '%s' to '%s' in group '%s'", old_perm, new_perm, group_name
  1209. )
  1210. # For Administrators, also ensure they get *_all permissions if they have any new *_own
  1211. if group_name == "Administrators":
  1212. for _own_perm, all_perm in [
  1213. ("queue:update_own", "queue:update_all"),
  1214. ("queue:delete_own", "queue:delete_all"),
  1215. ("archives:update_own", "archives:update_all"),
  1216. ("archives:delete_own", "archives:delete_all"),
  1217. ("archives:reprint_own", "archives:reprint_all"),
  1218. ("library:update_own", "library:update_all"),
  1219. ("library:delete_own", "library:delete_all"),
  1220. ]:
  1221. # Add *_all if not present
  1222. if all_perm not in new_permissions:
  1223. new_permissions.append(all_perm)
  1224. updated = True
  1225. if updated:
  1226. group.permissions = new_permissions
  1227. await session.commit()
  1228. # Migrate new permissions: grant printers:clear_plate to all groups with printers:control
  1229. result = await session.execute(select(Group))
  1230. all_groups = result.scalars().all()
  1231. for group in all_groups:
  1232. if (
  1233. group.permissions
  1234. and "printers:control" in group.permissions
  1235. and "printers:clear_plate" not in group.permissions
  1236. ):
  1237. group.permissions = [*group.permissions, "printers:clear_plate"]
  1238. logger.info("Added printers:clear_plate to group '%s' (has printers:control)", group.name)
  1239. await session.commit()
  1240. # Migrate existing users to groups if they're not already in any group
  1241. if groups_created:
  1242. # Refresh to get newly created groups
  1243. admin_result = await session.execute(select(Group).where(Group.name == "Administrators"))
  1244. admin_group = admin_result.scalar_one_or_none()
  1245. operators_result = await session.execute(select(Group).where(Group.name == "Operators"))
  1246. operators_group = operators_result.scalar_one_or_none()
  1247. # Get all users
  1248. users_result = await session.execute(select(User))
  1249. users = users_result.scalars().all()
  1250. for user in users:
  1251. # Skip if user already has groups
  1252. if user.groups:
  1253. continue
  1254. if user.role == "admin" and admin_group:
  1255. user.groups.append(admin_group)
  1256. logger.info("Migrated admin user '%s' to Administrators group", user.username)
  1257. elif operators_group:
  1258. user.groups.append(operators_group)
  1259. logger.info("Migrated user '%s' to Operators group", user.username)
  1260. await session.commit()
  1261. async def seed_spool_catalog():
  1262. """Seed the spool catalog with default entries if empty."""
  1263. import logging
  1264. from sqlalchemy import func, select
  1265. from backend.app.core.catalog_defaults import DEFAULT_SPOOL_CATALOG
  1266. from backend.app.models.spool_catalog import SpoolCatalogEntry
  1267. logger = logging.getLogger(__name__)
  1268. async with async_session() as session:
  1269. result = await session.execute(select(func.count()).select_from(SpoolCatalogEntry))
  1270. count = result.scalar() or 0
  1271. if count > 0:
  1272. return # Already seeded
  1273. for name, weight in DEFAULT_SPOOL_CATALOG:
  1274. session.add(SpoolCatalogEntry(name=name, weight=weight, is_default=True))
  1275. await session.commit()
  1276. logger.info("Seeded %d default spool catalog entries", len(DEFAULT_SPOOL_CATALOG))
  1277. async def seed_color_catalog():
  1278. """Seed the color catalog with default entries if empty."""
  1279. import logging
  1280. from sqlalchemy import func, select
  1281. from backend.app.core.catalog_defaults import DEFAULT_COLOR_CATALOG
  1282. from backend.app.models.color_catalog import ColorCatalogEntry
  1283. logger = logging.getLogger(__name__)
  1284. async with async_session() as session:
  1285. result = await session.execute(select(func.count()).select_from(ColorCatalogEntry))
  1286. count = result.scalar() or 0
  1287. if count > 0:
  1288. return # Already seeded
  1289. for manufacturer, color_name, hex_color, material in DEFAULT_COLOR_CATALOG:
  1290. session.add(
  1291. ColorCatalogEntry(
  1292. manufacturer=manufacturer,
  1293. color_name=color_name,
  1294. hex_color=hex_color,
  1295. material=material,
  1296. is_default=True,
  1297. )
  1298. )
  1299. await session.commit()
  1300. logger.info("Seeded %d default color catalog entries", len(DEFAULT_COLOR_CATALOG))