@rebasepro/cli 0.31.0-canary.g902a0ce → 0.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. package/dist/{auth-CyADghFQ.js → auth-ofQETpM5.js} +3 -3
  2. package/dist/{auth-CyADghFQ.js.map → auth-ofQETpM5.js.map} +1 -1
  3. package/dist/{console-CMjhgMUE.js → console-DjThnr3f.js} +2 -2
  4. package/dist/{console-CMjhgMUE.js.map → console-DjThnr3f.js.map} +1 -1
  5. package/dist/{db-A7XDCIIO.js → db-Bz2RFGJj.js} +26 -6
  6. package/dist/db-Bz2RFGJj.js.map +1 -0
  7. package/dist/{dev-tWwPyWkQ.js → dev-D8TxrE_I.js} +2 -2
  8. package/dist/{dev-tWwPyWkQ.js.map → dev-D8TxrE_I.js.map} +1 -1
  9. package/dist/dev-db/__fixtures__/unanswering-postgres.d.ts +11 -0
  10. package/dist/dev-db/attver.d.ts +38 -6
  11. package/dist/dev-db/local-timeouts.d.ts +25 -0
  12. package/dist/dev-db/pull.d.ts +39 -9
  13. package/dist/{doctor-BBM0dWvZ.js → doctor-Du-mSxoE.js} +2 -2
  14. package/dist/{doctor-BBM0dWvZ.js.map → doctor-Du-mSxoE.js.map} +1 -1
  15. package/dist/index.es.js +11 -11
  16. package/dist/local-timeouts-B1Kwligw.js +30 -0
  17. package/dist/local-timeouts-B1Kwligw.js.map +1 -0
  18. package/dist/{pull-D9WvpDYY.js → pull-BMuiUyjb.js} +274 -54
  19. package/dist/pull-BMuiUyjb.js.map +1 -0
  20. package/dist/{schema-DwFDI1iU.js → schema-Dv6nHkb0.js} +3 -3
  21. package/dist/{schema-DwFDI1iU.js.map → schema-Dv6nHkb0.js.map} +1 -1
  22. package/dist/schema-reconcile-CjxLaRHB.js +2 -0
  23. package/dist/{status-DQ_PPz4C.js → status-BTgTWzlQ.js} +2 -2
  24. package/dist/{status-DQ_PPz4C.js.map → status-BTgTWzlQ.js.map} +1 -1
  25. package/dist/{verify-DkJqhYPI.js → verify-BkUVGLqo.js} +2 -2
  26. package/dist/{verify-DkJqhYPI.js.map → verify-BkUVGLqo.js.map} +1 -1
  27. package/package.json +10 -10
  28. package/dist/db-A7XDCIIO.js.map +0 -1
  29. package/dist/pull-D9WvpDYY.js.map +0 -1
  30. package/dist/schema-reconcile-Tto54Qbv.js +0 -2
