| 1 | package fichas |
| 2 | |
| 3 | import ( |
| 4 | "embed" |
| 5 | |
| 6 | "amadan.net/rastrillo/rastrillo/auth" |
| 7 | "amadan.net/rastrillo/rastrillo/migrate" |
| 8 | "amadan.net/rastrillo/rastrillo/sessions" |
| 9 | ) |
| 10 | |
| 11 | //go:embed migrations/*.sql |
| 12 | var migrationFS embed.FS |
| 13 | |
| 14 | // Schema is this app's own migrations, in order — nothing else. It is |
| 15 | // what `rastrillo migration generate` and `rastrillo migration check` read |
| 16 | // and write: both work by replaying a set into an in-memory database |
| 17 | // and diffing the result against Models, so Schema must never carry a |
| 18 | // framework subsystem's migrations too — mixed in, the diff would see |
| 19 | // tables (sessions, auth_links, ...) that Models knows nothing about |
| 20 | // and want to drop every one of them. |
| 21 | // |
| 22 | // Add to it with `rastrillo migration generate` after changing a |
| 23 | // model, or `rastrillo migration new <name>` to write one by hand. |
| 24 | // Migrations apply at boot and are never re-run or reversed: never |
| 25 | // edit one that has shipped, add a new one instead. |
| 26 | var Schema = migrate.MustFromFS(migrationFS, "fichas") |
| 27 | |
| 28 | // BootSchema is everything applied at boot, in order — this app's own |
| 29 | // Schema plus every framework subsystem's — and it is what App() below |
| 30 | // hands to migrate.Apply. It is a separate variable from Schema for |
| 31 | // the same reason Schema must stay narrow: generate and check would |
| 32 | // misread a subsystem's tables as ones Models forgot. |
| 33 | // |
| 34 | // A fresh scaffold uses no subsystem yet, so today this is just |
| 35 | // Schema. Adding one (say, sessions) means editing this line, not |
| 36 | // Schema: |
| 37 | // |
| 38 | // var BootSchema = migrate.Merge(sessions.Schema, Schema) |
| 39 | // |
| 40 | // Merge's argument order is apply order, so a subsystem whose |
| 41 | // migrations another one reads (auth after sessions) goes first. |
| 42 | // Get this line wrong and either boot silently skips a subsystem's |
| 43 | // tables, or generate/check try to drop them. |
| 44 | // sessions and auth come first: auth's tables reference nothing of ours, |
| 45 | // and ours reference nothing of theirs, but apply order is argument order |
| 46 | // and theirs are the ones every request depends on. |
| 47 | // |
| 48 | // **This line was `migrate.Merge(Schema)` for the first eight days in |
| 49 | // production.** An earlier edit meant to add the two subsystems matched a |
| 50 | // different spelling of the line, changed nothing, and was never checked — |
| 51 | // so auth_links and the sessions table did not exist, and no sign-in on |
| 52 | // the deployed app could complete by either path. TestBootSchema and |
| 53 | // TestASignInCompletesEndToEnd are what stop that happening silently again. |
| 54 | var BootSchema = migrate.Merge(sessions.Schema, auth.Schema, Schema) |
| 55 | |