database.py 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  1. import asyncio
  2. import logging
  3. from sqlalchemy import event
  4. from sqlalchemy.exc import IntegrityError, OperationalError, ProgrammingError
  5. from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
  6. from sqlalchemy.orm import DeclarativeBase
  7. from backend.app.core.config import settings
  8. from backend.app.core.db_dialect import is_sqlite
  9. logger = logging.getLogger(__name__)
  10. def _set_sqlite_pragmas(dbapi_conn, connection_record):
  11. """Set SQLite pragmas on each new connection for concurrency and performance."""
  12. cursor = dbapi_conn.cursor()
  13. # WAL mode allows concurrent readers + one writer (vs default DELETE mode which locks entirely)
  14. cursor.execute("PRAGMA journal_mode = WAL")
  15. # Wait up to 15 seconds when the database is locked instead of failing immediately
  16. cursor.execute("PRAGMA busy_timeout = 15000")
  17. cursor.execute("PRAGMA synchronous = NORMAL")
  18. cursor.close()
  19. def _create_engine():
  20. """Create the async engine with dialect-appropriate settings."""
  21. if is_sqlite():
  22. kwargs = {"pool_size": 20, "max_overflow": 200}
  23. else:
  24. kwargs = {"pool_size": 10, "max_overflow": 20}
  25. eng = create_async_engine(
  26. settings.database_url,
  27. echo=settings.debug,
  28. **kwargs,
  29. )
  30. if is_sqlite():
  31. event.listen(eng.sync_engine, "connect", _set_sqlite_pragmas)
  32. else:
  33. # Strip timezone info from aware datetimes before they reach asyncpg.
  34. # asyncpg rejects timezone-aware values for TIMESTAMP WITHOUT TIME ZONE columns.
  35. # The codebase uses datetime.now(timezone.utc) in many places — this makes
  36. # Postgres behave like SQLite which ignores timezone info entirely.
  37. @event.listens_for(eng.sync_engine, "before_cursor_execute", retval=True)
  38. def _strip_tz_from_params(conn, cursor, statement, parameters, context, executemany):
  39. import datetime
  40. if parameters is None:
  41. return statement, parameters
  42. def _strip(val):
  43. if isinstance(val, datetime.datetime) and val.tzinfo is not None:
  44. return val.replace(tzinfo=None)
  45. return val
  46. def _strip_container(params):
  47. if isinstance(params, dict):
  48. return {k: _strip(v) for k, v in params.items()}
  49. elif isinstance(params, tuple):
  50. return tuple(_strip(v) for v in params)
  51. return params
  52. if executemany and isinstance(parameters, (list, tuple)):
  53. # Batch: list of dicts or list of tuples
  54. parameters = [_strip_container(row) for row in parameters]
  55. else:
  56. parameters = _strip_container(parameters)
  57. return statement, parameters
  58. return eng
  59. engine = _create_engine()
  60. async_session = async_sessionmaker(
  61. engine,
  62. class_=AsyncSession,
  63. expire_on_commit=False,
  64. )
  65. async def run_with_retry(fn, *, max_attempts: int = 3, label: str = ""):
  66. """Run an async DB operation with retry for SQLite 'database is locked' errors.
  67. ``fn`` is an async callable that receives an ``AsyncSession`` and performs
  68. the full query-mutate-commit cycle. On each retry a fresh session is used
  69. so there are no stale-object / expired-attribute issues after rollback.
  70. On PostgreSQL this calls ``fn`` once with no retry (Postgres uses row-level
  71. locking and doesn't suffer from single-writer contention).
  72. """
  73. if not is_sqlite():
  74. async with async_session() as db:
  75. return await fn(db)
  76. last_exc: OperationalError | None = None
  77. for attempt in range(1, max_attempts + 1):
  78. try:
  79. async with async_session() as db:
  80. return await fn(db)
  81. except OperationalError as exc:
  82. last_exc = exc
  83. if "database is locked" not in str(exc) or attempt == max_attempts:
  84. raise
  85. delay = 0.5 * attempt # 0.5s, 1.0s
  86. logger.warning(
  87. "SQLite locked%s (attempt %d/%d), retrying in %.1fs: %s",
  88. f" ({label})" if label else "",
  89. attempt,
  90. max_attempts,
  91. delay,
  92. exc,
  93. )
  94. await asyncio.sleep(delay)
  95. raise last_exc # unreachable, but keeps type checkers happy
  96. async def close_all_connections():
  97. """Close all database connections for backup/restore operations."""
  98. global engine
  99. await engine.dispose()
  100. async def reinitialize_database():
  101. """Reinitialize database connection after restore."""
  102. global engine, async_session
  103. engine = _create_engine()
  104. async_session = async_sessionmaker(
  105. engine,
  106. class_=AsyncSession,
  107. expire_on_commit=False,
  108. )
  109. class Base(DeclarativeBase):
  110. pass
  111. async def get_db() -> AsyncSession:
  112. async with async_session() as session:
  113. try:
  114. yield session
  115. await session.commit()
  116. except Exception:
  117. await session.rollback()
  118. raise
  119. finally:
  120. await session.close()
  121. async def init_db():
  122. # Import models to register them with SQLAlchemy
  123. from backend.app.models import ( # noqa: F401
  124. active_print_spoolman,
  125. ams_history,
  126. ams_label,
  127. api_key,
  128. archive,
  129. bug_report,
  130. color_catalog,
  131. external_link,
  132. filament,
  133. github_backup,
  134. group,
  135. kprofile_note,
  136. library,
  137. local_preset,
  138. maintenance,
  139. notification,
  140. notification_template,
  141. orca_base_cache,
  142. pending_upload,
  143. print_batch,
  144. print_log,
  145. print_queue,
  146. printer,
  147. project,
  148. project_bom,
  149. settings,
  150. slot_preset,
  151. smart_plug,
  152. smart_plug_energy_snapshot,
  153. spool,
  154. spool_assignment,
  155. spool_catalog,
  156. spool_k_profile,
  157. spool_usage_history,
  158. spoolbuddy_device,
  159. user,
  160. user_email_pref,
  161. virtual_printer,
  162. )
  163. async with engine.begin() as conn:
  164. await conn.run_sync(Base.metadata.create_all)
  165. # Run migrations for new columns (SQLite doesn't auto-add columns)
  166. await run_migrations(conn)
  167. # Seed default notification templates
  168. await seed_notification_templates()
  169. # Seed default groups and migrate existing users
  170. await seed_default_groups()
  171. # Seed default catalog entries
  172. await seed_spool_catalog()
  173. await seed_color_catalog()
  174. async def _safe_execute(conn, sql):
  175. """Execute a migration statement, ignoring 'already exists' errors.
  176. Uses a savepoint so that a failed statement doesn't poison the
  177. surrounding transaction (required for PostgreSQL).
  178. """
  179. from sqlalchemy import text
  180. try:
  181. async with conn.begin_nested():
  182. await conn.execute(text(sql))
  183. except (OperationalError, ProgrammingError):
  184. pass
  185. async def run_migrations(conn):
  186. """Add new columns to existing tables if they don't exist."""
  187. from sqlalchemy import text
  188. # Migration: Add is_favorite column to print_archives
  189. await _safe_execute(conn, "ALTER TABLE print_archives ADD COLUMN is_favorite BOOLEAN DEFAULT 0")
  190. # Migration: Add content_hash column to print_archives for duplicate detection
  191. await _safe_execute(conn, "ALTER TABLE print_archives ADD COLUMN content_hash VARCHAR(64)")
  192. # Migration: Add auto_off_executed column to smart_plugs
  193. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN auto_off_executed BOOLEAN DEFAULT 0")
  194. # Migration: Add on_print_stopped column to notification_providers
  195. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN on_print_stopped BOOLEAN DEFAULT 1")
  196. # Migration: Add source_3mf_path column to print_archives
  197. await _safe_execute(conn, "ALTER TABLE print_archives ADD COLUMN source_3mf_path VARCHAR(500)")
  198. # Migration: Add f3d_path column to print_archives for Fusion 360 design files
  199. await _safe_execute(conn, "ALTER TABLE print_archives ADD COLUMN f3d_path VARCHAR(500)")
  200. # Migration: Add on_maintenance_due column to notification_providers
  201. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN on_maintenance_due BOOLEAN DEFAULT 0")
  202. # Migration: Add location column to printers for grouping
  203. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN location VARCHAR(100)")
  204. # Migration: Add interval_type column to maintenance_types
  205. await _safe_execute(conn, "ALTER TABLE maintenance_types ADD COLUMN interval_type VARCHAR(20) DEFAULT 'hours'")
  206. # Migration: Add is_deleted column to maintenance_types for soft-deletes
  207. await _safe_execute(conn, "ALTER TABLE maintenance_types ADD COLUMN is_deleted BOOLEAN DEFAULT 0")
  208. # Migration: Add custom_interval_type column to printer_maintenance
  209. await _safe_execute(conn, "ALTER TABLE printer_maintenance ADD COLUMN custom_interval_type VARCHAR(20)")
  210. # Migration: Add power alert columns to smart_plugs
  211. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN power_alert_enabled BOOLEAN DEFAULT 0")
  212. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN power_alert_high REAL")
  213. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN power_alert_low REAL")
  214. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN power_alert_last_triggered DATETIME")
  215. # Migration: Add schedule columns to smart_plugs
  216. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN schedule_enabled BOOLEAN DEFAULT 0")
  217. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN schedule_on_time VARCHAR(5)")
  218. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN schedule_off_time VARCHAR(5)")
  219. # Migration: Add daily digest columns to notification_providers
  220. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN daily_digest_enabled BOOLEAN DEFAULT 0")
  221. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN daily_digest_time VARCHAR(5)")
  222. # Migration: Add missing-spool-assignment print-start notification toggle
  223. try:
  224. async with conn.begin_nested():
  225. await conn.execute(
  226. text(
  227. "ALTER TABLE notification_providers ADD COLUMN on_print_missing_spool_assignment BOOLEAN DEFAULT 0"
  228. )
  229. )
  230. except (OperationalError, ProgrammingError):
  231. pass # Already applied
  232. # Migration: Add project_id column to print_archives
  233. try:
  234. async with conn.begin_nested():
  235. await conn.execute(
  236. text(
  237. "ALTER TABLE print_archives ADD COLUMN project_id INTEGER REFERENCES projects(id) ON DELETE SET NULL"
  238. )
  239. )
  240. except (OperationalError, ProgrammingError):
  241. pass # Already applied
  242. # Migration: Add project_id column to print_queue
  243. try:
  244. async with conn.begin_nested():
  245. await conn.execute(
  246. text("ALTER TABLE print_queue ADD COLUMN project_id INTEGER REFERENCES projects(id) ON DELETE SET NULL")
  247. )
  248. except (OperationalError, ProgrammingError):
  249. pass # Already applied
  250. # Migration: Create FTS5 virtual table for archive full-text search (SQLite only)
  251. # PostgreSQL uses tsvector + GIN index instead (set up in archives.py search route)
  252. if is_sqlite():
  253. try:
  254. await conn.execute(
  255. text("""
  256. CREATE VIRTUAL TABLE IF NOT EXISTS archive_fts USING fts5(
  257. print_name,
  258. filename,
  259. tags,
  260. notes,
  261. designer,
  262. filament_type,
  263. content='print_archives',
  264. content_rowid='id'
  265. )
  266. """)
  267. )
  268. except (OperationalError, ProgrammingError):
  269. pass # Already applied
  270. # Migration: Create triggers to keep FTS index in sync
  271. try:
  272. await conn.execute(
  273. text("""
  274. CREATE TRIGGER IF NOT EXISTS archive_fts_insert AFTER INSERT ON print_archives BEGIN
  275. INSERT INTO archive_fts(rowid, print_name, filename, tags, notes, designer, filament_type)
  276. VALUES (new.id, new.print_name, new.filename, new.tags, new.notes, new.designer, new.filament_type);
  277. END
  278. """)
  279. )
  280. except (OperationalError, ProgrammingError):
  281. pass # Already applied
  282. try:
  283. await conn.execute(
  284. text("""
  285. CREATE TRIGGER IF NOT EXISTS archive_fts_delete AFTER DELETE ON print_archives BEGIN
  286. INSERT INTO archive_fts(archive_fts, rowid, print_name, filename, tags, notes, designer, filament_type)
  287. VALUES ('delete', old.id, old.print_name, old.filename, old.tags, old.notes, old.designer, old.filament_type);
  288. END
  289. """)
  290. )
  291. except (OperationalError, ProgrammingError):
  292. pass # Already applied
  293. try:
  294. await conn.execute(
  295. text("""
  296. CREATE TRIGGER IF NOT EXISTS archive_fts_update AFTER UPDATE ON print_archives BEGIN
  297. INSERT INTO archive_fts(archive_fts, rowid, print_name, filename, tags, notes, designer, filament_type)
  298. VALUES ('delete', old.id, old.print_name, old.filename, old.tags, old.notes, old.designer, old.filament_type);
  299. INSERT INTO archive_fts(rowid, print_name, filename, tags, notes, designer, filament_type)
  300. VALUES (new.id, new.print_name, new.filename, new.tags, new.notes, new.designer, new.filament_type);
  301. END
  302. """)
  303. )
  304. except (OperationalError, ProgrammingError):
  305. pass # Already applied
  306. # Migration: Add auto_off_pending columns to smart_plugs (for restart recovery)
  307. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN auto_off_pending BOOLEAN DEFAULT 0")
  308. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN auto_off_pending_since DATETIME")
  309. # Migration: Add auto_off_persistent column to smart_plugs (keep auto-off enabled between prints)
  310. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN auto_off_persistent BOOLEAN DEFAULT 0")
  311. # Migration: Add AMS alarm notification columns to notification_providers
  312. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN on_ams_humidity_high BOOLEAN DEFAULT 0")
  313. try:
  314. async with conn.begin_nested():
  315. await conn.execute(
  316. text("ALTER TABLE notification_providers ADD COLUMN on_ams_temperature_high BOOLEAN DEFAULT 0")
  317. )
  318. except (OperationalError, ProgrammingError):
  319. pass # Already applied
  320. # Migration: Add AMS-HT alarm notification columns to notification_providers
  321. try:
  322. async with conn.begin_nested():
  323. await conn.execute(
  324. text("ALTER TABLE notification_providers ADD COLUMN on_ams_ht_humidity_high BOOLEAN DEFAULT 0")
  325. )
  326. except (OperationalError, ProgrammingError):
  327. pass # Already applied
  328. try:
  329. async with conn.begin_nested():
  330. await conn.execute(
  331. text("ALTER TABLE notification_providers ADD COLUMN on_ams_ht_temperature_high BOOLEAN DEFAULT 0")
  332. )
  333. except (OperationalError, ProgrammingError):
  334. pass # Already applied
  335. # Migration: Add plate not empty notification column to notification_providers
  336. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN on_plate_not_empty BOOLEAN DEFAULT 1")
  337. # Migration: Add notes column to projects (Phase 2)
  338. await _safe_execute(conn, "ALTER TABLE projects ADD COLUMN notes TEXT")
  339. # Migration: Add attachments column to projects (Phase 3)
  340. await _safe_execute(conn, "ALTER TABLE projects ADD COLUMN attachments JSON")
  341. # Migration: Add tags column to projects (Phase 4)
  342. await _safe_execute(conn, "ALTER TABLE projects ADD COLUMN tags TEXT")
  343. # Migration: Add due_date column to projects (Phase 5)
  344. await _safe_execute(conn, "ALTER TABLE projects ADD COLUMN due_date DATETIME")
  345. # Migration: Add priority column to projects (Phase 5)
  346. await _safe_execute(conn, "ALTER TABLE projects ADD COLUMN priority VARCHAR(20) DEFAULT 'normal'")
  347. # Migration: Add budget column to projects (Phase 6)
  348. await _safe_execute(conn, "ALTER TABLE projects ADD COLUMN budget REAL")
  349. # Migration: Add is_template column to projects (Phase 8)
  350. await _safe_execute(conn, "ALTER TABLE projects ADD COLUMN is_template BOOLEAN DEFAULT 0")
  351. # Migration: Add template_source_id column to projects (Phase 8)
  352. await _safe_execute(conn, "ALTER TABLE projects ADD COLUMN template_source_id INTEGER")
  353. # Migration: Add parent_id column to projects (Phase 10)
  354. try:
  355. async with conn.begin_nested():
  356. await conn.execute(
  357. text("ALTER TABLE projects ADD COLUMN parent_id INTEGER REFERENCES projects(id) ON DELETE SET NULL")
  358. )
  359. except (OperationalError, ProgrammingError):
  360. pass # Already applied
  361. # Migration: Rename quantity_printed to quantity_acquired in project_bom_items
  362. await _safe_execute(conn, "ALTER TABLE project_bom_items RENAME COLUMN quantity_printed TO quantity_acquired")
  363. # Migration: Add unit_price column to project_bom_items
  364. await _safe_execute(conn, "ALTER TABLE project_bom_items ADD COLUMN unit_price REAL")
  365. # Migration: Add sourcing_url column to project_bom_items
  366. await _safe_execute(conn, "ALTER TABLE project_bom_items ADD COLUMN sourcing_url VARCHAR(512)")
  367. # Migration: Rename notes to remarks in project_bom_items
  368. await _safe_execute(conn, "ALTER TABLE project_bom_items RENAME COLUMN notes TO remarks")
  369. # Migration: Add show_in_switchbar column to smart_plugs
  370. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN show_in_switchbar BOOLEAN DEFAULT 0")
  371. # Migration: Add runtime tracking columns to printers
  372. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN runtime_seconds INTEGER DEFAULT 0")
  373. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN last_runtime_update DATETIME")
  374. # Migration: Add quantity column to print_archives for tracking item count
  375. await _safe_execute(conn, "ALTER TABLE print_archives ADD COLUMN quantity INTEGER DEFAULT 1")
  376. # Migration: Add manual_start column to print_queue for staged prints
  377. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN manual_start BOOLEAN DEFAULT 0")
  378. # Migration: Add wiki_url column to maintenance_types for documentation links
  379. await _safe_execute(conn, "ALTER TABLE maintenance_types ADD COLUMN wiki_url VARCHAR(500)")
  380. # Migration: Add ams_mapping column to print_queue for storing filament slot assignments
  381. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN ams_mapping TEXT")
  382. # Migration: Add target_parts_count column to projects for tracking total parts needed
  383. await _safe_execute(conn, "ALTER TABLE projects ADD COLUMN target_parts_count INTEGER")
  384. # Migration: Make printer_id nullable in print_queue for unassigned queue items
  385. # SQLite doesn't support ALTER COLUMN, so we need to recreate the table
  386. # PostgreSQL gets the correct schema from create_all(), so skip this
  387. if is_sqlite():
  388. try:
  389. result = await conn.execute(text("SELECT sql FROM sqlite_master WHERE type='table' AND name='print_queue'"))
  390. row = result.fetchone()
  391. if row and "printer_id INTEGER NOT NULL" in (row[0] or ""):
  392. await conn.execute(
  393. text("""
  394. CREATE TABLE print_queue_new (
  395. id INTEGER PRIMARY KEY,
  396. printer_id INTEGER REFERENCES printers(id) ON DELETE CASCADE,
  397. archive_id INTEGER NOT NULL REFERENCES print_archives(id) ON DELETE CASCADE,
  398. project_id INTEGER REFERENCES projects(id) ON DELETE SET NULL,
  399. position INTEGER DEFAULT 0,
  400. scheduled_time DATETIME,
  401. manual_start BOOLEAN DEFAULT 0,
  402. require_previous_success BOOLEAN DEFAULT 0,
  403. auto_off_after BOOLEAN DEFAULT 0,
  404. ams_mapping TEXT,
  405. status VARCHAR(20) DEFAULT 'pending',
  406. started_at DATETIME,
  407. completed_at DATETIME,
  408. error_message TEXT,
  409. created_at DATETIME DEFAULT CURRENT_TIMESTAMP
  410. )
  411. """)
  412. )
  413. await conn.execute(
  414. text("""
  415. INSERT INTO print_queue_new
  416. SELECT id, printer_id, archive_id, project_id, position, scheduled_time,
  417. manual_start, require_previous_success, auto_off_after, ams_mapping,
  418. status, started_at, completed_at, error_message, created_at
  419. FROM print_queue
  420. """)
  421. )
  422. await conn.execute(text("DROP TABLE print_queue"))
  423. await conn.execute(text("ALTER TABLE print_queue_new RENAME TO print_queue"))
  424. except (OperationalError, ProgrammingError):
  425. pass # Already applied
  426. # Migration: Add plug_type column to smart_plugs for HA integration
  427. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN plug_type VARCHAR(20) DEFAULT 'tasmota'")
  428. # Migration: Add ha_entity_id column to smart_plugs for HA integration
  429. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN ha_entity_id VARCHAR(100)")
  430. # Migration: Add project_id column to library_folders for linking folders to projects
  431. try:
  432. async with conn.begin_nested():
  433. await conn.execute(
  434. text(
  435. "ALTER TABLE library_folders ADD COLUMN project_id INTEGER REFERENCES projects(id) ON DELETE SET NULL"
  436. )
  437. )
  438. except (OperationalError, ProgrammingError):
  439. pass # Already applied
  440. # Migration: Add archive_id column to library_folders for linking folders to archives
  441. try:
  442. async with conn.begin_nested():
  443. await conn.execute(
  444. text(
  445. "ALTER TABLE library_folders ADD COLUMN archive_id INTEGER REFERENCES print_archives(id) ON DELETE SET NULL"
  446. )
  447. )
  448. except (OperationalError, ProgrammingError):
  449. pass # Already applied
  450. # Migration: Make ip_address nullable for HA plugs (SQLite requires table recreation)
  451. # PostgreSQL gets the correct schema from create_all(), so skip this
  452. if is_sqlite():
  453. try:
  454. result = await conn.execute(text("SELECT sql FROM sqlite_master WHERE type='table' AND name='smart_plugs'"))
  455. row = result.fetchone()
  456. if row and "ip_address VARCHAR(45) NOT NULL" in (row[0] or ""):
  457. await conn.execute(
  458. text("""
  459. CREATE TABLE smart_plugs_new (
  460. id INTEGER PRIMARY KEY,
  461. name VARCHAR(100) NOT NULL,
  462. ip_address VARCHAR(45),
  463. plug_type VARCHAR(20) DEFAULT 'tasmota',
  464. ha_entity_id VARCHAR(100),
  465. printer_id INTEGER UNIQUE REFERENCES printers(id) ON DELETE SET NULL,
  466. enabled BOOLEAN NOT NULL DEFAULT 1,
  467. auto_on BOOLEAN NOT NULL DEFAULT 1,
  468. auto_off BOOLEAN NOT NULL DEFAULT 1,
  469. auto_off_persistent BOOLEAN NOT NULL DEFAULT 0,
  470. off_delay_mode VARCHAR(20) NOT NULL DEFAULT 'time',
  471. off_delay_minutes INTEGER NOT NULL DEFAULT 5,
  472. off_temp_threshold INTEGER NOT NULL DEFAULT 70,
  473. username VARCHAR(50),
  474. password VARCHAR(100),
  475. power_alert_enabled BOOLEAN NOT NULL DEFAULT 0,
  476. power_alert_high FLOAT,
  477. power_alert_low FLOAT,
  478. power_alert_last_triggered DATETIME,
  479. schedule_enabled BOOLEAN NOT NULL DEFAULT 0,
  480. schedule_on_time VARCHAR(5),
  481. schedule_off_time VARCHAR(5),
  482. show_in_switchbar BOOLEAN DEFAULT 0,
  483. last_state VARCHAR(10),
  484. last_checked DATETIME,
  485. auto_off_executed BOOLEAN NOT NULL DEFAULT 0,
  486. auto_off_pending BOOLEAN DEFAULT 0,
  487. auto_off_pending_since DATETIME,
  488. created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
  489. updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
  490. )
  491. """)
  492. )
  493. await conn.execute(
  494. text("""
  495. INSERT INTO smart_plugs_new
  496. SELECT id, name, ip_address,
  497. COALESCE(plug_type, 'tasmota'), ha_entity_id, printer_id,
  498. enabled, auto_on, auto_off, COALESCE(auto_off_persistent, 0),
  499. off_delay_mode, off_delay_minutes, off_temp_threshold,
  500. username, password, power_alert_enabled, power_alert_high, power_alert_low,
  501. power_alert_last_triggered, schedule_enabled, schedule_on_time, schedule_off_time,
  502. COALESCE(show_in_switchbar, 0), last_state, last_checked, auto_off_executed,
  503. COALESCE(auto_off_pending, 0), auto_off_pending_since, created_at, updated_at
  504. FROM smart_plugs
  505. """)
  506. )
  507. await conn.execute(text("DROP TABLE smart_plugs"))
  508. await conn.execute(text("ALTER TABLE smart_plugs_new RENAME TO smart_plugs"))
  509. except (OperationalError, ProgrammingError):
  510. pass # Already applied
  511. # Migration: Add plate_id column to print_queue for multi-plate 3MF support
  512. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN plate_id INTEGER")
  513. # Migration: Add print options columns to print_queue
  514. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN bed_levelling BOOLEAN DEFAULT 1")
  515. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN flow_cali BOOLEAN DEFAULT 0")
  516. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN vibration_cali BOOLEAN DEFAULT 1")
  517. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN layer_inspect BOOLEAN DEFAULT 0")
  518. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN timelapse BOOLEAN DEFAULT 0")
  519. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN use_ams BOOLEAN DEFAULT 1")
  520. # Migration: Add library_file_id column to print_queue and make archive_id nullable
  521. # This allows queue items to reference library files directly (archive created at print start)
  522. try:
  523. async with conn.begin_nested():
  524. await conn.execute(
  525. text(
  526. "ALTER TABLE print_queue ADD COLUMN library_file_id INTEGER REFERENCES library_files(id) ON DELETE CASCADE"
  527. )
  528. )
  529. except (OperationalError, ProgrammingError):
  530. pass # Already applied
  531. # Check if archive_id needs to be made nullable (requires table recreation in SQLite)
  532. # PostgreSQL gets the correct schema from create_all(), so skip this
  533. if is_sqlite():
  534. try:
  535. result = await conn.execute(text("SELECT sql FROM sqlite_master WHERE type='table' AND name='print_queue'"))
  536. row = result.fetchone()
  537. if row and "archive_id INTEGER NOT NULL" in (row[0] or ""):
  538. await conn.execute(
  539. text("""
  540. CREATE TABLE print_queue_new2 (
  541. id INTEGER PRIMARY KEY,
  542. printer_id INTEGER REFERENCES printers(id) ON DELETE CASCADE,
  543. archive_id INTEGER REFERENCES print_archives(id) ON DELETE CASCADE,
  544. library_file_id INTEGER REFERENCES library_files(id) ON DELETE CASCADE,
  545. project_id INTEGER REFERENCES projects(id) ON DELETE SET NULL,
  546. position INTEGER DEFAULT 0,
  547. scheduled_time DATETIME,
  548. manual_start BOOLEAN DEFAULT 0,
  549. require_previous_success BOOLEAN DEFAULT 0,
  550. auto_off_after BOOLEAN DEFAULT 0,
  551. ams_mapping TEXT,
  552. plate_id INTEGER,
  553. bed_levelling BOOLEAN DEFAULT 1,
  554. flow_cali BOOLEAN DEFAULT 0,
  555. vibration_cali BOOLEAN DEFAULT 1,
  556. layer_inspect BOOLEAN DEFAULT 0,
  557. timelapse BOOLEAN DEFAULT 0,
  558. use_ams BOOLEAN DEFAULT 1,
  559. status VARCHAR(20) DEFAULT 'pending',
  560. started_at DATETIME,
  561. completed_at DATETIME,
  562. error_message TEXT,
  563. created_at DATETIME DEFAULT CURRENT_TIMESTAMP
  564. )
  565. """)
  566. )
  567. await conn.execute(
  568. text("""
  569. INSERT INTO print_queue_new2
  570. SELECT id, printer_id, archive_id, NULL, project_id, position, scheduled_time,
  571. manual_start, require_previous_success, auto_off_after, ams_mapping, plate_id,
  572. COALESCE(bed_levelling, 1), COALESCE(flow_cali, 0), COALESCE(vibration_cali, 1),
  573. COALESCE(layer_inspect, 0), COALESCE(timelapse, 0), COALESCE(use_ams, 1),
  574. status, started_at, completed_at, error_message, created_at
  575. FROM print_queue
  576. """)
  577. )
  578. await conn.execute(text("DROP TABLE print_queue"))
  579. await conn.execute(text("ALTER TABLE print_queue_new2 RENAME TO print_queue"))
  580. except (OperationalError, ProgrammingError):
  581. pass # Already applied
  582. # Migration: Add HA energy sensor entity columns to smart_plugs
  583. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN ha_power_entity VARCHAR(100)")
  584. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN ha_energy_today_entity VARCHAR(100)")
  585. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN ha_energy_total_entity VARCHAR(100)")
  586. # Migration: Create users table for authentication
  587. try:
  588. async with conn.begin_nested():
  589. await conn.execute(
  590. text("""
  591. CREATE TABLE IF NOT EXISTS users (
  592. id INTEGER PRIMARY KEY,
  593. username VARCHAR(100) NOT NULL UNIQUE,
  594. password_hash VARCHAR(255) NOT NULL,
  595. role VARCHAR(20) NOT NULL DEFAULT 'user',
  596. is_active BOOLEAN NOT NULL DEFAULT 1,
  597. created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  598. updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
  599. )
  600. """)
  601. )
  602. await conn.execute(text("CREATE INDEX IF NOT EXISTS ix_users_username ON users(username)"))
  603. except (OperationalError, ProgrammingError):
  604. pass # Already applied
  605. # Migration: Add external camera columns to printers
  606. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN external_camera_url VARCHAR(500)")
  607. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN external_camera_type VARCHAR(20)")
  608. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN external_camera_enabled BOOLEAN DEFAULT 0")
  609. # Migration: Add external_url column to print_archives for user-defined links (Printables, etc.)
  610. await _safe_execute(conn, "ALTER TABLE print_archives ADD COLUMN external_url VARCHAR(500)")
  611. # Migration: Add sliced_for_model column to print_archives for model-based queue assignment
  612. await _safe_execute(conn, "ALTER TABLE print_archives ADD COLUMN sliced_for_model VARCHAR(50)")
  613. # Migration: Add is_external column to library_files for external cloud files
  614. await _safe_execute(conn, "ALTER TABLE library_files ADD COLUMN is_external BOOLEAN DEFAULT 0")
  615. # Migration: Add project_id column to library_files
  616. try:
  617. async with conn.begin_nested():
  618. await conn.execute(
  619. text(
  620. "ALTER TABLE library_files ADD COLUMN project_id INTEGER REFERENCES projects(id) ON DELETE SET NULL"
  621. )
  622. )
  623. except (OperationalError, ProgrammingError):
  624. pass # Already applied
  625. # Migration: Add is_external column to library_folders for external cloud folders
  626. await _safe_execute(conn, "ALTER TABLE library_folders ADD COLUMN is_external BOOLEAN DEFAULT 0")
  627. # Migration: Add external folder settings columns to library_folders
  628. await _safe_execute(conn, "ALTER TABLE library_folders ADD COLUMN external_readonly BOOLEAN DEFAULT 0")
  629. await _safe_execute(conn, "ALTER TABLE library_folders ADD COLUMN external_show_hidden BOOLEAN DEFAULT 0")
  630. await _safe_execute(conn, "ALTER TABLE library_folders ADD COLUMN external_path VARCHAR(500)")
  631. # Migration: Add plate_detection_enabled column to printers
  632. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN plate_detection_enabled BOOLEAN DEFAULT 0")
  633. # Migration: Add plate detection ROI columns to printers
  634. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN plate_detection_roi_x REAL")
  635. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN plate_detection_roi_y REAL")
  636. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN plate_detection_roi_w REAL")
  637. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN plate_detection_roi_h REAL")
  638. # Migration: Remove UNIQUE constraint from smart_plugs.printer_id
  639. # This allows HA scripts to coexist with regular plugs (scripts are for multi-device control)
  640. # SQLite requires table recreation to drop constraints
  641. # PostgreSQL gets the correct schema from create_all(), so skip this
  642. if is_sqlite():
  643. try:
  644. needs_migration = False
  645. result = await conn.execute(text("SELECT sql FROM sqlite_master WHERE type='table' AND name='smart_plugs'"))
  646. row = result.fetchone()
  647. table_sql = (row[0] or "").upper() if row else ""
  648. if "PRINTER_ID" in table_sql and "UNIQUE" in table_sql:
  649. import re
  650. if re.search(r'"?PRINTER_ID"?\s+\w+\s+UNIQUE', table_sql) or re.search(
  651. r'UNIQUE\s*\([^)]*"?PRINTER_ID"?', table_sql
  652. ):
  653. needs_migration = True
  654. idx_result = await conn.execute(
  655. text("SELECT sql FROM sqlite_master WHERE type='index' AND tbl_name='smart_plugs' AND sql IS NOT NULL")
  656. )
  657. for idx_row in idx_result.fetchall():
  658. idx_sql = (idx_row[0] or "").upper()
  659. if "UNIQUE" in idx_sql and "PRINTER_ID" in idx_sql:
  660. needs_migration = True
  661. break
  662. if needs_migration:
  663. # Create new table without UNIQUE constraint on printer_id
  664. await conn.execute(
  665. text("""
  666. CREATE TABLE smart_plugs_temp (
  667. id INTEGER PRIMARY KEY,
  668. name VARCHAR(100) NOT NULL,
  669. ip_address VARCHAR(45),
  670. plug_type VARCHAR(20) DEFAULT 'tasmota',
  671. ha_entity_id VARCHAR(100),
  672. ha_power_entity VARCHAR(100),
  673. ha_energy_today_entity VARCHAR(100),
  674. ha_energy_total_entity VARCHAR(100),
  675. printer_id INTEGER REFERENCES printers(id) ON DELETE SET NULL,
  676. enabled BOOLEAN NOT NULL DEFAULT 1,
  677. auto_on BOOLEAN NOT NULL DEFAULT 1,
  678. auto_off BOOLEAN NOT NULL DEFAULT 1,
  679. auto_off_persistent BOOLEAN NOT NULL DEFAULT 0,
  680. off_delay_mode VARCHAR(20) NOT NULL DEFAULT 'time',
  681. off_delay_minutes INTEGER NOT NULL DEFAULT 5,
  682. off_temp_threshold INTEGER NOT NULL DEFAULT 70,
  683. username VARCHAR(50),
  684. password VARCHAR(100),
  685. power_alert_enabled BOOLEAN NOT NULL DEFAULT 0,
  686. power_alert_high FLOAT,
  687. power_alert_low FLOAT,
  688. power_alert_last_triggered DATETIME,
  689. schedule_enabled BOOLEAN NOT NULL DEFAULT 0,
  690. schedule_on_time VARCHAR(5),
  691. schedule_off_time VARCHAR(5),
  692. show_in_switchbar BOOLEAN DEFAULT 0,
  693. last_state VARCHAR(10),
  694. last_checked DATETIME,
  695. auto_off_executed BOOLEAN NOT NULL DEFAULT 0,
  696. auto_off_pending BOOLEAN DEFAULT 0,
  697. auto_off_pending_since DATETIME,
  698. created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
  699. updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
  700. )
  701. """)
  702. )
  703. # Copy data
  704. await conn.execute(
  705. text("""
  706. INSERT INTO smart_plugs_temp
  707. SELECT id, name, ip_address, plug_type, ha_entity_id, ha_power_entity,
  708. ha_energy_today_entity, ha_energy_total_entity, printer_id, enabled,
  709. auto_on, auto_off, COALESCE(auto_off_persistent, 0),
  710. off_delay_mode, off_delay_minutes, off_temp_threshold,
  711. username, password, power_alert_enabled, power_alert_high, power_alert_low,
  712. power_alert_last_triggered, schedule_enabled, schedule_on_time, schedule_off_time,
  713. show_in_switchbar, last_state, last_checked, auto_off_executed,
  714. auto_off_pending, auto_off_pending_since, created_at, updated_at
  715. FROM smart_plugs
  716. """)
  717. )
  718. # Drop old table and rename new one
  719. await conn.execute(text("DROP TABLE smart_plugs"))
  720. await conn.execute(text("ALTER TABLE smart_plugs_temp RENAME TO smart_plugs"))
  721. except (OperationalError, ProgrammingError):
  722. pass # Already applied
  723. # Migration: Add show_on_printer_card column to smart_plugs
  724. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN show_on_printer_card BOOLEAN DEFAULT 1")
  725. # Migration: Add MQTT smart plug fields (legacy)
  726. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN mqtt_topic VARCHAR(200)")
  727. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN mqtt_power_path VARCHAR(100)")
  728. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN mqtt_energy_path VARCHAR(100)")
  729. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN mqtt_state_path VARCHAR(100)")
  730. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN mqtt_multiplier REAL DEFAULT 1.0")
  731. # Migration: Add enhanced MQTT smart plug fields (separate topics and multipliers)
  732. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN mqtt_power_topic VARCHAR(200)")
  733. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN mqtt_power_multiplier REAL DEFAULT 1.0")
  734. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN mqtt_energy_topic VARCHAR(200)")
  735. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN mqtt_energy_multiplier REAL DEFAULT 1.0")
  736. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN mqtt_state_topic VARCHAR(200)")
  737. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN mqtt_state_on_value VARCHAR(50)")
  738. # Migration: Copy existing mqtt_topic to mqtt_power_topic for backward compatibility
  739. try:
  740. async with conn.begin_nested():
  741. await conn.execute(
  742. text("""
  743. UPDATE smart_plugs
  744. SET mqtt_power_topic = mqtt_topic,
  745. mqtt_power_multiplier = mqtt_multiplier
  746. WHERE mqtt_topic IS NOT NULL AND mqtt_power_topic IS NULL
  747. """)
  748. )
  749. except (OperationalError, ProgrammingError):
  750. pass # Already applied
  751. # Migration: Create groups table for permission-based access control
  752. try:
  753. async with conn.begin_nested():
  754. await conn.execute(
  755. text("""
  756. CREATE TABLE IF NOT EXISTS groups (
  757. id INTEGER PRIMARY KEY,
  758. name VARCHAR(100) NOT NULL UNIQUE,
  759. description VARCHAR(500),
  760. permissions JSON,
  761. is_system BOOLEAN NOT NULL DEFAULT 0,
  762. created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  763. updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
  764. )
  765. """)
  766. )
  767. await conn.execute(text("CREATE INDEX IF NOT EXISTS ix_groups_name ON groups(name)"))
  768. except (OperationalError, ProgrammingError):
  769. pass # Already applied
  770. # Migration: Create user_groups association table
  771. try:
  772. async with conn.begin_nested():
  773. await conn.execute(
  774. text("""
  775. CREATE TABLE IF NOT EXISTS user_groups (
  776. user_id INTEGER NOT NULL,
  777. group_id INTEGER NOT NULL,
  778. PRIMARY KEY (user_id, group_id),
  779. FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
  780. FOREIGN KEY (group_id) REFERENCES groups(id) ON DELETE CASCADE
  781. )
  782. """)
  783. )
  784. except (OperationalError, ProgrammingError):
  785. pass # Already applied
  786. # Migration: Add model-based queue assignment columns to print_queue
  787. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN target_model VARCHAR(50)")
  788. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN required_filament_types TEXT")
  789. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN waiting_reason TEXT")
  790. # Migration: Add nozzle_count column to printers (for dual-extruder detection)
  791. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN nozzle_count INTEGER DEFAULT 1")
  792. # Migration: Add print_hours_offset column to printers (baseline hours adjustment)
  793. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN print_hours_offset REAL DEFAULT 0.0")
  794. # Migration: Add queue notification event columns to notification_providers
  795. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN on_queue_job_added BOOLEAN DEFAULT 0")
  796. try:
  797. async with conn.begin_nested():
  798. await conn.execute(
  799. text("ALTER TABLE notification_providers ADD COLUMN on_queue_job_assigned BOOLEAN DEFAULT 0")
  800. )
  801. except (OperationalError, ProgrammingError):
  802. pass # Already applied
  803. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN on_queue_job_started BOOLEAN DEFAULT 0")
  804. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN on_queue_job_waiting BOOLEAN DEFAULT 1")
  805. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN on_queue_job_skipped BOOLEAN DEFAULT 1")
  806. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN on_queue_job_failed BOOLEAN DEFAULT 1")
  807. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN on_queue_completed BOOLEAN DEFAULT 0")
  808. # Migration: Add created_by_id column to print_archives for user tracking (Issue #206)
  809. try:
  810. async with conn.begin_nested():
  811. await conn.execute(
  812. text(
  813. "ALTER TABLE print_archives ADD COLUMN created_by_id INTEGER REFERENCES users(id) ON DELETE SET NULL"
  814. )
  815. )
  816. except (OperationalError, ProgrammingError):
  817. pass # Already applied
  818. # Migration: Add created_by_id column to print_queue for user tracking (Issue #206)
  819. try:
  820. async with conn.begin_nested():
  821. await conn.execute(
  822. text("ALTER TABLE print_queue ADD COLUMN created_by_id INTEGER REFERENCES users(id) ON DELETE SET NULL")
  823. )
  824. except (OperationalError, ProgrammingError):
  825. pass # Already applied
  826. # Migration: Add created_by_id column to library_files for user tracking (Issue #206)
  827. try:
  828. async with conn.begin_nested():
  829. await conn.execute(
  830. text(
  831. "ALTER TABLE library_files ADD COLUMN created_by_id INTEGER REFERENCES users(id) ON DELETE SET NULL"
  832. )
  833. )
  834. except (OperationalError, ProgrammingError):
  835. pass # Already applied
  836. # Migration: Add target_location column to print_queue for location-based filtering (Issue #220)
  837. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN target_location VARCHAR(100)")
  838. # Migration: Convert absolute paths to relative paths in library_files table
  839. # This ensures backup/restore portability across different installations
  840. try:
  841. async with conn.begin_nested():
  842. base_dir_str = str(settings.base_dir)
  843. # Ensure we have a trailing slash for clean replacement
  844. if not base_dir_str.endswith("/"):
  845. base_dir_str += "/"
  846. # Update file_path - remove base_dir prefix from absolute paths
  847. await conn.execute(
  848. text("""
  849. UPDATE library_files
  850. SET file_path = SUBSTR(file_path, LENGTH(:base_dir) + 1)
  851. WHERE file_path LIKE :pattern
  852. """),
  853. {"base_dir": base_dir_str, "pattern": base_dir_str + "%"},
  854. )
  855. # Update thumbnail_path - remove base_dir prefix from absolute paths
  856. await conn.execute(
  857. text("""
  858. UPDATE library_files
  859. SET thumbnail_path = SUBSTR(thumbnail_path, LENGTH(:base_dir) + 1)
  860. WHERE thumbnail_path LIKE :pattern
  861. """),
  862. {"base_dir": base_dir_str, "pattern": base_dir_str + "%"},
  863. )
  864. except (OperationalError, ProgrammingError):
  865. pass # Already applied
  866. # Create active_print_spoolman table for Spoolman per-filament tracking
  867. try:
  868. async with conn.begin_nested():
  869. await conn.execute(
  870. text("""
  871. CREATE TABLE IF NOT EXISTS active_print_spoolman (
  872. id INTEGER PRIMARY KEY AUTOINCREMENT,
  873. printer_id INTEGER NOT NULL REFERENCES printers(id) ON DELETE CASCADE,
  874. archive_id INTEGER NOT NULL REFERENCES print_archives(id) ON DELETE CASCADE,
  875. filament_usage TEXT NOT NULL,
  876. ams_trays TEXT NOT NULL,
  877. slot_to_tray TEXT,
  878. layer_usage TEXT,
  879. filament_properties TEXT,
  880. UNIQUE(printer_id, archive_id)
  881. )
  882. """)
  883. )
  884. except (OperationalError, ProgrammingError):
  885. pass # Already applied
  886. # Migration: Add preset_source column to slot_preset_mappings for local preset support
  887. try:
  888. async with conn.begin_nested():
  889. await conn.execute(
  890. text("ALTER TABLE slot_preset_mappings ADD COLUMN preset_source VARCHAR(20) DEFAULT 'cloud'")
  891. )
  892. except (OperationalError, ProgrammingError):
  893. pass # Already applied
  894. # Migration: Add email column to users for Advanced Auth (PR #322)
  895. await _safe_execute(conn, "ALTER TABLE users ADD COLUMN email VARCHAR(255)")
  896. # Migration: Add inventory spool tracking columns
  897. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN added_full BOOLEAN")
  898. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN last_used DATETIME")
  899. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN encode_time DATETIME")
  900. # Migration: Add RFID tag matching columns to spool
  901. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN tag_uid VARCHAR(16)")
  902. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN tray_uuid VARCHAR(32)")
  903. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN data_origin VARCHAR(20)")
  904. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN tag_type VARCHAR(20)")
  905. # Migration: Add core_weight_catalog_id to track which catalog entry was used for empty spool weight
  906. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN core_weight_catalog_id INTEGER")
  907. # Migration: Create spool_usage_history table for filament consumption tracking
  908. try:
  909. async with conn.begin_nested():
  910. await conn.execute(
  911. text("""
  912. CREATE TABLE IF NOT EXISTS spool_usage_history (
  913. id INTEGER PRIMARY KEY AUTOINCREMENT,
  914. spool_id INTEGER NOT NULL REFERENCES spool(id) ON DELETE CASCADE,
  915. printer_id INTEGER REFERENCES printers(id) ON DELETE SET NULL,
  916. print_name VARCHAR(500),
  917. weight_used REAL NOT NULL DEFAULT 0,
  918. percent_used INTEGER NOT NULL DEFAULT 0,
  919. status VARCHAR(20) NOT NULL DEFAULT 'completed',
  920. created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
  921. )
  922. """)
  923. )
  924. except (OperationalError, ProgrammingError):
  925. pass # Already applied
  926. # Migration: Add open_in_new_tab column to external_links
  927. await _safe_execute(conn, "ALTER TABLE external_links ADD COLUMN open_in_new_tab BOOLEAN DEFAULT 0")
  928. # Migration: Add bed cooled notification column to notification_providers
  929. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN on_bed_cooled BOOLEAN DEFAULT 0")
  930. # Migration: Add first layer complete notification column to notification_providers
  931. try:
  932. async with conn.begin_nested():
  933. await conn.execute(
  934. text("ALTER TABLE notification_providers ADD COLUMN on_first_layer_complete BOOLEAN DEFAULT 0")
  935. )
  936. except (OperationalError, ProgrammingError):
  937. pass # Already applied
  938. # Migration: Add weight_locked flag to spool table (skip AMS auto-sync for manually-entered weights)
  939. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN weight_locked BOOLEAN DEFAULT 0")
  940. # Migration: Add SpoolBuddy scale weight tracking columns to spool table
  941. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN last_scale_weight INTEGER")
  942. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN last_weighed_at DATETIME")
  943. # Migration: Add cost tracking fields to spool table
  944. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN cost_per_kg REAL")
  945. # Migration: Add cost field to spool_usage_history table
  946. await _safe_execute(conn, "ALTER TABLE spool_usage_history ADD COLUMN cost REAL")
  947. # Migration: Add archive_id field to spool_usage_history table
  948. try:
  949. async with conn.begin_nested():
  950. await conn.execute(
  951. text("ALTER TABLE spool_usage_history ADD COLUMN archive_id INTEGER REFERENCES print_archives(id)")
  952. )
  953. except (OperationalError, ProgrammingError):
  954. pass # Already applied
  955. # Migration: Migrate single virtual printer key-value settings to virtual_printers table
  956. try:
  957. async with conn.begin_nested():
  958. result = await conn.execute(text("SELECT COUNT(*) FROM virtual_printers"))
  959. count = result.scalar() or 0
  960. if count == 0:
  961. result = await conn.execute(text("SELECT value FROM settings WHERE key = 'virtual_printer_enabled'"))
  962. row = result.fetchone()
  963. if row:
  964. # Old settings exist — migrate to first virtual printer row
  965. old_enabled = row[0] == "true" if row[0] else False
  966. result = await conn.execute(
  967. text("SELECT value FROM settings WHERE key = 'virtual_printer_access_code'")
  968. )
  969. row = result.fetchone()
  970. old_access_code = row[0] if row else None
  971. result = await conn.execute(text("SELECT value FROM settings WHERE key = 'virtual_printer_mode'"))
  972. row = result.fetchone()
  973. old_mode = row[0] if row else "immediate"
  974. if old_mode == "queue":
  975. old_mode = "review"
  976. result = await conn.execute(text("SELECT value FROM settings WHERE key = 'virtual_printer_model'"))
  977. row = result.fetchone()
  978. old_model = row[0] if row else "BL-P001"
  979. result = await conn.execute(
  980. text("SELECT value FROM settings WHERE key = 'virtual_printer_target_printer_id'")
  981. )
  982. row = result.fetchone()
  983. old_target_id = int(row[0]) if row and row[0] else None
  984. result = await conn.execute(
  985. text("SELECT value FROM settings WHERE key = 'virtual_printer_remote_interface_ip'")
  986. )
  987. row = result.fetchone()
  988. old_remote_iface = row[0] if row else None
  989. await conn.execute(
  990. text("""
  991. INSERT INTO virtual_printers
  992. (name, enabled, mode, model, access_code, target_printer_id,
  993. bind_ip, remote_interface_ip, serial_suffix, position)
  994. VALUES
  995. (:name, :enabled, :mode, :model, :access_code, :target_id,
  996. NULL, :remote_iface, '391800001', 0)
  997. """),
  998. {
  999. "name": "Bambuddy",
  1000. "enabled": old_enabled,
  1001. "mode": old_mode or "immediate",
  1002. "model": old_model,
  1003. "access_code": old_access_code,
  1004. "target_id": old_target_id,
  1005. "remote_iface": old_remote_iface,
  1006. },
  1007. )
  1008. except (OperationalError, ProgrammingError, IntegrityError):
  1009. pass # Table may not exist yet on first run, or columns have different constraints
  1010. # Migration: Add filament_overrides column to print_queue for filament override in model-based assignment
  1011. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN filament_overrides TEXT")
  1012. # Migration: Add NFC reader and display control columns to spoolbuddy_devices
  1013. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN nfc_reader_type VARCHAR(20)")
  1014. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN nfc_connection VARCHAR(20)")
  1015. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN display_brightness INTEGER DEFAULT 100")
  1016. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN display_blank_timeout INTEGER DEFAULT 0")
  1017. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN has_backlight BOOLEAN DEFAULT 0")
  1018. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN last_calibrated_at DATETIME")
  1019. # Migration: Add NFC tag write payload column to spoolbuddy_devices
  1020. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN pending_write_payload TEXT")
  1021. # Migration: Add OTA update tracking columns to spoolbuddy_devices
  1022. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN update_status VARCHAR(20)")
  1023. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN update_message VARCHAR(255)")
  1024. # Migration: Persist SpoolBuddy backend URL and queued system payload
  1025. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN backend_url VARCHAR(255)")
  1026. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN pending_system_payload TEXT")
  1027. # Migration: Add system_stats JSON blob column to spoolbuddy_devices
  1028. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN system_stats TEXT")
  1029. # Migration: Convert ams_labels table from (printer_id, ams_id) key to ams_serial_number key
  1030. # Labels are now keyed by AMS serial number so they persist when the AMS is moved to another printer.
  1031. # PostgreSQL gets the correct schema from create_all(), so skip this
  1032. if is_sqlite():
  1033. try:
  1034. await conn.execute(text("DROP TABLE IF EXISTS ams_labels_new"))
  1035. result = await conn.execute(text("SELECT sql FROM sqlite_master WHERE type='table' AND name='ams_labels'"))
  1036. row = result.fetchone()
  1037. if row and "printer_id" in (row[0] or ""):
  1038. # Old schema: rebuild the table with ams_serial_number as the unique key.
  1039. # Existing rows get a synthetic serial "p{printer_id}a{ams_id}" so data is preserved.
  1040. await conn.execute(
  1041. text("""
  1042. CREATE TABLE ams_labels_new (
  1043. id INTEGER PRIMARY KEY,
  1044. ams_serial_number VARCHAR(50) NOT NULL,
  1045. ams_id INTEGER,
  1046. label VARCHAR(100) NOT NULL,
  1047. created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  1048. updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  1049. CONSTRAINT uq_ams_label_serial UNIQUE (ams_serial_number)
  1050. )
  1051. """)
  1052. )
  1053. await conn.execute(
  1054. text("""
  1055. INSERT INTO ams_labels_new (id, ams_serial_number, ams_id, label, created_at, updated_at)
  1056. SELECT id,
  1057. 'p' || CAST(printer_id AS TEXT) || 'a' || CAST(ams_id AS TEXT),
  1058. ams_id,
  1059. label,
  1060. created_at,
  1061. updated_at
  1062. FROM ams_labels
  1063. """)
  1064. )
  1065. await conn.execute(text("DROP TABLE ams_labels"))
  1066. await conn.execute(text("ALTER TABLE ams_labels_new RENAME TO ams_labels"))
  1067. except (OperationalError, ProgrammingError):
  1068. pass # Already migrated or table does not exist yet
  1069. # Migration: Add auto_dispatch column to virtual_printers
  1070. await _safe_execute(conn, "ALTER TABLE virtual_printers ADD COLUMN auto_dispatch BOOLEAN DEFAULT 1")
  1071. # Migration: Fix VP model codes — convert legacy SSDP codes and display names to correct SSDP codes
  1072. # Legacy codes (from multi-VP refactor) and display names (from proxy auto-inherit)
  1073. vp_model_fixes = {
  1074. "3DPrinter-X1-Carbon": "BL-P001",
  1075. "3DPrinter-X1": "BL-P002",
  1076. "X1C": "BL-P001",
  1077. "X1": "BL-P002",
  1078. "X1E": "C13",
  1079. "P1P": "C11",
  1080. "P1S": "C12",
  1081. "P2S": "N7",
  1082. "A1": "N2S",
  1083. "A1 Mini": "N1",
  1084. "H2D": "O1D",
  1085. "H2C": "O1C",
  1086. "H2S": "O1S",
  1087. }
  1088. for old_val, new_val in vp_model_fixes.items():
  1089. await conn.execute(
  1090. text("UPDATE virtual_printers SET model = :new WHERE model = :old"),
  1091. {"old": old_val, "new": new_val},
  1092. )
  1093. await conn.execute(
  1094. text("UPDATE settings SET value = :new WHERE key = 'virtual_printer_model' AND value = :old"),
  1095. {"old": old_val, "new": new_val},
  1096. )
  1097. # Migration: Add per-user Bambu Cloud credential columns
  1098. await _safe_execute(conn, "ALTER TABLE users ADD COLUMN cloud_token VARCHAR(500)")
  1099. await _safe_execute(conn, "ALTER TABLE users ADD COLUMN cloud_email VARCHAR(255)")
  1100. # Cleanup: Remove obsolete settings keys that are no longer used
  1101. obsolete_keys = ["slicer_binary_path"]
  1102. for key in obsolete_keys:
  1103. await conn.execute(text("DELETE FROM settings WHERE key = :key"), {"key": key})
  1104. # Migration: Create user_email_preferences table for user-specific email notification settings
  1105. try:
  1106. async with conn.begin_nested():
  1107. await conn.execute(
  1108. text("""
  1109. CREATE TABLE IF NOT EXISTS user_email_preferences (
  1110. id INTEGER PRIMARY KEY,
  1111. user_id INTEGER NOT NULL UNIQUE REFERENCES users(id) ON DELETE CASCADE,
  1112. notify_print_start BOOLEAN NOT NULL DEFAULT 1,
  1113. notify_print_complete BOOLEAN NOT NULL DEFAULT 1,
  1114. notify_print_failed BOOLEAN NOT NULL DEFAULT 1,
  1115. notify_print_stopped BOOLEAN NOT NULL DEFAULT 1,
  1116. created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  1117. updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
  1118. )
  1119. """)
  1120. )
  1121. await conn.execute(
  1122. text("CREATE INDEX IF NOT EXISTS ix_user_email_preferences_user_id ON user_email_preferences(user_id)")
  1123. )
  1124. except (OperationalError, ProgrammingError):
  1125. pass # Already applied
  1126. # Legacy migration: Add notify_print_stopped column (for any existing partial tables)
  1127. try:
  1128. async with conn.begin_nested():
  1129. await conn.execute(
  1130. text("ALTER TABLE user_email_preferences ADD COLUMN notify_print_stopped BOOLEAN NOT NULL DEFAULT 1")
  1131. )
  1132. except (OperationalError, ProgrammingError):
  1133. pass # Column already exists or table created with full schema
  1134. # Migration: Add camera_rotation column to printers
  1135. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN camera_rotation INTEGER DEFAULT 0")
  1136. # Migration: Add REST/Webhook smart plug fields
  1137. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_on_url VARCHAR(500)")
  1138. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_on_body TEXT")
  1139. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_off_url VARCHAR(500)")
  1140. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_off_body TEXT")
  1141. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_method VARCHAR(10)")
  1142. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_headers TEXT")
  1143. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_status_url VARCHAR(500)")
  1144. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_status_path VARCHAR(200)")
  1145. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_status_on_value VARCHAR(50)")
  1146. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_power_path VARCHAR(200)")
  1147. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_energy_path VARCHAR(200)")
  1148. # Migration: Add separate REST power/energy URLs and multipliers
  1149. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_power_url VARCHAR(500)")
  1150. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_power_multiplier REAL DEFAULT 1.0")
  1151. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_energy_url VARCHAR(500)")
  1152. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_energy_multiplier REAL DEFAULT 1.0")
  1153. # Migration: Add batch_id column to print_queue for batch grouping
  1154. try:
  1155. async with conn.begin_nested():
  1156. await conn.execute(
  1157. text(
  1158. "ALTER TABLE print_queue ADD COLUMN batch_id INTEGER REFERENCES print_batches(id) ON DELETE SET NULL"
  1159. )
  1160. )
  1161. except (OperationalError, ProgrammingError):
  1162. pass
  1163. # Migration: Shortest-job-first scheduling columns on print_queue
  1164. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN print_time_seconds INTEGER")
  1165. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN been_jumped BOOLEAN DEFAULT FALSE NOT NULL")
  1166. # Migration: Auto-print G-code injection (#422)
  1167. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN gcode_injection BOOLEAN DEFAULT FALSE NOT NULL")
  1168. # Migration: Add backup_spools and backup_archives columns to github_backup_config
  1169. await _safe_execute(conn, "ALTER TABLE github_backup_config ADD COLUMN backup_spools BOOLEAN DEFAULT 0")
  1170. await _safe_execute(conn, "ALTER TABLE github_backup_config ADD COLUMN backup_archives BOOLEAN DEFAULT 0")
  1171. # Migration: Widen columns where SQLite allowed data beyond the declared VARCHAR limit
  1172. if not is_sqlite():
  1173. await _safe_execute(conn, "ALTER TABLE api_keys ALTER COLUMN key_hash TYPE VARCHAR(255)")
  1174. await _safe_execute(conn, "ALTER TABLE api_keys ALTER COLUMN key_prefix TYPE VARCHAR(20)")
  1175. await _safe_execute(conn, "ALTER TABLE print_archives ALTER COLUMN filament_color TYPE VARCHAR(200)")
  1176. # Migration: Create GIN index for full-text search on PostgreSQL
  1177. # (SQLite uses FTS5 virtual table instead, set up above)
  1178. if not is_sqlite():
  1179. try:
  1180. await conn.execute(
  1181. text("""
  1182. CREATE INDEX IF NOT EXISTS idx_archives_fulltext
  1183. ON print_archives
  1184. USING GIN (to_tsvector('simple',
  1185. COALESCE(print_name, '') || ' ' ||
  1186. COALESCE(filename, '') || ' ' ||
  1187. COALESCE(tags, '') || ' ' ||
  1188. COALESCE(notes, '') || ' ' ||
  1189. COALESCE(designer, '') || ' ' ||
  1190. COALESCE(filament_type, '')
  1191. ))
  1192. """)
  1193. )
  1194. except (OperationalError, ProgrammingError):
  1195. pass # Already applied
  1196. # Migration: Normalize empty printer_ids [] to NULL (global access) on API keys
  1197. # Previously both None and [] meant "all printers"; now [] means "no printers"
  1198. await _safe_execute(conn, "UPDATE api_keys SET printer_ids = NULL WHERE printer_ids = '[]'")
  1199. # Migration: Add auth_source column to users for LDAP support (#794)
  1200. await _safe_execute(conn, "ALTER TABLE users ADD COLUMN auth_source VARCHAR(20) DEFAULT 'local' NOT NULL")
  1201. # Migration: Make password_hash nullable for LDAP users (#794)
  1202. if not is_sqlite():
  1203. await _safe_execute(conn, "ALTER TABLE users ALTER COLUMN password_hash DROP NOT NULL")
  1204. # Migration: Add energy_start_kwh to print_archives (#941)
  1205. # Persists the smart plug lifetime counter captured at print start, so per-print
  1206. # energy tracking survives a backend restart mid-print.
  1207. await _safe_execute(conn, "ALTER TABLE print_archives ADD COLUMN energy_start_kwh REAL")
  1208. # Migration: Create smart_plug_energy_snapshots table (#941)
  1209. # Hourly snapshots of each plug's lifetime counter, so date-range queries in
  1210. # "total consumption" energy mode can compute (last - first) deltas.
  1211. await _safe_execute(
  1212. conn,
  1213. """
  1214. CREATE TABLE IF NOT EXISTS smart_plug_energy_snapshots (
  1215. id INTEGER PRIMARY KEY AUTOINCREMENT,
  1216. plug_id INTEGER NOT NULL REFERENCES smart_plugs(id) ON DELETE CASCADE,
  1217. recorded_at DATETIME NOT NULL,
  1218. lifetime_kwh REAL NOT NULL
  1219. )
  1220. """
  1221. if is_sqlite()
  1222. else """
  1223. CREATE TABLE IF NOT EXISTS smart_plug_energy_snapshots (
  1224. id SERIAL PRIMARY KEY,
  1225. plug_id INTEGER NOT NULL REFERENCES smart_plugs(id) ON DELETE CASCADE,
  1226. recorded_at TIMESTAMP NOT NULL,
  1227. lifetime_kwh REAL NOT NULL
  1228. )
  1229. """,
  1230. )
  1231. await _safe_execute(
  1232. conn,
  1233. "CREATE INDEX IF NOT EXISTS ix_plug_energy_snapshots_plug_time "
  1234. "ON smart_plug_energy_snapshots(plug_id, recorded_at)",
  1235. )
  1236. # Seed default settings keys that must exist on fresh install
  1237. default_settings = [
  1238. ("advanced_auth_enabled", "false"),
  1239. ("smtp_auth_enabled", "true"),
  1240. ]
  1241. for key, value in default_settings:
  1242. try:
  1243. if is_sqlite():
  1244. await conn.execute(
  1245. text("INSERT OR IGNORE INTO settings (key, value) VALUES (:key, :value)"),
  1246. {"key": key, "value": value},
  1247. )
  1248. else:
  1249. await conn.execute(
  1250. text("INSERT INTO settings (key, value) VALUES (:key, :value) ON CONFLICT (key) DO NOTHING"),
  1251. {"key": key, "value": value},
  1252. )
  1253. except (OperationalError, ProgrammingError):
  1254. pass
  1255. async def seed_notification_templates():
  1256. """Seed default notification templates if they don't exist."""
  1257. from sqlalchemy import select
  1258. from backend.app.models.notification_template import DEFAULT_TEMPLATES, NotificationTemplate
  1259. async with async_session() as session:
  1260. # Get existing template event types
  1261. result = await session.execute(select(NotificationTemplate.event_type))
  1262. existing_types = {row[0] for row in result.fetchall()}
  1263. if not existing_types:
  1264. # No templates exist - insert all defaults
  1265. for template_data in DEFAULT_TEMPLATES:
  1266. template = NotificationTemplate(
  1267. event_type=template_data["event_type"],
  1268. name=template_data["name"],
  1269. title_template=template_data["title_template"],
  1270. body_template=template_data["body_template"],
  1271. is_default=True,
  1272. )
  1273. session.add(template)
  1274. else:
  1275. # Templates exist - only add missing ones
  1276. for template_data in DEFAULT_TEMPLATES:
  1277. if template_data["event_type"] not in existing_types:
  1278. template = NotificationTemplate(
  1279. event_type=template_data["event_type"],
  1280. name=template_data["name"],
  1281. title_template=template_data["title_template"],
  1282. body_template=template_data["body_template"],
  1283. is_default=True,
  1284. )
  1285. session.add(template)
  1286. await session.commit()
  1287. async def seed_default_groups():
  1288. """Seed default groups and migrate existing users to appropriate groups.
  1289. Creates the default system groups (Administrators, Operators, Viewers) if they
  1290. don't exist, then migrates existing users:
  1291. - Users with role='admin' -> Administrators group
  1292. - Users with role='user' -> Operators group
  1293. Also migrates old permissions to new ownership-based permissions (Issue #205).
  1294. """
  1295. import logging
  1296. from sqlalchemy import select
  1297. from backend.app.core.permissions import DEFAULT_GROUPS
  1298. from backend.app.models.group import Group
  1299. from backend.app.models.user import User
  1300. logger = logging.getLogger(__name__)
  1301. # Map old permissions to new ones for migration
  1302. # Administrators get *_all permissions, Operators get *_own permissions
  1303. PERMISSION_MIGRATION_ALL = {
  1304. "queue:update": "queue:update_all",
  1305. "queue:delete": "queue:delete_all",
  1306. "archives:update": "archives:update_all",
  1307. "archives:delete": "archives:delete_all",
  1308. "archives:reprint": "archives:reprint_all",
  1309. "library:update": "library:update_all",
  1310. "library:delete": "library:delete_all",
  1311. }
  1312. PERMISSION_MIGRATION_OWN = {
  1313. "queue:update": "queue:update_own",
  1314. "queue:delete": "queue:delete_own",
  1315. "archives:update": "archives:update_own",
  1316. "archives:delete": "archives:delete_own",
  1317. "archives:reprint": "archives:reprint_own",
  1318. "library:update": "library:update_own",
  1319. "library:delete": "library:delete_own",
  1320. }
  1321. async with async_session() as session:
  1322. # Get existing groups
  1323. result = await session.execute(select(Group))
  1324. existing_groups = {group.name: group for group in result.scalars().all()}
  1325. # Create default groups if they don't exist
  1326. groups_created = []
  1327. for group_name, group_config in DEFAULT_GROUPS.items():
  1328. if group_name not in existing_groups:
  1329. group = Group(
  1330. name=group_name,
  1331. description=group_config["description"],
  1332. permissions=group_config["permissions"],
  1333. is_system=group_config["is_system"],
  1334. )
  1335. session.add(group)
  1336. groups_created.append(group_name)
  1337. logger.info("Created default group: %s", group_name)
  1338. else:
  1339. # Migrate existing group's permissions from old to new format
  1340. group = existing_groups[group_name]
  1341. if group.permissions:
  1342. updated = False
  1343. new_permissions = list(group.permissions)
  1344. # Determine which migration map to use based on group
  1345. migration_map = (
  1346. PERMISSION_MIGRATION_ALL if group_name == "Administrators" else PERMISSION_MIGRATION_OWN
  1347. )
  1348. for old_perm, new_perm in migration_map.items():
  1349. if old_perm in new_permissions:
  1350. new_permissions.remove(old_perm)
  1351. if new_perm not in new_permissions:
  1352. new_permissions.append(new_perm)
  1353. updated = True
  1354. logger.info(
  1355. "Migrated permission '%s' to '%s' in group '%s'", old_perm, new_perm, group_name
  1356. )
  1357. # For Administrators, also ensure they get *_all permissions if they have any new *_own
  1358. if group_name == "Administrators":
  1359. for _own_perm, all_perm in [
  1360. ("queue:update_own", "queue:update_all"),
  1361. ("queue:delete_own", "queue:delete_all"),
  1362. ("archives:update_own", "archives:update_all"),
  1363. ("archives:delete_own", "archives:delete_all"),
  1364. ("archives:reprint_own", "archives:reprint_all"),
  1365. ("library:update_own", "library:update_all"),
  1366. ("library:delete_own", "library:delete_all"),
  1367. ]:
  1368. # Add *_all if not present
  1369. if all_perm not in new_permissions:
  1370. new_permissions.append(all_perm)
  1371. updated = True
  1372. if updated:
  1373. group.permissions = new_permissions
  1374. await session.commit()
  1375. # Migrate new permissions: grant printers:clear_plate to all groups with printers:control
  1376. result = await session.execute(select(Group))
  1377. all_groups = result.scalars().all()
  1378. for group in all_groups:
  1379. if (
  1380. group.permissions
  1381. and "printers:control" in group.permissions
  1382. and "printers:clear_plate" not in group.permissions
  1383. ):
  1384. group.permissions = [*group.permissions, "printers:clear_plate"]
  1385. logger.info("Added printers:clear_plate to group '%s' (has printers:control)", group.name)
  1386. await session.commit()
  1387. # Migrate existing users to groups if they're not already in any group
  1388. if groups_created:
  1389. # Refresh to get newly created groups
  1390. admin_result = await session.execute(select(Group).where(Group.name == "Administrators"))
  1391. admin_group = admin_result.scalar_one_or_none()
  1392. operators_result = await session.execute(select(Group).where(Group.name == "Operators"))
  1393. operators_group = operators_result.scalar_one_or_none()
  1394. # Get all users
  1395. users_result = await session.execute(select(User))
  1396. users = users_result.scalars().all()
  1397. for user in users:
  1398. # Skip if user already has groups
  1399. if user.groups:
  1400. continue
  1401. if user.role == "admin" and admin_group:
  1402. user.groups.append(admin_group)
  1403. logger.info("Migrated admin user '%s' to Administrators group", user.username)
  1404. elif operators_group:
  1405. user.groups.append(operators_group)
  1406. logger.info("Migrated user '%s' to Operators group", user.username)
  1407. await session.commit()
  1408. async def seed_spool_catalog():
  1409. """Seed the spool catalog with default entries if empty."""
  1410. import logging
  1411. from sqlalchemy import func, select
  1412. from backend.app.core.catalog_defaults import DEFAULT_SPOOL_CATALOG
  1413. from backend.app.models.spool_catalog import SpoolCatalogEntry
  1414. logger = logging.getLogger(__name__)
  1415. async with async_session() as session:
  1416. result = await session.execute(select(func.count()).select_from(SpoolCatalogEntry))
  1417. count = result.scalar() or 0
  1418. if count > 0:
  1419. return # Already seeded
  1420. for name, weight in DEFAULT_SPOOL_CATALOG:
  1421. session.add(SpoolCatalogEntry(name=name, weight=weight, is_default=True))
  1422. await session.commit()
  1423. logger.info("Seeded %d default spool catalog entries", len(DEFAULT_SPOOL_CATALOG))
  1424. async def seed_color_catalog():
  1425. """Seed the color catalog with default entries if empty."""
  1426. import logging
  1427. from sqlalchemy import func, select
  1428. from backend.app.core.catalog_defaults import DEFAULT_COLOR_CATALOG
  1429. from backend.app.models.color_catalog import ColorCatalogEntry
  1430. logger = logging.getLogger(__name__)
  1431. async with async_session() as session:
  1432. result = await session.execute(select(func.count()).select_from(ColorCatalogEntry))
  1433. count = result.scalar() or 0
  1434. if count > 0:
  1435. return # Already seeded
  1436. for manufacturer, color_name, hex_color, material in DEFAULT_COLOR_CATALOG:
  1437. session.add(
  1438. ColorCatalogEntry(
  1439. manufacturer=manufacturer,
  1440. color_name=color_name,
  1441. hex_color=hex_color,
  1442. material=material,
  1443. is_default=True,
  1444. )
  1445. )
  1446. await session.commit()
  1447. logger.info("Seeded %d default color catalog entries", len(DEFAULT_COLOR_CATALOG))