| 1 | package home |
| 2 | |
| 3 | import ( |
| 4 | "embed" |
| 5 | "io/fs" |
| 6 | |
| 7 | "amadan.net/rastrillo/rastrillo/migrate" |
| 8 | "amadan.net/rastrillo/rastrillo/passkey" |
| 9 | "amadan.net/rastrillo/rastrillo/sessions" |
| 10 | ) |
| 11 | |
| 12 | //go:embed migrations/*.sql |
| 13 | var migrationsFS embed.FS |
| 14 | |
| 15 | // Schema is this app's own migrations: forward-only, ledgered by ID and |
| 16 | // checksum, named migrations/NNNN_lowercase_name.sql. |
| 17 | var Schema = migrate.MustFromFS(migrationsFS, "home") |
| 18 | |
| 19 | // BootSchema is everything applied at boot. |
| 20 | // |
| 21 | // rastrillo/auth's schema is deliberately absent. This app does not use |
| 22 | // that package — sign-in is hand-rolled on keymaildev/signin — and |
| 23 | // auth's tables hold addresses, which must never be created here. An |
| 24 | // app that merged auth.Schema "just in case" would create an address |
| 25 | // column that gate 2's schema half cannot see, because that gate reads |
| 26 | // this repo's migrations and auth's are a dependency's. |
| 27 | // |
| 28 | // sessions.Schema carries no address either: its columns are token_hash, |
| 29 | // subject, method, auth_time, created_at, expires_at. What goes INTO |
| 30 | // subject is the part that matters — it must be the person ref, never |
| 31 | // the address — and that is enforced where sessions are minted, not |
| 32 | // here. |
| 33 | // passkey.Schema is not optional: Home's own half-session and recovery |
| 34 | // codes (id_passkey.go, id_recovery.go) live in the passkey_pending and |
| 35 | // passkey_recovery_codes tables it creates, so omitting it fails at the |
| 36 | // database rather than at boot. secondfactor.Schema, where rastrillo |
| 37 | // moved both at v0.27.1, must never be merged here: its second |
| 38 | // migration copies the codes out and drops those two tables. |
| 39 | // |
| 40 | // All four passkey tables key on subject, which here is already the |
| 41 | // person ref — so the worry about inheriting an address-bearing subject |
| 42 | // does not arise. Said here rather than left to be rediscovered. |
| 43 | // |
| 44 | // passkey.Schema enters as passkeyFrozen, not whole: BootSchema's objects |
| 45 | // are frozen in the admission and compatibility profiles, and rastrillo |
| 46 | // v0.27.1 grew passkey.Schema a 0002_credential_details that ALTERs |
| 47 | // passkey_credentials. Home writes that table itself (id_passkey.go) and |
| 48 | // reads none of the new columns, so it takes 0001 alone, as it always |
| 49 | // has. Adopting 0002 is a schema change with its own profile review. |
| 50 | var BootSchema = migrate.Merge(sessions.Schema, passkeyFrozen, Schema) |
| 51 | |
| 52 | // passkeyFrozen is passkey.Schema as Home has always applied it: |
| 53 | // passkey/0001_init, byte for byte (the ledger checksums the SQL). |
| 54 | var passkeyFrozen = frozenMigrations(passkey.Schema, "passkey/0001_init") |
| 55 | |
| 56 | // frozenMigrations is the named migrations of set, in order, under the |
| 57 | // same IDs and SQL — so the ledger cannot tell them from set's own — |
| 58 | // and panics if one is missing, rather than booting a schema short. |
| 59 | func frozenMigrations(set *migrate.Set, ids ...string) *migrate.Set { |
| 60 | byID := map[string]migrate.Migration{} |
| 61 | for _, m := range set.All() { |
| 62 | byID[m.ID] = m |
| 63 | } |
| 64 | out := &migrate.Set{} |
| 65 | for _, id := range ids { |
| 66 | m, ok := byID[id] |
| 67 | if !ok { |
| 68 | panic("home: frozen migration " + id + " is gone from its set") |
| 69 | } |
| 70 | out.Add(m) |
| 71 | } |
| 72 | return out |
| 73 | } |
| 74 | |
| 75 | // Identity migrations extend the serving credential role. Keep the historical |
| 76 | // BootSchema unchanged: its exact objects are frozen in admission profiles. |
| 77 | // |
| 78 | //go:embed identity/migrations/*.sql |
| 79 | var identityFS embed.FS |
| 80 | var identitySchema = func() *migrate.Set { |
| 81 | sub, err := fs.Sub(identityFS, "identity") |
| 82 | if err != nil { |
| 83 | panic(err) |
| 84 | } |
| 85 | return migrate.MustFromFS(sub, "home_id") |
| 86 | }() |
| 87 | |
| 88 | // directoryIDSchema is the directory's Oficina ID additions: today only the |
| 89 | // "Sign out all devices" mark. Applied by the directory's ServingSchema, so |
| 90 | // the frozen role schema is unchanged. |
| 91 | // |
| 92 | //go:embed directory_id/migrations/*.sql |
| 93 | var directoryIDFS embed.FS |
| 94 | var directoryIDSchema = func() *migrate.Set { |
| 95 | sub, err := fs.Sub(directoryIDFS, "directory_id") |
| 96 | if err != nil { |
| 97 | panic(err) |
| 98 | } |
| 99 | return migrate.MustFromFS(sub, "home_directory_id") |
| 100 | }() |
| 101 | |