database.py 143 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955
  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. # Recursive strip that walks any nesting of dict/list/tuple. Needed
  43. # because SQLAlchemy passes parameters in several shapes depending
  44. # on the path: a dict for named binds, a tuple for positional, a
  45. # list of dicts/tuples for executemany, and for insertmanyvalues
  46. # sometimes a list of tuples inside an outer list. The simplest
  47. # correct answer is "strip datetimes at any depth".
  48. def _strip(val):
  49. if isinstance(val, datetime.datetime) and val.tzinfo is not None:
  50. return val.replace(tzinfo=None)
  51. if isinstance(val, dict):
  52. return {k: _strip(v) for k, v in val.items()}
  53. if isinstance(val, list):
  54. return [_strip(v) for v in val]
  55. if isinstance(val, tuple):
  56. return tuple(_strip(v) for v in val)
  57. return val
  58. return statement, _strip(parameters)
  59. return eng
  60. engine = _create_engine()
  61. async_session = async_sessionmaker(
  62. engine,
  63. class_=AsyncSession,
  64. expire_on_commit=False,
  65. )
  66. async def run_with_retry(fn, *, max_attempts: int = 3, label: str = ""):
  67. """Run an async DB operation with retry for SQLite 'database is locked' errors.
  68. ``fn`` is an async callable that receives an ``AsyncSession`` and performs
  69. the full query-mutate-commit cycle. On each retry a fresh session is used
  70. so there are no stale-object / expired-attribute issues after rollback.
  71. On PostgreSQL this calls ``fn`` once with no retry (Postgres uses row-level
  72. locking and doesn't suffer from single-writer contention).
  73. """
  74. if not is_sqlite():
  75. async with async_session() as db:
  76. return await fn(db)
  77. last_exc: OperationalError | None = None
  78. for attempt in range(1, max_attempts + 1):
  79. try:
  80. async with async_session() as db:
  81. return await fn(db)
  82. except OperationalError as exc:
  83. last_exc = exc
  84. if "database is locked" not in str(exc) or attempt == max_attempts:
  85. raise
  86. delay = 0.5 * attempt # 0.5s, 1.0s
  87. logger.warning(
  88. "SQLite locked%s (attempt %d/%d), retrying in %.1fs: %s",
  89. f" ({label})" if label else "",
  90. attempt,
  91. max_attempts,
  92. delay,
  93. exc,
  94. )
  95. await asyncio.sleep(delay)
  96. raise last_exc # unreachable, but keeps type checkers happy
  97. async def close_all_connections():
  98. """Close all database connections for backup/restore operations."""
  99. global engine
  100. await engine.dispose()
  101. async def reinitialize_database():
  102. """Reinitialize database connection after restore."""
  103. global engine, async_session
  104. engine = _create_engine()
  105. async_session = async_sessionmaker(
  106. engine,
  107. class_=AsyncSession,
  108. expire_on_commit=False,
  109. )
  110. class Base(DeclarativeBase):
  111. pass
  112. async def get_db() -> AsyncSession:
  113. async with async_session() as session:
  114. try:
  115. yield session
  116. await session.commit()
  117. except BaseException:
  118. # Catch BaseException (not just Exception) so CancelledError —
  119. # raised when Starlette's BaseHTTPMiddleware cancels the inner
  120. # task scope on client disconnect — also triggers rollback.
  121. # `asyncio.shield` keeps the rollback running to completion
  122. # even when the await itself gets cancelled, so the SQLite
  123. # write lock is released promptly instead of being held until
  124. # the connection is GC'd ages later (which was producing the
  125. # "database is locked" cascade in #1112's support package).
  126. try:
  127. await asyncio.shield(session.rollback())
  128. except BaseException: # noqa: BLE001 — rollback failure must not mask the original
  129. pass
  130. raise
  131. finally:
  132. try:
  133. await asyncio.shield(session.close())
  134. except BaseException: # noqa: BLE001 — close failure must not mask the original
  135. pass
  136. async def init_db():
  137. # Import models to register them with SQLAlchemy
  138. from backend.app.models import ( # noqa: F401
  139. active_print_spoolman,
  140. ams_history,
  141. ams_label,
  142. api_key,
  143. archive,
  144. auth_ephemeral,
  145. bug_report,
  146. color_catalog,
  147. external_link,
  148. filament,
  149. filament_sku_settings,
  150. github_backup,
  151. group,
  152. kprofile_note,
  153. library,
  154. local_preset,
  155. long_lived_token,
  156. maintenance,
  157. notification,
  158. notification_template,
  159. oidc_provider,
  160. orca_base_cache,
  161. pending_upload,
  162. print_batch,
  163. print_log,
  164. print_queue,
  165. printer,
  166. project,
  167. project_bom,
  168. settings,
  169. shopping_list,
  170. slot_preset,
  171. smart_plug,
  172. smart_plug_energy_snapshot,
  173. spool,
  174. spool_assignment,
  175. spool_catalog,
  176. spool_k_profile,
  177. spool_usage_history,
  178. spoolbuddy_device,
  179. spoolman_k_profile,
  180. spoolman_slot_assignment,
  181. user,
  182. user_email_pref,
  183. user_otp_code,
  184. user_totp,
  185. virtual_printer,
  186. )
  187. async with engine.begin() as conn:
  188. await conn.run_sync(Base.metadata.create_all)
  189. # Run migrations for new columns (SQLite doesn't auto-add columns)
  190. await run_migrations(conn)
  191. # Re-encrypt any legacy plaintext OIDC client_secret / TOTP secret rows
  192. # that exist from before the encryption key was configured.
  193. # Runs on a fresh AsyncSession (NOT the run_migrations() connection) so it
  194. # doesn't share a transaction with the schema-DDL block above — required to
  195. # avoid SQLite "database is locked" contention on the WAL writer.
  196. await _migrate_encrypt_legacy_secrets()
  197. # Seed default notification templates
  198. await seed_notification_templates()
  199. # Seed default groups and migrate existing users
  200. await seed_default_groups()
  201. # Seed default catalog entries
  202. await seed_spool_catalog()
  203. await seed_color_catalog()
  204. # B2: Module-level counter exposing the number of rows skipped during the last
  205. # _migrate_encrypt_legacy_secrets() invocation. Surfaced via /encryption-status
  206. # (migration_error_count) so operators can spot poison rows that need attention.
  207. _migration_error_count: int = 0
  208. def get_migration_error_count() -> int:
  209. """Return the number of rows that failed to re-encrypt during the last
  210. _migrate_encrypt_legacy_secrets() run."""
  211. return _migration_error_count
  212. async def _migrate_encrypt_legacy_secrets() -> None:
  213. """Re-encrypt OIDC ``client_secret`` and TOTP ``secret`` rows that are still
  214. stored as plaintext (no ``fernet:`` prefix).
  215. Called from :func:`init_db` after :func:`run_migrations` finishes. No-ops
  216. when no encryption key is configured (so plaintext storage stays the
  217. legacy behaviour for installs without a key).
  218. B2: per-row strategy — each row is committed in its own AsyncSession so a
  219. single corrupt row does NOT block other successful re-encryptions on every
  220. startup forever. The skipped-row count is exposed via
  221. :func:`get_migration_error_count` and surfaced on /encryption-status.
  222. B3: unexpected (non-row) failures during the read phase are re-raised so
  223. operators see the problem instead of silent data corruption — startup
  224. fails loudly rather than running with half-migrated rows.
  225. Idempotent: rows that already start with ``fernet:`` are skipped, and the
  226. write-phase re-checks the prefix before encrypting (guards against double
  227. encryption from concurrent workers).
  228. """
  229. from sqlalchemy import not_, select
  230. from backend.app.core.encryption import is_encryption_active
  231. from backend.app.models.oidc_provider import OIDCProvider
  232. from backend.app.models.user_totp import UserTOTP
  233. global _migration_error_count
  234. if not is_encryption_active():
  235. # Reset stale counter from a previous active-key run — we no longer
  236. # have any rows to migrate, so the count must not leak across runs.
  237. _migration_error_count = 0
  238. return
  239. # Phase 1 (read): collect (id, stored_value) tuples for plaintext rows.
  240. # Read phase failures are startup-fatal — re-raise (B3).
  241. try:
  242. async with async_session() as ro:
  243. oidc_rows = await ro.execute(
  244. select(OIDCProvider.id, OIDCProvider._client_secret_enc).where(
  245. not_(OIDCProvider._client_secret_enc.like("fernet:%"))
  246. )
  247. )
  248. oidc_candidates = [(r[0], r[1]) for r in oidc_rows.all()]
  249. totp_rows = await ro.execute(
  250. select(UserTOTP.id, UserTOTP._secret_enc).where(not_(UserTOTP._secret_enc.like("fernet:%")))
  251. )
  252. totp_candidates = [(r[0], r[1]) for r in totp_rows.all()]
  253. except Exception:
  254. logger.error("_migrate_encrypt_legacy_secrets: phase 1 read failed", exc_info=True)
  255. raise # B3
  256. oidc_count = totp_count = error_count = 0
  257. # Phase 2 (write): each row in its own AsyncSession + transaction.
  258. # Failure of one row does NOT block the others.
  259. for oidc_id, stored in oidc_candidates:
  260. if not stored:
  261. continue # defensive: skip empty strings
  262. try:
  263. async with async_session() as wr:
  264. provider = await wr.get(OIDCProvider, oidc_id)
  265. if provider is None:
  266. continue # row deleted between phase 1 and phase 2
  267. # Idempotent guard: re-check inside the write session in case a
  268. # concurrent worker beat us to it.
  269. if not provider._client_secret_enc.startswith("fernet:"):
  270. provider.client_secret = stored # setter -> mfa_encrypt
  271. await wr.commit()
  272. oidc_count += 1
  273. except Exception:
  274. logger.error(
  275. "Failed to re-encrypt OIDCProvider id=%s — skipping",
  276. oidc_id,
  277. exc_info=True,
  278. )
  279. error_count += 1
  280. for totp_id, stored in totp_candidates:
  281. if not stored:
  282. continue
  283. try:
  284. async with async_session() as wr:
  285. totp = await wr.get(UserTOTP, totp_id)
  286. if totp is None:
  287. continue
  288. if not totp._secret_enc.startswith("fernet:"):
  289. totp.secret = stored
  290. await wr.commit()
  291. totp_count += 1
  292. except Exception:
  293. logger.error(
  294. "Failed to re-encrypt UserTOTP id=%s — skipping",
  295. totp_id,
  296. exc_info=True,
  297. )
  298. error_count += 1
  299. _migration_error_count = error_count
  300. if oidc_count or totp_count:
  301. logger.info(
  302. "Re-encrypted legacy plaintext secrets: %d OIDC client_secret(s), %d TOTP secret(s)",
  303. oidc_count,
  304. totp_count,
  305. )
  306. elif error_count == 0:
  307. logger.debug("_migrate_encrypt_legacy_secrets: no rows needed re-encryption")
  308. if error_count:
  309. logger.error(
  310. "_migrate_encrypt_legacy_secrets: %d row(s) skipped due to errors. "
  311. "See /api/v1/auth/encryption-status (migration_error_count).",
  312. error_count,
  313. )
  314. async def _safe_execute(conn, sql):
  315. """Execute a DDL migration statement, silently ignoring idempotency errors.
  316. 'already exists', 'duplicate column name' (SQLite ADD COLUMN), 'no such column'
  317. (SQLite RENAME COLUMN), 'duplicate key', and the compound
  318. 'column … does not exist' (PostgreSQL RENAME COLUMN idempotency) are swallowed
  319. so that re-running DDL migrations is safe. The compound check additionally
  320. requires the SQL to be a RENAME COLUMN statement so that "does not exist" errors
  321. from ADD COLUMN or CREATE INDEX (which would indicate schema corruption, not
  322. idempotency) are never silently swallowed.
  323. Any other error is logged and re-raised — callers must not assume silent
  324. recovery, as a failure will abort the migration sequence and prevent
  325. application startup.
  326. Only use for DDL statements (ALTER TABLE, CREATE INDEX, etc.).
  327. For DML backfills (UPDATE, DELETE) use conn.execute() directly inside
  328. async with conn.begin_nested() so failures are never silently swallowed.
  329. Uses a savepoint so that a failed statement doesn't poison the surrounding
  330. transaction (required for PostgreSQL).
  331. """
  332. from sqlalchemy import text
  333. try:
  334. async with conn.begin_nested():
  335. await conn.execute(text(sql))
  336. except (OperationalError, ProgrammingError) as exc:
  337. msg = str(exc).lower()
  338. # Only swallow "column … does not exist" for RENAME COLUMN — not for ADD COLUMN
  339. # or CREATE INDEX where it would indicate schema corruption, not idempotency.
  340. column_not_exists = "rename column" in sql.lower() and "column" in msg and "does not exist" in msg
  341. if (
  342. not any(k in msg for k in ("already exists", "duplicate key", "duplicate column name", "no such column"))
  343. and not column_not_exists
  344. ):
  345. logger.error("Migration statement failed: %s | SQL: %.200s", exc, sql)
  346. raise
  347. async def _migrate_normalize_printer_ids(conn) -> None:
  348. from sqlalchemy import text
  349. async with conn.begin_nested():
  350. if is_sqlite():
  351. await conn.execute(text("UPDATE api_keys SET printer_ids = NULL WHERE printer_ids = '[]'"))
  352. else:
  353. await conn.execute(text("UPDATE api_keys SET printer_ids = NULL WHERE printer_ids::text = '[]'"))
  354. async def _migrate_drop_library_print_name(conn) -> None:
  355. """Strip the embedded 3MF Title (``print_name``) from library file metadata (#1489).
  356. Library files stored the 3MF's ``<metadata name="Title">`` as
  357. ``file_metadata.print_name`` — generic ("Exported 3D Model") for Bambu
  358. Studio exports, a marketing title for MakerWorld downloads — and the
  359. FileManager wrongly preferred it over the filename for the card label,
  360. search and sort. New imports no longer store it; this clears it from rows
  361. imported before the fix so existing libraries don't need a rename
  362. round-trip. Idempotent — rows without the key are untouched.
  363. """
  364. from sqlalchemy import text
  365. async with conn.begin_nested():
  366. if is_sqlite():
  367. await conn.execute(
  368. text(
  369. "UPDATE library_files SET file_metadata = json_remove(file_metadata, '$.print_name') "
  370. "WHERE json_extract(file_metadata, '$.print_name') IS NOT NULL"
  371. )
  372. )
  373. else:
  374. # file_metadata is a JSON (not JSONB) column — cast to jsonb for the
  375. # key-exists test (jsonb_exists, avoiding the `?` operator which
  376. # clashes with driver parameter syntax) and the `- key` removal.
  377. await conn.execute(
  378. text(
  379. "UPDATE library_files SET file_metadata = (file_metadata::jsonb - 'print_name')::json "
  380. "WHERE jsonb_exists(file_metadata::jsonb, 'print_name')"
  381. )
  382. )
  383. async def _migrate_update_auto_link_constraint(conn) -> None:
  384. """Update the auto_link CHECK constraint to allow Fall C (custom email claim).
  385. Old formula: auto_link = FALSE OR (require_ev = TRUE AND email_claim = 'email')
  386. New formula: auto_link = FALSE OR email_claim != 'email' OR require_ev = TRUE
  387. Only Fall B (email_claim='email' + require_ev=False) remains blocked.
  388. Fall C (custom claim, e.g. Azure preferred_username/upn) is now allowed.
  389. PostgreSQL: DROP CONSTRAINT IF EXISTS + ADD new formula via _safe_execute (idempotent).
  390. SQLite: table recreation when old formula is detected in sqlite_master (idempotent).
  391. """
  392. from sqlalchemy import text
  393. _NEW_FORMULA = "auto_link_existing_accounts = FALSE OR email_claim != 'email' OR require_email_verified = TRUE"
  394. _CONSTRAINT_NAME = "ck_auto_link_requires_verified_email_claim"
  395. if not is_sqlite():
  396. await _safe_execute(conn, f"ALTER TABLE oidc_providers DROP CONSTRAINT IF EXISTS {_CONSTRAINT_NAME}")
  397. await _safe_execute(
  398. conn,
  399. f"ALTER TABLE oidc_providers ADD CONSTRAINT {_CONSTRAINT_NAME} CHECK ({_NEW_FORMULA})",
  400. )
  401. else:
  402. row = (
  403. await conn.execute(text("SELECT sql FROM sqlite_master WHERE type='table' AND name='oidc_providers'"))
  404. ).fetchone()
  405. # Only recreate if the old (more restrictive) formula is still present.
  406. # Fresh installs created with the new __table_args__ already have the correct formula.
  407. # Installs without any constraint (pre-SEC-1 upgrades) are skipped — app-level guards suffice.
  408. if row and "require_email_verified = TRUE AND email_claim = 'email'" in row[0]:
  409. try:
  410. async with conn.begin_nested():
  411. await conn.execute(text("DROP TABLE IF EXISTS oidc_providers_v2"))
  412. await conn.execute(
  413. text(
  414. "CREATE TABLE oidc_providers_v2 ("
  415. "id INTEGER NOT NULL, "
  416. "name VARCHAR(100) NOT NULL, "
  417. "issuer_url VARCHAR(500) NOT NULL, "
  418. "client_id VARCHAR(255) NOT NULL, "
  419. "client_secret VARCHAR(512) NOT NULL, "
  420. "scopes VARCHAR(500), "
  421. "is_enabled BOOLEAN, "
  422. "auto_create_users BOOLEAN, "
  423. "auto_link_existing_accounts BOOLEAN DEFAULT 0, "
  424. "email_claim VARCHAR(64) DEFAULT 'email', "
  425. "require_email_verified BOOLEAN DEFAULT 1, "
  426. "icon_url TEXT, "
  427. "created_at DATETIME DEFAULT CURRENT_TIMESTAMP, "
  428. "updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, "
  429. "PRIMARY KEY (id), "
  430. f"UNIQUE (name), "
  431. f"CONSTRAINT {_CONSTRAINT_NAME} CHECK ({_NEW_FORMULA})"
  432. ")"
  433. )
  434. )
  435. await conn.execute(
  436. text(
  437. "INSERT INTO oidc_providers_v2 "
  438. "(id, name, issuer_url, client_id, client_secret, scopes, is_enabled, "
  439. "auto_create_users, auto_link_existing_accounts, email_claim, "
  440. "require_email_verified, icon_url, created_at, updated_at) "
  441. "SELECT id, name, issuer_url, client_id, client_secret, scopes, is_enabled, "
  442. "auto_create_users, auto_link_existing_accounts, email_claim, "
  443. "require_email_verified, icon_url, created_at, updated_at "
  444. "FROM oidc_providers"
  445. )
  446. )
  447. original = (await conn.execute(text("SELECT count(*) FROM oidc_providers"))).scalar_one()
  448. copied = (await conn.execute(text("SELECT count(*) FROM oidc_providers_v2"))).scalar_one()
  449. if copied != original:
  450. raise RuntimeError(
  451. f"auto_link constraint migration: row count mismatch after copy "
  452. f"({original} in source, {copied} in copy)"
  453. )
  454. await conn.execute(text("DROP TABLE oidc_providers"))
  455. await conn.execute(text("ALTER TABLE oidc_providers_v2 RENAME TO oidc_providers"))
  456. except Exception as exc:
  457. logger.error(
  458. "auto_link constraint update (SQLite table recreation) FAILED: %s",
  459. exc,
  460. exc_info=True,
  461. )
  462. raise
  463. async def _migrate_widen_spoolman_slot_ams_id_range(conn) -> None:
  464. """Widen ck_ams_id_range on spoolman_slot_assignments to admit AMS-HT (#1274).
  465. Old formula: (ams_id >= 0 AND ams_id <= 7) OR ams_id = 255
  466. New formula: (ams_id >= 0 AND ams_id <= 7) OR (ams_id >= 128 AND ams_id <= 191) OR ams_id = 255
  467. The H2C/H2D AMS-HT reports ams_id 128+. The old constraint rejected every
  468. AMS-HT slot link with `IntegrityError: CHECK constraint failed: ck_ams_id_range`.
  469. PostgreSQL: DROP CONSTRAINT IF EXISTS + ADD new formula via _safe_execute.
  470. SQLite: table recreation when the old (narrower) formula is detected in
  471. sqlite_master. Fresh installs already have the widened constraint from
  472. the CREATE TABLE migration above.
  473. """
  474. from sqlalchemy import text
  475. _NEW_FORMULA = "(ams_id >= 0 AND ams_id <= 7) OR (ams_id >= 128 AND ams_id <= 191) OR ams_id = 255"
  476. _CONSTRAINT_NAME = "ck_ams_id_range"
  477. if not is_sqlite():
  478. await _safe_execute(
  479. conn,
  480. f"ALTER TABLE spoolman_slot_assignments DROP CONSTRAINT IF EXISTS {_CONSTRAINT_NAME}",
  481. )
  482. await _safe_execute(
  483. conn,
  484. f"ALTER TABLE spoolman_slot_assignments ADD CONSTRAINT {_CONSTRAINT_NAME} CHECK ({_NEW_FORMULA})",
  485. )
  486. return
  487. row = (
  488. await conn.execute(
  489. text("SELECT sql FROM sqlite_master WHERE type='table' AND name='spoolman_slot_assignments'")
  490. )
  491. ).fetchone()
  492. if not row:
  493. return
  494. sql = row[0] or ""
  495. # Already widened by an earlier run or by the fresh-install CREATE TABLE above.
  496. if "ams_id >= 128" in sql:
  497. return
  498. # Pre-migration table without any CHECK constraint at all → leave alone;
  499. # the app-level validation handles correctness and we don't risk a
  500. # destructive table rebuild for a constraint that isn't blocking anyone.
  501. if "ck_ams_id_range" not in sql and "ams_id <= 7" not in sql:
  502. return
  503. try:
  504. async with conn.begin_nested():
  505. await conn.execute(text("DROP TABLE IF EXISTS spoolman_slot_assignments_v2"))
  506. await conn.execute(
  507. text(
  508. "CREATE TABLE spoolman_slot_assignments_v2 ("
  509. "id INTEGER PRIMARY KEY AUTOINCREMENT, "
  510. "printer_id INTEGER NOT NULL REFERENCES printers(id) ON DELETE CASCADE, "
  511. f"ams_id INTEGER NOT NULL CHECK ({_NEW_FORMULA}), "
  512. "tray_id INTEGER NOT NULL CHECK (tray_id >= 0 AND tray_id <= 3), "
  513. "spoolman_spool_id INTEGER NOT NULL, "
  514. "assigned_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, "
  515. "CONSTRAINT uq_slot_assignment UNIQUE(printer_id, ams_id, tray_id)"
  516. ")"
  517. )
  518. )
  519. await conn.execute(
  520. text(
  521. "INSERT INTO spoolman_slot_assignments_v2 "
  522. "(id, printer_id, ams_id, tray_id, spoolman_spool_id, assigned_at) "
  523. "SELECT id, printer_id, ams_id, tray_id, spoolman_spool_id, assigned_at "
  524. "FROM spoolman_slot_assignments"
  525. )
  526. )
  527. original = (await conn.execute(text("SELECT count(*) FROM spoolman_slot_assignments"))).scalar_one()
  528. copied = (await conn.execute(text("SELECT count(*) FROM spoolman_slot_assignments_v2"))).scalar_one()
  529. if copied != original:
  530. raise RuntimeError(
  531. f"spoolman_slot_assignments migration: row count mismatch after copy "
  532. f"({original} in source, {copied} in copy)"
  533. )
  534. await conn.execute(text("DROP TABLE spoolman_slot_assignments"))
  535. await conn.execute(text("ALTER TABLE spoolman_slot_assignments_v2 RENAME TO spoolman_slot_assignments"))
  536. # The index sits on the renamed table; recreate it idempotently
  537. # to handle older sqlite versions that don't auto-rename indexes.
  538. await conn.execute(
  539. text(
  540. "CREATE INDEX IF NOT EXISTS ix_slot_assignment_spool "
  541. "ON spoolman_slot_assignments (spoolman_spool_id)"
  542. )
  543. )
  544. except Exception as exc:
  545. logger.error(
  546. "spoolman_slot_assignments ck_ams_id_range widening (SQLite table recreation) FAILED: %s",
  547. exc,
  548. exc_info=True,
  549. )
  550. raise
  551. async def run_migrations(conn):
  552. """Run all schema migrations and data backfills on startup.
  553. Includes ALTER TABLE (add columns, rename columns, add constraints),
  554. CREATE INDEX, CREATE TRIGGER, data UPDATE backfills, and table recreations
  555. for complex SQLite schema changes that ALTER TABLE cannot handle.
  556. DDL statements are wrapped in _safe_execute for idempotency.
  557. DML backfills (UPDATE/DELETE) are executed directly via conn.execute()
  558. inside begin_nested() so any failure is always fatal and never silently
  559. swallowed.
  560. """
  561. from sqlalchemy import text
  562. # Migration: Add is_favorite column to print_archives
  563. await _safe_execute(conn, "ALTER TABLE print_archives ADD COLUMN is_favorite BOOLEAN DEFAULT 0")
  564. # Migration: Add content_hash column to print_archives for duplicate detection
  565. await _safe_execute(conn, "ALTER TABLE print_archives ADD COLUMN content_hash VARCHAR(64)")
  566. # Migration: Add auto_off_executed column to smart_plugs
  567. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN auto_off_executed BOOLEAN DEFAULT 0")
  568. # Migration: Add on_print_stopped column to notification_providers
  569. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN on_print_stopped BOOLEAN DEFAULT 1")
  570. # Migration: Add source_3mf_path column to print_archives
  571. await _safe_execute(conn, "ALTER TABLE print_archives ADD COLUMN source_3mf_path VARCHAR(500)")
  572. # Migration: Add f3d_path column to print_archives for Fusion 360 design files
  573. await _safe_execute(conn, "ALTER TABLE print_archives ADD COLUMN f3d_path VARCHAR(500)")
  574. # Migration: Add on_maintenance_due column to notification_providers
  575. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN on_maintenance_due BOOLEAN DEFAULT 0")
  576. # Migration: Add location column to printers for grouping
  577. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN location VARCHAR(100)")
  578. # Migration: Add interval_type column to maintenance_types
  579. await _safe_execute(conn, "ALTER TABLE maintenance_types ADD COLUMN interval_type VARCHAR(20) DEFAULT 'hours'")
  580. # Migration: Add is_deleted column to maintenance_types for soft-deletes
  581. await _safe_execute(conn, "ALTER TABLE maintenance_types ADD COLUMN is_deleted BOOLEAN DEFAULT 0")
  582. # Migration: Add custom_interval_type column to printer_maintenance
  583. await _safe_execute(conn, "ALTER TABLE printer_maintenance ADD COLUMN custom_interval_type VARCHAR(20)")
  584. # Migration: Add power alert columns to smart_plugs
  585. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN power_alert_enabled BOOLEAN DEFAULT 0")
  586. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN power_alert_high REAL")
  587. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN power_alert_low REAL")
  588. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN power_alert_last_triggered DATETIME")
  589. # Migration: Add schedule columns to smart_plugs
  590. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN schedule_enabled BOOLEAN DEFAULT 0")
  591. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN schedule_on_time VARCHAR(5)")
  592. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN schedule_off_time VARCHAR(5)")
  593. # Migration: Add daily digest columns to notification_providers
  594. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN daily_digest_enabled BOOLEAN DEFAULT 0")
  595. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN daily_digest_time VARCHAR(5)")
  596. # Migration: Add missing-spool-assignment print-start notification toggle
  597. try:
  598. async with conn.begin_nested():
  599. await conn.execute(
  600. text(
  601. "ALTER TABLE notification_providers ADD COLUMN on_print_missing_spool_assignment BOOLEAN DEFAULT 0"
  602. )
  603. )
  604. except (OperationalError, ProgrammingError):
  605. pass # Already applied
  606. # Migration: Add project_id column to print_archives
  607. try:
  608. async with conn.begin_nested():
  609. await conn.execute(
  610. text(
  611. "ALTER TABLE print_archives ADD COLUMN project_id INTEGER REFERENCES projects(id) ON DELETE SET NULL"
  612. )
  613. )
  614. except (OperationalError, ProgrammingError):
  615. pass # Already applied
  616. # Migration: Add project_id column to print_queue
  617. try:
  618. async with conn.begin_nested():
  619. await conn.execute(
  620. text("ALTER TABLE print_queue ADD COLUMN project_id INTEGER REFERENCES projects(id) ON DELETE SET NULL")
  621. )
  622. except (OperationalError, ProgrammingError):
  623. pass # Already applied
  624. # Migration: Enforce uniqueness on user_oidc_links for existing rows.
  625. # create_all() is idempotent and does not add constraints to existing tables,
  626. # so we create covering unique indexes explicitly here.
  627. await _safe_execute(
  628. conn,
  629. "CREATE UNIQUE INDEX IF NOT EXISTS uq_oidc_link_provider_sub"
  630. " ON user_oidc_links (provider_id, provider_user_id)",
  631. )
  632. await _safe_execute(
  633. conn,
  634. "CREATE UNIQUE INDEX IF NOT EXISTS uq_oidc_link_user_provider ON user_oidc_links (user_id, provider_id)",
  635. )
  636. # Migration: Create FTS5 virtual table for archive full-text search (SQLite only)
  637. # PostgreSQL uses tsvector + GIN index instead (set up in archives.py search route)
  638. if is_sqlite():
  639. try:
  640. await conn.execute(
  641. text("""
  642. CREATE VIRTUAL TABLE IF NOT EXISTS archive_fts USING fts5(
  643. print_name,
  644. filename,
  645. tags,
  646. notes,
  647. designer,
  648. filament_type,
  649. content='print_archives',
  650. content_rowid='id'
  651. )
  652. """)
  653. )
  654. except (OperationalError, ProgrammingError):
  655. pass # Already applied
  656. # Migration: Create triggers to keep FTS index in sync
  657. try:
  658. await conn.execute(
  659. text("""
  660. CREATE TRIGGER IF NOT EXISTS archive_fts_insert AFTER INSERT ON print_archives BEGIN
  661. INSERT INTO archive_fts(rowid, print_name, filename, tags, notes, designer, filament_type)
  662. VALUES (new.id, new.print_name, new.filename, new.tags, new.notes, new.designer, new.filament_type);
  663. END
  664. """)
  665. )
  666. except (OperationalError, ProgrammingError):
  667. pass # Already applied
  668. try:
  669. await conn.execute(
  670. text("""
  671. CREATE TRIGGER IF NOT EXISTS archive_fts_delete AFTER DELETE ON print_archives BEGIN
  672. INSERT INTO archive_fts(archive_fts, rowid, print_name, filename, tags, notes, designer, filament_type)
  673. VALUES ('delete', old.id, old.print_name, old.filename, old.tags, old.notes, old.designer, old.filament_type);
  674. END
  675. """)
  676. )
  677. except (OperationalError, ProgrammingError):
  678. pass # Already applied
  679. try:
  680. await conn.execute(
  681. text("""
  682. CREATE TRIGGER IF NOT EXISTS archive_fts_update AFTER UPDATE ON print_archives BEGIN
  683. INSERT INTO archive_fts(archive_fts, rowid, print_name, filename, tags, notes, designer, filament_type)
  684. VALUES ('delete', old.id, old.print_name, old.filename, old.tags, old.notes, old.designer, old.filament_type);
  685. INSERT INTO archive_fts(rowid, print_name, filename, tags, notes, designer, filament_type)
  686. VALUES (new.id, new.print_name, new.filename, new.tags, new.notes, new.designer, new.filament_type);
  687. END
  688. """)
  689. )
  690. except (OperationalError, ProgrammingError):
  691. pass # Already applied
  692. # Migration: Add auto_off_pending columns to smart_plugs (for restart recovery)
  693. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN auto_off_pending BOOLEAN DEFAULT 0")
  694. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN auto_off_pending_since DATETIME")
  695. # Migration: Add auto_off_persistent column to smart_plugs (keep auto-off enabled between prints)
  696. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN auto_off_persistent BOOLEAN DEFAULT 0")
  697. # Migration: Add AMS alarm notification columns to notification_providers
  698. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN on_ams_humidity_high BOOLEAN DEFAULT 0")
  699. try:
  700. async with conn.begin_nested():
  701. await conn.execute(
  702. text("ALTER TABLE notification_providers ADD COLUMN on_ams_temperature_high BOOLEAN DEFAULT 0")
  703. )
  704. except (OperationalError, ProgrammingError):
  705. pass # Already applied
  706. # Migration: Add AMS-HT alarm notification columns to notification_providers
  707. try:
  708. async with conn.begin_nested():
  709. await conn.execute(
  710. text("ALTER TABLE notification_providers ADD COLUMN on_ams_ht_humidity_high BOOLEAN DEFAULT 0")
  711. )
  712. except (OperationalError, ProgrammingError):
  713. pass # Already applied
  714. try:
  715. async with conn.begin_nested():
  716. await conn.execute(
  717. text("ALTER TABLE notification_providers ADD COLUMN on_ams_ht_temperature_high BOOLEAN DEFAULT 0")
  718. )
  719. except (OperationalError, ProgrammingError):
  720. pass # Already applied
  721. # Migration: Add plate not empty notification column to notification_providers
  722. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN on_plate_not_empty BOOLEAN DEFAULT 1")
  723. # Migration: Add notes column to projects (Phase 2)
  724. await _safe_execute(conn, "ALTER TABLE projects ADD COLUMN notes TEXT")
  725. # Migration: Add attachments column to projects (Phase 3)
  726. await _safe_execute(conn, "ALTER TABLE projects ADD COLUMN attachments JSON")
  727. # Migration: Add tags column to projects (Phase 4)
  728. await _safe_execute(conn, "ALTER TABLE projects ADD COLUMN tags TEXT")
  729. # Migration: Add due_date column to projects (Phase 5)
  730. await _safe_execute(conn, "ALTER TABLE projects ADD COLUMN due_date DATETIME")
  731. # Migration: Add priority column to projects (Phase 5)
  732. await _safe_execute(conn, "ALTER TABLE projects ADD COLUMN priority VARCHAR(20) DEFAULT 'normal'")
  733. # Migration: Add budget column to projects (Phase 6)
  734. await _safe_execute(conn, "ALTER TABLE projects ADD COLUMN budget REAL")
  735. # Migration: Add is_template column to projects (Phase 8)
  736. await _safe_execute(conn, "ALTER TABLE projects ADD COLUMN is_template BOOLEAN DEFAULT 0")
  737. # Migration: Add template_source_id column to projects (Phase 8)
  738. await _safe_execute(conn, "ALTER TABLE projects ADD COLUMN template_source_id INTEGER")
  739. # Migration: Add parent_id column to projects (Phase 10)
  740. try:
  741. async with conn.begin_nested():
  742. await conn.execute(
  743. text("ALTER TABLE projects ADD COLUMN parent_id INTEGER REFERENCES projects(id) ON DELETE SET NULL")
  744. )
  745. except (OperationalError, ProgrammingError):
  746. pass # Already applied
  747. # Migration: Rename quantity_printed to quantity_acquired in project_bom_items
  748. await _safe_execute(conn, "ALTER TABLE project_bom_items RENAME COLUMN quantity_printed TO quantity_acquired")
  749. # Migration: Add unit_price column to project_bom_items
  750. await _safe_execute(conn, "ALTER TABLE project_bom_items ADD COLUMN unit_price REAL")
  751. # Migration: Add sourcing_url column to project_bom_items
  752. await _safe_execute(conn, "ALTER TABLE project_bom_items ADD COLUMN sourcing_url VARCHAR(512)")
  753. # Migration: Rename notes to remarks in project_bom_items
  754. await _safe_execute(conn, "ALTER TABLE project_bom_items RENAME COLUMN notes TO remarks")
  755. # Migration: Add show_in_switchbar column to smart_plugs
  756. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN show_in_switchbar BOOLEAN DEFAULT 0")
  757. # Migration: Add runtime tracking columns to printers
  758. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN runtime_seconds INTEGER DEFAULT 0")
  759. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN last_runtime_update DATETIME")
  760. # Migration: Add quantity column to print_archives for tracking item count
  761. await _safe_execute(conn, "ALTER TABLE print_archives ADD COLUMN quantity INTEGER DEFAULT 1")
  762. # Migration: Add manual_start column to print_queue for staged prints
  763. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN manual_start BOOLEAN DEFAULT 0")
  764. # Migration: Add wiki_url column to maintenance_types for documentation links
  765. await _safe_execute(conn, "ALTER TABLE maintenance_types ADD COLUMN wiki_url VARCHAR(500)")
  766. # Migration: Add tailscale_disabled column to virtual_printers. Opt-in: default TRUE so
  767. # the auto-detect + fallback noise only runs for users who explicitly enable it.
  768. # Postgres rejects `DEFAULT 1` for BOOLEAN (#1070 round-2 review).
  769. if is_sqlite():
  770. await _safe_execute(conn, "ALTER TABLE virtual_printers ADD COLUMN tailscale_disabled BOOLEAN DEFAULT 1")
  771. else:
  772. await _safe_execute(conn, "ALTER TABLE virtual_printers ADD COLUMN tailscale_disabled BOOLEAN DEFAULT true")
  773. # Migration: Add ams_mapping column to print_queue for storing filament slot assignments
  774. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN ams_mapping TEXT")
  775. # Migration: Add queue_force_color_match column to virtual_printers (#1188).
  776. # Opt-in flag: when true, VP queue-mode uploads pin the per-slot type+color
  777. # from the 3MF onto the queue item's filament_overrides so the scheduler
  778. # refuses to dispatch onto a printer with the wrong filament loaded.
  779. # Default false to preserve current behaviour for upgraders.
  780. if is_sqlite():
  781. await _safe_execute(conn, "ALTER TABLE virtual_printers ADD COLUMN queue_force_color_match BOOLEAN DEFAULT 0")
  782. else:
  783. await _safe_execute(
  784. conn, "ALTER TABLE virtual_printers ADD COLUMN queue_force_color_match BOOLEAN DEFAULT FALSE"
  785. )
  786. # Migration: Add target_parts_count column to projects for tracking total parts needed
  787. await _safe_execute(conn, "ALTER TABLE projects ADD COLUMN target_parts_count INTEGER")
  788. # Migration: Add url + cover_image_filename columns to projects (#1155).
  789. # url: external link rendered next to the project name on the card.
  790. # cover_image_filename: filename of the project's hero image inside the
  791. # existing attachments dir; rendered as a thumbnail on the card.
  792. await _safe_execute(conn, "ALTER TABLE projects ADD COLUMN url VARCHAR(2048)")
  793. await _safe_execute(conn, "ALTER TABLE projects ADD COLUMN cover_image_filename VARCHAR(255)")
  794. # Migration: enhanced filament colour handling on color_catalog (#1154).
  795. # Mirrors the Spool columns added below; widens hex_color to VARCHAR(9)
  796. # so catalog entries can store an alpha component (#RRGGBBAA). SQLite
  797. # ignores VARCHAR length, so the widen only matters on PostgreSQL.
  798. await _safe_execute(conn, "ALTER TABLE color_catalog ADD COLUMN extra_colors VARCHAR(255)")
  799. await _safe_execute(conn, "ALTER TABLE color_catalog ADD COLUMN effect_type VARCHAR(20)")
  800. if not is_sqlite():
  801. await _safe_execute(conn, "ALTER TABLE color_catalog ALTER COLUMN hex_color TYPE VARCHAR(9)")
  802. # Migration: Make printer_id nullable in print_queue for unassigned queue items
  803. # SQLite doesn't support ALTER COLUMN, so we need to recreate the table
  804. # PostgreSQL gets the correct schema from create_all(), so skip this
  805. if is_sqlite():
  806. try:
  807. result = await conn.execute(text("SELECT sql FROM sqlite_master WHERE type='table' AND name='print_queue'"))
  808. row = result.fetchone()
  809. if row and "printer_id INTEGER NOT NULL" in (row[0] or ""):
  810. await conn.execute(
  811. text("""
  812. CREATE TABLE print_queue_new (
  813. id INTEGER PRIMARY KEY,
  814. printer_id INTEGER REFERENCES printers(id) ON DELETE CASCADE,
  815. archive_id INTEGER NOT NULL REFERENCES print_archives(id) ON DELETE CASCADE,
  816. project_id INTEGER REFERENCES projects(id) ON DELETE SET NULL,
  817. position INTEGER DEFAULT 0,
  818. scheduled_time DATETIME,
  819. manual_start BOOLEAN DEFAULT 0,
  820. require_previous_success BOOLEAN DEFAULT 0,
  821. auto_off_after BOOLEAN DEFAULT 0,
  822. ams_mapping TEXT,
  823. status VARCHAR(20) DEFAULT 'pending',
  824. started_at DATETIME,
  825. completed_at DATETIME,
  826. error_message TEXT,
  827. created_at DATETIME DEFAULT CURRENT_TIMESTAMP
  828. )
  829. """)
  830. )
  831. await conn.execute(
  832. text("""
  833. INSERT INTO print_queue_new
  834. SELECT id, printer_id, archive_id, project_id, position, scheduled_time,
  835. manual_start, require_previous_success, auto_off_after, ams_mapping,
  836. status, started_at, completed_at, error_message, created_at
  837. FROM print_queue
  838. """)
  839. )
  840. await conn.execute(text("DROP TABLE print_queue"))
  841. await conn.execute(text("ALTER TABLE print_queue_new RENAME TO print_queue"))
  842. except (OperationalError, ProgrammingError):
  843. pass # Already applied
  844. # Migration: Add plug_type column to smart_plugs for HA integration
  845. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN plug_type VARCHAR(20) DEFAULT 'tasmota'")
  846. # Migration: Add ha_entity_id column to smart_plugs for HA integration
  847. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN ha_entity_id VARCHAR(100)")
  848. # Migration: Add project_id column to library_folders for linking folders to projects
  849. try:
  850. async with conn.begin_nested():
  851. await conn.execute(
  852. text(
  853. "ALTER TABLE library_folders ADD COLUMN project_id INTEGER REFERENCES projects(id) ON DELETE SET NULL"
  854. )
  855. )
  856. except (OperationalError, ProgrammingError):
  857. pass # Already applied
  858. # Migration: Add archive_id column to library_folders for linking folders to archives
  859. try:
  860. async with conn.begin_nested():
  861. await conn.execute(
  862. text(
  863. "ALTER TABLE library_folders ADD COLUMN archive_id INTEGER REFERENCES print_archives(id) ON DELETE SET NULL"
  864. )
  865. )
  866. except (OperationalError, ProgrammingError):
  867. pass # Already applied
  868. # Migration: Make ip_address nullable for HA plugs (SQLite requires table recreation)
  869. # PostgreSQL gets the correct schema from create_all(), so skip this
  870. if is_sqlite():
  871. try:
  872. result = await conn.execute(text("SELECT sql FROM sqlite_master WHERE type='table' AND name='smart_plugs'"))
  873. row = result.fetchone()
  874. if row and "ip_address VARCHAR(45) NOT NULL" in (row[0] or ""):
  875. await conn.execute(
  876. text("""
  877. CREATE TABLE smart_plugs_new (
  878. id INTEGER PRIMARY KEY,
  879. name VARCHAR(100) NOT NULL,
  880. ip_address VARCHAR(45),
  881. plug_type VARCHAR(20) DEFAULT 'tasmota',
  882. ha_entity_id VARCHAR(100),
  883. printer_id INTEGER UNIQUE REFERENCES printers(id) ON DELETE SET NULL,
  884. enabled BOOLEAN NOT NULL DEFAULT 1,
  885. auto_on BOOLEAN NOT NULL DEFAULT 1,
  886. auto_off BOOLEAN NOT NULL DEFAULT 1,
  887. auto_off_persistent BOOLEAN NOT NULL DEFAULT 0,
  888. off_delay_mode VARCHAR(20) NOT NULL DEFAULT 'time',
  889. off_delay_minutes INTEGER NOT NULL DEFAULT 5,
  890. off_temp_threshold INTEGER NOT NULL DEFAULT 70,
  891. username VARCHAR(50),
  892. password VARCHAR(100),
  893. power_alert_enabled BOOLEAN NOT NULL DEFAULT 0,
  894. power_alert_high FLOAT,
  895. power_alert_low FLOAT,
  896. power_alert_last_triggered DATETIME,
  897. schedule_enabled BOOLEAN NOT NULL DEFAULT 0,
  898. schedule_on_time VARCHAR(5),
  899. schedule_off_time VARCHAR(5),
  900. show_in_switchbar BOOLEAN DEFAULT 0,
  901. last_state VARCHAR(10),
  902. last_checked DATETIME,
  903. auto_off_executed BOOLEAN NOT NULL DEFAULT 0,
  904. auto_off_pending BOOLEAN DEFAULT 0,
  905. auto_off_pending_since DATETIME,
  906. created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
  907. updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
  908. )
  909. """)
  910. )
  911. await conn.execute(
  912. text("""
  913. INSERT INTO smart_plugs_new
  914. SELECT id, name, ip_address,
  915. COALESCE(plug_type, 'tasmota'), ha_entity_id, printer_id,
  916. enabled, auto_on, auto_off, COALESCE(auto_off_persistent, 0),
  917. off_delay_mode, off_delay_minutes, off_temp_threshold,
  918. username, password, power_alert_enabled, power_alert_high, power_alert_low,
  919. power_alert_last_triggered, schedule_enabled, schedule_on_time, schedule_off_time,
  920. COALESCE(show_in_switchbar, 0), last_state, last_checked, auto_off_executed,
  921. COALESCE(auto_off_pending, 0), auto_off_pending_since, created_at, updated_at
  922. FROM smart_plugs
  923. """)
  924. )
  925. await conn.execute(text("DROP TABLE smart_plugs"))
  926. await conn.execute(text("ALTER TABLE smart_plugs_new RENAME TO smart_plugs"))
  927. except (OperationalError, ProgrammingError):
  928. pass # Already applied
  929. # Migration: Add plate_id column to print_queue for multi-plate 3MF support
  930. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN plate_id INTEGER")
  931. # Migration: Add print options columns to print_queue
  932. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN bed_levelling BOOLEAN DEFAULT 1")
  933. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN flow_cali BOOLEAN DEFAULT 0")
  934. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN vibration_cali BOOLEAN DEFAULT 1")
  935. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN layer_inspect BOOLEAN DEFAULT 0")
  936. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN timelapse BOOLEAN DEFAULT 0")
  937. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN use_ams BOOLEAN DEFAULT 1")
  938. # Migration: Add library_file_id column to print_queue and make archive_id nullable
  939. # This allows queue items to reference library files directly (archive created at print start)
  940. try:
  941. async with conn.begin_nested():
  942. await conn.execute(
  943. text(
  944. "ALTER TABLE print_queue ADD COLUMN library_file_id INTEGER REFERENCES library_files(id) ON DELETE CASCADE"
  945. )
  946. )
  947. except (OperationalError, ProgrammingError):
  948. pass # Already applied
  949. # Check if archive_id needs to be made nullable (requires table recreation in SQLite)
  950. # PostgreSQL gets the correct schema from create_all(), so skip this
  951. if is_sqlite():
  952. try:
  953. result = await conn.execute(text("SELECT sql FROM sqlite_master WHERE type='table' AND name='print_queue'"))
  954. row = result.fetchone()
  955. if row and "archive_id INTEGER NOT NULL" in (row[0] or ""):
  956. await conn.execute(
  957. text("""
  958. CREATE TABLE print_queue_new2 (
  959. id INTEGER PRIMARY KEY,
  960. printer_id INTEGER REFERENCES printers(id) ON DELETE CASCADE,
  961. archive_id INTEGER REFERENCES print_archives(id) ON DELETE CASCADE,
  962. library_file_id INTEGER REFERENCES library_files(id) ON DELETE CASCADE,
  963. project_id INTEGER REFERENCES projects(id) ON DELETE SET NULL,
  964. position INTEGER DEFAULT 0,
  965. scheduled_time DATETIME,
  966. manual_start BOOLEAN DEFAULT 0,
  967. require_previous_success BOOLEAN DEFAULT 0,
  968. auto_off_after BOOLEAN DEFAULT 0,
  969. ams_mapping TEXT,
  970. plate_id INTEGER,
  971. bed_levelling BOOLEAN DEFAULT 1,
  972. flow_cali BOOLEAN DEFAULT 0,
  973. vibration_cali BOOLEAN DEFAULT 1,
  974. layer_inspect BOOLEAN DEFAULT 0,
  975. timelapse BOOLEAN DEFAULT 0,
  976. use_ams BOOLEAN DEFAULT 1,
  977. status VARCHAR(20) DEFAULT 'pending',
  978. started_at DATETIME,
  979. completed_at DATETIME,
  980. error_message TEXT,
  981. created_at DATETIME DEFAULT CURRENT_TIMESTAMP
  982. )
  983. """)
  984. )
  985. await conn.execute(
  986. text("""
  987. INSERT INTO print_queue_new2
  988. SELECT id, printer_id, archive_id, NULL, project_id, position, scheduled_time,
  989. manual_start, require_previous_success, auto_off_after, ams_mapping, plate_id,
  990. COALESCE(bed_levelling, 1), COALESCE(flow_cali, 0), COALESCE(vibration_cali, 1),
  991. COALESCE(layer_inspect, 0), COALESCE(timelapse, 0), COALESCE(use_ams, 1),
  992. status, started_at, completed_at, error_message, created_at
  993. FROM print_queue
  994. """)
  995. )
  996. await conn.execute(text("DROP TABLE print_queue"))
  997. await conn.execute(text("ALTER TABLE print_queue_new2 RENAME TO print_queue"))
  998. except (OperationalError, ProgrammingError):
  999. pass # Already applied
  1000. # Migration: Add HA energy sensor entity columns to smart_plugs
  1001. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN ha_power_entity VARCHAR(100)")
  1002. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN ha_energy_today_entity VARCHAR(100)")
  1003. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN ha_energy_total_entity VARCHAR(100)")
  1004. # Migration: Create users table for authentication
  1005. try:
  1006. async with conn.begin_nested():
  1007. await conn.execute(
  1008. text("""
  1009. CREATE TABLE IF NOT EXISTS users (
  1010. id INTEGER PRIMARY KEY,
  1011. username VARCHAR(100) NOT NULL UNIQUE,
  1012. password_hash VARCHAR(255) NOT NULL,
  1013. role VARCHAR(20) NOT NULL DEFAULT 'user',
  1014. is_active BOOLEAN NOT NULL DEFAULT 1,
  1015. created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  1016. updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
  1017. )
  1018. """)
  1019. )
  1020. await conn.execute(text("CREATE INDEX IF NOT EXISTS ix_users_username ON users(username)"))
  1021. except (OperationalError, ProgrammingError):
  1022. pass # Already applied
  1023. # Migration: Add external camera columns to printers
  1024. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN external_camera_url VARCHAR(500)")
  1025. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN external_camera_type VARCHAR(20)")
  1026. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN external_camera_enabled BOOLEAN DEFAULT 0")
  1027. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN external_camera_snapshot_url VARCHAR(500)")
  1028. # Migration: Add external_url column to print_archives for user-defined links (Printables, etc.)
  1029. await _safe_execute(conn, "ALTER TABLE print_archives ADD COLUMN external_url VARCHAR(500)")
  1030. # Migration: Add sliced_for_model column to print_archives for model-based queue assignment
  1031. await _safe_execute(conn, "ALTER TABLE print_archives ADD COLUMN sliced_for_model VARCHAR(50)")
  1032. # Migration: Add is_external column to library_files for external cloud files
  1033. await _safe_execute(conn, "ALTER TABLE library_files ADD COLUMN is_external BOOLEAN DEFAULT 0")
  1034. # Migration: Add project_id column to library_files
  1035. try:
  1036. async with conn.begin_nested():
  1037. await conn.execute(
  1038. text(
  1039. "ALTER TABLE library_files ADD COLUMN project_id INTEGER REFERENCES projects(id) ON DELETE SET NULL"
  1040. )
  1041. )
  1042. except (OperationalError, ProgrammingError):
  1043. pass # Already applied
  1044. # Migration: Add is_external column to library_folders for external cloud folders
  1045. await _safe_execute(conn, "ALTER TABLE library_folders ADD COLUMN is_external BOOLEAN DEFAULT 0")
  1046. # Migration: Add external folder settings columns to library_folders
  1047. await _safe_execute(conn, "ALTER TABLE library_folders ADD COLUMN external_readonly BOOLEAN DEFAULT 0")
  1048. await _safe_execute(conn, "ALTER TABLE library_folders ADD COLUMN external_show_hidden BOOLEAN DEFAULT 0")
  1049. await _safe_execute(conn, "ALTER TABLE library_folders ADD COLUMN external_path VARCHAR(500)")
  1050. # Migration: Add plate_detection_enabled column to printers
  1051. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN plate_detection_enabled BOOLEAN DEFAULT 0")
  1052. # Migration: Add plate detection ROI columns to printers
  1053. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN plate_detection_roi_x REAL")
  1054. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN plate_detection_roi_y REAL")
  1055. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN plate_detection_roi_w REAL")
  1056. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN plate_detection_roi_h REAL")
  1057. # Migration: Remove UNIQUE constraint from smart_plugs.printer_id
  1058. # This allows HA scripts to coexist with regular plugs (scripts are for multi-device control)
  1059. # SQLite requires table recreation to drop constraints
  1060. # PostgreSQL gets the correct schema from create_all(), so skip this
  1061. if is_sqlite():
  1062. try:
  1063. needs_migration = False
  1064. result = await conn.execute(text("SELECT sql FROM sqlite_master WHERE type='table' AND name='smart_plugs'"))
  1065. row = result.fetchone()
  1066. table_sql = (row[0] or "").upper() if row else ""
  1067. if "PRINTER_ID" in table_sql and "UNIQUE" in table_sql:
  1068. import re
  1069. if re.search(r'"?PRINTER_ID"?\s+\w+\s+UNIQUE', table_sql) or re.search(
  1070. r'UNIQUE\s*\([^)]*"?PRINTER_ID"?', table_sql
  1071. ):
  1072. needs_migration = True
  1073. idx_result = await conn.execute(
  1074. text("SELECT sql FROM sqlite_master WHERE type='index' AND tbl_name='smart_plugs' AND sql IS NOT NULL")
  1075. )
  1076. for idx_row in idx_result.fetchall():
  1077. idx_sql = (idx_row[0] or "").upper()
  1078. if "UNIQUE" in idx_sql and "PRINTER_ID" in idx_sql:
  1079. needs_migration = True
  1080. break
  1081. if needs_migration:
  1082. # Create new table without UNIQUE constraint on printer_id
  1083. await conn.execute(
  1084. text("""
  1085. CREATE TABLE smart_plugs_temp (
  1086. id INTEGER PRIMARY KEY,
  1087. name VARCHAR(100) NOT NULL,
  1088. ip_address VARCHAR(45),
  1089. plug_type VARCHAR(20) DEFAULT 'tasmota',
  1090. ha_entity_id VARCHAR(100),
  1091. ha_power_entity VARCHAR(100),
  1092. ha_energy_today_entity VARCHAR(100),
  1093. ha_energy_total_entity VARCHAR(100),
  1094. printer_id INTEGER REFERENCES printers(id) ON DELETE SET NULL,
  1095. enabled BOOLEAN NOT NULL DEFAULT 1,
  1096. auto_on BOOLEAN NOT NULL DEFAULT 1,
  1097. auto_off BOOLEAN NOT NULL DEFAULT 1,
  1098. auto_off_persistent BOOLEAN NOT NULL DEFAULT 0,
  1099. off_delay_mode VARCHAR(20) NOT NULL DEFAULT 'time',
  1100. off_delay_minutes INTEGER NOT NULL DEFAULT 5,
  1101. off_temp_threshold INTEGER NOT NULL DEFAULT 70,
  1102. username VARCHAR(50),
  1103. password VARCHAR(100),
  1104. power_alert_enabled BOOLEAN NOT NULL DEFAULT 0,
  1105. power_alert_high FLOAT,
  1106. power_alert_low FLOAT,
  1107. power_alert_last_triggered DATETIME,
  1108. schedule_enabled BOOLEAN NOT NULL DEFAULT 0,
  1109. schedule_on_time VARCHAR(5),
  1110. schedule_off_time VARCHAR(5),
  1111. show_in_switchbar BOOLEAN DEFAULT 0,
  1112. last_state VARCHAR(10),
  1113. last_checked DATETIME,
  1114. auto_off_executed BOOLEAN NOT NULL DEFAULT 0,
  1115. auto_off_pending BOOLEAN DEFAULT 0,
  1116. auto_off_pending_since DATETIME,
  1117. created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
  1118. updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
  1119. )
  1120. """)
  1121. )
  1122. # Copy data
  1123. await conn.execute(
  1124. text("""
  1125. INSERT INTO smart_plugs_temp
  1126. SELECT id, name, ip_address, plug_type, ha_entity_id, ha_power_entity,
  1127. ha_energy_today_entity, ha_energy_total_entity, printer_id, enabled,
  1128. auto_on, auto_off, COALESCE(auto_off_persistent, 0),
  1129. off_delay_mode, off_delay_minutes, off_temp_threshold,
  1130. username, password, power_alert_enabled, power_alert_high, power_alert_low,
  1131. power_alert_last_triggered, schedule_enabled, schedule_on_time, schedule_off_time,
  1132. show_in_switchbar, last_state, last_checked, auto_off_executed,
  1133. auto_off_pending, auto_off_pending_since, created_at, updated_at
  1134. FROM smart_plugs
  1135. """)
  1136. )
  1137. # Drop old table and rename new one
  1138. await conn.execute(text("DROP TABLE smart_plugs"))
  1139. await conn.execute(text("ALTER TABLE smart_plugs_temp RENAME TO smart_plugs"))
  1140. except (OperationalError, ProgrammingError):
  1141. pass # Already applied
  1142. # Migration: Add show_on_printer_card column to smart_plugs
  1143. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN show_on_printer_card BOOLEAN DEFAULT 1")
  1144. # Migration: Add MQTT smart plug fields (legacy)
  1145. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN mqtt_topic VARCHAR(200)")
  1146. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN mqtt_power_path VARCHAR(100)")
  1147. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN mqtt_energy_path VARCHAR(100)")
  1148. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN mqtt_state_path VARCHAR(100)")
  1149. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN mqtt_multiplier REAL DEFAULT 1.0")
  1150. # Migration: Add enhanced MQTT smart plug fields (separate topics and multipliers)
  1151. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN mqtt_power_topic VARCHAR(200)")
  1152. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN mqtt_power_multiplier REAL DEFAULT 1.0")
  1153. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN mqtt_energy_topic VARCHAR(200)")
  1154. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN mqtt_energy_multiplier REAL DEFAULT 1.0")
  1155. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN mqtt_state_topic VARCHAR(200)")
  1156. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN mqtt_state_on_value VARCHAR(50)")
  1157. # Migration: Copy existing mqtt_topic to mqtt_power_topic for backward compatibility
  1158. try:
  1159. async with conn.begin_nested():
  1160. await conn.execute(
  1161. text("""
  1162. UPDATE smart_plugs
  1163. SET mqtt_power_topic = mqtt_topic,
  1164. mqtt_power_multiplier = mqtt_multiplier
  1165. WHERE mqtt_topic IS NOT NULL AND mqtt_power_topic IS NULL
  1166. """)
  1167. )
  1168. except (OperationalError, ProgrammingError):
  1169. pass # Already applied
  1170. # Migration: Create groups table for permission-based access control
  1171. try:
  1172. async with conn.begin_nested():
  1173. await conn.execute(
  1174. text("""
  1175. CREATE TABLE IF NOT EXISTS groups (
  1176. id INTEGER PRIMARY KEY,
  1177. name VARCHAR(100) NOT NULL UNIQUE,
  1178. description VARCHAR(500),
  1179. permissions JSON,
  1180. is_system BOOLEAN NOT NULL DEFAULT 0,
  1181. created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  1182. updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
  1183. )
  1184. """)
  1185. )
  1186. await conn.execute(text("CREATE INDEX IF NOT EXISTS ix_groups_name ON groups(name)"))
  1187. except (OperationalError, ProgrammingError):
  1188. pass # Already applied
  1189. # Migration: Create user_groups association table
  1190. try:
  1191. async with conn.begin_nested():
  1192. await conn.execute(
  1193. text("""
  1194. CREATE TABLE IF NOT EXISTS user_groups (
  1195. user_id INTEGER NOT NULL,
  1196. group_id INTEGER NOT NULL,
  1197. PRIMARY KEY (user_id, group_id),
  1198. FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
  1199. FOREIGN KEY (group_id) REFERENCES groups(id) ON DELETE CASCADE
  1200. )
  1201. """)
  1202. )
  1203. except (OperationalError, ProgrammingError):
  1204. pass # Already applied
  1205. # Migration: Add model-based queue assignment columns to print_queue
  1206. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN target_model VARCHAR(50)")
  1207. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN required_filament_types TEXT")
  1208. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN waiting_reason TEXT")
  1209. # Migration: Add nozzle_count column to printers (for dual-extruder detection)
  1210. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN nozzle_count INTEGER DEFAULT 1")
  1211. # Migration: Add print_hours_offset column to printers (baseline hours adjustment)
  1212. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN print_hours_offset REAL DEFAULT 0.0")
  1213. # Migration: Add queue notification event columns to notification_providers
  1214. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN on_queue_job_added BOOLEAN DEFAULT 0")
  1215. try:
  1216. async with conn.begin_nested():
  1217. await conn.execute(
  1218. text("ALTER TABLE notification_providers ADD COLUMN on_queue_job_assigned BOOLEAN DEFAULT 0")
  1219. )
  1220. except (OperationalError, ProgrammingError):
  1221. pass # Already applied
  1222. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN on_queue_job_started BOOLEAN DEFAULT 0")
  1223. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN on_queue_job_waiting BOOLEAN DEFAULT 1")
  1224. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN on_queue_job_skipped BOOLEAN DEFAULT 1")
  1225. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN on_queue_job_failed BOOLEAN DEFAULT 1")
  1226. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN on_queue_completed BOOLEAN DEFAULT 0")
  1227. # Migration: Add created_by_id column to print_archives for user tracking (Issue #206)
  1228. try:
  1229. async with conn.begin_nested():
  1230. await conn.execute(
  1231. text(
  1232. "ALTER TABLE print_archives ADD COLUMN created_by_id INTEGER REFERENCES users(id) ON DELETE SET NULL"
  1233. )
  1234. )
  1235. except (OperationalError, ProgrammingError):
  1236. pass # Already applied
  1237. # Migration: Add created_by_id column to print_queue for user tracking (Issue #206)
  1238. try:
  1239. async with conn.begin_nested():
  1240. await conn.execute(
  1241. text("ALTER TABLE print_queue ADD COLUMN created_by_id INTEGER REFERENCES users(id) ON DELETE SET NULL")
  1242. )
  1243. except (OperationalError, ProgrammingError):
  1244. pass # Already applied
  1245. # Migration: Add created_by_id column to library_files for user tracking (Issue #206)
  1246. try:
  1247. async with conn.begin_nested():
  1248. await conn.execute(
  1249. text(
  1250. "ALTER TABLE library_files ADD COLUMN created_by_id INTEGER REFERENCES users(id) ON DELETE SET NULL"
  1251. )
  1252. )
  1253. except (OperationalError, ProgrammingError):
  1254. pass # Already applied
  1255. # Migration: Add target_location column to print_queue for location-based filtering (Issue #220)
  1256. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN target_location VARCHAR(100)")
  1257. # Migration: Convert absolute paths to relative paths in library_files table
  1258. # This ensures backup/restore portability across different installations
  1259. try:
  1260. async with conn.begin_nested():
  1261. base_dir_str = str(settings.base_dir)
  1262. # Ensure we have a trailing slash for clean replacement
  1263. if not base_dir_str.endswith("/"):
  1264. base_dir_str += "/"
  1265. # Update file_path - remove base_dir prefix from absolute paths
  1266. await conn.execute(
  1267. text("""
  1268. UPDATE library_files
  1269. SET file_path = SUBSTR(file_path, LENGTH(:base_dir) + 1)
  1270. WHERE file_path LIKE :pattern
  1271. """),
  1272. {"base_dir": base_dir_str, "pattern": base_dir_str + "%"},
  1273. )
  1274. # Update thumbnail_path - remove base_dir prefix from absolute paths
  1275. await conn.execute(
  1276. text("""
  1277. UPDATE library_files
  1278. SET thumbnail_path = SUBSTR(thumbnail_path, LENGTH(:base_dir) + 1)
  1279. WHERE thumbnail_path LIKE :pattern
  1280. """),
  1281. {"base_dir": base_dir_str, "pattern": base_dir_str + "%"},
  1282. )
  1283. except (OperationalError, ProgrammingError):
  1284. pass # Already applied
  1285. # Create active_print_spoolman table for Spoolman per-filament tracking
  1286. await _safe_execute(
  1287. conn,
  1288. """
  1289. CREATE TABLE IF NOT EXISTS active_print_spoolman (
  1290. id INTEGER PRIMARY KEY AUTOINCREMENT,
  1291. printer_id INTEGER NOT NULL REFERENCES printers(id) ON DELETE CASCADE,
  1292. archive_id INTEGER NOT NULL REFERENCES print_archives(id) ON DELETE CASCADE,
  1293. filament_usage TEXT NOT NULL,
  1294. ams_trays TEXT NOT NULL,
  1295. slot_to_tray TEXT,
  1296. layer_usage TEXT,
  1297. filament_properties TEXT,
  1298. UNIQUE(printer_id, archive_id)
  1299. )
  1300. """
  1301. if is_sqlite()
  1302. else """
  1303. CREATE TABLE IF NOT EXISTS active_print_spoolman (
  1304. id SERIAL PRIMARY KEY,
  1305. printer_id INTEGER NOT NULL REFERENCES printers(id) ON DELETE CASCADE,
  1306. archive_id INTEGER NOT NULL REFERENCES print_archives(id) ON DELETE CASCADE,
  1307. filament_usage TEXT NOT NULL,
  1308. ams_trays TEXT NOT NULL,
  1309. slot_to_tray TEXT,
  1310. layer_usage TEXT,
  1311. filament_properties TEXT,
  1312. UNIQUE(printer_id, archive_id)
  1313. )
  1314. """,
  1315. )
  1316. # Migration: Add preset_source column to slot_preset_mappings for local preset support
  1317. try:
  1318. async with conn.begin_nested():
  1319. await conn.execute(
  1320. text("ALTER TABLE slot_preset_mappings ADD COLUMN preset_source VARCHAR(20) DEFAULT 'cloud'")
  1321. )
  1322. except (OperationalError, ProgrammingError):
  1323. pass # Already applied
  1324. # Migration: Add email column to users for Advanced Auth (PR #322)
  1325. await _safe_execute(conn, "ALTER TABLE users ADD COLUMN email VARCHAR(255)")
  1326. # Migration: Add inventory spool tracking columns
  1327. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN added_full BOOLEAN")
  1328. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN last_used DATETIME")
  1329. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN encode_time DATETIME")
  1330. # Migration: Add RFID tag matching columns to spool
  1331. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN tag_uid VARCHAR(16)")
  1332. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN tray_uuid VARCHAR(32)")
  1333. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN data_origin VARCHAR(20)")
  1334. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN tag_type VARCHAR(20)")
  1335. # Migration: Add core_weight_catalog_id to track which catalog entry was used for empty spool weight
  1336. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN core_weight_catalog_id INTEGER")
  1337. # Migration: Create spool_usage_history table for filament consumption tracking
  1338. await _safe_execute(
  1339. conn,
  1340. """
  1341. CREATE TABLE IF NOT EXISTS spool_usage_history (
  1342. id INTEGER PRIMARY KEY AUTOINCREMENT,
  1343. spool_id INTEGER NOT NULL REFERENCES spool(id) ON DELETE CASCADE,
  1344. printer_id INTEGER REFERENCES printers(id) ON DELETE SET NULL,
  1345. print_name VARCHAR(500),
  1346. weight_used REAL NOT NULL DEFAULT 0,
  1347. percent_used INTEGER NOT NULL DEFAULT 0,
  1348. status VARCHAR(20) NOT NULL DEFAULT 'completed',
  1349. created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
  1350. )
  1351. """
  1352. if is_sqlite()
  1353. else """
  1354. CREATE TABLE IF NOT EXISTS spool_usage_history (
  1355. id SERIAL PRIMARY KEY,
  1356. spool_id INTEGER NOT NULL REFERENCES spool(id) ON DELETE CASCADE,
  1357. printer_id INTEGER REFERENCES printers(id) ON DELETE SET NULL,
  1358. print_name VARCHAR(500),
  1359. weight_used REAL NOT NULL DEFAULT 0,
  1360. percent_used INTEGER NOT NULL DEFAULT 0,
  1361. status VARCHAR(20) NOT NULL DEFAULT 'completed',
  1362. created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
  1363. )
  1364. """,
  1365. )
  1366. # Migration: Add open_in_new_tab column to external_links
  1367. await _safe_execute(conn, "ALTER TABLE external_links ADD COLUMN open_in_new_tab BOOLEAN DEFAULT 0")
  1368. # Migration: Add bed cooled notification column to notification_providers
  1369. await _safe_execute(conn, "ALTER TABLE notification_providers ADD COLUMN on_bed_cooled BOOLEAN DEFAULT 0")
  1370. # Migration: Add first layer complete notification column to notification_providers
  1371. try:
  1372. async with conn.begin_nested():
  1373. await conn.execute(
  1374. text("ALTER TABLE notification_providers ADD COLUMN on_first_layer_complete BOOLEAN DEFAULT 0")
  1375. )
  1376. except (OperationalError, ProgrammingError):
  1377. pass # Already applied
  1378. # Migration: Add weight_locked flag to spool table (skip AMS auto-sync for manually-entered weights)
  1379. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN weight_locked BOOLEAN DEFAULT 0")
  1380. # Migration: Add SpoolBuddy scale weight tracking columns to spool table
  1381. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN last_scale_weight INTEGER")
  1382. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN last_weighed_at DATETIME")
  1383. # Migration: Add cost tracking fields to spool table
  1384. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN cost_per_kg REAL")
  1385. # Migration: Per-spool category + low-stock threshold override (#729). Both
  1386. # nullable — NULL category leaves the spool uncategorised, NULL threshold
  1387. # falls back to the global low_stock_threshold setting.
  1388. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN category VARCHAR(50)")
  1389. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN low_stock_threshold_pct INTEGER")
  1390. # Migration: Add user-editable storage location to spool table
  1391. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN storage_location VARCHAR(255)")
  1392. # Migration: Add weight_used_baseline anchor for the resettable "Total
  1393. # Consumed" stat (#1390). Existing spools default to 0 (no baseline),
  1394. # so the counter starts unaffected; pressing "Reset usage to 0" now
  1395. # stamps baseline = weight_used without touching remaining.
  1396. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN weight_used_baseline REAL DEFAULT 0")
  1397. # Migration: Widen tag_uid column from VARCHAR(16) to VARCHAR(32) to accommodate 7-byte NFC
  1398. # UIDs (14 hex chars) in addition to 8-byte Bambu Lab UIDs (16 hex chars).
  1399. # ALTER COLUMN ... TYPE is PostgreSQL-only syntax; SQLite ignores VARCHAR sizes so no-op there.
  1400. if not is_sqlite():
  1401. await _safe_execute(conn, "ALTER TABLE spool ALTER COLUMN tag_uid TYPE VARCHAR(32)")
  1402. # Migration: enhanced filament colour handling (#1154). `extra_colors` is
  1403. # a comma-separated list of 6- or 8-char hex tokens (no `#`) for multi-
  1404. # colour gradients; `effect_type` is one of {sparkle, wood, marble, glow,
  1405. # matte} as a visual rendering hint. Both nullable — NULL keeps the
  1406. # current single-rgba/no-effect behaviour.
  1407. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN extra_colors VARCHAR(255)")
  1408. await _safe_execute(conn, "ALTER TABLE spool ADD COLUMN effect_type VARCHAR(20)")
  1409. # Migration: Add cost field to spool_usage_history table
  1410. await _safe_execute(conn, "ALTER TABLE spool_usage_history ADD COLUMN cost REAL")
  1411. # Migration: Add archive_id field to spool_usage_history table
  1412. try:
  1413. async with conn.begin_nested():
  1414. await conn.execute(
  1415. text("ALTER TABLE spool_usage_history ADD COLUMN archive_id INTEGER REFERENCES print_archives(id)")
  1416. )
  1417. except (OperationalError, ProgrammingError):
  1418. pass # Already applied
  1419. # Migration: Migrate single virtual printer key-value settings to virtual_printers table
  1420. try:
  1421. async with conn.begin_nested():
  1422. result = await conn.execute(text("SELECT COUNT(*) FROM virtual_printers"))
  1423. count = result.scalar() or 0
  1424. if count == 0:
  1425. result = await conn.execute(text("SELECT value FROM settings WHERE key = 'virtual_printer_enabled'"))
  1426. row = result.fetchone()
  1427. if row:
  1428. # Old settings exist — migrate to first virtual printer row
  1429. old_enabled = row[0] == "true" if row[0] else False
  1430. result = await conn.execute(
  1431. text("SELECT value FROM settings WHERE key = 'virtual_printer_access_code'")
  1432. )
  1433. row = result.fetchone()
  1434. old_access_code = row[0] if row else None
  1435. result = await conn.execute(text("SELECT value FROM settings WHERE key = 'virtual_printer_mode'"))
  1436. row = result.fetchone()
  1437. old_mode = row[0] if row else "immediate"
  1438. if old_mode == "queue":
  1439. old_mode = "review"
  1440. result = await conn.execute(text("SELECT value FROM settings WHERE key = 'virtual_printer_model'"))
  1441. row = result.fetchone()
  1442. old_model = row[0] if row else "BL-P001"
  1443. result = await conn.execute(
  1444. text("SELECT value FROM settings WHERE key = 'virtual_printer_target_printer_id'")
  1445. )
  1446. row = result.fetchone()
  1447. old_target_id = int(row[0]) if row and row[0] else None
  1448. result = await conn.execute(
  1449. text("SELECT value FROM settings WHERE key = 'virtual_printer_remote_interface_ip'")
  1450. )
  1451. row = result.fetchone()
  1452. old_remote_iface = row[0] if row else None
  1453. await conn.execute(
  1454. text("""
  1455. INSERT INTO virtual_printers
  1456. (name, enabled, mode, model, access_code, target_printer_id,
  1457. bind_ip, remote_interface_ip, serial_suffix, position)
  1458. VALUES
  1459. (:name, :enabled, :mode, :model, :access_code, :target_id,
  1460. NULL, :remote_iface, '391800001', 0)
  1461. """),
  1462. {
  1463. "name": "Bambuddy",
  1464. "enabled": old_enabled,
  1465. "mode": old_mode or "immediate",
  1466. "model": old_model,
  1467. "access_code": old_access_code,
  1468. "target_id": old_target_id,
  1469. "remote_iface": old_remote_iface,
  1470. },
  1471. )
  1472. except (OperationalError, ProgrammingError, IntegrityError):
  1473. pass # Table may not exist yet on first run, or columns have different constraints
  1474. # Migration: Add filament_overrides column to print_queue for filament override in model-based assignment
  1475. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN filament_overrides TEXT")
  1476. # Migration: Add NFC reader and display control columns to spoolbuddy_devices
  1477. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN nfc_reader_type VARCHAR(20)")
  1478. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN nfc_connection VARCHAR(20)")
  1479. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN display_brightness INTEGER DEFAULT 100")
  1480. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN display_blank_timeout INTEGER DEFAULT 0")
  1481. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN has_backlight BOOLEAN DEFAULT 0")
  1482. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN last_calibrated_at DATETIME")
  1483. # Migration: Add NFC tag write payload column to spoolbuddy_devices
  1484. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN pending_write_payload TEXT")
  1485. # Migration: Add OTA update tracking columns to spoolbuddy_devices
  1486. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN update_status VARCHAR(20)")
  1487. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN update_message VARCHAR(255)")
  1488. # Migration: Persist SpoolBuddy backend URL and queued system payload
  1489. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN backend_url VARCHAR(255)")
  1490. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN pending_system_payload TEXT")
  1491. # Migration: Add system_stats JSON blob column to spoolbuddy_devices
  1492. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN system_stats TEXT")
  1493. # Migration: Add SSH host key for TOFU verification (H1 security fix)
  1494. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ADD COLUMN ssh_host_key VARCHAR(500)")
  1495. # Migration: Widen ssh_host_key from VARCHAR(500) to TEXT — RSA-3072+ host keys
  1496. # in OpenSSH format exceed 500 chars (RSA-4096 ~720 chars). PostgreSQL enforces
  1497. # the limit and rejects the UPDATE; SQLite ignores VARCHAR length so no-op there.
  1498. if not is_sqlite():
  1499. await _safe_execute(conn, "ALTER TABLE spoolbuddy_devices ALTER COLUMN ssh_host_key TYPE TEXT")
  1500. # Migration: Convert ams_labels table from (printer_id, ams_id) key to ams_serial_number key
  1501. # Labels are now keyed by AMS serial number so they persist when the AMS is moved to another printer.
  1502. # PostgreSQL gets the correct schema from create_all(), so skip this
  1503. if is_sqlite():
  1504. try:
  1505. await conn.execute(text("DROP TABLE IF EXISTS ams_labels_new"))
  1506. result = await conn.execute(text("SELECT sql FROM sqlite_master WHERE type='table' AND name='ams_labels'"))
  1507. row = result.fetchone()
  1508. if row and "printer_id" in (row[0] or ""):
  1509. # Old schema: rebuild the table with ams_serial_number as the unique key.
  1510. # Existing rows get a synthetic serial "p{printer_id}a{ams_id}" so data is preserved.
  1511. await conn.execute(
  1512. text("""
  1513. CREATE TABLE ams_labels_new (
  1514. id INTEGER PRIMARY KEY,
  1515. ams_serial_number VARCHAR(50) NOT NULL,
  1516. ams_id INTEGER,
  1517. label VARCHAR(100) NOT NULL,
  1518. created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  1519. updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  1520. CONSTRAINT uq_ams_label_serial UNIQUE (ams_serial_number)
  1521. )
  1522. """)
  1523. )
  1524. await conn.execute(
  1525. text("""
  1526. INSERT INTO ams_labels_new (id, ams_serial_number, ams_id, label, created_at, updated_at)
  1527. SELECT id,
  1528. 'p' || CAST(printer_id AS TEXT) || 'a' || CAST(ams_id AS TEXT),
  1529. ams_id,
  1530. label,
  1531. created_at,
  1532. updated_at
  1533. FROM ams_labels
  1534. """)
  1535. )
  1536. await conn.execute(text("DROP TABLE ams_labels"))
  1537. await conn.execute(text("ALTER TABLE ams_labels_new RENAME TO ams_labels"))
  1538. except (OperationalError, ProgrammingError):
  1539. pass # Already migrated or table does not exist yet
  1540. # Migration: Add auto_dispatch column to virtual_printers
  1541. await _safe_execute(conn, "ALTER TABLE virtual_printers ADD COLUMN auto_dispatch BOOLEAN DEFAULT 1")
  1542. # Migration: Fix VP model codes — convert legacy SSDP codes and display names to correct SSDP codes
  1543. # Legacy codes (from multi-VP refactor) and display names (from proxy auto-inherit)
  1544. vp_model_fixes = {
  1545. "3DPrinter-X1-Carbon": "BL-P001",
  1546. "3DPrinter-X1": "BL-P002",
  1547. "X1C": "BL-P001",
  1548. "X1": "BL-P002",
  1549. "X1E": "C13",
  1550. "X2D": "N6",
  1551. "P1P": "C11",
  1552. "P1S": "C12",
  1553. "P2S": "N7",
  1554. "A1": "N2S",
  1555. "A1 Mini": "N1",
  1556. "H2D": "O1D",
  1557. "H2C": "O1C",
  1558. "H2S": "O1S",
  1559. }
  1560. for old_val, new_val in vp_model_fixes.items():
  1561. await conn.execute(
  1562. text("UPDATE virtual_printers SET model = :new WHERE model = :old"),
  1563. {"old": old_val, "new": new_val},
  1564. )
  1565. await conn.execute(
  1566. text("UPDATE settings SET value = :new WHERE key = 'virtual_printer_model' AND value = :old"),
  1567. {"old": old_val, "new": new_val},
  1568. )
  1569. # Migration: Add per-user Bambu Cloud credential columns
  1570. await _safe_execute(conn, "ALTER TABLE users ADD COLUMN cloud_token VARCHAR(500)")
  1571. await _safe_execute(conn, "ALTER TABLE users ADD COLUMN cloud_email VARCHAR(255)")
  1572. await _safe_execute(conn, "ALTER TABLE users ADD COLUMN cloud_region VARCHAR(10)")
  1573. # Cleanup: Remove obsolete settings keys that are no longer used
  1574. obsolete_keys = ["slicer_binary_path"]
  1575. for key in obsolete_keys:
  1576. await conn.execute(text("DELETE FROM settings WHERE key = :key"), {"key": key})
  1577. # Migration: Create user_email_preferences table for user-specific email notification settings
  1578. try:
  1579. async with conn.begin_nested():
  1580. await conn.execute(
  1581. text("""
  1582. CREATE TABLE IF NOT EXISTS user_email_preferences (
  1583. id INTEGER PRIMARY KEY,
  1584. user_id INTEGER NOT NULL UNIQUE REFERENCES users(id) ON DELETE CASCADE,
  1585. notify_print_start BOOLEAN NOT NULL DEFAULT 1,
  1586. notify_print_complete BOOLEAN NOT NULL DEFAULT 1,
  1587. notify_print_failed BOOLEAN NOT NULL DEFAULT 1,
  1588. notify_print_stopped BOOLEAN NOT NULL DEFAULT 1,
  1589. created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  1590. updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
  1591. )
  1592. """)
  1593. )
  1594. await conn.execute(
  1595. text("CREATE INDEX IF NOT EXISTS ix_user_email_preferences_user_id ON user_email_preferences(user_id)")
  1596. )
  1597. except (OperationalError, ProgrammingError):
  1598. pass # Already applied
  1599. # Legacy migration: Add notify_print_stopped column (for any existing partial tables)
  1600. try:
  1601. async with conn.begin_nested():
  1602. await conn.execute(
  1603. text("ALTER TABLE user_email_preferences ADD COLUMN notify_print_stopped BOOLEAN NOT NULL DEFAULT 1")
  1604. )
  1605. except (OperationalError, ProgrammingError):
  1606. pass # Column already exists or table created with full schema
  1607. # Migration: Add camera_rotation column to printers
  1608. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN camera_rotation INTEGER DEFAULT 0")
  1609. # Migration: Add awaiting_plate_clear column to printers (#961)
  1610. await _safe_execute(conn, "ALTER TABLE printers ADD COLUMN awaiting_plate_clear BOOLEAN DEFAULT FALSE NOT NULL")
  1611. # Migration: Add REST/Webhook smart plug fields
  1612. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_on_url VARCHAR(500)")
  1613. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_on_body TEXT")
  1614. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_off_url VARCHAR(500)")
  1615. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_off_body TEXT")
  1616. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_method VARCHAR(10)")
  1617. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_headers TEXT")
  1618. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_status_url VARCHAR(500)")
  1619. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_status_path VARCHAR(200)")
  1620. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_status_on_value VARCHAR(50)")
  1621. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_power_path VARCHAR(200)")
  1622. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_energy_path VARCHAR(200)")
  1623. # Migration: Add separate REST power/energy URLs and multipliers
  1624. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_power_url VARCHAR(500)")
  1625. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_power_multiplier REAL DEFAULT 1.0")
  1626. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_energy_url VARCHAR(500)")
  1627. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN rest_energy_multiplier REAL DEFAULT 1.0")
  1628. # Migration: Add batch_id column to print_queue for batch grouping
  1629. try:
  1630. async with conn.begin_nested():
  1631. await conn.execute(
  1632. text(
  1633. "ALTER TABLE print_queue ADD COLUMN batch_id INTEGER REFERENCES print_batches(id) ON DELETE SET NULL"
  1634. )
  1635. )
  1636. except (OperationalError, ProgrammingError):
  1637. pass
  1638. # Migration: Shortest-job-first scheduling columns on print_queue
  1639. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN print_time_seconds INTEGER")
  1640. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN been_jumped BOOLEAN DEFAULT FALSE NOT NULL")
  1641. # Migration: Auto-print G-code injection (#422)
  1642. await _safe_execute(conn, "ALTER TABLE print_queue ADD COLUMN gcode_injection BOOLEAN DEFAULT FALSE NOT NULL")
  1643. # Migration: Add backup_spools and backup_archives columns to github_backup_config
  1644. await _safe_execute(conn, "ALTER TABLE github_backup_config ADD COLUMN backup_spools BOOLEAN DEFAULT 0")
  1645. await _safe_execute(conn, "ALTER TABLE github_backup_config ADD COLUMN backup_archives BOOLEAN DEFAULT 0")
  1646. # Migration: Widen columns where SQLite allowed data beyond the declared VARCHAR limit
  1647. if not is_sqlite():
  1648. await _safe_execute(conn, "ALTER TABLE api_keys ALTER COLUMN key_hash TYPE VARCHAR(255)")
  1649. await _safe_execute(conn, "ALTER TABLE api_keys ALTER COLUMN key_prefix TYPE VARCHAR(20)")
  1650. await _safe_execute(conn, "ALTER TABLE print_archives ALTER COLUMN filament_color TYPE VARCHAR(200)")
  1651. # Migration: Create GIN index for full-text search on PostgreSQL
  1652. # (SQLite uses FTS5 virtual table instead, set up above)
  1653. if not is_sqlite():
  1654. try:
  1655. await conn.execute(
  1656. text("""
  1657. CREATE INDEX IF NOT EXISTS idx_archives_fulltext
  1658. ON print_archives
  1659. USING GIN (to_tsvector('simple',
  1660. COALESCE(print_name, '') || ' ' ||
  1661. COALESCE(filename, '') || ' ' ||
  1662. COALESCE(tags, '') || ' ' ||
  1663. COALESCE(notes, '') || ' ' ||
  1664. COALESCE(designer, '') || ' ' ||
  1665. COALESCE(filament_type, '')
  1666. ))
  1667. """)
  1668. )
  1669. except (OperationalError, ProgrammingError):
  1670. pass # Already applied
  1671. # Migration: Normalize empty printer_ids [] to NULL (global access) on API keys
  1672. # Previously both None and [] meant "all printers"; now [] means "no printers"
  1673. # PostgreSQL stores printer_ids as JSONB; comparing JSONB to a string literal fails
  1674. # with "operator does not exist: jsonb = unknown" — cast the literal to jsonb explicitly.
  1675. await _migrate_normalize_printer_ids(conn)
  1676. # Migration: Add auth_source column to users for LDAP support (#794)
  1677. await _safe_execute(conn, "ALTER TABLE users ADD COLUMN auth_source VARCHAR(20) DEFAULT 'local' NOT NULL")
  1678. # Migration: Make password_hash nullable for LDAP users (#794)
  1679. # LDAP users have no local password — the column must allow NULL so auto-provisioning
  1680. # doesn't hit a NOT NULL constraint failure on upgraded installs whose users table was
  1681. # originally created before LDAP support landed.
  1682. if is_sqlite():
  1683. # SQLite can't ALTER COLUMN; patch sqlite_master directly via writable_schema.
  1684. # Bump schema_version afterwards so SQLite reloads the table definition from disk —
  1685. # without that bump, the current connection keeps enforcing the old NOT NULL from
  1686. # its cached schema. Safe because row data is untouched and the replace() is a
  1687. # no-op if the constraint has already been removed.
  1688. try:
  1689. result = await conn.execute(text("SELECT sql FROM sqlite_master WHERE type='table' AND name='users'"))
  1690. users_sql = result.scalar()
  1691. if users_sql and "password_hash VARCHAR(255) NOT NULL" in users_sql:
  1692. version_result = await conn.execute(text("PRAGMA schema_version"))
  1693. schema_version = version_result.scalar() or 0
  1694. await conn.execute(text("PRAGMA writable_schema = ON"))
  1695. await conn.execute(
  1696. text(
  1697. "UPDATE sqlite_master "
  1698. "SET sql = replace(sql, 'password_hash VARCHAR(255) NOT NULL', 'password_hash VARCHAR(255)') "
  1699. "WHERE type = 'table' AND name = 'users'"
  1700. )
  1701. )
  1702. await conn.execute(text(f"PRAGMA schema_version = {schema_version + 1}"))
  1703. await conn.execute(text("PRAGMA writable_schema = OFF"))
  1704. except (OperationalError, ProgrammingError) as exc:
  1705. logger.error(
  1706. "Failed to remove NOT NULL from users.password_hash via writable_schema — "
  1707. "OIDC/LDAP user creation will fail on this install: %s",
  1708. exc,
  1709. exc_info=True,
  1710. )
  1711. else:
  1712. await _safe_execute(conn, "ALTER TABLE users ALTER COLUMN password_hash DROP NOT NULL")
  1713. # Migration: Add energy_start_kwh to print_archives (#941)
  1714. # Persists the smart plug lifetime counter captured at print start, so per-print
  1715. # energy tracking survives a backend restart mid-print.
  1716. await _safe_execute(conn, "ALTER TABLE print_archives ADD COLUMN energy_start_kwh REAL")
  1717. # Migration: Add subtask_id to print_archives (#972)
  1718. # MQTT-provided task identifier used to resume the same archive row across a
  1719. # backend restart mid-print. Without it, a long print (e.g. 13h) triggers
  1720. # stale-cancel + new-archive, losing started_at continuity.
  1721. await _safe_execute(conn, "ALTER TABLE print_archives ADD COLUMN subtask_id VARCHAR(64)")
  1722. # Migration: Add bed_type to print_archives (#1253)
  1723. # Build plate type extracted from 3MF (curr_bed_type), drives the bed icon
  1724. # rendered on archive cards.
  1725. await _safe_execute(conn, "ALTER TABLE print_archives ADD COLUMN bed_type VARCHAR(64)")
  1726. # Migration: Add deleted_at to print_archives (#1343)
  1727. # Soft-delete sentinel so deleting an archive entry from the UI no longer
  1728. # wipes its filament / time / cost contribution from Quick Stats. Listings
  1729. # hide rows where deleted_at IS NOT NULL; the stats endpoint counts them all.
  1730. # DATETIME on SQLite, TIMESTAMP on PostgreSQL (PG doesn't accept DATETIME on
  1731. # ALTER TABLE the same way it tolerates it inside CREATE TABLE).
  1732. _deleted_at_type = "DATETIME" if is_sqlite() else "TIMESTAMP"
  1733. await _safe_execute(conn, f"ALTER TABLE print_archives ADD COLUMN deleted_at {_deleted_at_type}")
  1734. await _safe_execute(
  1735. conn,
  1736. "CREATE INDEX IF NOT EXISTS ix_print_archives_deleted_at ON print_archives (deleted_at)",
  1737. )
  1738. # Migration: Create smart_plug_energy_snapshots table (#941)
  1739. # Hourly snapshots of each plug's lifetime counter, so date-range queries in
  1740. # "total consumption" energy mode can compute (last - first) deltas.
  1741. await _safe_execute(
  1742. conn,
  1743. """
  1744. CREATE TABLE IF NOT EXISTS smart_plug_energy_snapshots (
  1745. id INTEGER PRIMARY KEY AUTOINCREMENT,
  1746. plug_id INTEGER NOT NULL REFERENCES smart_plugs(id) ON DELETE CASCADE,
  1747. recorded_at DATETIME NOT NULL,
  1748. lifetime_kwh REAL NOT NULL
  1749. )
  1750. """
  1751. if is_sqlite()
  1752. else """
  1753. CREATE TABLE IF NOT EXISTS smart_plug_energy_snapshots (
  1754. id SERIAL PRIMARY KEY,
  1755. plug_id INTEGER NOT NULL REFERENCES smart_plugs(id) ON DELETE CASCADE,
  1756. recorded_at TIMESTAMP NOT NULL,
  1757. lifetime_kwh REAL NOT NULL
  1758. )
  1759. """,
  1760. )
  1761. await _safe_execute(
  1762. conn,
  1763. "CREATE INDEX IF NOT EXISTS ix_plug_energy_snapshots_plug_time "
  1764. "ON smart_plug_energy_snapshots(plug_id, recorded_at)",
  1765. )
  1766. # Migration: Add PKCE code_verifier column to auth_ephemeral_tokens
  1767. await _safe_execute(conn, "ALTER TABLE auth_ephemeral_tokens ADD COLUMN code_verifier VARCHAR(128)")
  1768. # Migration: Add TOTP replay-protection counter to user_totp
  1769. await _safe_execute(conn, "ALTER TABLE user_totp ADD COLUMN last_totp_counter BIGINT")
  1770. # Migration: Add challenge_id for pre-auth token client binding (HttpOnly cookie)
  1771. await _safe_execute(conn, "ALTER TABLE auth_ephemeral_tokens ADD COLUMN challenge_id VARCHAR(128)")
  1772. # Migration: Add auto_link_existing_accounts column to oidc_providers (M-4)
  1773. # Postgres rejects `DEFAULT 0` for BOOLEAN columns.
  1774. if is_sqlite():
  1775. await _safe_execute(conn, "ALTER TABLE oidc_providers ADD COLUMN auto_link_existing_accounts BOOLEAN DEFAULT 0")
  1776. else:
  1777. await _safe_execute(
  1778. conn, "ALTER TABLE oidc_providers ADD COLUMN auto_link_existing_accounts BOOLEAN DEFAULT false"
  1779. )
  1780. # Migration: Azure Entra ID support — configurable email claim and verification requirement
  1781. await _safe_execute(conn, "ALTER TABLE oidc_providers ADD COLUMN email_claim VARCHAR(64) DEFAULT 'email'")
  1782. # Postgres rejects `DEFAULT 1` for BOOLEAN columns.
  1783. if is_sqlite():
  1784. await _safe_execute(conn, "ALTER TABLE oidc_providers ADD COLUMN require_email_verified BOOLEAN DEFAULT 1")
  1785. else:
  1786. await _safe_execute(conn, "ALTER TABLE oidc_providers ADD COLUMN require_email_verified BOOLEAN DEFAULT true")
  1787. # SEC-1 backfill: reset auto_link only for Fall B (email_claim='email' + require_email_verified=False).
  1788. # Fall C (custom claim) is now allowed to use auto_link — do NOT reset those rows.
  1789. # Runs BEFORE the CHECK constraint below so Fall B rows self-heal rather than failing
  1790. # PostgreSQL's "check constraint is violated by some row" on ADD CONSTRAINT.
  1791. # On fresh installs the column defaults guarantee this UPDATE matches zero rows.
  1792. # TRUE/FALSE literals are accepted by both SQLite (≥ 3.23) and PostgreSQL — no dialect branch needed.
  1793. try:
  1794. async with conn.begin_nested():
  1795. await conn.execute(
  1796. text(
  1797. "UPDATE oidc_providers SET auto_link_existing_accounts = FALSE "
  1798. "WHERE auto_link_existing_accounts = TRUE "
  1799. "AND email_claim = 'email' AND require_email_verified = FALSE"
  1800. )
  1801. )
  1802. except Exception as exc:
  1803. logger.error(
  1804. "SEC-1 safety backfill FAILED — auto_link_existing_accounts may remain enabled "
  1805. "on providers with unsafe email settings: %s",
  1806. exc,
  1807. exc_info=True,
  1808. )
  1809. raise
  1810. # SEC-1: Add DB-level CHECK constraint for existing PostgreSQL installs.
  1811. # SQLite does not support ALTER TABLE ADD CONSTRAINT — handled by __table_args__ at creation.
  1812. # Runs AFTER the backfill so Fall B rows don't fail constraint validation.
  1813. if not is_sqlite():
  1814. try:
  1815. async with conn.begin_nested():
  1816. await conn.execute(
  1817. text(
  1818. "ALTER TABLE oidc_providers ADD CONSTRAINT ck_auto_link_requires_verified_email_claim "
  1819. "CHECK (auto_link_existing_accounts = FALSE OR email_claim != 'email' OR require_email_verified = TRUE)"
  1820. )
  1821. )
  1822. except (OperationalError, ProgrammingError) as exc:
  1823. msg = str(exc).lower()
  1824. if "already exists" not in msg:
  1825. logger.error(
  1826. "Security constraint migration FAILED — auto_link safety constraint may not be enforced: %s",
  1827. exc,
  1828. exc_info=True,
  1829. )
  1830. raise
  1831. # Migration: Update auto_link CHECK constraint formula (existing installs).
  1832. # Existing PostgreSQL installs that ran the ADD CONSTRAINT above with the old formula
  1833. # (or a previous version of this code) need an explicit DROP + ADD to update it.
  1834. # For SQLite, the table is recreated with the new constraint formula if the old formula
  1835. # is still present in sqlite_master (SQLite cannot ALTER TABLE DROP/ADD CONSTRAINT).
  1836. await _migrate_update_auto_link_constraint(conn)
  1837. # Migration: Add default_group_id to oidc_providers.
  1838. # Must run AFTER _migrate_update_auto_link_constraint to avoid being dropped during
  1839. # the SQLite table recreation that function performs on stale-formula databases.
  1840. await _safe_execute(
  1841. conn,
  1842. "ALTER TABLE oidc_providers ADD COLUMN default_group_id INTEGER REFERENCES groups(id) ON DELETE SET NULL",
  1843. )
  1844. # Migration: Add cached-icon columns to oidc_providers (#1333).
  1845. # SPA's strict CSP (img-src 'self' data: blob:) blocks hotlinking external
  1846. # icon hosts, so we proxy them: admin sets icon_url, backend fetches and
  1847. # caches the bytes here, the SPA renders <img src="/api/v1/auth/oidc/providers/{id}/icon">.
  1848. # Must run AFTER _migrate_update_auto_link_constraint for the same reason as
  1849. # default_group_id above (SQLite table recreation drops unknown columns).
  1850. # Dialect-conditional type: BLOB on SQLite, BYTEA on PostgreSQL.
  1851. _blob_type = "BLOB" if is_sqlite() else "BYTEA"
  1852. await _safe_execute(conn, f"ALTER TABLE oidc_providers ADD COLUMN icon_data {_blob_type}")
  1853. await _safe_execute(conn, "ALTER TABLE oidc_providers ADD COLUMN icon_content_type VARCHAR(20)")
  1854. await _safe_execute(conn, "ALTER TABLE oidc_providers ADD COLUMN icon_etag VARCHAR(64)")
  1855. # PostgreSQL-only: enforce the all-or-nothing triplet at the DB layer.
  1856. # SQLite cannot ADD CONSTRAINT to an existing table — fresh SQLite
  1857. # installs get the CHECK via metadata.create_all (model __table_args__);
  1858. # stale SQLite installs rely on the application layer, same trade-off
  1859. # as the default_group_id FK ON DELETE SET NULL above.
  1860. if not is_sqlite():
  1861. await _safe_execute(
  1862. conn,
  1863. "ALTER TABLE oidc_providers ADD CONSTRAINT ck_oidc_icon_triplet_co_null "
  1864. "CHECK ((icon_data IS NULL) = (icon_content_type IS NULL) "
  1865. "AND (icon_content_type IS NULL) = (icon_etag IS NULL))",
  1866. )
  1867. # Migration: Add password_changed_at to users (M-R7-B)
  1868. # Tracks the last time a user's password was changed/reset. JWTs whose iat
  1869. # predates this timestamp are rejected in all six auth validation paths.
  1870. # R4 fix: TIMESTAMP is accepted by both SQLite and PostgreSQL; DATETIME
  1871. # is rejected by Postgres ("type 'datetime' does not exist"), which made
  1872. # _safe_execute swallow the error and leave existing Postgres installs
  1873. # without the column — causing UndefinedColumnError on every User query.
  1874. await _safe_execute(conn, "ALTER TABLE users ADD COLUMN password_changed_at TIMESTAMP")
  1875. # Migration: Back-fill password_changed_at = created_at for existing users (I2).
  1876. # Users who never changed their password would have NULL here, meaning old
  1877. # tokens could never be invalidated via the freshness check. Setting it to
  1878. # created_at is conservative: any token issued before the account was created
  1879. # is always invalid, so this is a safe lower bound.
  1880. async with conn.begin_nested():
  1881. await conn.execute(text("UPDATE users SET password_changed_at = created_at WHERE password_changed_at IS NULL"))
  1882. # Migration: Provenance columns on library_files for MakerWorld imports.
  1883. # source_url is indexed so "already imported" dedupe lookups stay O(log N)
  1884. # as the library grows.
  1885. await _safe_execute(conn, "ALTER TABLE library_files ADD COLUMN source_type VARCHAR(32)")
  1886. await _safe_execute(conn, "ALTER TABLE library_files ADD COLUMN source_url VARCHAR(512)")
  1887. await _safe_execute(
  1888. conn,
  1889. "CREATE INDEX IF NOT EXISTS ix_library_files_source_url ON library_files(source_url)",
  1890. )
  1891. # Migration: Cache metadata title on pending uploads (#1152 follow-up).
  1892. # Without this column the review card always shows the FTP filename while
  1893. # the eventual archive's print_name comes from the 3MF metadata title,
  1894. # creating a confusing review→archive name mismatch. Captured at upload
  1895. # time so /pending-uploads/ list calls don't have to reopen each 3MF.
  1896. await _safe_execute(
  1897. conn,
  1898. "ALTER TABLE pending_uploads ADD COLUMN metadata_print_name VARCHAR(255)",
  1899. )
  1900. # Migration: Per-user API key ownership + cloud-access scope (#1182).
  1901. # user_id is nullable so legacy keys (created before #1182) survive the
  1902. # migration; cloud routes reject calls from keys without an owner so the
  1903. # operator is forced to recreate them. ON DELETE CASCADE so deleting a user
  1904. # takes their keys with them — orphan keys must never authenticate.
  1905. # SQLite ignores REFERENCES on ADD COLUMN (not enforced but not an error);
  1906. # PostgreSQL enforces the FK from this point forward. Indexed for the
  1907. # auth-gate's owner→keys lookup that runs on every API-keyed request.
  1908. await _safe_execute(
  1909. conn,
  1910. "ALTER TABLE api_keys ADD COLUMN user_id INTEGER REFERENCES users(id) ON DELETE CASCADE",
  1911. )
  1912. await _safe_execute(
  1913. conn,
  1914. "CREATE INDEX IF NOT EXISTS ix_api_keys_user_id ON api_keys(user_id)",
  1915. )
  1916. # ``DEFAULT 0`` works on SQLite (boolean is just integer-coerced) but
  1917. # asyncpg's strict type-check rejects it: "column is of type boolean but
  1918. # default expression is of type integer". Use ``DEFAULT FALSE`` so both
  1919. # dialects accept the same statement — same pattern as the print_queue
  1920. # gcode_injection migration above.
  1921. await _safe_execute(
  1922. conn,
  1923. "ALTER TABLE api_keys ADD COLUMN can_access_cloud BOOLEAN DEFAULT FALSE",
  1924. )
  1925. # Narrowly-scoped settings-write toggle for the dynamic-tariff push case
  1926. # documented in wiki/features/energy.md (#1356). Defaults FALSE so existing
  1927. # keys never silently gain settings-write capability on upgrade.
  1928. await _safe_execute(
  1929. conn,
  1930. "ALTER TABLE api_keys ADD COLUMN can_update_energy_cost BOOLEAN DEFAULT FALSE",
  1931. )
  1932. # Migration: Soft-delete column for trash bin (Issue #1008). Indexed so the
  1933. # sweeper's "SELECT ... WHERE deleted_at < cutoff" and the trash list's
  1934. # "WHERE deleted_at IS NOT NULL" stay cheap as the table grows.
  1935. #
  1936. # ``DATETIME`` is a SQLite-only type alias — PostgreSQL rejects it as
  1937. # invalid syntax, _safe_execute swallows the error, and the column is
  1938. # never added (breaking every query that references it). Emit
  1939. # dialect-appropriate SQL so both backends get the column.
  1940. if is_sqlite():
  1941. await _safe_execute(conn, "ALTER TABLE library_files ADD COLUMN deleted_at DATETIME")
  1942. else:
  1943. await _safe_execute(conn, "ALTER TABLE library_files ADD COLUMN deleted_at TIMESTAMP")
  1944. await _safe_execute(
  1945. conn,
  1946. "CREATE INDEX IF NOT EXISTS ix_library_files_deleted_at ON library_files(deleted_at)",
  1947. )
  1948. # Legacy SQLite installs created `settings` without a UNIQUE constraint on `key`,
  1949. # so `INSERT OR IGNORE` below silently degrades to a plain INSERT and dupes rows on
  1950. # every restart. Dedupe (keep lowest id per key) and add the missing unique index
  1951. # before seeding. Safe/idempotent on both dialects — fresh installs already have
  1952. # no dupes and `create_all` already emits the index.
  1953. async with conn.begin_nested():
  1954. await conn.execute(text("DELETE FROM settings WHERE id NOT IN (SELECT MIN(id) FROM settings GROUP BY key)"))
  1955. await _safe_execute(conn, "CREATE UNIQUE INDEX IF NOT EXISTS ix_settings_key ON settings(key)")
  1956. # Migration: Normalise provider_email to lowercase (SEC-3).
  1957. # Required for Entra ID where UPN/email claims may arrive in mixed case.
  1958. # LOWER() is supported by both SQLite and PostgreSQL; the UPDATE is idempotent.
  1959. # Executed directly (not via _safe_execute) so any column-reference failure
  1960. # is always fatal and never silently swallowed.
  1961. async with conn.begin_nested():
  1962. await conn.execute(
  1963. text(
  1964. "UPDATE user_oidc_links SET provider_email = LOWER(provider_email) "
  1965. "WHERE provider_email IS NOT NULL AND provider_email != LOWER(provider_email)"
  1966. )
  1967. )
  1968. # Migration: Create spoolman_slot_assignments table for local AMS-slot→Spoolman-spool mapping.
  1969. # Replaces the pattern of writing spool.location in Spoolman (which polluted the
  1970. # user-editable storage_location field in the UI).
  1971. # ck_ams_id_range formula was widened in #1274 to admit AMS-HT (ams_id 128-191).
  1972. await _safe_execute(
  1973. conn,
  1974. """
  1975. CREATE TABLE IF NOT EXISTS spoolman_slot_assignments (
  1976. id INTEGER PRIMARY KEY AUTOINCREMENT,
  1977. printer_id INTEGER NOT NULL REFERENCES printers(id) ON DELETE CASCADE,
  1978. ams_id INTEGER NOT NULL CHECK ((ams_id >= 0 AND ams_id <= 7) OR (ams_id >= 128 AND ams_id <= 191) OR ams_id = 255),
  1979. tray_id INTEGER NOT NULL CHECK (tray_id >= 0 AND tray_id <= 3),
  1980. spoolman_spool_id INTEGER NOT NULL,
  1981. assigned_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  1982. CONSTRAINT uq_slot_assignment UNIQUE(printer_id, ams_id, tray_id)
  1983. )
  1984. """
  1985. if is_sqlite()
  1986. else """
  1987. CREATE TABLE IF NOT EXISTS spoolman_slot_assignments (
  1988. id SERIAL PRIMARY KEY,
  1989. printer_id INTEGER NOT NULL REFERENCES printers(id) ON DELETE CASCADE,
  1990. ams_id INTEGER NOT NULL CHECK ((ams_id >= 0 AND ams_id <= 7) OR (ams_id >= 128 AND ams_id <= 191) OR ams_id = 255),
  1991. tray_id INTEGER NOT NULL CHECK (tray_id >= 0 AND tray_id <= 3),
  1992. spoolman_spool_id INTEGER NOT NULL,
  1993. assigned_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  1994. CONSTRAINT uq_slot_assignment UNIQUE(printer_id, ams_id, tray_id)
  1995. )
  1996. """,
  1997. )
  1998. await _safe_execute(
  1999. conn,
  2000. "CREATE INDEX IF NOT EXISTS ix_slot_assignment_spool ON spoolman_slot_assignments (spoolman_spool_id)",
  2001. )
  2002. # Migration: widen ck_ams_id_range on spoolman_slot_assignments to allow
  2003. # AMS-HT ids (128-191). Existing installs created before #1274 carry the
  2004. # stale formula which rejects every AMS-HT slot link with a CHECK violation.
  2005. await _migrate_widen_spoolman_slot_ams_id_range(conn)
  2006. # Migration: Create spoolman_k_profile table for K-value calibration profiles linked to Spoolman spools.
  2007. await _safe_execute(
  2008. conn,
  2009. """
  2010. CREATE TABLE IF NOT EXISTS spoolman_k_profile (
  2011. id INTEGER PRIMARY KEY AUTOINCREMENT,
  2012. spoolman_spool_id INTEGER NOT NULL,
  2013. printer_id INTEGER NOT NULL REFERENCES printers(id) ON DELETE CASCADE,
  2014. extruder INTEGER NOT NULL DEFAULT 0 CHECK (extruder >= 0 AND extruder <= 1),
  2015. nozzle_diameter VARCHAR(10) NOT NULL DEFAULT '0.4',
  2016. nozzle_type VARCHAR(50),
  2017. k_value REAL NOT NULL,
  2018. name VARCHAR(100),
  2019. cali_idx INTEGER,
  2020. setting_id VARCHAR(50),
  2021. created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  2022. CONSTRAINT uq_spoolman_k_profile UNIQUE(spoolman_spool_id, printer_id, extruder, nozzle_diameter)
  2023. )
  2024. """
  2025. if is_sqlite()
  2026. else """
  2027. CREATE TABLE IF NOT EXISTS spoolman_k_profile (
  2028. id SERIAL PRIMARY KEY,
  2029. spoolman_spool_id INTEGER NOT NULL,
  2030. printer_id INTEGER NOT NULL REFERENCES printers(id) ON DELETE CASCADE,
  2031. extruder INTEGER NOT NULL DEFAULT 0 CHECK (extruder >= 0 AND extruder <= 1),
  2032. nozzle_diameter VARCHAR(10) NOT NULL DEFAULT '0.4',
  2033. nozzle_type VARCHAR(50),
  2034. k_value DOUBLE PRECISION NOT NULL,
  2035. name VARCHAR(100),
  2036. cali_idx INTEGER,
  2037. setting_id VARCHAR(50),
  2038. created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  2039. CONSTRAINT uq_spoolman_k_profile UNIQUE(spoolman_spool_id, printer_id, extruder, nozzle_diameter)
  2040. )
  2041. """,
  2042. )
  2043. await _safe_execute(
  2044. conn,
  2045. "CREATE INDEX IF NOT EXISTS ix_spoolman_k_profile_spool ON spoolman_k_profile (spoolman_spool_id)",
  2046. )
  2047. # Migration: Add provider column to github_backup_config for multi-provider support
  2048. await _safe_execute(conn, "ALTER TABLE github_backup_config ADD COLUMN provider VARCHAR(30) DEFAULT 'github'")
  2049. # Migration: Add allow_insecure_http column to github_backup_config for self-hosted HTTP instances
  2050. await _safe_execute(conn, "ALTER TABLE github_backup_config ADD COLUMN allow_insecure_http BOOLEAN DEFAULT FALSE")
  2051. # Seed default settings keys that must exist on fresh install
  2052. default_settings = [
  2053. ("advanced_auth_enabled", "false"),
  2054. ("smtp_auth_enabled", "true"),
  2055. ]
  2056. for key, value in default_settings:
  2057. try:
  2058. if is_sqlite():
  2059. await conn.execute(
  2060. text("INSERT OR IGNORE INTO settings (key, value) VALUES (:key, :value)"),
  2061. {"key": key, "value": value},
  2062. )
  2063. else:
  2064. await conn.execute(
  2065. text("INSERT INTO settings (key, value) VALUES (:key, :value) ON CONFLICT (key) DO NOTHING"),
  2066. {"key": key, "value": value},
  2067. )
  2068. except (OperationalError, ProgrammingError):
  2069. pass
  2070. # Migration: Create filament_sku_settings table for reorder forecasting
  2071. if is_sqlite():
  2072. await _safe_execute(
  2073. conn,
  2074. """CREATE TABLE IF NOT EXISTS filament_sku_settings (
  2075. id INTEGER PRIMARY KEY AUTOINCREMENT,
  2076. material VARCHAR(50) NOT NULL,
  2077. subtype VARCHAR(50),
  2078. brand VARCHAR(100),
  2079. lead_time_days INTEGER NOT NULL DEFAULT 0,
  2080. safety_margin_value INTEGER NOT NULL DEFAULT 14,
  2081. safety_margin_unit VARCHAR(10) NOT NULL DEFAULT 'days',
  2082. created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  2083. updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  2084. UNIQUE (material, subtype, brand)
  2085. )""",
  2086. )
  2087. async with conn.begin_nested():
  2088. await conn.execute(text("UPDATE filament_sku_settings SET lead_time_days = 0 WHERE lead_time_days = 7"))
  2089. await _safe_execute(
  2090. conn, "ALTER TABLE filament_sku_settings ADD COLUMN safety_margin_value INTEGER NOT NULL DEFAULT 14"
  2091. )
  2092. await _safe_execute(
  2093. conn, "ALTER TABLE filament_sku_settings ADD COLUMN safety_margin_unit VARCHAR(10) NOT NULL DEFAULT 'days'"
  2094. )
  2095. await _safe_execute(
  2096. conn, "ALTER TABLE filament_sku_settings ADD COLUMN alerts_snoozed BOOLEAN NOT NULL DEFAULT 0"
  2097. )
  2098. # Backfill and drop legacy safety_margin_days column — SQLite requires a table rebuild.
  2099. # Only run if the stale column still exists.
  2100. cols_result = await conn.execute(text("PRAGMA table_info(filament_sku_settings)"))
  2101. col_names = [row[1] for row in cols_result.fetchall()]
  2102. if "safety_margin_days" in col_names:
  2103. async with conn.begin_nested():
  2104. # Defensive: a previous startup may have crashed mid-rebuild leaving
  2105. # filament_sku_settings_new behind, which would break the CREATE below.
  2106. await conn.execute(text("DROP TABLE IF EXISTS filament_sku_settings_new"))
  2107. await conn.execute(
  2108. text(
  2109. "UPDATE filament_sku_settings SET safety_margin_value = safety_margin_days "
  2110. "WHERE safety_margin_value = 14 AND safety_margin_days != 14"
  2111. )
  2112. )
  2113. await conn.execute(
  2114. text(
  2115. """CREATE TABLE filament_sku_settings_new (
  2116. id INTEGER PRIMARY KEY AUTOINCREMENT,
  2117. material VARCHAR(50) NOT NULL,
  2118. subtype VARCHAR(50),
  2119. brand VARCHAR(100),
  2120. lead_time_days INTEGER NOT NULL DEFAULT 0,
  2121. safety_margin_value INTEGER NOT NULL DEFAULT 14,
  2122. safety_margin_unit VARCHAR(10) NOT NULL DEFAULT 'days',
  2123. alerts_snoozed BOOLEAN NOT NULL DEFAULT 0,
  2124. created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  2125. updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  2126. UNIQUE (material, subtype, brand)
  2127. )"""
  2128. )
  2129. )
  2130. await conn.execute(
  2131. text(
  2132. """INSERT INTO filament_sku_settings_new
  2133. (id, material, subtype, brand, lead_time_days, safety_margin_value,
  2134. safety_margin_unit, alerts_snoozed, created_at, updated_at)
  2135. SELECT id, material, subtype, brand, lead_time_days, safety_margin_value,
  2136. safety_margin_unit, COALESCE(alerts_snoozed, 0), created_at, updated_at
  2137. FROM filament_sku_settings"""
  2138. )
  2139. )
  2140. await conn.execute(text("DROP TABLE filament_sku_settings"))
  2141. await conn.execute(text("ALTER TABLE filament_sku_settings_new RENAME TO filament_sku_settings"))
  2142. await _safe_execute(
  2143. conn,
  2144. """CREATE TABLE IF NOT EXISTS filament_shopping_list (
  2145. id INTEGER PRIMARY KEY AUTOINCREMENT,
  2146. material VARCHAR(50) NOT NULL,
  2147. subtype VARCHAR(50),
  2148. brand VARCHAR(100),
  2149. quantity_spools INTEGER NOT NULL DEFAULT 1,
  2150. note VARCHAR(500),
  2151. status VARCHAR(20) NOT NULL DEFAULT 'pending',
  2152. purchased_at DATETIME,
  2153. added_at DATETIME DEFAULT CURRENT_TIMESTAMP
  2154. )""",
  2155. )
  2156. # SQLite has no implicit updated_at trigger — add one so the column stays current.
  2157. await _safe_execute(
  2158. conn,
  2159. """CREATE TRIGGER IF NOT EXISTS trg_filament_sku_settings_updated_at
  2160. AFTER UPDATE ON filament_sku_settings FOR EACH ROW
  2161. BEGIN
  2162. UPDATE filament_sku_settings SET updated_at = CURRENT_TIMESTAMP WHERE id = OLD.id;
  2163. END""",
  2164. )
  2165. else:
  2166. await _safe_execute(
  2167. conn,
  2168. """CREATE TABLE IF NOT EXISTS filament_sku_settings (
  2169. id SERIAL PRIMARY KEY,
  2170. material VARCHAR(50) NOT NULL,
  2171. subtype VARCHAR(50),
  2172. brand VARCHAR(100),
  2173. lead_time_days INTEGER NOT NULL DEFAULT 0,
  2174. safety_margin_value INTEGER NOT NULL DEFAULT 14,
  2175. safety_margin_unit VARCHAR(10) NOT NULL DEFAULT 'days',
  2176. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  2177. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  2178. UNIQUE (material, subtype, brand)
  2179. )""",
  2180. )
  2181. async with conn.begin_nested():
  2182. await conn.execute(text("UPDATE filament_sku_settings SET lead_time_days = 0 WHERE lead_time_days = 7"))
  2183. await _safe_execute(
  2184. conn,
  2185. "ALTER TABLE filament_sku_settings ADD COLUMN IF NOT EXISTS safety_margin_value INTEGER NOT NULL DEFAULT 14",
  2186. )
  2187. await _safe_execute(
  2188. conn,
  2189. "ALTER TABLE filament_sku_settings ADD COLUMN IF NOT EXISTS safety_margin_unit VARCHAR(10) NOT NULL DEFAULT 'days'",
  2190. )
  2191. await _safe_execute(
  2192. conn,
  2193. "ALTER TABLE filament_sku_settings ADD COLUMN IF NOT EXISTS alerts_snoozed BOOLEAN NOT NULL DEFAULT FALSE",
  2194. )
  2195. # Only backfill from safety_margin_days if that column still exists (PostgreSQL).
  2196. col_check = await conn.execute(
  2197. text(
  2198. "SELECT 1 FROM information_schema.columns "
  2199. "WHERE table_name = 'filament_sku_settings' AND column_name = 'safety_margin_days'"
  2200. )
  2201. )
  2202. if col_check.fetchone():
  2203. async with conn.begin_nested():
  2204. await conn.execute(
  2205. text(
  2206. "UPDATE filament_sku_settings SET safety_margin_value = safety_margin_days "
  2207. "WHERE safety_margin_value = 14 AND safety_margin_days != 14"
  2208. )
  2209. )
  2210. await _safe_execute(
  2211. conn,
  2212. """CREATE TABLE IF NOT EXISTS filament_shopping_list (
  2213. id SERIAL PRIMARY KEY,
  2214. material VARCHAR(50) NOT NULL,
  2215. subtype VARCHAR(50),
  2216. brand VARCHAR(100),
  2217. quantity_spools INTEGER NOT NULL DEFAULT 1,
  2218. note VARCHAR(500),
  2219. status VARCHAR(20) NOT NULL DEFAULT 'pending',
  2220. purchased_at TIMESTAMP,
  2221. added_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  2222. )""",
  2223. )
  2224. await _safe_execute(
  2225. conn,
  2226. "ALTER TABLE filament_shopping_list ADD COLUMN IF NOT EXISTS status VARCHAR(20) NOT NULL DEFAULT 'pending'",
  2227. )
  2228. await _safe_execute(conn, "ALTER TABLE filament_shopping_list ADD COLUMN IF NOT EXISTS purchased_at TIMESTAMP")
  2229. # Migration: Add inventory stock alert columns to notification_providers.
  2230. # Postgres rejects `DEFAULT 0` for BOOLEAN columns.
  2231. if is_sqlite():
  2232. await _safe_execute(
  2233. conn, "ALTER TABLE notification_providers ADD COLUMN on_stock_reorder_alert BOOLEAN DEFAULT 0"
  2234. )
  2235. await _safe_execute(
  2236. conn, "ALTER TABLE notification_providers ADD COLUMN on_stock_break_alert BOOLEAN DEFAULT 0"
  2237. )
  2238. else:
  2239. await _safe_execute(
  2240. conn, "ALTER TABLE notification_providers ADD COLUMN on_stock_reorder_alert BOOLEAN DEFAULT false"
  2241. )
  2242. await _safe_execute(
  2243. conn, "ALTER TABLE notification_providers ADD COLUMN on_stock_break_alert BOOLEAN DEFAULT false"
  2244. )
  2245. # Migration: Heal orphan auth-related rows left behind by user-delete
  2246. # on SQLite. user_oidc_links, user_totp, user_otp_codes (introduced in
  2247. # PR #933) and long_lived_tokens (PR #1108) all declare ON DELETE
  2248. # CASCADE on user_id — both predate the explicit APIKey-cleanup
  2249. # pattern in PR #1182. PostgreSQL enforces the cascade, but SQLite
  2250. # ships with FK enforcement off, so rows pointing to a deleted user
  2251. # persisted — blocking SSO re-login (the OIDC callback finds the
  2252. # orphan link, fails to resolve the missing user, and falls through
  2253. # to "account_inactive" instead of triggering auto_create), leaking
  2254. # MFA secrets, and leaving camera-stream tokens whose secret_hash is
  2255. # still verify()-able by lookup_prefix. See issue #1285 (#1295 review
  2256. # extended the cleanup to long_lived_tokens). This migration is a
  2257. # no-op on PostgreSQL and idempotent on SQLite.
  2258. async with conn.begin_nested():
  2259. oidc_result = await conn.execute(
  2260. text("DELETE FROM user_oidc_links WHERE user_id NOT IN (SELECT id FROM users)")
  2261. )
  2262. totp_result = await conn.execute(text("DELETE FROM user_totp WHERE user_id NOT IN (SELECT id FROM users)"))
  2263. otp_result = await conn.execute(text("DELETE FROM user_otp_codes WHERE user_id NOT IN (SELECT id FROM users)"))
  2264. llt_result = await conn.execute(
  2265. text("DELETE FROM long_lived_tokens WHERE user_id NOT IN (SELECT id FROM users)")
  2266. )
  2267. oidc_n = oidc_result.rowcount or 0
  2268. totp_n = totp_result.rowcount or 0
  2269. otp_n = otp_result.rowcount or 0
  2270. llt_n = llt_result.rowcount or 0
  2271. if oidc_n or totp_n or otp_n or llt_n:
  2272. logger.info(
  2273. "Cleaned up orphan auth rows: %d OIDC links, %d TOTP, %d OTP codes, %d long-lived tokens",
  2274. oidc_n,
  2275. totp_n,
  2276. otp_n,
  2277. llt_n,
  2278. )
  2279. # Migration: extend print_log_entries with archive_id, cost, energy, failure_reason,
  2280. # created_by_id (#1378). Statistics queries shift from PrintArchive to PrintLogEntry
  2281. # so reprints contribute new rows instead of overwriting the source archive's data.
  2282. if is_sqlite():
  2283. await _safe_execute(conn, "ALTER TABLE print_log_entries ADD COLUMN archive_id INTEGER")
  2284. await _safe_execute(conn, "ALTER TABLE print_log_entries ADD COLUMN cost REAL")
  2285. await _safe_execute(conn, "ALTER TABLE print_log_entries ADD COLUMN energy_kwh REAL")
  2286. await _safe_execute(conn, "ALTER TABLE print_log_entries ADD COLUMN energy_cost REAL")
  2287. await _safe_execute(conn, "ALTER TABLE print_log_entries ADD COLUMN failure_reason VARCHAR(100)")
  2288. await _safe_execute(conn, "ALTER TABLE print_log_entries ADD COLUMN created_by_id INTEGER")
  2289. else:
  2290. await _safe_execute(conn, "ALTER TABLE print_log_entries ADD COLUMN IF NOT EXISTS archive_id INTEGER")
  2291. await _safe_execute(conn, "ALTER TABLE print_log_entries ADD COLUMN IF NOT EXISTS cost DOUBLE PRECISION")
  2292. await _safe_execute(conn, "ALTER TABLE print_log_entries ADD COLUMN IF NOT EXISTS energy_kwh DOUBLE PRECISION")
  2293. await _safe_execute(conn, "ALTER TABLE print_log_entries ADD COLUMN IF NOT EXISTS energy_cost DOUBLE PRECISION")
  2294. await _safe_execute(conn, "ALTER TABLE print_log_entries ADD COLUMN IF NOT EXISTS failure_reason VARCHAR(100)")
  2295. await _safe_execute(conn, "ALTER TABLE print_log_entries ADD COLUMN IF NOT EXISTS created_by_id INTEGER")
  2296. await _safe_execute(
  2297. conn, "CREATE INDEX IF NOT EXISTS ix_print_log_entries_archive_id ON print_log_entries (archive_id)"
  2298. )
  2299. # Backfill PrintLogEntry → PrintArchive linkage and per-event cost/energy
  2300. # for pre-#1378 rows the column-add migration left NULL (#1390).
  2301. #
  2302. # Without this backfill the user's Quick Stats show Filament Cost = 0 and
  2303. # Time Accuracy empty even though their archives carry both, because:
  2304. #
  2305. # - the new stats queries SUM PrintLogEntry.cost (NULL for old rows)
  2306. # - the time-accuracy query JOINs PrintArchive ON archive_id (NULL for
  2307. # old rows, so old runs get excluded from the average)
  2308. #
  2309. # Pre-#1378, archive.cost / energy_kwh / energy_cost were overwritten by
  2310. # each rerun, so the current archive values represent the *latest* run.
  2311. # Backfilling them onto the latest matching PrintLogEntry per archive
  2312. # reconstructs the pre-fix total exactly (sum across archives stays
  2313. # unchanged), and leaves earlier reprints with NULL cost so they
  2314. # contribute zero — matching the "first/latest writes, rest stay NULL"
  2315. # convention #1378 introduced for new prints.
  2316. #
  2317. # DML, not DDL — use conn.execute() inside a savepoint per _safe_execute's
  2318. # own docstring. SQL is plain ANSI (correlated UPDATE, MAX/GROUP BY/HAVING,
  2319. # CASE in HAVING) and runs unchanged on SQLite + PostgreSQL; verified
  2320. # against postgres:16-alpine + asyncpg.
  2321. #
  2322. # Step 1: link old log entries to their archive via print_name + printer_id.
  2323. # Picks the highest-id matching archive when multiple share the same key
  2324. # (newest archive wins — closest to the log's overwrite-then-leave shape).
  2325. from sqlalchemy import text as _text
  2326. async with conn.begin_nested():
  2327. await conn.execute(
  2328. _text("""
  2329. UPDATE print_log_entries
  2330. SET archive_id = (
  2331. SELECT a.id
  2332. FROM print_archives a
  2333. WHERE a.print_name = print_log_entries.print_name
  2334. AND (
  2335. a.printer_id = print_log_entries.printer_id
  2336. OR (a.printer_id IS NULL AND print_log_entries.printer_id IS NULL)
  2337. )
  2338. ORDER BY a.id DESC
  2339. LIMIT 1
  2340. )
  2341. WHERE archive_id IS NULL AND print_name IS NOT NULL
  2342. """)
  2343. )
  2344. # Step 2: backfill cost / energy_kwh / energy_cost onto the latest linked
  2345. # log entry per archive — the row whose creation time best matches the
  2346. # value currently stored on the archive (overwrite-on-reprint semantics
  2347. # under the old design). Only fires for archives where NO log entry has
  2348. # cost set yet, which gives the migration a clean idempotency property:
  2349. # the second pass sees the archive already has a cost-bearing run and
  2350. # leaves the rest of its history NULL (instead of marching up the
  2351. # ID-ordered list of NULL runs on every pass).
  2352. async with conn.begin_nested():
  2353. await conn.execute(
  2354. _text("""
  2355. UPDATE print_log_entries
  2356. SET cost = (SELECT cost FROM print_archives WHERE id = print_log_entries.archive_id),
  2357. energy_kwh = (SELECT energy_kwh FROM print_archives WHERE id = print_log_entries.archive_id),
  2358. energy_cost = (SELECT energy_cost FROM print_archives WHERE id = print_log_entries.archive_id)
  2359. WHERE id IN (
  2360. SELECT MAX(id)
  2361. FROM print_log_entries
  2362. WHERE archive_id IS NOT NULL
  2363. GROUP BY archive_id
  2364. HAVING SUM(CASE WHEN cost IS NOT NULL THEN 1 ELSE 0 END) = 0
  2365. )
  2366. """)
  2367. )
  2368. # Migration: smart_plugs gets per-plug auto-off-after-drying toggle and
  2369. # delay (#1349). Fires whenever any AMS attached to the linked printer
  2370. # finishes a dry cycle. Plain ANSI ALTER TABLE works on both SQLite and
  2371. # Postgres for INTEGER/BOOLEAN with simple defaults.
  2372. if is_sqlite():
  2373. await _safe_execute(conn, "ALTER TABLE smart_plugs ADD COLUMN auto_off_after_drying BOOLEAN DEFAULT 0")
  2374. await _safe_execute(
  2375. conn, "ALTER TABLE smart_plugs ADD COLUMN off_delay_after_drying_minutes INTEGER DEFAULT 10"
  2376. )
  2377. else:
  2378. await _safe_execute(
  2379. conn,
  2380. "ALTER TABLE smart_plugs ADD COLUMN IF NOT EXISTS auto_off_after_drying BOOLEAN DEFAULT false",
  2381. )
  2382. await _safe_execute(
  2383. conn,
  2384. "ALTER TABLE smart_plugs ADD COLUMN IF NOT EXISTS off_delay_after_drying_minutes INTEGER DEFAULT 10",
  2385. )
  2386. # Data migration: drop the embedded 3MF Title (`print_name`) from library
  2387. # file metadata so the FileManager displays the filename, not the title (#1489).
  2388. await _migrate_drop_library_print_name(conn)
  2389. async def seed_notification_templates():
  2390. """Seed default notification templates if they don't exist."""
  2391. from sqlalchemy import select
  2392. from backend.app.models.notification_template import DEFAULT_TEMPLATES, NotificationTemplate
  2393. async with async_session() as session:
  2394. # Get existing template event types
  2395. result = await session.execute(select(NotificationTemplate.event_type))
  2396. existing_types = {row[0] for row in result.fetchall()}
  2397. if not existing_types:
  2398. # No templates exist - insert all defaults
  2399. for template_data in DEFAULT_TEMPLATES:
  2400. template = NotificationTemplate(
  2401. event_type=template_data["event_type"],
  2402. name=template_data["name"],
  2403. title_template=template_data["title_template"],
  2404. body_template=template_data["body_template"],
  2405. is_default=True,
  2406. )
  2407. session.add(template)
  2408. else:
  2409. # Templates exist - only add missing ones
  2410. for template_data in DEFAULT_TEMPLATES:
  2411. if template_data["event_type"] not in existing_types:
  2412. template = NotificationTemplate(
  2413. event_type=template_data["event_type"],
  2414. name=template_data["name"],
  2415. title_template=template_data["title_template"],
  2416. body_template=template_data["body_template"],
  2417. is_default=True,
  2418. )
  2419. session.add(template)
  2420. await session.commit()
  2421. async def seed_default_groups():
  2422. """Seed default groups and migrate existing users to appropriate groups.
  2423. Creates the default system groups (Administrators, Operators, Viewers) if they
  2424. don't exist, then migrates existing users:
  2425. - Users with role='admin' -> Administrators group
  2426. - Users with role='user' -> Operators group
  2427. Also migrates old permissions to new ownership-based permissions (Issue #205).
  2428. """
  2429. import logging
  2430. from sqlalchemy import select
  2431. from backend.app.core.permissions import DEFAULT_GROUPS
  2432. from backend.app.models.group import Group
  2433. from backend.app.models.user import User
  2434. logger = logging.getLogger(__name__)
  2435. # Map old permissions to new ones for migration
  2436. # Administrators get *_all permissions, Operators get *_own permissions
  2437. PERMISSION_MIGRATION_ALL = {
  2438. "queue:update": "queue:update_all",
  2439. "queue:delete": "queue:delete_all",
  2440. "archives:update": "archives:update_all",
  2441. "archives:delete": "archives:delete_all",
  2442. "archives:reprint": "archives:reprint_all",
  2443. "library:update": "library:update_all",
  2444. "library:delete": "library:delete_all",
  2445. }
  2446. PERMISSION_MIGRATION_OWN = {
  2447. "queue:update": "queue:update_own",
  2448. "queue:delete": "queue:delete_own",
  2449. "archives:update": "archives:update_own",
  2450. "archives:delete": "archives:delete_own",
  2451. "archives:reprint": "archives:reprint_own",
  2452. "library:update": "library:update_own",
  2453. "library:delete": "library:delete_own",
  2454. }
  2455. async with async_session() as session:
  2456. # Get existing groups
  2457. result = await session.execute(select(Group))
  2458. existing_groups = {group.name: group for group in result.scalars().all()}
  2459. # Create default groups if they don't exist
  2460. groups_created = []
  2461. for group_name, group_config in DEFAULT_GROUPS.items():
  2462. if group_name not in existing_groups:
  2463. group = Group(
  2464. name=group_name,
  2465. description=group_config["description"],
  2466. permissions=group_config["permissions"],
  2467. is_system=group_config["is_system"],
  2468. )
  2469. session.add(group)
  2470. groups_created.append(group_name)
  2471. logger.info("Created default group: %s", group_name)
  2472. else:
  2473. # Migrate existing group's permissions from old to new format
  2474. group = existing_groups[group_name]
  2475. if group.permissions:
  2476. updated = False
  2477. new_permissions = list(group.permissions)
  2478. # Determine which migration map to use based on group
  2479. migration_map = (
  2480. PERMISSION_MIGRATION_ALL if group_name == "Administrators" else PERMISSION_MIGRATION_OWN
  2481. )
  2482. for old_perm, new_perm in migration_map.items():
  2483. if old_perm in new_permissions:
  2484. new_permissions.remove(old_perm)
  2485. if new_perm not in new_permissions:
  2486. new_permissions.append(new_perm)
  2487. updated = True
  2488. logger.info(
  2489. "Migrated permission '%s' to '%s' in group '%s'", old_perm, new_perm, group_name
  2490. )
  2491. # For Administrators, also ensure they get *_all permissions if they have any new *_own
  2492. if group_name == "Administrators":
  2493. for _own_perm, all_perm in [
  2494. ("queue:update_own", "queue:update_all"),
  2495. ("queue:delete_own", "queue:delete_all"),
  2496. ("archives:update_own", "archives:update_all"),
  2497. ("archives:delete_own", "archives:delete_all"),
  2498. ("archives:reprint_own", "archives:reprint_all"),
  2499. ("library:update_own", "library:update_all"),
  2500. ("library:delete_own", "library:delete_all"),
  2501. ]:
  2502. # Add *_all if not present
  2503. if all_perm not in new_permissions:
  2504. new_permissions.append(all_perm)
  2505. updated = True
  2506. if updated:
  2507. group.permissions = new_permissions
  2508. await session.commit()
  2509. # Migrate new permissions: grant printers:clear_plate to all groups with printers:control
  2510. result = await session.execute(select(Group))
  2511. all_groups = result.scalars().all()
  2512. for group in all_groups:
  2513. if (
  2514. group.permissions
  2515. and "printers:control" in group.permissions
  2516. and "printers:clear_plate" not in group.permissions
  2517. ):
  2518. group.permissions = [*group.permissions, "printers:clear_plate"]
  2519. logger.info("Added printers:clear_plate to group '%s' (has printers:control)", group.name)
  2520. await session.commit()
  2521. # Migrate new permissions for MakerWorld integration: groups that
  2522. # already have library:upload (i.e. can write to the library) are
  2523. # the correct audience for makerworld:view + makerworld:import, and
  2524. # groups that only have library:read get makerworld:view (browse
  2525. # only). Matches the intent of DEFAULT_GROUPS without clobbering
  2526. # any user-customised permission lists.
  2527. result = await session.execute(select(Group))
  2528. for group in result.scalars().all():
  2529. if not group.permissions:
  2530. continue
  2531. perms = list(group.permissions)
  2532. changed = False
  2533. if "library:upload" in perms:
  2534. for new_perm in ("makerworld:view", "makerworld:import"):
  2535. if new_perm not in perms:
  2536. perms.append(new_perm)
  2537. changed = True
  2538. logger.info("Added %s to group '%s' (has library:upload)", new_perm, group.name)
  2539. elif "library:read" in perms and "makerworld:view" not in perms:
  2540. perms.append("makerworld:view")
  2541. changed = True
  2542. logger.info("Added makerworld:view to group '%s' (has library:read)", group.name)
  2543. if changed:
  2544. group.permissions = perms
  2545. await session.commit()
  2546. # Backfill library:purge + archives:purge for the Administrators group
  2547. # on existing installs. Both permissions were added after Administrators
  2548. # was first seeded, so upgrading users miss them even though the default
  2549. # config (ALL_PERMISSIONS) includes them for fresh installs.
  2550. result = await session.execute(select(Group).where(Group.name == "Administrators"))
  2551. admin_group = result.scalar_one_or_none()
  2552. if admin_group and admin_group.permissions is not None:
  2553. perms = list(admin_group.permissions)
  2554. added = False
  2555. for new_perm in ("library:purge", "archives:purge"):
  2556. if new_perm not in perms:
  2557. perms.append(new_perm)
  2558. added = True
  2559. logger.info("Added %s to Administrators group (backfill)", new_perm)
  2560. if added:
  2561. admin_group.permissions = perms
  2562. await session.commit()
  2563. # Backfill inventory forecast permissions for existing groups.
  2564. # inventory:forecast_read was added after initial seeding, so groups
  2565. # that already have inventory:read (or inventory:update) need it added.
  2566. # inventory:forecast_write goes to any group with inventory:update.
  2567. result = await session.execute(select(Group))
  2568. for group in result.scalars().all():
  2569. if not group.permissions:
  2570. continue
  2571. perms = list(group.permissions)
  2572. changed = False
  2573. if "inventory:read" in perms and "inventory:forecast_read" not in perms:
  2574. perms.append("inventory:forecast_read")
  2575. changed = True
  2576. logger.info("Added inventory:forecast_read to group '%s' (backfill)", group.name)
  2577. if "inventory:update" in perms and "inventory:forecast_write" not in perms:
  2578. perms.append("inventory:forecast_write")
  2579. changed = True
  2580. logger.info("Added inventory:forecast_write to group '%s' (backfill)", group.name)
  2581. if changed:
  2582. group.permissions = perms
  2583. await session.commit()
  2584. # Migrate existing users to groups if they're not already in any group
  2585. if groups_created:
  2586. # Refresh to get newly created groups
  2587. admin_result = await session.execute(select(Group).where(Group.name == "Administrators"))
  2588. admin_group = admin_result.scalar_one_or_none()
  2589. operators_result = await session.execute(select(Group).where(Group.name == "Operators"))
  2590. operators_group = operators_result.scalar_one_or_none()
  2591. # Get all users
  2592. users_result = await session.execute(select(User))
  2593. users = users_result.scalars().all()
  2594. for user in users:
  2595. # Skip if user already has groups
  2596. if user.groups:
  2597. continue
  2598. if user.role == "admin" and admin_group:
  2599. user.groups.append(admin_group)
  2600. logger.info("Migrated admin user '%s' to Administrators group", user.username)
  2601. elif operators_group:
  2602. user.groups.append(operators_group)
  2603. logger.info("Migrated user '%s' to Operators group", user.username)
  2604. await session.commit()
  2605. async def seed_spool_catalog():
  2606. """Seed the spool catalog with default entries if empty."""
  2607. import logging
  2608. from sqlalchemy import func, select
  2609. from backend.app.core.catalog_defaults import DEFAULT_SPOOL_CATALOG
  2610. from backend.app.models.spool_catalog import SpoolCatalogEntry
  2611. logger = logging.getLogger(__name__)
  2612. async with async_session() as session:
  2613. result = await session.execute(select(func.count()).select_from(SpoolCatalogEntry))
  2614. count = result.scalar() or 0
  2615. if count > 0:
  2616. return # Already seeded
  2617. for name, weight in DEFAULT_SPOOL_CATALOG:
  2618. session.add(SpoolCatalogEntry(name=name, weight=weight, is_default=True))
  2619. await session.commit()
  2620. logger.info("Seeded %d default spool catalog entries", len(DEFAULT_SPOOL_CATALOG))
  2621. async def seed_color_catalog():
  2622. """Seed the color catalog with default entries if empty."""
  2623. import logging
  2624. from sqlalchemy import func, select
  2625. from backend.app.core.catalog_defaults import DEFAULT_COLOR_CATALOG
  2626. from backend.app.models.color_catalog import ColorCatalogEntry
  2627. logger = logging.getLogger(__name__)
  2628. async with async_session() as session:
  2629. result = await session.execute(select(func.count()).select_from(ColorCatalogEntry))
  2630. count = result.scalar() or 0
  2631. if count > 0:
  2632. return # Already seeded
  2633. for manufacturer, color_name, hex_color, material in DEFAULT_COLOR_CATALOG:
  2634. session.add(
  2635. ColorCatalogEntry(
  2636. manufacturer=manufacturer,
  2637. color_name=color_name,
  2638. hex_color=hex_color,
  2639. material=material,
  2640. is_default=True,
  2641. )
  2642. )
  2643. await session.commit()
  2644. logger.info("Seeded %d default color catalog entries", len(DEFAULT_COLOR_CATALOG))