Database Service
RetentionTarget
dataclass
Declarative specification for a table managed by retention cleanup and size failsafe.
Source code in src/hassette/core/database_service.py
73 74 75 76 77 78 79 80 81 | |
DatabaseService
Bases: Service
Manages the SQLite database for operational telemetry.
Handles PRAGMA user_version migrations, heartbeat updates, and retention cleanup of old execution records.
Source code in src/hassette/core/database_service.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 | |
is_db_ready: bool
property
Whether the write database connection is open and usable.
is_accepting_writes: bool
property
Whether the write queue is live and accepting submissions.
False before on_initialize() creates the queue and after shutdown drains it.
db: aiosqlite.Connection
property
Return the active write database connection.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the database connection is not initialized. |
read_db: aiosqlite.Connection
property
Return the dedicated read-only database connection.
Uses a separate WAL snapshot so reads never block the write worker.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the read connection is not initialized. |
on_initialize() -> None
async
Set up the database: check schema version, run migrations and open connection.
Source code in src/hassette/core/database_service.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |
serve() -> None
async
Run the heartbeat, retention, and size failsafe loop until shutdown.
Source code in src/hassette/core/database_service.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
on_shutdown() -> None
async
Drain the write queue, cancel the worker, then close the database connection.
Source code in src/hassette/core/database_service.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | |
close_remaining_queue_items(queue: asyncio.Queue[_WriteQueueItem] | None) -> None
Close any coroutines left on the write queue without executing them.
Called during shutdown to prevent unawaited-coroutine warnings from GC.
Source code in src/hassette/core/database_service.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |
close_connections() -> None
async
Close both database connections. Idempotent — safe to call multiple times.
Always attempts both connections even if the first close raises CancelledError. aiosqlite's worker threads are set to daemon in on_initialize() as a safety net, but this method still does a best-effort close to avoid resource warnings and ensure clean WAL checkpoints.
Source code in src/hassette/core/database_service.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |
cleanup(timeout: float | None = None) -> None
async
Close database connections if on_shutdown was interrupted or never ran.
Source code in src/hassette/core/database_service.py
348 349 350 351 | |
db_write_worker() -> None
async
Drain _db_write_queue sequentially.
Each item is a (coroutine, future) pair. If future is not None, the coroutine's result (or exception) is delivered through it. If future is None, any exception is logged and the worker continues.
The loop runs until cancelled by on_shutdown().
Source code in src/hassette/core/database_service.py
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | |
submit(coro: Coroutine[Any, Any, Any]) -> Any
async
Submit a coroutine for serialized execution and await its result.
The coroutine is placed on the write queue and executed by the single-writer worker. The caller is suspended until the coroutine completes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coro
|
Coroutine[Any, Any, Any]
|
The coroutine to execute. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The return value of the coroutine. |
Raises:
| Type | Description |
|---|---|
Exception
|
Whatever exception the coroutine raises. |
Source code in src/hassette/core/database_service.py
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | |
enqueue(coro: Coroutine[Any, Any, Any]) -> bool
Submit a coroutine for fire-and-forget execution.
Returns immediately. The coroutine is placed on the write queue and executed by the single-writer worker. Any exception is logged; the worker continues processing subsequent items.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coro
|
Coroutine[Any, Any, Any]
|
The coroutine to execute. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if enqueued successfully, False if dropped due to a full queue. |
Source code in src/hassette/core/database_service.py
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 | |
resolve_db_path() -> Path
Resolve the database path from config or use default.
Source code in src/hassette/core/database_service.py
436 437 438 439 440 | |
get_expected_head_version() -> int
Return the highest migration version number from migrations_sql/ (synchronous).
Scans the migrations_sql/ directory for *.sql files with numeric stems and returns the largest version number found.
Source code in src/hassette/core/database_service.py
442 443 444 445 446 447 448 449 450 451 | |
get_current_db_version(db_path: Path) -> int
Return PRAGMA user_version from the on-disk DB (synchronous). Returns 0 for fresh databases.
Source code in src/hassette/core/database_service.py
453 454 455 | |
handle_schema_version(db_path: Path) -> None
async
Check schema version and handle mismatches.
If the DB file does not exist yet, does nothing (migrations will create it). If the DB version matches the expected head, does nothing. If the DB version is behind head (0 < current < head), does nothing — run_migrations() will apply pending migrations incrementally, preserving data. If the DB version is 0 on an existing file (pre-PRAGMA-era or fresh DB with no migrations applied), deletes the file so migrations recreate it cleanly. If the DB version is ahead of head (newer DB on older binary), logs an ERROR and raises SchemaVersionError — auto-delete is refused in this case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_path
|
Path
|
Path to the SQLite database file. |
required |
Raises:
| Type | Description |
|---|---|
SchemaVersionError
|
When the DB version is ahead of the expected head version. |
RuntimeError
|
When the DB file cannot be deleted due to permissions. |
Source code in src/hassette/core/database_service.py
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 | |
run_migrations() -> None
Run PRAGMA user_version migrations to the latest version (synchronous, called via to_thread).
auto_vacuum = INCREMENTAL is set by the runner before any tables are created.
Source code in src/hassette/core/database_service.py
520 521 522 523 524 525 | |
set_pragmas() -> None
async
Configure SQLite PRAGMAs for performance and safety.
Source code in src/hassette/core/database_service.py
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 | |
update_heartbeat() -> None
async
Await a heartbeat update for the current session.
Early-return guards run inline; the DB write is awaited via submit() so that _consecutive_heartbeat_failures is updated before returning.
Source code in src/hassette/core/database_service.py
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | |
run_retention_cleanup() -> None
async
Enqueue a retention cleanup; fire-and-forget via enqueue().
Source code in src/hassette/core/database_service.py
581 582 583 584 585 586 587 | |
get_db_size_mb() -> float
Return total database size (main + WAL + SHM) in megabytes.
Source code in src/hassette/core/database_service.py
663 664 665 666 667 668 669 670 | |
run_size_failsafe() -> None
async
Enqueue a size failsafe check; fire-and-forget via enqueue().
Source code in src/hassette/core/database_service.py
751 752 753 754 755 756 757 | |