| 1 | package docs |
| 2 | |
| 3 | // Moving Docs' Home links onto home/connect, once, at boot. |
| 4 | // |
| 5 | // Before this branch Docs linked a local person to a Home identity in its |
| 6 | // own docs_identity_links (issuer, subject, person_id). home/connect keeps |
| 7 | // links in home_identity_links, where every row also carries |
| 8 | // person_binding, a keyed hash of the person's address that Resolve checks. |
| 9 | // A SQL copy would make rows connect refuses, so each row is carried with |
| 10 | // connect's Bridge.Adopt, which writes exactly the row linking through |
| 11 | // connect would have written (Home's implementation/linkstore-adopt.md). |
| 12 | // |
| 13 | // The move is one transaction, run in New after the migrations and |
| 14 | // connect.New (whose Sweep has already committed) and before the team |
| 15 | // runtime's census or any request: nothing else holds the single writer |
| 16 | // connection while it runs. |
| 17 | // |
| 18 | // 1. In a team database, principalstore's link triggers are re-pointed |
| 19 | // at home_identity_links first, so the disjointness trigger guards |
| 20 | // every adopted row. Its refusal is a RAISE(ABORT), which Adopt |
| 21 | // reports as ErrStorage, not ErrRefused (Fable, oficina/home #99 note |
| 22 | // 7); either one aborts. |
| 23 | // 2. Every row is adopted. ErrRefused aborts the whole move: a dropped |
| 24 | // row would be a link lost for good (#99 note 9). Nothing is skipped. |
| 25 | // 3. docs_link_clock's high-water mark is carried with MAX, so connect's |
| 26 | // rollback guard does not restart at 0. |
| 27 | // 4. The old table, its append-only triggers, and the flow and clock |
| 28 | // tables are dropped. In-flight link flows (five minutes long) are not |
| 29 | // carried; whoever was mid-link starts again. |
| 30 | // |
| 31 | // Commit makes all of it visible at once. A crash or a refusal leaves the |
| 32 | // old table exactly as it was, for a full redo on the next boot, and a |
| 33 | // boot after a successful move finds no old table and does nothing. |
| 34 | |
| 35 | import ( |
| 36 | "context" |
| 37 | "database/sql" |
| 38 | "errors" |
| 39 | "fmt" |
| 40 | |
| 41 | "amadan.net/oficina/home/connect/linkstore" |
| 42 | "github.com/carlosframework/docs/internal/docs/principalstore" |
| 43 | ) |
| 44 | |
| 45 | var ( |
| 46 | // errLinksRefused: some row cannot be carried as it stands (connect's |
| 47 | // ErrRefused: another issuer, a malformed subject, a person who no |
| 48 | // longer has an address). Nothing moved; Docs does not start. |
| 49 | errLinksRefused = errors.New("docs: an existing Home link cannot move onto home/connect; nothing was moved, docs_identity_links is intact") |
| 50 | // errLinksUnavailable: storage failed, or a trigger refused a row. |
| 51 | errLinksUnavailable = errors.New("docs: moving Home links onto home/connect failed; nothing was moved, docs_identity_links is intact") |
| 52 | ) |
| 53 | |
| 54 | // linkAdopter is connect.Bridge's Adopt (a seam for the move's tests). |
| 55 | type linkAdopter interface { |
| 56 | Adopt(ctx context.Context, tx *sql.Tx, issuer, subject string, person int64) error |
| 57 | } |
| 58 | |
| 59 | type oldLink struct { |
| 60 | issuer, subject string |
| 61 | person int64 |
| 62 | } |
| 63 | |
| 64 | // moveLinksOntoConnect carries docs_identity_links into connect's table and |
| 65 | // retires it, all in one transaction on db (the writer). See the file |
| 66 | // comment for the order and why. |
| 67 | func moveLinksOntoConnect(ctx context.Context, db *sql.DB, links linkAdopter) error { |
| 68 | if db == nil || links == nil { |
| 69 | return errLinksUnavailable |
| 70 | } |
| 71 | tx, err := db.BeginTx(ctx, nil) |
| 72 | if err != nil { |
| 73 | return errLinksUnavailable |
| 74 | } |
| 75 | defer tx.Rollback() |
| 76 | old, err := tableExists(ctx, tx, "docs_identity_links") |
| 77 | if err != nil { |
| 78 | return errLinksUnavailable |
| 79 | } |
| 80 | if !old { |
| 81 | // Moved already (or never created): nothing to do, nothing written. |
| 82 | return nil |
| 83 | } |
| 84 | principals, err := tableExists(ctx, tx, "docs_home_identities") |
| 85 | if err != nil { |
| 86 | return errLinksUnavailable |
| 87 | } |
| 88 | if principals { |
| 89 | if err := principalstore.RepointLinks(ctx, tx); err != nil { |
| 90 | return errLinksUnavailable |
| 91 | } |
| 92 | } |
| 93 | rows, err := readOldLinks(ctx, tx) |
| 94 | if err != nil { |
| 95 | return errLinksUnavailable |
| 96 | } |
| 97 | for _, row := range rows { |
| 98 | switch err := links.Adopt(ctx, tx, row.issuer, row.subject, row.person); { |
| 99 | case err == nil: |
| 100 | case errors.Is(err, linkstore.ErrRefused): |
| 101 | // The person ID locates the row for an operator; the subject |
| 102 | // and the address are left out of the log. |
| 103 | return fmt.Errorf("%w (person %d)", errLinksRefused, row.person) |
| 104 | default: |
| 105 | return errLinksUnavailable |
| 106 | } |
| 107 | } |
| 108 | clock, err := tableExists(ctx, tx, "docs_link_clock") |
| 109 | if err != nil { |
| 110 | return errLinksUnavailable |
| 111 | } |
| 112 | if clock { |
| 113 | var high int64 |
| 114 | if err := tx.QueryRowContext(ctx, "SELECT high_water FROM docs_link_clock WHERE id = 1").Scan(&high); err != nil && !errors.Is(err, sql.ErrNoRows) { |
| 115 | return errLinksUnavailable |
| 116 | } |
| 117 | res, err := tx.ExecContext(ctx, "UPDATE home_link_clock SET high_water = MAX(high_water, ?) WHERE id = 1", high) |
| 118 | if err != nil { |
| 119 | return errLinksUnavailable |
| 120 | } |
| 121 | if n, err := res.RowsAffected(); err != nil || n != 1 { |
| 122 | return errLinksUnavailable |
| 123 | } |
| 124 | } |
| 125 | for _, q := range []string{ |
| 126 | "DROP TRIGGER IF EXISTS docs_identity_links_keep_update", |
| 127 | "DROP TRIGGER IF EXISTS docs_identity_links_keep_delete", |
| 128 | "DROP TABLE docs_identity_links", |
| 129 | "DROP TABLE IF EXISTS docs_link_flows", |
| 130 | "DROP TABLE IF EXISTS docs_link_clock", |
| 131 | } { |
| 132 | if _, err := tx.ExecContext(ctx, q); err != nil { |
| 133 | return errLinksUnavailable |
| 134 | } |
| 135 | } |
| 136 | if err := tx.Commit(); err != nil { |
| 137 | return errLinksUnavailable |
| 138 | } |
| 139 | return nil |
| 140 | } |
| 141 | |
| 142 | // readOldLinks reads every row before adopting any, so no cursor is open |
| 143 | // on the transaction's one connection while Adopt uses it. |
| 144 | func readOldLinks(ctx context.Context, tx *sql.Tx) ([]oldLink, error) { |
| 145 | rows, err := tx.QueryContext(ctx, "SELECT issuer, subject, person_id FROM docs_identity_links ORDER BY issuer, subject") |
| 146 | if err != nil { |
| 147 | return nil, err |
| 148 | } |
| 149 | defer rows.Close() |
| 150 | var out []oldLink |
| 151 | for rows.Next() { |
| 152 | var l oldLink |
| 153 | if err := rows.Scan(&l.issuer, &l.subject, &l.person); err != nil { |
| 154 | return nil, err |
| 155 | } |
| 156 | out = append(out, l) |
| 157 | } |
| 158 | return out, rows.Err() |
| 159 | } |
| 160 | |
| 161 | func tableExists(ctx context.Context, tx *sql.Tx, name string) (bool, error) { |
| 162 | var n int |
| 163 | err := tx.QueryRowContext(ctx, "SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = ?", name).Scan(&n) |
| 164 | return n == 1, err |
| 165 | } |
| 166 | |