@@ -0,0 +1,30 @@
1
+ //#region src/dev-db/local-timeouts.ts
2
+ /**
3
+ * How long `rebase db pull` waits on the local database before it stops
4
+ * waiting and says so.
5
+ *
6
+ * The managed database can stop answering without refusing anything. Its
7
+ * daemon accepts the socket and never finishes the start-up handshake, or
8
+ * finishes it and never answers a query. A pull from a LATIN1 database did
9
+ * exactly that: pg_restore's `SET client_encoding = 'LATIN1'` left the daemon
10
+ * answering nobody, and the re-grant that followed opened a connection with
11
+ * no bound. The pull then hung for good, after it had already emptied the local
12
+ * database and before it printed the failure it had already computed.
13
+ *
14
+ * Every connection a pull opens to the local database takes
15
+ * {@link LOCAL_CONNECT_TIMEOUT_MS}. A connection whose statements are all short
16
+ * also takes {@link LOCAL_QUERY_TIMEOUT_MS}: catalog reads, `RESET`, grants.
17
+ * A connection that rewrites whole tables does not take it, because those
18
+ * statements run as long as the tables are big.
19
+ *
20
+ * These live in their own module so that a test can shorten them for every
21
+ * caller at once.
22
+ */
23
+ /** The connection, start-up handshake included: a wedged daemon accepts the socket and never answers. */
24
+ var LOCAL_CONNECT_TIMEOUT_MS = 1e4;
25
+ /** One short statement: a catalog read, a `RESET`, a `GRANT`. */
26
+ var LOCAL_QUERY_TIMEOUT_MS = 15e3;
27
+ //#endregion
28
+ export { LOCAL_QUERY_TIMEOUT_MS as n, LOCAL_CONNECT_TIMEOUT_MS as t };
29
+
30
+ //# sourceMappingURL=local-timeouts-B1Kwligw.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"local-timeouts-B1Kwligw.js","names":[],"sources":["../src/dev-db/local-timeouts.ts"],"sourcesContent":["/**\n * How long `rebase db pull` waits on the local database before it stops\n * waiting and says so.\n *\n * The managed database can stop answering without refusing anything. Its\n * daemon accepts the socket and never finishes the start-up handshake, or\n * finishes it and never answers a query. A pull from a LATIN1 database did\n * exactly that: pg_restore's `SET client_encoding = 'LATIN1'` left the daemon\n * answering nobody, and the re-grant that followed opened a connection with\n * no bound. The pull then hung for good, after it had already emptied the local\n * database and before it printed the failure it had already computed.\n *\n * Every connection a pull opens to the local database takes\n * {@link LOCAL_CONNECT_TIMEOUT_MS}. A connection whose statements are all short\n * also takes {@link LOCAL_QUERY_TIMEOUT_MS}: catalog reads, `RESET`, grants.\n * A connection that rewrites whole tables does not take it, because those\n * statements run as long as the tables are big.\n *\n * These live in their own module so that a test can shorten them for every\n * caller at once.\n */\n\n/** The connection, start-up handshake included: a wedged daemon accepts the socket and never answers. */\nexport const LOCAL_CONNECT_TIMEOUT_MS = 10_000;\n\n/** One short statement: a catalog read, a `RESET`, a `GRANT`. */\nexport const LOCAL_QUERY_TIMEOUT_MS = 15_000;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAuBA,IAAa,2BAA2B;;AAGxC,IAAa,yBAAyB"}
@@ -1,4 +1,5 @@
1
1
  import { r as PGLITE_EXTENSIONS } from "./constraints-CtiylsGf.js";
2
+ import { n as LOCAL_QUERY_TIMEOUT_MS, t as LOCAL_CONNECT_TIMEOUT_MS } from "./local-timeouts-B1Kwligw.js";
2
3
  import { i as hostOf, n as connectionErrorText, t as banner } from "./connection-banner-DbLJzWf4.js";
3
4
  import fs from "fs";
4
5
  import { execa } from "execa";
@@ -28,24 +29,93 @@ import { execa } from "execa";
28
29
  * overwritten with spaces — the whole statement when it is the only option,
29
30
  * the key and its comma when there are others. Byte for byte the same length,
30
31
  * because the TOC records the file offset of every data block after it.
32
+ *
33
+ * The definitions are edited as bytes, never decoded: pg_dump writes them in
34
+ * the database's encoding, which need not be UTF-8. Every encoding a server
35
+ * can store is ASCII-safe — a byte below 0x80 is always that ASCII character —
36
+ * so the ASCII statement is found by reading one character per byte, and the
37
+ * bytes around it are written back exactly as they were. The client-only
38
+ * encodings (SJIS, BIG5, GBK, UHC, GB18030, JOHAB, SHIFT_JIS_2004) are not
39
+ * ASCII-safe; a dump taken in one, through `PGCLIENTENCODING` or a
40
+ * `client_encoding` in the connection string, is left as it was.
41
+ *
42
+ * The same read of the TOC also answers which encoding a dump is written in
43
+ * ({@link archiveEncoding}). A pull into the managed database asks that before
44
+ * it empties anything.
31
45
  */
32
46
  var ATTVER_ONLY = /^(ALTER (?:FOREIGN )?TABLE (?:ONLY )?.* ALTER COLUMN .* SET )\(pg_attver=\d+\);$/gm;
33
47
  var ATTVER_FIRST = /(?<=^ALTER (?:FOREIGN )?TABLE (?:ONLY )?.* ALTER COLUMN .* SET \()pg_attver=\d+, (?=.*\);$)/gm;
34
48
  var ATTVER_LATER = /(?<=^ALTER (?:FOREIGN )?TABLE (?:ONLY )?.* ALTER COLUMN .* SET \(.*), pg_attver=\d+(?=.*\);$)/gm;
35
49
  function spaces(text) {
36
- return " ".repeat(Buffer.byteLength(text, "utf8"));
50
+ return " ".repeat(text.length);
37
51
  }
