hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiBc44f362Ahmet Kaan Gümüş2 months ago520 commits
| 1 | //! PostgreSQL connections (sqlx) + the two migration sets + the |
| 2 | //! guarded-transaction retry loop. |
| 3 | //! |
| 4 | //! Two sets of migrations live side by side and never share a tracking table: |
| 5 | //! `migrations/control` for the control database (schools, builders, the |
| 6 | //! shared rate-limit window) and `migrations/school` for every school |
| 7 | //! database. They are applied by two separate [`sqlx::migrate::Migrator`]s — |
| 8 | //! the compile-time `migrate!` macro cannot be aimed at two directories, so |
| 9 | //! each is resolved at runtime from the checkout's `CARGO_MANIFEST_DIR` or, |
| 10 | //! for a deployed binary, from beside the executable (see [`migrations_path`]). |
| 11 | //! Their table names are disjoint by design, which is what lets one prepare |
| 12 | //! database carry the union for the compile-time `query!` checks. |
| 13 | |
| 14 | use HashSet; |
| 15 | use Path; |
| 16 | use ; |
| 17 | use Duration; |
| 18 | |
| 19 | use ; |
| 20 | use PgPool; |
| 21 | use ; |
| 22 | use ; |
| 23 | |
| 24 | use crateConfig; |
| 25 | use crate |
| 26 | |
| 27 | ; |
| 28 | use crateTimestamp; |
| 29 | use crate; |
| 30 | use crateAppError; |
| 31 | use crateModuleSet; |
| 32 | use crate; |
| 33 | |
| 34 | /// The shared database handle. |
| 35 | /// |
| 36 | /// `PgPool` is an `Arc` around its internals, so cloning shares the one pool — |
| 37 | /// axum cloning the application state per request costs an atomic increment, |
| 38 | /// nothing more. |
| 39 | pub type Database = PgPool; |
| 40 | |
| 41 | /// Connect to the **control** database, apply the control schema, seed the |
| 42 | /// builder account and ensure the school-template database. School databases |
| 43 | /// are brought up separately, one per school, by [`crate::tenant::Tenants`]. |
| 44 | /// |
| 45 | /// The dial is retried forever (one-second cadence): the database is normally |
| 46 | /// a sibling container booting in parallel, and a process that exits on the |
| 47 | /// first refusal gets restarted by the runtime, fails again in ~100ms, and |
| 48 | /// burns its whole restart budget inside a second — leaving the backend |
| 49 | /// permanently down over a startup skew that would have cleared on its own. |
| 50 | /// Only the *connection* is retried. Bad credentials or a broken migration |
| 51 | /// still fail hard, since no amount of waiting fixes those. |
| 52 | pub async |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | /// Parse `DATABASE_URL` into dial options plus the control database's name — |
| 96 | /// the name every school database hangs off (`{control}_school_{slug}`). |
| 97 | pub |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | /// The pool knobs. The control pool holds 10 connections; each school pool |
| 116 | /// holds 4 (a school's traffic is one building's worth, and every school pool |
| 117 | /// draws from the same server's connection budget). |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | /// A pool over one school database with the school pool sizing. The schema is |
| 128 | /// the caller's business — [`migrate_school`] for a mint, nothing for a |
| 129 | /// re-dial of an existing school. |
| 130 | pub async |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | /// A `CREATE DATABASE` / `DROP DATABASE` statement for a quoted identifier. |
| 141 | /// The names passed here are the control database's name (from |
| 142 | /// `DATABASE_URL`), the template's (`{control}_school_template`) or |
| 143 | /// [`crate::tenant::school_db_name`]'s output — never user input: the control |
| 144 | /// and template names come from configuration, the school name from a uuid's |
| 145 | /// hex (fixed `[0-9a-f]`), and an embedded quote is doubled besides, so the |
| 146 | /// statement cannot be escaped from. That manual audit is what licenses the |
| 147 | /// `AssertSqlSafe` wrapper every caller applies (sqlx refuses non-literal |
| 148 | /// query strings without it). |
| 149 | pub |
| 150 | |
| 151 | |
| 152 | |
| 153 | /// Create `name` unless the server already has it. |
| 154 | /// |
| 155 | /// Postgres has no `IF NOT EXISTS` for `CREATE DATABASE` — the clause is a |
| 156 | /// syntax error there — so the "already there" case can only be settled by a |
| 157 | /// probe or by swallowing the refusal. Swallowing alone is not enough: a |
| 158 | /// refused create is *not* quiet, the server logs it as |
| 159 | /// `ERROR: database "…" already exists`, and a boot that finds its template in |
| 160 | /// place would print that line on every restart. The probe handles the common |
| 161 | /// case; the [`is_database_exists`] swallow stays as the backstop for the |
| 162 | /// window between probe and create, where rival boots win the race and the |
| 163 | /// statement is issued anyway. |
| 164 | pub async |
| 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 | async |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | /// Is this boot failure the database not being *there* yet — the only thing |
| 203 | /// a second of patience can fix? Everything else (a bad migration, a bad |
| 204 | /// credential, a missing migrations directory) aborts startup immediately. |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | /// Connection-class errors: the server was unreachable or the pool could not |
| 216 | /// hand out a connection. A migrations statement that *failed* is not here — |
| 217 | /// its error is `Error::Database`, and no sleep rewrites SQL. |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | /// The `migrations/<set>` directory: the one under `CARGO_MANIFEST_DIR` (the |
| 232 | /// dev/CI checkout) when that exists, else the one beside the executable. |
| 233 | /// Two sets cannot both feed the compile-time `migrate!` macro, so both are |
| 234 | /// resolved from their paths at runtime — and the compile-time path is baked |
| 235 | /// on the build machine, so a binary deployed outside a checkout (the release |
| 236 | /// layout ships `migrations/` next to the binary) must fall back to its own |
| 237 | /// directory or look for the schema where it was compiled. |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | async |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | /// Apply the control schema (schools, builders, the shared rate-limit |
| 261 | /// window) to a control database. Idempotent: an already-migrated database |
| 262 | /// runs nothing. The boot path folds this into [`boot_control`]; suites that |
| 263 | /// re-boot an existing control database call it directly. |
| 264 | pub async |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | /// Apply the school schema to a school database — the template at boot, every |
| 274 | /// newly minted school at create — then sweep the chatbot and RAG turns the |
| 275 | /// previous process owed an answer. Idempotent: an already-migrated database |
| 276 | /// runs only the sweep. |
| 277 | pub async |
| 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 | /// The advisory-lock key [`ensure_template`] holds while it creates and |
| 319 | /// migrates the template: one name for one control database, contended only |
| 320 | /// by concurrent boots of the same deployment. Distinct from |
| 321 | /// [`TEST_TEMPLATE_LOCK_KEY`], which guards a different database. |
| 322 | const TEMPLATE_LOCK_KEY: i64 = 0x6865_7A74_6D70_0001; |
| 323 | |
| 324 | /// Ensure the school-template database exists and carries the current school |
| 325 | /// schema. `CREATE DATABASE` runs on the control pool outside any |
| 326 | /// transaction; a template another boot already made is adopted. |
| 327 | /// |
| 328 | /// Boots of one deployment can overlap (a rolling restart, two replicas), and |
| 329 | /// this pair is a critical section across processes for the same reasons the |
| 330 | /// test harness's is (see [`ensure_test_template`]): unsynchronized, the |
| 331 | /// creates collide — the loser's `pg_database_datname_index` violation is |
| 332 | /// logged as a server-side `ERROR` even though [`ensure_database`] swallows |
| 333 | /// it — and two migrators interleave their DDL on the same fresh database and |
| 334 | /// kill each other on a duplicate table. |
| 335 | async |
| 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 | /// The template's create-and-migrate, under [`ensure_template`]'s lock. |
| 361 | async |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | /// `42P04` — the database already exists. The other side of every |
| 377 | /// "create it unless a rival got there first" move. |
| 378 | pub |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | /// Is this error "the database is already there"? Two SQLSTATEs say yes: |
| 384 | /// `42P04`, the duplicate-database check `CREATE DATABASE` runs against the |
| 385 | /// catalog, and `23505` on `pg_database_datname_index` — how Postgres reports |
| 386 | /// the *losing* side of two concurrent creates, the name check and the |
| 387 | /// catalog insert not being one atomic step. Measured on 18.6: four boots |
| 388 | /// racing on an absent template, three of them got `23505` and died, so a |
| 389 | /// `42P04`-only guard is too narrow for the very race it exists for. |
| 390 | pub |
| 391 | |
| 392 | |
| 393 | |
| 394 | /// The violated `UNIQUE` constraint's name, when `err` is a unique violation |
| 395 | /// (`23505`). The constraint name is the stable handle call sites map |
| 396 | /// refusals onto — `unique_violation(&e) == Some("school_pkey")` — and it is |
| 397 | /// borrowed from the error, not `'static`: the server owns the text. |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | /// The violated `EXCLUDE` constraint's name, when `err` is an exclusion |
| 408 | /// violation (`23P01`) — the SQLSTATE an exclusion constraint raises, as |
| 409 | /// distinct from a plain `UNIQUE` (`23505`). |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | /// A foreign-key violation (`23503`): a parent row a statement names is gone. |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | /// Is this error the transaction *contended*, rather than decided? `40001` |
| 426 | /// (serialization failure) and `40P01` (deadlock) are Postgres telling two |
| 427 | /// writers to take turns — the whole reason the retry loop below exists. |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | /// [`is_retryable_tx`] plus the foreign-key violation. For **cascade** |
| 436 | /// transactions only: a child insert whose parent vanished mid-cascade means |
| 437 | /// a racing cascade is still in flight and a re-run sees the settled state — |
| 438 | /// but to a plain insert a `23503` is a real refusal (the parent is simply |
| 439 | /// gone), and retrying it would only burn the budget before returning the |
| 440 | /// same verdict. |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | /// Send one guarded `BEGIN…COMMIT` transaction, re-running it while Postgres |
| 446 | /// answers "contended, retry", and hand back the closure's value. |
| 447 | /// |
| 448 | /// The closure writes through the transaction's connection (`&mut |
| 449 | /// PgConnection`), returns its value, and reports refusals as ordinary `Err`s |
| 450 | /// carrying exactly the `AppError` the web layer owes the caller. A refusal |
| 451 | /// is a *decision*, so it outranks the retry loop — only `AppError::Db` |
| 452 | /// errors that [`is_retryable_tx`] (or, when `cascade`, [`is_retryable_cascade`]) |
| 453 | /// recognise are re-sent; everything else comes straight back. |
| 454 | /// |
| 455 | /// Isolation is Postgres's default **READ COMMITTED** everywhere. Invariants |
| 456 | /// that span rows are enforced by single-statement CTEs or row locks |
| 457 | /// (`FOR UPDATE` / `FOR NO KEY UPDATE`) inside the closure, not by stronger |
| 458 | /// isolation. |
| 459 | /// The closure is an [`AsyncFnMut`] (edition-2024 async closure, or a plain |
| 460 | /// closure whose async block the compiler infers as such) because every real |
| 461 | /// call site's future borrows the connection — a named `Fut` generic cannot |
| 462 | /// express that lifetime relationship. |
| 463 | /// |
| 464 | /// Isolation is Postgres's default **READ COMMITTED** everywhere. Invariants |
| 465 | /// that span rows are enforced by single-statement CTEs or row locks |
| 466 | /// (`FOR UPDATE` / `FOR NO KEY UPDATE`) inside the closure, not by stronger |
| 467 | /// isolation; if a transaction genuinely cannot be row-locked, escalate that |
| 468 | /// one to `SERIALIZABLE` (the retry loop already covers the `40001` it can |
| 469 | /// then raise) and record the reason in its own call site's comment. |
| 470 | /// |
| 471 | /// The budget and backoff are the guard layer's usual: `CAP_WRITE_TRIES` |
| 472 | /// rounds, `backoff`'s exponential-with-jitter wait, attempt zero waiting |
| 473 | /// not at all. |
| 474 | pub async |
| 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 | /// Wait out one lost round. Exponential with jitter, because racers arrive in |
| 520 | /// lockstep (one HTTP burst) and a fixed delay would just re-synchronize them. |
| 521 | /// Attempt zero waits not at all. |
| 522 | pub async |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | /// The `BUILDER_USERNAME` / `BUILDER_PASSWORD` bootstrap pair, if configured. |
| 532 | /// Both-or-neither: half a pair aborts startup rather than silently running |
| 533 | /// without the seed — and without a builder, a fresh deployment has nobody who |
| 534 | /// can create the first school. |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | // ---- per-test databases ----------------------------------------------------- |
| 551 | // |
| 552 | // Every test run gets a **private pair of databases** on the compose Postgres: |
| 553 | // a control database and the demo school's database, both named after one |
| 554 | // random suffix — `heztest_<16 hex>` and `heztest_<…>_school_<uuid hex>` — so |
| 555 | // nextest's parallel processes neither collide nor see each other's rows. The |
| 556 | // school schema is not migrated per test: it is cloned from a shared template |
| 557 | // database whose name carries a hash of `migrations/school`, so a schema edit |
| 558 | // mints a fresh template exactly once and every test after that pays only a |
| 559 | // cheap `CREATE DATABASE … TEMPLATE`. |
| 560 | // |
| 561 | // Cleanup is a lease: every database a deployment minted is recorded in a |
| 562 | // [`TestDatabases`], and its `Drop` — fired when the deployment's last handle |
| 563 | // dies — sends `DROP DATABASE … WITH (FORCE)`. A best-effort janitor at first |
| 564 | // use per process sweeps what a crashed run left behind: only databases past |
| 565 | // a minimum age are candidates, and their `DROP` carries no `WITH (FORCE)`, |
| 566 | // so a live parallel test's databases (connected backends, young name) are |
| 567 | // never touched. |
| 568 | // |
| 569 | // These functions are `pub` rather than `#[cfg(test)]` because the |
| 570 | // integration tests live outside the crate, and `#[doc(hidden)]` because they |
| 571 | // are not part of the API. Their SQL is runtime-checked — the query-style |
| 572 | // rule exempts all test-side SQL from the macros. |
| 573 | |
| 574 | /// The Postgres server the test databases are minted on. The default is the |
| 575 | /// compose stack's maintenance database (`.env.example` documents it). |
| 576 | const TEST_DATABASE_URL_ENV: &str = "HEZARFEN_TEST_DATABASE_URL"; |
| 577 | const TEST_DATABASE_URL_DEFAULT: &str = "postgres://hezarfen:hezarfen@127.0.0.1:5432/postgres"; |
| 578 | |
| 579 | /// Every test database carries this prefix; the janitor only ever drops |
| 580 | /// databases it names. |
| 581 | const TEST_DB_PREFIX: &str = "heztest_"; |
| 582 | |
| 583 | /// How old a `heztest_%` database must be before the janitor will touch it. |
| 584 | /// Anything younger belongs to a test in a parallel process — nextest mints |
| 585 | /// them by the dozen in overlapping seconds. |
| 586 | const TEST_DB_LEFTOVER_MIN_AGE_MS: i64 = 5 * 60 * 1000; |
| 587 | |
| 588 | /// The advisory-lock key the template's create-and-migrate critical section |
| 589 | /// holds (see [`ensure_test_template`]). An arbitrary constant — it only has |
| 590 | /// to be one nobody else on the server uses. |
| 591 | const TEST_TEMPLATE_LOCK_KEY: i64 = 0x6865_7A74_6500_0001; |
| 592 | |
| 593 | /// The databases one test deployment minted, and the promise to drop them. |
| 594 | /// |
| 595 | /// Cloning shares the lease; the `DROP` statements fire when the **last** |
| 596 | /// copy drops — which is why a [`Tenants`] can carry one: the databases die |
| 597 | /// exactly when the test deployment does, even though the `Router` and the |
| 598 | /// test body hold separate clones of the registry. |
| 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 | /// A complete test deployment: a fresh control database plus the demo school, |
| 695 | /// whose database is cloned from the shared school template and registered in |
| 696 | /// the control registry under the slug every suite names ([`DEMO_SLUG`]). |
| 697 | /// |
| 698 | /// The databases die with the returned registry's last handle (see |
| 699 | /// [`TestDatabases`]); the janitor sweeps anything a crashed run left behind. |
| 700 | |
| 701 | pub async |
| 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 | /// One school database cloned from the template, for the src-side unit tests |
| 757 | /// that exercise a single store with no tenancy around it. Keep the lease |
| 758 | /// alive for the database's lifetime (`let (db, _leases) = …` binds it to the |
| 759 | /// test's scope); dropping it early drops the database. |
| 760 | |
| 761 | pub async |
| 762 | |
| 763 | |
| 764 | |
| 765 | |
| 766 | |
| 767 | |
| 768 | |
| 769 | |
| 770 | |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | /// The school template's name, and as a side effect its existence: created |
| 777 | /// and migrated on first use, adopted when something else already made it. |
| 778 | /// The name carries a hash of `migrations/school`, so a schema edit mints a |
| 779 | /// fresh template and every process converges on the same new one. |
| 780 | /// |
| 781 | /// Competing first uses are real: nextest mints tests in parallel processes |
| 782 | /// and in parallel test threads, all overlapping on the same millisecond. |
| 783 | /// One server-wide advisory lock makes the create-and-migrate a critical |
| 784 | /// section — without it, two migrators interleave their DDL on the same |
| 785 | /// fresh database and one dies on a duplicate table mid-flight (observed). |
| 786 | /// The probe + `42P04` swallow are the belt to that brace: a template made |
| 787 | /// before the lock was ever taken is adopted, not fought. |
| 788 | async |
| 789 | |
| 790 | |
| 791 | |
| 792 | |
| 793 | |
| 794 | |
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | |
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | |
| 813 | |
| 814 | |
| 815 | |
| 816 | |
| 817 | |
| 818 | |
| 819 | |
| 820 | |
| 821 | |
| 822 | |
| 823 | |
| 824 | |
| 825 | |
| 826 | |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | |
| 832 | |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | /// `heztest_tpl_school_<hash>` — the hash is over `migrations/school`'s file |
| 838 | /// names and bytes, sorted, so any schema edit changes it. |
| 839 | |
| 840 | |
| 841 | |
| 842 | |
| 843 | |
| 844 | |
| 845 | |
| 846 | |
| 847 | |
| 848 | |
| 849 | |
| 850 | |
| 851 | |
| 852 | |
| 853 | |
| 854 | |
| 855 | |
| 856 | |
| 857 | |
| 858 | |
| 859 | |
| 860 | |
| 861 | /// One sweep per process, at the first mint: `heztest_%` databases a crashed |
| 862 | /// run left behind. See the section header for why a candidate must be old |
| 863 | /// *and* unconnected before it is dropped. |
| 864 | async |
| 865 | |
| 866 | |
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | |
| 873 | |
| 874 | |
| 875 | |
| 876 | |
| 877 | |
| 878 | |
| 879 | |
| 880 | |
| 881 | |
| 882 | |
| 883 | |
| 884 | |
| 885 | |
| 886 | |
| 887 | |
| 888 | |
| 889 | |
| 890 | /// Should this `heztest_%` database be swept? The suffix's leading eleven hex |
| 891 | /// digits are the mint time in unix milliseconds; anything that cannot be |
| 892 | /// read that way is either a template of an older schema hash (swept) or |
| 893 | /// junk (swept too). |
| 894 | |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | |
| 901 | |
| 902 | |
| 903 | |
| 904 | /// `<11 hex of unix-ms><5 hex random>`: the mint time, so the janitor can |
| 905 | /// tell a crashed run's leftovers from a parallel test's fresh databases, |
| 906 | /// plus just enough randomness that two processes minting in the same |
| 907 | /// millisecond never collide. The tail is not an id and is drawn straight from |
| 908 | /// the OS rng: the leading hex digits of a v7 uuid *are* the mint time, which |
| 909 | /// is exactly what two processes minting in one millisecond share. |
| 910 | |
| 911 | |
| 912 | |
| 913 | |
| 914 | |
| 915 | |
| 916 | |
| 917 | |
| 918 | /// `CREATE DATABASE` for a harness-minted name, optionally `TEMPLATE`-cloned. |
| 919 | /// Both names are generated here (prefix + hex suffix), never user input — |
| 920 | /// the same audit that licenses [`create_database_sql`]'s `AssertSqlSafe` |
| 921 | /// callers. |
| 922 | async |
| 923 | |
| 924 | |
| 925 | |
| 926 | |
| 927 | |
| 928 | |
| 929 | |
| 930 | |
| 931 | |
| 932 | |
| 933 | |
| 934 | |
| 935 | /// Dial options for the test Postgres — `HEZARFEN_TEST_DATABASE_URL` or the |
| 936 | /// compose default. The URL's database is the *maintenance* database the |
| 937 | /// `CREATE DATABASE`s run against; the harness names every database itself. |
| 938 | |
| 939 | |
| 940 | |
| 941 | |
| 942 | |
| 943 | |
| 944 | |
| 945 | |
| 946 | |
| 947 | |
| 948 | /// A small pool over the maintenance database: the janitor's listing and |
| 949 | /// every `CREATE DATABASE` (DDL — not usable inside a transaction) go |
| 950 | /// through it, and each lease keeps a clone for its drops. |
| 951 | async |
| 952 | |
| 953 | |
| 954 | |
| 955 | |
| 956 | |
| 957 | |
| 958 | |
| 959 | |
| 960 | |
| 961 | |
| 962 | |
| 963 | |
| 964 | |
| 965 | |
| 966 | |
| 967 | |
| 968 | |
| 969 | |
| 970 | |
| 971 | |
| 972 | |
| 973 | |
| 974 | |
| 975 | |
| 976 | |
| 977 | |
| 978 | |
| 979 | |
| 980 | |
| 981 | |
| 982 | |
| 983 | |
| 984 | |
| 985 | |
| 986 | |
| 987 | |
| 988 | |
| 989 | |
| 990 | |
| 991 | |
| 992 | |
| 993 |