| 1 |
|
| #ifndef PQ_MIGRATE_H |
| 2 |
|
| #define PQ_MIGRATE_H |
| 3 |
|
| |
| 4 |
|
| /* pq_migrate.h - the migration runner, shared by both backends. |
| 5 |
|
| * |
| 6 |
|
| * Minion's model, kept: a single named row in pq_migrations holds the |
| 7 |
|
| * schema version, and migrate($to) applies steps version+1 .. $to inside |
| 8 |
|
| * one transaction. Both backends have transactional DDL, so a failed |
| 9 |
|
| * migration leaves the schema exactly as it was rather than half-applied. |
| 10 |
|
| * |
| 11 |
|
| * v0.01 is forward-only, and says so rather than shipping a down path that |
| 12 |
|
| * has never been run. `punk-queue migrate --check` (phase 3) is the deploy |
| 13 |
|
| * hook. |
| 14 |
|
| * |
| 15 |
|
| * The lock is per-backend: pg_advisory_xact_lock on Pg, BEGIN IMMEDIATE on |
| 16 |
|
| * SQLite. Without one, two workers booting together race to apply step 1 |
| 17 |
|
| * and one of them gets "table already exists". |
| 18 |
|
| * |
| 19 |
|
| * Include after pq_dbi.h and the per-backend step arrays. */ |
| 20 |
|
| |
| 21 |
|
| #define PQ_MIGRATION_NAME "punk_queue" |
| 22 |
|
| |
| 23 |
|
| /* Split one migration into statements on a line containing only "-- @@". |
| 24 |
|
| * Mortal AV of mortal-safe copies. */ |
| 25 |
414 |
| static AV *pq_migrate_split(pTHX_ const char *step) { |
| 26 |
414 |
| AV *out = (AV *)sv_2mortal((SV *)newAV()); |
| 27 |
414 |
| const char *p = step, *start = step; |
| 28 |
14214 |
| for (;;) { |
| 29 |
14628 |
| const char *nl = strchr(p, '\n'); |
| 30 |
14628 |
▶ | STRLEN linelen = nl ? (STRLEN)(nl - p) : strlen(p); |
Branch: 1 branch
| Branch | True | False | Total |
| STRLEN linelen = nl ? (STRLEN)(nl - p) : strlen(p); |
14214 |
414 |
14628 |
|
| 31 |
|
| /* a separator line is exactly "-- @@" possibly with trailing space */ |
| 32 |
14628 |
| int sep = 0; |
| 33 |
14628 |
▶ | if (linelen >= 5 && memEQ(p, "-- @@", 5)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (linelen >= 5 && memEQ(p, "-- @@", 5)) { |
13386 |
1242 |
14628 |
| if (linelen >= 5 && memEQ(p, "-- @@", 5)) { |
2208 |
11178 |
13386 |
|
| 34 |
|
| STRLEN i; |
| 35 |
2208 |
| sep = 1; |
| 36 |
2208 |
▶ | for (i = 5; i < linelen; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 5; i < linelen; i++) |
0 |
2208 |
2208 |
|
| 37 |
0 |
▶ | if (p[i] != ' ' && p[i] != '\t' && p[i] != '\r') { sep = 0; break; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (p[i] != ' ' && p[i] != '\t' && p[i] != '\r') { sep = 0; break; } |
0 |
0 |
0 |
| if (p[i] != ' ' && p[i] != '\t' && p[i] != '\r') { sep = 0; break; } |
0 |
0 |
0 |
| if (p[i] != ' ' && p[i] != '\t' && p[i] != '\r') { sep = 0; break; } |
0 |
0 |
0 |
|
| 38 |
|
| } |
| 39 |
14628 |
▶ | if (sep) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sep) { |
2208 |
12420 |
14628 |
|
| 40 |
2208 |
▶ | if (p > start) av_push(out, newSVpvn(start, (STRLEN)(p - start))); |
Branch: 1 branch
| Branch | True | False | Total |
| if (p > start) av_push(out, newSVpvn(start, (STRLEN)(p - start))); |
2208 |
0 |
2208 |
|
| 41 |
2208 |
▶ | start = nl ? nl + 1 : p + linelen; |
Branch: 1 branch
| Branch | True | False | Total |
| start = nl ? nl + 1 : p + linelen; |
2208 |
0 |
2208 |
|
| 42 |
|
| } |
| 43 |
14628 |
▶ | if (!nl) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nl) break; |
414 |
14214 |
14628 |
|
| 44 |
14214 |
| p = nl + 1; |
| 45 |
|
| } |
| 46 |
414 |
▶ | if (*start) av_push(out, newSVpvn(start, strlen(start))); |
Branch: 1 branch
| Branch | True | False | Total |
| if (*start) av_push(out, newSVpvn(start, strlen(start))); |
414 |
0 |
414 |
|
| 47 |
414 |
| return out; |
| 48 |
|
| } |
| 49 |
|
| |
| 50 |
|
| /* The schema version recorded in the database, or 0 when the table does not |
| 51 |
|
| * exist yet. Never croaks on a missing table - that is the first-run case. */ |
| 52 |
442 |
| static IV pq_schema_version(pTHX_ SV *self) { |
| 53 |
442 |
| SV *sth, *sql = sv_2mortal(newSVpvs( |
| 54 |
|
| "SELECT version FROM pq_migrations WHERE name = ?")); |
| 55 |
442 |
| AV *bind = pq_binds(aTHX), *row; |
| 56 |
442 |
| IV v = 0; |
| 57 |
442 |
| int died = 0; |
| 58 |
442 |
| SV *dbh = pq_dbh(aTHX_ self); |
| 59 |
|
| SV *argv[3]; |
| 60 |
|
| |
| 61 |
442 |
| pq_bind_pv(aTHX_ bind, PQ_MIGRATION_NAME); |
| 62 |
|
| |
| 63 |
442 |
| argv[0] = sql; |
| 64 |
442 |
| argv[1] = &PL_sv_undef; |
| 65 |
442 |
| argv[2] = sv_2mortal(newSViv(3)); |
| 66 |
442 |
| sth = pq_call_meth_ev(aTHX_ dbh, "prepare_cached", argv, 3, 1, &died); |
| 67 |
442 |
▶ | if (died || !sth) { if (sth) SvREFCNT_dec(sth); return 0; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (died || !sth) { if (sth) SvREFCNT_dec(sth); return 0; } |
300 |
142 |
442 |
| if (died || !sth) { if (sth) SvREFCNT_dec(sth); return 0; } |
0 |
300 |
300 |
| if (died || !sth) { if (sth) SvREFCNT_dec(sth); return 0; } |
0 |
142 |
142 |
|
| 68 |
300 |
| sv_2mortal(sth); |
| 69 |
|
| |
| 70 |
|
| { |
| 71 |
300 |
| SSize_t n = av_len(bind) + 1, i; |
| 72 |
300 |
▶ | SV **ea = n ? (SV **)pq_xmalloc(aTHX_ sizeof(SV *) * (size_t)n) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **ea = n ? (SV **)pq_xmalloc(aTHX_ sizeof(SV *) * (size_t)n) : NULL; |
300 |
0 |
300 |
|
| 73 |
|
| SV *r; |
| 74 |
600 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
300 |
300 |
600 |
|
| 75 |
300 |
| SV **e = av_fetch(bind, i, 0); |
| 76 |
300 |
▶ | ea[i] = (e && *e) ? *e : &PL_sv_undef; |
Branch: 2 branches
| Branch | True | False | Total |
| ea[i] = (e && *e) ? *e : &PL_sv_undef; |
300 |
0 |
300 |
| ea[i] = (e && *e) ? *e : &PL_sv_undef; |
300 |
0 |
300 |
|
| 77 |
|
| } |
| 78 |
300 |
| r = pq_call_meth_ev(aTHX_ sth, "execute", ea, (int)n, 1, &died); |
| 79 |
300 |
▶ | if (ea) safefree(ea); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ea) safefree(ea); |
300 |
0 |
300 |
|
| 80 |
300 |
▶ | if (r) SvREFCNT_dec(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) SvREFCNT_dec(r); |
300 |
0 |
300 |
|
| 81 |
300 |
▶ | if (died) return 0; /* no such table: version 0 */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (died) return 0; /* no such table: version 0 */ |
0 |
300 |
300 |
|
| 82 |
|
| } |
| 83 |
|
| |
| 84 |
300 |
| row = pq_fetchrow(aTHX_ sth); |
| 85 |
300 |
▶ | if (row) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (row) { |
300 |
0 |
300 |
|
| 86 |
300 |
| v = pq_col_iv(aTHX_ row, 0); |
| 87 |
300 |
| SvREFCNT_dec((SV *)row); |
| 88 |
|
| } |
| 89 |
300 |
| { SV *r = pq_call_meth(aTHX_ sth, "finish", NULL, 0, 1); |
| 90 |
300 |
▶ | if (r) SvREFCNT_dec(r); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) SvREFCNT_dec(r); } |
300 |
0 |
300 |
|
| 91 |
300 |
| return v; |
| 92 |
|
| } |
| 93 |
|
| |
| 94 |
|
| /* Forward declarations: transaction control and the step table are |
| 95 |
|
| * per-backend. */ |
| 96 |
|
| static void pq_txn_begin_exclusive(pTHX_ SV *self); |
| 97 |
|
| static void pq_txn_commit(pTHX_ SV *self); |
| 98 |
|
| static void pq_txn_rollback(pTHX_ SV *self); |
| 99 |
|
| static const char *const *pq_migration_steps(pTHX_ SV *self); |
| 100 |
|
| static void pq_check_server_version(pTHX_ SV *self); |
| 101 |
|
| |
| 102 |
336 |
| static IV pq_migration_count(pTHX_ SV *self) { |
| 103 |
336 |
| const char *const *steps = pq_migration_steps(aTHX_ self); |
| 104 |
336 |
| IV n = 0; |
| 105 |
1344 |
▶ | while (steps && steps[n]) n++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (steps && steps[n]) n++; |
1344 |
0 |
1344 |
| while (steps && steps[n]) n++; |
1008 |
336 |
1344 |
|
| 106 |
336 |
| return n; |
| 107 |
|
| } |
| 108 |
|
| |
| 109 |
|
| /* Apply steps up to `to` (or all of them when to <= 0). Returns the version |
| 110 |
|
| * now in the database. Idempotent: re-running against a current schema does |
| 111 |
|
| * nothing and returns the same number. */ |
| 112 |
332 |
| static IV pq_migrate(pTHX_ SV *self, IV to) { |
| 113 |
332 |
| const char *const *steps = pq_migration_steps(aTHX_ self); |
| 114 |
332 |
| IV have, want = pq_migration_count(aTHX_ self), i; |
| 115 |
|
| |
| 116 |
332 |
| pq_check_server_version(aTHX_ self); |
| 117 |
|
| |
| 118 |
332 |
▶ | if (to > 0 && to < want) want = to; |
Branch: 2 branches
| Branch | True | False | Total |
| if (to > 0 && to < want) want = to; |
1 |
331 |
332 |
| if (to > 0 && to < want) want = to; |
1 |
0 |
1 |
|
| 119 |
|
| |
| 120 |
|
| /* Cheap pre-check outside the lock: the overwhelmingly common call is |
| 121 |
|
| * "already current" on every worker boot, and taking an exclusive lock |
| 122 |
|
| * for that would serialise startup across the fleet. */ |
| 123 |
332 |
| have = pq_schema_version(aTHX_ self); |
| 124 |
332 |
▶ | if (have >= want) return have; |
Branch: 1 branch
| Branch | True | False | Total |
| if (have >= want) return have; |
194 |
138 |
332 |
|
| 125 |
|
| |
| 126 |
138 |
| pq_txn_begin_exclusive(aTHX_ self); |
| 127 |
|
| |
| 128 |
|
| /* The version table is created BEFORE it is read inside the |
| 129 |
|
| * transaction, unconditionally. Order matters on PostgreSQL: any error |
| 130 |
|
| * inside a transaction aborts the whole transaction, so the tolerant |
| 131 |
|
| * "read the version, treat a missing table as 0" trick that works on |
| 132 |
|
| * autocommit (and that SQLite forgives anywhere) would poison the |
| 133 |
|
| * migration transaction on a fresh Pg database. With the CREATE first, |
| 134 |
|
| * the in-transaction read can never fail. */ |
| 135 |
138 |
| (void)pq_do_pv(aTHX_ self, |
| 136 |
|
| "CREATE TABLE IF NOT EXISTS pq_migrations (" |
| 137 |
|
| "name TEXT PRIMARY KEY, version INTEGER NOT NULL)"); |
| 138 |
|
| |
| 139 |
|
| /* Re-read under the lock: another process may have applied everything |
| 140 |
|
| * between the pre-check and here. */ |
| 141 |
|
| { |
| 142 |
138 |
| AV *b = pq_binds(aTHX), *row; |
| 143 |
138 |
| pq_bind_pv(aTHX_ b, PQ_MIGRATION_NAME); |
| 144 |
138 |
| row = pq_selectrow(aTHX_ self, sv_2mortal(newSVpvs( |
| 145 |
|
| "SELECT version FROM pq_migrations WHERE name = ?")), b); |
| 146 |
138 |
▶ | if (row) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (row) { |
0 |
138 |
138 |
|
| 147 |
0 |
| have = pq_col_iv(aTHX_ row, 0); |
| 148 |
0 |
| SvREFCNT_dec((SV *)row); |
| 149 |
|
| } |
| 150 |
|
| else { |
| 151 |
138 |
| have = 0; |
| 152 |
138 |
| b = pq_binds(aTHX); |
| 153 |
138 |
| pq_bind_pv(aTHX_ b, PQ_MIGRATION_NAME); |
| 154 |
138 |
| (void)pq_do(aTHX_ self, sv_2mortal(newSVpvs( |
| 155 |
|
| "INSERT INTO pq_migrations (name, version) VALUES (?, 0)")), b); |
| 156 |
|
| } |
| 157 |
|
| } |
| 158 |
138 |
▶ | if (have >= want) { pq_txn_commit(aTHX_ self); return have; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (have >= want) { pq_txn_commit(aTHX_ self); return have; } |
0 |
138 |
138 |
|
| 159 |
|
| |
| 160 |
552 |
▶ | for (i = have; i < want; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = have; i < want; i++) { |
414 |
138 |
552 |
|
| 161 |
414 |
| AV *stmts = pq_migrate_split(aTHX_ steps[i]); |
| 162 |
414 |
| SSize_t n = av_len(stmts) + 1, j; |
| 163 |
3036 |
▶ | for (j = 0; j < n; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < n; j++) { |
2622 |
414 |
3036 |
|
| 164 |
2622 |
| SV **e = av_fetch(stmts, j, 0); |
| 165 |
2622 |
▶ | if (!(e && *e)) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(e && *e)) continue; |
2622 |
0 |
2622 |
| if (!(e && *e)) continue; |
0 |
2622 |
2622 |
|
| 166 |
|
| /* Skip a chunk that is only whitespace - a trailing separator |
| 167 |
|
| * or a blank line between statements. */ |
| 168 |
|
| { |
| 169 |
|
| STRLEN l; |
| 170 |
2622 |
| const char *s = SvPV_const(*e, l); |
| 171 |
2622 |
| STRLEN k = 0; |
| 172 |
2622 |
▶ | while (k < l && isSPACE((U8)s[k])) k++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (k < l && isSPACE((U8)s[k])) k++; |
2622 |
0 |
2622 |
| while (k < l && isSPACE((U8)s[k])) k++; |
0 |
2622 |
2622 |
|
| 173 |
2622 |
▶ | if (k == l) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (k == l) continue; |
0 |
2622 |
2622 |
|
| 174 |
|
| } |
| 175 |
2622 |
| (void)pq_do(aTHX_ self, *e, NULL); |
| 176 |
|
| } |
| 177 |
|
| } |
| 178 |
|
| |
| 179 |
|
| { |
| 180 |
138 |
| AV *b = pq_binds(aTHX); |
| 181 |
138 |
| pq_bind_iv(aTHX_ b, want); |
| 182 |
138 |
| pq_bind_pv(aTHX_ b, PQ_MIGRATION_NAME); |
| 183 |
138 |
| (void)pq_do(aTHX_ self, sv_2mortal(newSVpvs( |
| 184 |
|
| "UPDATE pq_migrations SET version = ? WHERE name = ?")), b); |
| 185 |
|
| } |
| 186 |
|
| |
| 187 |
138 |
| pq_txn_commit(aTHX_ self); |
| 188 |
138 |
| return want; |
| 189 |
|
| } |
| 190 |
|
| |
| 191 |
|
| #endif /* PQ_MIGRATE_H */ |