38
52
  /**
39
- * A TOC entry's definition without its `pg_attver` options, as the same
40
- * number of UTF-8 bytes: what was taken out is spaces.
53
+ * A TOC entry's definition, as the bytes pg_dump wrote, without its
54
+ * `pg_attver` options: what was taken out is spaces, and every other byte is
55
+ * as it was. Matched one character per byte (`latin1`), which finds the ASCII
56
+ * statement in any ASCII-safe encoding without decoding anything else.
41
57
  */
42
58
  function blankAttverOptions(definition) {
43
- if (!definition.includes("pg_attver=")) return definition;
44
- return definition.replace(ATTVER_ONLY, (statement) => spaces(statement)).replace(ATTVER_FIRST, (option) => spaces(option)).replace(ATTVER_LATER, (option) => spaces(option));
59
+ const text = definition.toString("latin1");
60
+ if (!text.includes("pg_attver=")) return definition;
61
+ const blanked = text.replace(ATTVER_ONLY, (statement) => spaces(statement)).replace(ATTVER_FIRST, (option) => spaces(option)).replace(ATTVER_LATER, (option) => spaces(option));
62
+ return blanked === text ? definition : Buffer.from(blanked, "latin1");
45
63
  }
46
- /** `K_VERS_1_12`…`K_VERS_1_16`: the custom-format archives pg_dump 11 to 18 write. */
64
+ /**
65
+ * The entries whose definition can hold attribute options: pg_dump's
66
+ * `dumpTableSchema` writes `ALTER … ALTER COLUMN … SET (…)` for every relation
67
+ * but a view, under the entry it describes as one of these.
68
+ */
69
+ var RELATION_ENTRIES = /* @__PURE__ */ new Set([
70
+ "TABLE",
71
+ "FOREIGN TABLE",
72
+ "MATERIALIZED VIEW"
73
+ ]);
74
+ /**
75
+ * The encodings a server can store (`PG_SQL_ASCII` … `PG_KOI8U` in
76
+ * pg_wchar.h), named as `pg_encoding_to_char` names them in the ENCODING
77
+ * entry. Each is ASCII-safe; the client-only encodings after them are not.
78
+ */
79
+ var ASCII_SAFE_ENCODINGS = /* @__PURE__ */ new Set([
80
+ "SQL_ASCII",
81
+ "EUC_JP",
82
+ "EUC_CN",
83
+ "EUC_KR",
84
+ "EUC_TW",
85
+ "EUC_JIS_2004",
86
+ "UTF8",
87
+ "MULE_INTERNAL",
88
+ "LATIN1",
89
+ "LATIN2",
90
+ "LATIN3",
91
+ "LATIN4",
92
+ "LATIN5",
93
+ "LATIN6",
94
+ "LATIN7",
95
+ "LATIN8",
96
+ "LATIN9",
97
+ "LATIN10",
98
+ "WIN1256",
99
+ "WIN1258",
100
+ "WIN866",
101
+ "WIN874",
102
+ "KOI8R",
103
+ "WIN1251",
104
+ "WIN1252",
105
+ "ISO_8859_5",
106
+ "ISO_8859_6",
107
+ "ISO_8859_7",
108
+ "ISO_8859_8",
109
+ "WIN1250",
110
+ "WIN1253",
111
+ "WIN1254",
112
+ "WIN1255",
113
+ "WIN1257",
114
+ "KOI8U"
115
+ ]);
116
+ /** `K_VERS_1_12`…`K_VERS_MAX` (1.16 at any revision): the custom-format archives pg_dump 11 to 18 write. */
47
117
  var OLDEST = archiveVersion(1, 12, 0);
48
- var NEWEST = archiveVersion(1, 16, 0);
118
+ var NEWEST = archiveVersion(1, 16, 255);
49
119
  var V1_14 = archiveVersion(1, 14, 0);
50
120
  var V1_15 = archiveVersion(1, 15, 0);
51
121
  var V1_16 = archiveVersion(1, 16, 0);
@@ -90,15 +160,19 @@ var ArchiveReader = class {
90
160
  for (let i = digits.length - 1; i >= 0; i--) value = value * 256 + (digits[i] ?? 0);
91
161
  return negative ? -value : value;
92
162
  }
93
- /** `ReadStr`: a length, then that many bytes; a negative length is NULL. Returns where the bytes are. */
163
+ /** `ReadStr`: a length, then that many bytes; a negative length is NULL. Returns the bytes and where they are. */
94
164
  str() {
95
165
  const length = this.int();
96
166
  if (length < 0) return null;
97
167
  return {
98
168
  at: this.position,
99
- text: this.bytes(length).toString("utf8")
169
+ bytes: this.bytes(length)
100
170
  };
101
171
  }
