database.py 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  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: Create spool_usage_history table for filament consumption tracking
  1076. try:
  1077. await conn.execute(
  1078. text("""
  1079. CREATE TABLE IF NOT EXISTS spool_usage_history (
  1080. id INTEGER PRIMARY KEY AUTOINCREMENT,
  1081. spool_id INTEGER NOT NULL REFERENCES spool(id) ON DELETE CASCADE,
  1082. printer_id INTEGER REFERENCES printers(id) ON DELETE SET NULL,
  1083. print_name VARCHAR(500),
  1084. weight_used REAL NOT NULL DEFAULT 0,
  1085. percent_used INTEGER NOT NULL DEFAULT 0,
  1086. status VARCHAR(20) NOT NULL DEFAULT 'completed',
  1087. created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
  1088. )
  1089. """)
  1090. )
  1091. except OperationalError:
  1092. pass # Already applied
  1093. # Migration: Add open_in_new_tab column to external_links
  1094. try:
  1095. await conn.execute(text("ALTER TABLE external_links ADD COLUMN open_in_new_tab BOOLEAN DEFAULT 0"))
  1096. except OperationalError:
  1097. pass # Already applied
  1098. # Migration: Add bed cooled notification column to notification_providers
  1099. try:
  1100. await conn.execute(text("ALTER TABLE notification_providers ADD COLUMN on_bed_cooled BOOLEAN DEFAULT 0"))
  1101. except OperationalError:
  1102. pass # Already applied
  1103. async def seed_notification_templates():
  1104. """Seed default notification templates if they don't exist."""
  1105. from sqlalchemy import select
  1106. from backend.app.models.notification_template import DEFAULT_TEMPLATES, NotificationTemplate
  1107. async with async_session() as session:
  1108. # Get existing template event types
  1109. result = await session.execute(select(NotificationTemplate.event_type))
  1110. existing_types = {row[0] for row in result.fetchall()}
  1111. if not existing_types:
  1112. # No templates exist - insert all defaults
  1113. for template_data in DEFAULT_TEMPLATES:
  1114. template = NotificationTemplate(
  1115. event_type=template_data["event_type"],
  1116. name=template_data["name"],
  1117. title_template=template_data["title_template"],
  1118. body_template=template_data["body_template"],
  1119. is_default=True,
  1120. )
  1121. session.add(template)
  1122. else:
  1123. # Templates exist - only add missing ones
  1124. for template_data in DEFAULT_TEMPLATES:
  1125. if template_data["event_type"] not in existing_types:
  1126. template = NotificationTemplate(
  1127. event_type=template_data["event_type"],
  1128. name=template_data["name"],
  1129. title_template=template_data["title_template"],
  1130. body_template=template_data["body_template"],
  1131. is_default=True,
  1132. )
  1133. session.add(template)
  1134. await session.commit()
  1135. async def seed_default_groups():
  1136. """Seed default groups and migrate existing users to appropriate groups.
  1137. Creates the default system groups (Administrators, Operators, Viewers) if they
  1138. don't exist, then migrates existing users:
  1139. - Users with role='admin' -> Administrators group
  1140. - Users with role='user' -> Operators group
  1141. Also migrates old permissions to new ownership-based permissions (Issue #205).
  1142. """
  1143. import logging
  1144. from sqlalchemy import select
  1145. from backend.app.core.permissions import DEFAULT_GROUPS
  1146. from backend.app.models.group import Group
  1147. from backend.app.models.user import User
  1148. logger = logging.getLogger(__name__)
  1149. # Map old permissions to new ones for migration
  1150. # Administrators get *_all permissions, Operators get *_own permissions
  1151. PERMISSION_MIGRATION_ALL = {
  1152. "queue:update": "queue:update_all",
  1153. "queue:delete": "queue:delete_all",
  1154. "archives:update": "archives:update_all",
  1155. "archives:delete": "archives:delete_all",
  1156. "archives:reprint": "archives:reprint_all",
  1157. "library:update": "library:update_all",
  1158. "library:delete": "library:delete_all",
  1159. }
  1160. PERMISSION_MIGRATION_OWN = {
  1161. "queue:update": "queue:update_own",
  1162. "queue:delete": "queue:delete_own",
  1163. "archives:update": "archives:update_own",
  1164. "archives:delete": "archives:delete_own",
  1165. "archives:reprint": "archives:reprint_own",
  1166. "library:update": "library:update_own",
  1167. "library:delete": "library:delete_own",
  1168. }
  1169. async with async_session() as session:
  1170. # Get existing groups
  1171. result = await session.execute(select(Group))
  1172. existing_groups = {group.name: group for group in result.scalars().all()}
  1173. # Create default groups if they don't exist
  1174. groups_created = []
  1175. for group_name, group_config in DEFAULT_GROUPS.items():
  1176. if group_name not in existing_groups:
  1177. group = Group(
  1178. name=group_name,
  1179. description=group_config["description"],
  1180. permissions=group_config["permissions"],
  1181. is_system=group_config["is_system"],
  1182. )
  1183. session.add(group)
  1184. groups_created.append(group_name)
  1185. logger.info("Created default group: %s", group_name)
  1186. else:
  1187. # Migrate existing group's permissions from old to new format
  1188. group = existing_groups[group_name]
  1189. if group.permissions:
  1190. updated = False
  1191. new_permissions = list(group.permissions)
  1192. # Determine which migration map to use based on group
  1193. migration_map = (
  1194. PERMISSION_MIGRATION_ALL if group_name == "Administrators" else PERMISSION_MIGRATION_OWN
  1195. )
  1196. for old_perm, new_perm in migration_map.items():
  1197. if old_perm in new_permissions:
  1198. new_permissions.remove(old_perm)
  1199. if new_perm not in new_permissions:
  1200. new_permissions.append(new_perm)
  1201. updated = True
  1202. logger.info(
  1203. "Migrated permission '%s' to '%s' in group '%s'", old_perm, new_perm, group_name
  1204. )
  1205. # For Administrators, also ensure they get *_all permissions if they have any new *_own
  1206. if group_name == "Administrators":
  1207. for _own_perm, all_perm in [
  1208. ("queue:update_own", "queue:update_all"),
  1209. ("queue:delete_own", "queue:delete_all"),
  1210. ("archives:update_own", "archives:update_all"),
  1211. ("archives:delete_own", "archives:delete_all"),
  1212. ("archives:reprint_own", "archives:reprint_all"),
  1213. ("library:update_own", "library:update_all"),
  1214. ("library:delete_own", "library:delete_all"),
  1215. ]:
  1216. # Add *_all if not present
  1217. if all_perm not in new_permissions:
  1218. new_permissions.append(all_perm)
  1219. updated = True
  1220. if updated:
  1221. group.permissions = new_permissions
  1222. await session.commit()
  1223. # Migrate existing users to groups if they're not already in any group
  1224. if groups_created:
  1225. # Refresh to get newly created groups
  1226. admin_result = await session.execute(select(Group).where(Group.name == "Administrators"))
  1227. admin_group = admin_result.scalar_one_or_none()
  1228. operators_result = await session.execute(select(Group).where(Group.name == "Operators"))
  1229. operators_group = operators_result.scalar_one_or_none()
  1230. # Get all users
  1231. users_result = await session.execute(select(User))
  1232. users = users_result.scalars().all()
  1233. for user in users:
  1234. # Skip if user already has groups
  1235. if user.groups:
  1236. continue
  1237. if user.role == "admin" and admin_group:
  1238. user.groups.append(admin_group)
  1239. logger.info("Migrated admin user '%s' to Administrators group", user.username)
  1240. elif operators_group:
  1241. user.groups.append(operators_group)
  1242. logger.info("Migrated user '%s' to Operators group", user.username)
  1243. await session.commit()
  1244. async def seed_spool_catalog():
  1245. """Seed the spool catalog with default entries if empty."""
  1246. import logging
  1247. from sqlalchemy import func, select
  1248. from backend.app.core.catalog_defaults import DEFAULT_SPOOL_CATALOG
  1249. from backend.app.models.spool_catalog import SpoolCatalogEntry
  1250. logger = logging.getLogger(__name__)
  1251. async with async_session() as session:
  1252. result = await session.execute(select(func.count()).select_from(SpoolCatalogEntry))
  1253. count = result.scalar() or 0
  1254. if count > 0:
  1255. return # Already seeded
  1256. for name, weight in DEFAULT_SPOOL_CATALOG:
  1257. session.add(SpoolCatalogEntry(name=name, weight=weight, is_default=True))
  1258. await session.commit()
  1259. logger.info("Seeded %d default spool catalog entries", len(DEFAULT_SPOOL_CATALOG))
  1260. async def seed_color_catalog():
  1261. """Seed the color catalog with default entries if empty."""
  1262. import logging
  1263. from sqlalchemy import func, select
  1264. from backend.app.core.catalog_defaults import DEFAULT_COLOR_CATALOG
  1265. from backend.app.models.color_catalog import ColorCatalogEntry
  1266. logger = logging.getLogger(__name__)
  1267. async with async_session() as session:
  1268. result = await session.execute(select(func.count()).select_from(ColorCatalogEntry))
  1269. count = result.scalar() or 0
  1270. if count > 0:
  1271. return # Already seeded
  1272. for manufacturer, color_name, hex_color, material in DEFAULT_COLOR_CATALOG:
  1273. session.add(
  1274. ColorCatalogEntry(
  1275. manufacturer=manufacturer,
  1276. color_name=color_name,
  1277. hex_color=hex_color,
  1278. material=material,
  1279. is_default=True,
  1280. )
  1281. )
  1282. await session.commit()
  1283. logger.info("Seeded %d default color catalog entries", len(DEFAULT_COLOR_CATALOG))