172
+ /** `ReadStr` for a field that is ASCII in every dump: a tag, a description, a version. */
173
+ ascii() {
174
+ return this.str()?.bytes.toString("latin1") ?? null;
175
+ }
102
176
  /** `ReadOffset`: a flag byte, then `offSize` bytes, least significant first. */
103
177
  offset() {
104
178
  const flag = this.byte();
@@ -111,33 +185,47 @@ var ArchiveReader = class {
111
185
  };
112
186
  }
113
187
  };
114
- /** The edits that blank `pg_attver` in an archive's TOC, or `null` when the file is not one this can read. */
115
- function planPatches(fd, size) {
188
+ var NOT_AN_ARCHIVE = "it is not a pg_dump custom-format archive this can read";
189
+ /** The edits that blank `pg_attver` in an archive's TOC, or why there are none to make safely. */
190
+ function planPatches(fd, size, blank) {
116
191
  const reader = new ArchiveReader(fd, size);
117
- if (reader.bytes(5).toString("latin1") !== "PGDMP") return null;
118
- const version = archiveVersion(reader.byte(), reader.byte(), reader.byte());
119
- if (version < OLDEST || version > NEWEST) return null;
192
+ let serverVersion = null;
193
+ let dumpVersion = null;
194
+ let encoding = null;
195
+ const refuse = (reason) => ({
196
+ ok: false,
197
+ reason,
198
+ serverVersion,
199
+ dumpVersion,
200
+ encoding
201
+ });
202
+ if (reader.bytes(5).toString("latin1") !== "PGDMP") return refuse(NOT_AN_ARCHIVE);
203
+ const major = reader.byte();
204
+ const minor = reader.byte();
205
+ const revision = reader.byte();
206
+ const version = archiveVersion(major, minor, revision);
207
+ if (version < OLDEST || version > NEWEST) return refuse(`its archive format is version ${major}.${minor}.${revision}, which this version of Rebase does not read`);
120
208
  reader.intSize = reader.byte();
121
209
  reader.offSize = reader.byte();
122
- if (reader.intSize < 1 || reader.intSize > 8 || reader.offSize < 1 || reader.offSize > 8) return null;
123
- if (reader.byte() !== CUSTOM_FORMAT) return null;
210
+ if (reader.intSize < 1 || reader.intSize > 8 || reader.offSize < 1 || reader.offSize > 8) return refuse(NOT_AN_ARCHIVE);
211
+ if (reader.byte() !== CUSTOM_FORMAT) return refuse(NOT_AN_ARCHIVE);
124
212
  if (version >= V1_15) reader.byte();
125
213
  else reader.int();
126
214
  for (let i = 0; i < 7; i++) reader.int();
127
215
  reader.str();
128
- reader.str();
129
- reader.str();
216
+ serverVersion = reader.ascii();
217
+ dumpVersion = reader.ascii();
130
218
  const patches = [];
131
219
  let firstData = Number.POSITIVE_INFINITY;
132
220
  const entries = reader.int();
133
- if (entries < 0) return null;
221
+ if (entries < 0) return refuse(NOT_AN_ARCHIVE);
134
222
  for (let i = 0; i < entries; i++) {
135
223
  reader.int();
136
224
  reader.int();
137
225
  reader.str();
138
226
  reader.str();
139
227
  reader.str();
140
- reader.str();
228
+ const desc = reader.ascii();
141
229
  reader.int();
142
230
  const definition = reader.str();
143
231
  reader.str();
@@ -151,39 +239,93 @@ function planPatches(fd, size) {
151
239
  while (reader.str() !== null);
152
240
  const data = reader.offset();
153
241
  if (data.set) firstData = Math.min(firstData, data.value);
154
- if (definition) {
155
- const blanked = blankAttverOptions(definition.text);
156
- if (blanked !== definition.text) {
157
- const bytes = Buffer.from(blanked, "utf8");
158
- if (bytes.length !== Buffer.byteLength(definition.text, "utf8")) return null;
159
- patches.push({
160
- at: definition.at,
161
- bytes
162
- });
163
- }
242
+ if (!definition) continue;
243
+ if (desc === "ENCODING") encoding = /client_encoding = '([^']*)'/.exec(definition.bytes.toString("latin1"))?.[1] ?? null;
244
+ else if (desc !== null && RELATION_ENTRIES.has(desc)) {
245
+ const blanked = blank(definition.bytes);
246
+ if (blanked.equals(definition.bytes)) continue;
247
+ if (blanked.length !== definition.bytes.length) return refuse(`taking pg_attver out of one of its entries would have changed that entry's length (${definition.bytes.length} bytes to ${blanked.length})`);
248
+ patches.push({
249
+ at: definition.at,
250
+ bytes: blanked
251
+ });
164
252
  }
165
253
  }
166
- if (reader.position > firstData) return null;
167
- return patches;
254
+ if (reader.position > firstData) return refuse(NOT_AN_ARCHIVE);
255
+ if (patches.length > 0 && (encoding === null || !ASCII_SAFE_ENCODINGS.has(encoding))) return refuse(encoding === null ? "it does not say which encoding it is written in" : `it is written in the ${encoding} encoding, which this cannot edit safely; dump in the database's own encoding instead (unset PGCLIENTENCODING)`);
256
+ return {
257
+ ok: true,
258
+ patches,
259
+ serverVersion,
260
+ dumpVersion,
261
+ encoding
262
+ };
263
+ }
264
+ /** Reads every definition as it is: what {@link archiveEncoding} walks the TOC with. */
265
+ function unchanged(definition) {
266
+ return definition;
267
+ }
268
+ /**
269
+ * The encoding a custom-format dump is written in, as its ENCODING entry
270
+ * names it (`LATIN1`, `UTF8`, …), or null when this cannot read the file as
271
+ * an archive.
272
+ *
273
+ * That entry is the `SET client_encoding` pg_restore starts every restore
274
+ * with: the source database's encoding, unless pg_dump ran with
275
+ * `PGCLIENTENCODING` naming another. The file is opened read-only and nothing
276
+ * in it changes.
277
+ */
278
+ function archiveEncoding(dumpFile) {
279
+ const fd = fs.openSync(dumpFile, "r");
280
+ try {
281
+ return planPatches(fd, fs.fstatSync(fd).size, unchanged).encoding;
282
+ } catch (error) {
283
+ if (!(error instanceof RangeError)) throw error;
284
+ return null;
285
+ } finally {
286
+ fs.closeSync(fd);
287
+ }
168
288
  }
169
289
  /**
170
290
  * Blank every `pg_attver` attribute option in a custom-format dump, in place.
171
- * Returns how many TOC entries were changed; 0 also for a file this does not
172
- * recognise, which pg_restore then reads as it is.
291
+ *
292
+ * A file this does not recognise, or cannot edit safely, is left byte for
293
+ * byte as it was — pg_restore then reads it as it is — and the result says
294
+ * why. `blank` is the edit made to each table's definition: the pull always
295
+ * uses the default, {@link blankAttverOptions}; it is a parameter so that the
296
+ * guard against an edit that changes a definition's length can be tested.
173
297
  */
174
- function stripAttverFromArchive(dumpFile) {
298
+ function stripAttverFromArchive(dumpFile, blank = blankAttverOptions) {
175
299
  const fd = fs.openSync(dumpFile, "r+");
176
300
  try {
177
- let patches;
301
+ let plan;
178
302
  try {
179
- patches = planPatches(fd, fs.fstatSync(fd).size);
303
+ plan = planPatches(fd, fs.fstatSync(fd).size, blank);
180
304
  } catch (error) {
181
- if (error instanceof RangeError) return 0;
182
- throw error;
305
+ if (!(error instanceof RangeError)) throw error;
306
+ plan = {
307
+ ok: false,
308
+ reason: NOT_AN_ARCHIVE,
309
+ serverVersion: null,
310
+ dumpVersion: null,
311
+ encoding: null
312
+ };
183
313
  }
184
- if (!patches) return 0;
185
- for (const patch of patches) fs.writeSync(fd, patch.bytes, 0, patch.bytes.length, patch.at);
186
- return patches.length;
314
+ const versions = {
315
+ serverVersion: plan.serverVersion,
316
+ dumpVersion: plan.dumpVersion
317
+ };
318
+ if (!plan.ok) return {
319
+ entries: 0,
320
+ skipped: plan.reason,
321
+ ...versions
322
+ };
323
+ for (const patch of plan.patches) fs.writeSync(fd, patch.bytes, 0, patch.bytes.length, patch.at);
324
+ return {
325
+ entries: plan.patches.length,
326
+ skipped: null,
327
+ ...versions
328
+ };
187
329
  } finally {
188
330
  fs.closeSync(fd);
189
331
  }
@@ -717,7 +859,8 @@ async function readTargetEngine(target) {
717
859
  const { Client } = await import("pg");
718
860
  const client = new Client({
719
861
  connectionString: target,
720
- connectionTimeoutMillis: 1e4
862
+ connectionTimeoutMillis: LOCAL_CONNECT_TIMEOUT_MS,
863
+ query_timeout: LOCAL_QUERY_TIMEOUT_MS
721
864
  });
722
865
  client.on("error", () => void 0);
723
866
  try {
@@ -858,8 +1001,13 @@ function describeDumpFailure(source, run) {
858
1001
  * advice names that, rather than "pull again", which sent people in a loop.
859
1002
  * On the managed database an extension cannot be installed at all, so the
860
1003
  * advice there is Postgres in Docker.
1004
+ *
1005
+ * One cause is named for what it is: a target refusing `pg_attver`, which
1006
+ * means the local pg_dump is older than the source server and the dump could
1007
+ * not be edited to drop the key (`attver`, what {@link stripAttverFromArchive}
1008
+ * said, tells why).
861
1009
  */
862
- function restoreOutcome(run, engine = "postgres") {
1010
+ function restoreOutcome(run, engine = "postgres", attver = null) {
863
1011
  if (run.signal) return {
864
1012
  started: true,
865
1013
  failure: `pg_restore was stopped by ${run.signal} before it finished.`,
@@ -876,7 +1024,7 @@ function restoreOutcome(run, engine = "postgres") {
876
1024
  advice: null
877
1025
  };
878
1026
  const ignored = /errors ignored on restore: (\d+)/.exec(run.stderr ?? "");
879
- const repeats = engine === "pglite" ? `Pulling again repeats these errors until what they name is fixed — most often an extension the source uses that the managed database does not have (it has ${PGLITE_EXTENSIONS.map((extension) => extension.name).join(", ")}). Pull into Postgres in Docker instead (\`rebase db pull --docker\`), or pull only the schemas that do not need it (--schema).` : "Pulling again repeats these errors until what they name is fixed — most often an extension the source uses that this Postgres does not have installed. Install it here, or pull only the schemas that do not need it (--schema).";
1027
+ const repeats = (run.stderr ?? "").includes("unrecognized parameter \"pg_attver\"") ? attverAdvice(attver) : engine === "pglite" ? `Pulling again repeats these errors until what they name is fixed — most often an extension the source uses that the managed database does not have (it has ${PGLITE_EXTENSIONS.map((extension) => extension.name).join(", ")}). Pull into Postgres in Docker instead (\`rebase db pull --docker\`), or pull only the schemas that do not need it (--schema).` : "Pulling again repeats these errors until what they name is fixed — most often an extension the source uses that this Postgres does not have installed. Install it here, or pull only the schemas that do not need it (--schema).";
880
1028
  return {
881
1029
  started: true,
882
1030
  failure: ignored ? `pg_restore could not restore ${ignored[1]} object(s) — the errors are above.` : `pg_restore exited with code ${run.exitCode} — the errors are above.`,
@@ -884,6 +1032,25 @@ function restoreOutcome(run, engine = "postgres") {
884
1032
  };
885
1033
  }
886
1034
  /**
1035
+ * What to do when the target refused the dump's `pg_attver` options: they are
1036
+ * there because the local pg_dump is older than the server it dumped from,
1037
+ * and a pg_dump at least as new leaves them out.
1038
+ */
1039
+ function attverAdvice(attver) {
1040
+ return `${attver?.dumpVersion && attver.serverVersion ? `Your pg_dump (${attver.dumpVersion}) is older than the server it dumped from (${attver.serverVersion})` : "Your pg_dump is older than the server it dumped from"}: it wrote pg_attver column options, which this database refuses${attver?.skipped ? `, and Rebase could not take them out of this dump: ${attver.skipped}` : ""}. Upgrade the PostgreSQL client tools to ${attver?.serverVersion ? `at least ${attver.serverVersion}` : "at least the server's minor version"} (check with \`pg_dump --version\`), then pull again.`;
1041
+ }
1042
+ /**
1043
+ * The line a pull prints when it took `pg_attver` out of the dump: the copy is
1044
+ * whole, but the local pg_dump is older than the source server, which is what
1045
+ * to fix. Null when nothing was taken out.
1046
+ */
1047
+ function attverNotice(attver) {
1048
+ if (attver.entries === 0) return null;
1049
+ const older = attver.dumpVersion && attver.serverVersion ? `Your pg_dump (${attver.dumpVersion}) is older than the server it dumped from (${attver.serverVersion})` : "Your pg_dump is older than the server it dumped from";
1050
+ const atLeast = attver.serverVersion ? `at least ${attver.serverVersion}` : "at least the server's minor version";
1051
+ return `${older}: its dump carried pg_attver column options this database would refuse, so they were taken out of ${attver.entries} table(s) before restoring. Upgrading the PostgreSQL client tools to ${atLeast} is the supported fix.`;
1052
+ }
1053
+ /**
887
1054
  * The local schemas a pull empties before it restores: every one of the local
888
1055
  * database's own for a whole-database pull, and those it copies for a
889
1056
  * `--schema` pull. `pg_catalog`, `information_schema` and the `pg_*` internals
@@ -915,6 +1082,47 @@ function clearStatements(schemas) {
915
1082
  * a transaction would otherwise make the pull hang without a word.
916
1083
  */
917
1084
  var CLEAR_LOCK_TIMEOUT = "15s";
1085
+ /**
1086
+ * How long one statement of {@link restoreCopy}'s may take before the pull
1087
+ * stops waiting on the database: twice the short bound, 30s. It has to stay
1088
+ * above {@link CLEAR_LOCK_TIMEOUT}, so that a lock the clear waits on is
1089
+ * reported as the lock it is (`55P03`, "stop `rebase dev`"). This bound is for
1090
+ * a database that answers nothing at all.
1091
+ */
1092
+ var CLEAR_QUERY_TIMEOUT_MS = 2 * LOCAL_QUERY_TIMEOUT_MS;
1093
+ /**
1094
+ * The one encoding the managed database restores a dump in.
1095
+ *
1096
+ * pg_restore starts every restore with the dump's own `SET client_encoding`.
1097
+ * PGlite cannot convert. Sent `SET client_encoding = 'LATIN1'`, its daemon
1098
+ * closed the connection and then answered nobody, on that connection or any
1099
+ * later one, until `rebase db stop`. A pull from a LATIN1 database had
1100
+ * already emptied the local one by then. Every statement after the SET failed
1101
+ * on "no connection to the server".
1102
+ */
1103
+ var MANAGED_ENCODING = "UTF8";
1104
+ /**
1105
+ * Why the local database cannot take a dump written in `encoding`, or null
1106
+ * when it can.
1107
+ *
1108
+ * Only the managed database refuses one. A Postgres target converts any
1109
+ * encoding to its own as pg_restore writes, which is how a LATIN1 source has
1110
+ * always pulled into Postgres in Docker. `encoding` is what
1111
+ * {@link archiveEncoding} read from the dump's ENCODING entry. Null means the
1112
+ * dump could not be read, and pg_restore is left to say what it makes of it,
1113
+ * as before. {@link restoreCopy} asks this before it empties anything, so the
1114
+ * outcome says the database is as it was.
1115
+ */
1116
+ function encodingRefusal(engine, encoding) {
1117
+ if (engine !== "pglite" || encoding === null) return null;
1118
+ if (encoding.replace(/[-_]/g, "").toUpperCase() === MANAGED_ENCODING) return null;
1119
+ return {
1120
+ cleared: false,
1121
+ started: false,
1122
+ failure: `The managed development database cannot take this dump: pg_dump wrote it in the ${encoding} encoding (the source database's, unless PGCLIENTENCODING named another), and PGlite restores UTF8 only. The \`SET client_encoding = '${encoding}'\` a restore starts with leaves it answering nobody.`,
1123
+ advice: "Pull into Postgres in Docker instead, which converts it: `rebase db pull --docker` with the same --from. Or set DATABASE_URL in .env to a Postgres you run, and pull into that."
1124
+ };
1125
+ }
918
1126
  /** Why emptying the local database failed, in the reader's terms, and what fixes it. */
919
1127
  function clearFailure(error) {
920
1128
  const code = typeof error === "object" && error !== null && "code" in error ? String(error.code) : "";
@@ -937,17 +1145,24 @@ function clearFailure(error) {
937
1145
  * restored, and the database is as it was.
938
1146
  *
939
1147
  * On the managed database the dump must be one {@link dumpSource} took for it,
940
- * with its rows as INSERTs: a COPY would leave the daemon answering nobody, so
941
- * one is refused before anything is emptied. And afterwards its one session —
1148
+ * with its rows as INSERTs, and written in UTF8: a COPY, or any other
1149
+ * encoding, would leave the daemon answering nobody, so either is refused
1150
+ * before anything is emptied. And afterwards its one session —
942
1151
  * every connection's — is given back its own settings: pg_restore leaves
943
1152
  * `search_path` empty and `row_security` off behind it, which a running
944
1153
  * `rebase dev` would otherwise query under.
945
1154
  */
946
1155
  async function restoreCopy(plan, dumpFile) {
1156
+ const encoding = archiveEncoding(dumpFile);
947
1157
  const { Client } = await import("pg");
948
- const client = new Client({ connectionString: plan.target });
1158
+ const client = new Client({
1159
+ connectionString: plan.target,
1160
+ connectionTimeoutMillis: LOCAL_CONNECT_TIMEOUT_MS,
1161
+ query_timeout: CLEAR_QUERY_TIMEOUT_MS
1162
+ });
949
1163
  client.on("error", () => void 0);
950
1164
  let engine;
1165
+ let begun = false;
951
1166
  try {
952
1167
  await client.connect();
953
1168
  engine = engineOf((await client.query("SELECT version() AS version")).rows[0]?.version ?? "");
@@ -957,18 +1172,21 @@ async function restoreCopy(plan, dumpFile) {
957
1172
  failure: "The managed development database cannot take this dump: its rows are COPY data, which PGlite's socket server cannot receive.",
958
1173
  advice: "Pull into Postgres in Docker instead: `rebase db pull --docker`."
959
1174
  };
1175
+ const refused = encodingRefusal(engine, encoding);
1176
+ if (refused) return refused;
960
1177
  const { rows } = await client.query("SELECT nspname AS \"schema\" FROM pg_namespace");
1178
+ begun = true;
961
1179
  await client.query("BEGIN");
962
1180
  await client.query(`SET LOCAL lock_timeout = '${CLEAR_LOCK_TIMEOUT}'`);
963
1181
  for (const statement of clearStatements(schemasToClear(rows, plan.schemas))) await client.query(statement);
964
1182
  await client.query("COMMIT");
965
1183
  } catch (error) {
966
- await client.query("ROLLBACK").catch(() => void 0);
1184
+ if (begun) await client.query("ROLLBACK").catch(() => void 0);
967
1185
  return clearFailure(error);
968
1186
  } finally {
969
1187
  await client.end().catch(() => void 0);
970
1188
  }
971
- stripAttverFromArchive(dumpFile);
1189
+ const attver = stripAttverFromArchive(dumpFile);
972
1190
  const run = await execa("pg_restore", restoreArgs(plan, dumpFile), {
973
1191
  env: restoreEnv(plan),
974
1192
  stdin: "ignore",
@@ -977,9 +1195,11 @@ async function restoreCopy(plan, dumpFile) {
977
1195
  reject: false
978
1196
  });
979
1197
  if (engine === "pglite") await resetSharedSession(plan.target);
1198
+ const notice = attverNotice(attver);
980
1199
  return {
981
1200
  cleared: true,
982
- ...restoreOutcome(run, engine)
1201
+ ...restoreOutcome(run, engine, attver),
1202
+ ...notice ? { notice } : {}
983
1203
  };
984
1204
  }
985
1205
  /**
@@ -995,8 +1215,8 @@ async function resetSharedSession(target) {
995
1215
  const { Client } = await import("pg");
996
1216
  const client = new Client({
997
1217
  connectionString: target,
998
- connectionTimeoutMillis: 1e4,
999
- query_timeout: 15e3
1218
+ connectionTimeoutMillis: LOCAL_CONNECT_TIMEOUT_MS,
1219
+ query_timeout: LOCAL_QUERY_TIMEOUT_MS
1000
1220
  });
1001
1221
  client.on("error", () => void 0);
1002
1222
  try {
@@ -1022,4 +1242,4 @@ function describeRestoreFailure(outcome, source) {
1022
1242
  //#endregion
1023
1243
  export { anonymizeCopy, describeRestoreFailure, describeTarget, dumpSource, findPgTool, provisionableSchemas, restoreCopy, sourceProblem };
1024
1244
 
1025
- //# sourceMappingURL=pull-D9WvpDYY.js.map
1245
+ //# sourceMappingURL=pull-BMuiUyjb.js.map