@otwdesign/db-core 1.4.1 → 1.5.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.
- package/README.md +38 -0
- package/dist/backup/api.js +2 -33
- package/dist/backup/api.js.map +1 -1
- package/dist/body.d.ts +17 -0
- package/dist/body.js +88 -0
- package/dist/body.js.map +1 -0
- package/dist/browser.d.ts +1 -0
- package/dist/drivers/mysql-connection.d.ts +101 -0
- package/dist/drivers/mysql-connection.js +204 -0
- package/dist/drivers/mysql-connection.js.map +1 -0
- package/dist/drivers/mysql-import.d.ts +22 -0
- package/dist/drivers/mysql-import.js +688 -0
- package/dist/drivers/mysql-import.js.map +1 -0
- package/dist/drivers/mysql.d.ts +11 -1
- package/dist/drivers/mysql.js +87 -97
- package/dist/drivers/mysql.js.map +1 -1
- package/dist/errors.d.ts +4 -0
- package/dist/errors.js.map +1 -1
- package/dist/handler.d.ts +31 -0
- package/dist/handler.js +93 -9
- package/dist/handler.js.map +1 -1
- package/dist/import/api.d.ts +65 -0
- package/dist/import/api.js +1282 -0
- package/dist/import/api.js.map +1 -0
- package/dist/import/audit.d.ts +47 -0
- package/dist/import/audit.js +434 -0
- package/dist/import/audit.js.map +1 -0
- package/dist/import/charsets.d.ts +25 -0
- package/dist/import/charsets.js +39 -0
- package/dist/import/charsets.js.map +1 -0
- package/dist/import/frames.d.ts +26 -0
- package/dist/import/frames.js +158 -0
- package/dist/import/frames.js.map +1 -0
- package/dist/import/grants.d.ts +26 -0
- package/dist/import/grants.js +122 -0
- package/dist/import/grants.js.map +1 -0
- package/dist/import/protocol.d.ts +462 -0
- package/dist/import/protocol.js +72 -0
- package/dist/import/protocol.js.map +1 -0
- package/dist/import/shared.d.ts +3 -0
- package/dist/import/shared.js +9 -0
- package/dist/import/shared.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/types.d.ts +308 -2
- package/package.json +7 -2
- package/src/backup/api.ts +2 -33
- package/src/body.ts +94 -0
- package/src/browser.ts +24 -0
- package/src/drivers/mysql-connection.ts +282 -0
- package/src/drivers/mysql-import.ts +769 -0
- package/src/drivers/mysql.ts +96 -107
- package/src/errors.ts +2 -0
- package/src/handler.ts +126 -8
- package/src/import/api.ts +1530 -0
- package/src/import/audit.ts +467 -0
- package/src/import/charsets.ts +46 -0
- package/src/import/frames.ts +191 -0
- package/src/import/grants.ts +117 -0
- package/src/import/protocol.ts +540 -0
- package/src/import/shared.ts +8 -0
- package/src/index.ts +24 -0
- package/src/types.ts +319 -2
|
@@ -0,0 +1,540 @@
|
|
|
1
|
+
import type { ImportError } from '../types.ts'
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* What the import endpoints and the browser say to each other, in one place so neither can drift
|
|
5
|
+
* from the other. Constants and types only, nothing from Node: the browser app takes it as it
|
|
6
|
+
* is, with frames.ts, as `@otwdesign/db-core/import`.
|
|
7
|
+
*
|
|
8
|
+
* The browser reads and splits the file; the server holds one connection for the whole import and
|
|
9
|
+
* keeps the position. Statements are numbered from 1, in the file's order.
|
|
10
|
+
*
|
|
11
|
+
* GET import-options?connection= ImportOptions
|
|
12
|
+
* GET import-status?connection=&import= ImportStatus
|
|
13
|
+
* POST import-start ImportStartRequest, as JSON ImportStarted
|
|
14
|
+
* POST import-end ImportEndRequest, as JSON ImportEnded
|
|
15
|
+
* POST import-run?… ImportRunQuery in the URL, and a batch of frames as the body (see
|
|
16
|
+
* frames.ts); answers lines of JSON, an ImportLine each
|
|
17
|
+
*
|
|
18
|
+
* A refusal is the API's usual `{ error: { code, message } }`. import-run refuses before its
|
|
19
|
+
* first line, and nothing of a refused batch runs. Before it reads the body, from what the
|
|
20
|
+
* server holds in memory:
|
|
21
|
+
*
|
|
22
|
+
* 404 an import it doesn't know (another viewer's is unknown too)
|
|
23
|
+
* 400 a URL that isn't a batch's (see `batchNumbers` in frames.ts)
|
|
24
|
+
* 409 the import has ended, or the server is ending it; or another batch of the import is
|
|
25
|
+
* running; or `start` isn't the statement after the last done; or `nbe` and `aq` aren't
|
|
26
|
+
* the session's sql_mode as it is now; or `skip` isn't a statement the server refused
|
|
27
|
+
*
|
|
28
|
+
* Then, of the body:
|
|
29
|
+
*
|
|
30
|
+
* 408 it didn't arrive whole within IMPORT_BODY_MS
|
|
31
|
+
* 413 it is past IMPORT_BATCH_BYTES
|
|
32
|
+
* 400 it isn't the batch the URL says (see `decodeFrames`)
|
|
33
|
+
*
|
|
34
|
+
* The browser never sends a batch again on its own. After a refusal, or a stream that stopped
|
|
35
|
+
* without a last line, it asks import-status and goes on from what the server says there: its
|
|
36
|
+
* position, and how the session reads strings.
|
|
37
|
+
*
|
|
38
|
+
* Where a statement ends depends on two things in the session's sql_mode (NO_BACKSLASH_ESCAPES
|
|
39
|
+
* and ANSI_QUOTES), and a file can change them. Text split under one and run under the other is
|
|
40
|
+
* other statements: the end of a string becomes SQL. So a batch says what it was split under
|
|
41
|
+
* (`nbe`, `aq`), and the server compares that with the session before every statement, the
|
|
42
|
+
* batch's first included. Before the first, a difference is the 409 above. After a statement
|
|
43
|
+
* that changed it, the batch ends there with a `mode` line.
|
|
44
|
+
*
|
|
45
|
+
* What the browser's check showed the person depends on more of the session: those two, how its
|
|
46
|
+
* character set reads the file's text (see `readsAs`), which decides what the import sends, and
|
|
47
|
+
* the database selected, where statements that name none go. That is the session's reading. A
|
|
48
|
+
* batch ends after a statement that changed any of it, with a `mode` line that says it whole;
|
|
49
|
+
* import-start and import-status say it too. A `failed` line doesn't: a statement refused can
|
|
50
|
+
* have changed it first, so the browser asks import-status before it goes on. The browser sends
|
|
51
|
+
* a batch only while the reading the server says is the one the check took for each of its
|
|
52
|
+
* statements: where they differ, it has the person read the rest of the file checked again
|
|
53
|
+
* before anything more is sent.
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
/** A batch's body at most, in bytes: 4 MiB, lengths included. */
|
|
57
|
+
export const IMPORT_BATCH_BYTES = 4_194_304
|
|
58
|
+
|
|
59
|
+
/** A batch's statements at most. */
|
|
60
|
+
export const IMPORT_BATCH_STATEMENTS = 1_000
|
|
61
|
+
|
|
62
|
+
/** What a frame's length takes, before its statement: four bytes, the big end first. */
|
|
63
|
+
export const IMPORT_LENGTH_BYTES = 4
|
|
64
|
+
|
|
65
|
+
/** One statement at most, in bytes of UTF-8: a batch to itself. A longer one can't be sent. */
|
|
66
|
+
export const IMPORT_STATEMENT_BYTES = IMPORT_BATCH_BYTES - IMPORT_LENGTH_BYTES
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* How often import-run writes a line while one statement runs, in milliseconds. A proxy or a
|
|
70
|
+
* browser that sees nothing for longer has lost the stream, not a slow statement.
|
|
71
|
+
*/
|
|
72
|
+
export const IMPORT_HEARTBEAT_MS = 10_000
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* How long a batch's body has to arrive whole, in milliseconds: 2 minutes. The import is marked
|
|
76
|
+
* busy while it is read, and its connection may hold locks, so a body that stops is let go. A
|
|
77
|
+
* line too slow for 4 MiB in that time is too slow for the file.
|
|
78
|
+
*/
|
|
79
|
+
export const IMPORT_BODY_MS = 120_000
|
|
80
|
+
|
|
81
|
+
/** What an import may take, for the dialog to say and the browser to keep to. */
|
|
82
|
+
export interface ImportLimits {
|
|
83
|
+
/** A batch's body at most, in bytes. */
|
|
84
|
+
batchBytes: number
|
|
85
|
+
/** A batch's statements at most. */
|
|
86
|
+
batchStatements: number
|
|
87
|
+
/** One statement at most, in bytes of UTF-8. */
|
|
88
|
+
statementBytes: number
|
|
89
|
+
/** How often import-run writes a line while a statement runs, in milliseconds. */
|
|
90
|
+
heartbeatMs: number
|
|
91
|
+
/** How long a batch's body has to arrive whole, in milliseconds. */
|
|
92
|
+
bodyMs: number
|
|
93
|
+
/** How long one statement may run before it is stopped, in milliseconds. */
|
|
94
|
+
statementTimeoutMs: number
|
|
95
|
+
/**
|
|
96
|
+
* How long an import stays open with no run, status or end request for it, in milliseconds.
|
|
97
|
+
* The browser asks its status more often than that for as long as its dialog is open.
|
|
98
|
+
*/
|
|
99
|
+
idleMs: number
|
|
100
|
+
/** How long the choice after a failed statement may take, in milliseconds. */
|
|
101
|
+
pauseMs: number
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** What the Import SQL dialog shows before a file is chosen. */
|
|
105
|
+
export interface ImportOptions {
|
|
106
|
+
/** `MySQL` or `MariaDB`. */
|
|
107
|
+
product: string
|
|
108
|
+
/** Such as `8.4.3`. */
|
|
109
|
+
version: string
|
|
110
|
+
/** The database the connection opens, offered first; null when its URL names none. */
|
|
111
|
+
database: string | null
|
|
112
|
+
/** The databases the login sees, empty ones too, the server's own left out. */
|
|
113
|
+
databases: ImportDatabase[]
|
|
114
|
+
limits: ImportLimits
|
|
115
|
+
/**
|
|
116
|
+
* This viewer's open or last import on the connection, null before the first. Never its id:
|
|
117
|
+
* only the dialog that started an import can go on with it. Any window of the viewer's can
|
|
118
|
+
* end it while it is open, with import-end and `current`.
|
|
119
|
+
*/
|
|
120
|
+
current: ImportStatus | null
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** A database an import can go into. */
|
|
124
|
+
export interface ImportDatabase {
|
|
125
|
+
name: string
|
|
126
|
+
/**
|
|
127
|
+
* What the login may do in it, in MySQL's own words and capitals (`CREATE`, `ALTER`, `DROP`,
|
|
128
|
+
* `INSERT`, `LOCK TABLES`, `TRIGGER`…), from its grants on everything and on this database;
|
|
129
|
+
* `ALL PRIVILEGES` stands for them all. Grants on single tables aren't counted. Null when the
|
|
130
|
+
* grants couldn't be read: the check then says nothing about them.
|
|
131
|
+
*/
|
|
132
|
+
privileges: string[] | null
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** What import-start is sent. */
|
|
136
|
+
export interface ImportStartRequest {
|
|
137
|
+
connection: string
|
|
138
|
+
/** The database to import into. */
|
|
139
|
+
database: string
|
|
140
|
+
/** The same name as the person typed it: checked against what the server says is selected. */
|
|
141
|
+
confirm: string
|
|
142
|
+
/** The file as the browser's check found it, for the audit log. */
|
|
143
|
+
file: {
|
|
144
|
+
name: string
|
|
145
|
+
/** In bytes, as chosen: a `.sql.gz`'s compressed size. */
|
|
146
|
+
size: number
|
|
147
|
+
/** How many statements the check counted. */
|
|
148
|
+
statements: number
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* The `thread` of this browser's last import on the connection, sent when the server answered
|
|
152
|
+
* that it doesn't know that import: it restarted while the import was open, and the statement
|
|
153
|
+
* that ran then may still be running on the database server. For as long as it is, the new
|
|
154
|
+
* import is refused (409 `import-thread`) with nothing opened, and the browser asks again.
|
|
155
|
+
*/
|
|
156
|
+
previousThread?: number
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** An import, open: its connection waits for the first batch. */
|
|
160
|
+
export interface ImportStarted {
|
|
161
|
+
/** Names the import in every later request. Random, and good for this viewer only. */
|
|
162
|
+
import: string
|
|
163
|
+
/** The connection's id on the server, as PROCESSLIST shows it. */
|
|
164
|
+
thread: number
|
|
165
|
+
/** The session's sql_mode, whole. */
|
|
166
|
+
sqlMode: string
|
|
167
|
+
/** sql_mode has NO_BACKSLASH_ESCAPES: split the file with a backslash as a plain character. */
|
|
168
|
+
noBackslashEscapes: boolean
|
|
169
|
+
/** sql_mode has ANSI_QUOTES: split the file with double quotes around names. */
|
|
170
|
+
ansiQuotes: boolean
|
|
171
|
+
/** The character set the session reads statements in, by its name in small letters. */
|
|
172
|
+
characterSet: string
|
|
173
|
+
/** The database selected: the one asked for. */
|
|
174
|
+
database: string | null
|
|
175
|
+
limits: ImportLimits
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** What import-run takes in its URL, beside the batch in its body. */
|
|
179
|
+
export interface ImportRunQuery {
|
|
180
|
+
connection: string
|
|
181
|
+
import: string
|
|
182
|
+
/**
|
|
183
|
+
* The number of the batch's first statement: always the one after the last done, whether it
|
|
184
|
+
* is sent or left out by `skip`.
|
|
185
|
+
*/
|
|
186
|
+
start: number
|
|
187
|
+
/** How many frames the body holds: 1 to 1,000, or with `skip`, 0 to 1,000 (0 has no body). */
|
|
188
|
+
count: number
|
|
189
|
+
/** 1 when the batch was split with NO_BACKSLASH_ESCAPES on, 0 with it off. */
|
|
190
|
+
nbe: 0 | 1
|
|
191
|
+
/** 1 when the batch was split with ANSI_QUOTES on, 0 with it off. */
|
|
192
|
+
aq: 0 | 1
|
|
193
|
+
/**
|
|
194
|
+
* The number of a refused statement to leave out, as `mysql --force` would. The batch is then
|
|
195
|
+
* statements `start` to `start` + `count` less this one: its frames are the others in the
|
|
196
|
+
* file's order, and it counts as done once those before it are. So it is `start` at the least
|
|
197
|
+
* and `start` + `count` at the most (see `batchNumbers` in frames.ts).
|
|
198
|
+
*
|
|
199
|
+
* After a failure that is `uncommitted` or `committed` it is `start` itself. After one that is
|
|
200
|
+
* `rolled-back`, `start` is the transaction's first statement, sent again with those after it,
|
|
201
|
+
* and `skip` lies further on: in a later batch when they don't all fit in this one. When no
|
|
202
|
+
* statement is left to send with it, `count` is 0.
|
|
203
|
+
*
|
|
204
|
+
* Taken only for a statement the server refused and that hasn't been done or left out since:
|
|
205
|
+
* the server remembers each until then, also while those before it run again.
|
|
206
|
+
*/
|
|
207
|
+
skip?: number
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** Which statements of the file a batch is, as its URL says. */
|
|
211
|
+
export type ImportBatch = Pick<ImportRunQuery, 'start' | 'count' | 'skip'>
|
|
212
|
+
|
|
213
|
+
/** One line of import-run's answer. The last is `done`, `failed`, `lost` or `stopped`. */
|
|
214
|
+
export type ImportLine =
|
|
215
|
+
| ImportAcceptedLine
|
|
216
|
+
| ImportStatementLine
|
|
217
|
+
| ImportBeatLine
|
|
218
|
+
| ImportModeLine
|
|
219
|
+
| ImportDoneLine
|
|
220
|
+
| ImportFailedLine
|
|
221
|
+
| ImportLostLine
|
|
222
|
+
| ImportStoppedLine
|
|
223
|
+
|
|
224
|
+
/** The first line: the batch was read whole and checked, and its first statement is next. */
|
|
225
|
+
export interface ImportAcceptedLine {
|
|
226
|
+
type: 'accepted'
|
|
227
|
+
start: number
|
|
228
|
+
count: number
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Statement `n` ran. The server counted it before it wrote this. */
|
|
232
|
+
export interface ImportStatementLine {
|
|
233
|
+
type: 'statement'
|
|
234
|
+
/** Its number in the file: with `skip`, the numbers step over the statement left out. */
|
|
235
|
+
n: number
|
|
236
|
+
/** Rows it changed. */
|
|
237
|
+
affected: number
|
|
238
|
+
warnings: number
|
|
239
|
+
/** Rows it returned, counted and not kept. */
|
|
240
|
+
rows: number
|
|
241
|
+
/** How long it took, in milliseconds. */
|
|
242
|
+
ms: number
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** Statement `n` has been running for `ms` milliseconds: written every 10 seconds while it does. */
|
|
246
|
+
export interface ImportBeatLine {
|
|
247
|
+
type: 'beat'
|
|
248
|
+
n: number
|
|
249
|
+
ms: number
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Statement `after` changed the session's reading: how it reads strings, how its character set
|
|
254
|
+
* reads the file's text (another set that reads it the same doesn't count), or the database
|
|
255
|
+
* selected. So the batch ended there: the rest of it, split and checked under the old reading,
|
|
256
|
+
* did not run. `done` follows. The browser splits the file again from `after` + 1 under these.
|
|
257
|
+
* Written when `after` is the batch's last statement too: the next batch is sent under these
|
|
258
|
+
* all the same.
|
|
259
|
+
*/
|
|
260
|
+
export interface ImportModeLine {
|
|
261
|
+
type: 'mode'
|
|
262
|
+
after: number
|
|
263
|
+
noBackslashEscapes: boolean
|
|
264
|
+
ansiQuotes: boolean
|
|
265
|
+
/** The character set the session reads statements in now, by its name in small letters. */
|
|
266
|
+
characterSet: string
|
|
267
|
+
/** The database selected now; null with none. */
|
|
268
|
+
database: string | null
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** The batch ended with every statement it ran done; the next batch starts at `done` + 1. */
|
|
272
|
+
export interface ImportDoneLine {
|
|
273
|
+
type: 'done'
|
|
274
|
+
/**
|
|
275
|
+
* The number of the last statement done or left out: the batch's last, or after a `mode`
|
|
276
|
+
* line, that line's `after`.
|
|
277
|
+
*/
|
|
278
|
+
done: number
|
|
279
|
+
/** The file has a transaction open. */
|
|
280
|
+
inTransaction: boolean
|
|
281
|
+
/** With `inTransaction`: where it began, as ImportStatus's `since` says. */
|
|
282
|
+
since?: number
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Statement `n` was refused, and the batch stopped there. Unless `ended`, the import waits for a
|
|
287
|
+
* choice, for as long as `pauseMs`: run again from `done` + 1, do the same and leave `n` out
|
|
288
|
+
* (`skip`), or end the import.
|
|
289
|
+
*/
|
|
290
|
+
export interface ImportFailedLine {
|
|
291
|
+
type: 'failed'
|
|
292
|
+
n: number
|
|
293
|
+
error: ImportError
|
|
294
|
+
state: ImportFailureState
|
|
295
|
+
/**
|
|
296
|
+
* The number of the last statement that still counts as done: `n` − 1, or with `rolled-back`
|
|
297
|
+
* and `unknown`, `since` − 1.
|
|
298
|
+
*/
|
|
299
|
+
done: number
|
|
300
|
+
/**
|
|
301
|
+
* With `uncommitted`, `rolled-back` and `unknown`: where the file's transaction began.
|
|
302
|
+
* - `uncommitted`: where the one open now began, as ImportStatus's `since` says: `n` itself
|
|
303
|
+
* when none was open before it. When one was, `since` stays where it was, whatever kind of
|
|
304
|
+
* statement `n` is (see ImportFailureState).
|
|
305
|
+
* - `rolled-back`: the first statement of the one the server undid, which held every
|
|
306
|
+
* statement from there to `n`.
|
|
307
|
+
* - `unknown`: the first of the statements of which it is not known whether they were kept.
|
|
308
|
+
*/
|
|
309
|
+
since?: number
|
|
310
|
+
/**
|
|
311
|
+
* The import is over, closed by the server: there is no choice to make. With `rolled-back`,
|
|
312
|
+
* when what was undone can't be run again as it first ran, and always with `unknown` (see
|
|
313
|
+
* ImportFailureState).
|
|
314
|
+
*/
|
|
315
|
+
ended?: true
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* What became of the statements before a failed one, as the server said when asked after it.
|
|
320
|
+
* A commit is never called a rollback, nor a rollback kept: where it can't be told, it is
|
|
321
|
+
* `unknown`.
|
|
322
|
+
* - `uncommitted`: a transaction is open. Either none was open before the failed statement, or
|
|
323
|
+
* that statement can't have rolled it back: it is a plain one (see below), a SET, or of a
|
|
324
|
+
* kind that commits first (but for an error a failed commit is known to give: see `unknown`);
|
|
325
|
+
* or it was never sent (`unsent`). What ran in it from `since` isn't committed yet, or that
|
|
326
|
+
* statement committed it.
|
|
327
|
+
* One of a kind that commits first was only seen to fail and leave a transaction open when
|
|
328
|
+
* the server refused it before its commit (a syntax error, a collation it doesn't know), on
|
|
329
|
+
* MySQL and on MariaDB: it committed nothing, and the transaction is the one from `since`.
|
|
330
|
+
* Nothing in the answer says so, though. So from then on nothing from `since` is called
|
|
331
|
+
* undone: the import's end leaves out ImportEnded's `since`, and a later failure that would
|
|
332
|
+
* be `rolled-back` is `unknown`.
|
|
333
|
+
* - `rolled-back`: the transaction is gone after a plain statement failed (see below), and no
|
|
334
|
+
* statement from its first on may have committed on its way (see below). A plain statement
|
|
335
|
+
* can't commit, so the server rolled back the one transaction that held every statement from
|
|
336
|
+
* `since` to `n`: on a deadlock, on a lock wait that ran out where it is set to, when its lock
|
|
337
|
+
* table is full. The error's number doesn't decide it. What every statement from `since` on
|
|
338
|
+
* did to rows is undone, but for rows in tables without transactions, and what it changed in
|
|
339
|
+
* the session is not (both below). They must be sent again to be run.
|
|
340
|
+
* - `committed`: no transaction is open, and what ran before is kept. Either none was open
|
|
341
|
+
* before the failed statement, and each one was committed as it ran; or the failed statement
|
|
342
|
+
* is of a kind that commits what is open before it does anything else, as CREATE, ALTER, DROP,
|
|
343
|
+
* RENAME, TRUNCATE, GRANT, REVOKE and LOCK TABLES do, and its error is one known to come from
|
|
344
|
+
* its own work after that commit: a table that exists or doesn't, a definition the server
|
|
345
|
+
* refuses, a privilege the login lacks, data an ALTER can't convert, and the like, each seen
|
|
346
|
+
* so on MySQL 8.4 or MariaDB 11.4. After any other error, see `unknown`.
|
|
347
|
+
* - `unknown`: the failed statement is one that may have ended the file's transaction either
|
|
348
|
+
* way: a CALL, an EXECUTE, a SET STATEMENT … FOR or a compound statement can commit inside, or
|
|
349
|
+
* roll back in a handler and pass the error on. Either the transaction is gone after it, or
|
|
350
|
+
* one is open that may be another: a procedure can roll back and begin anew, and the server
|
|
351
|
+
* only says that one is open, not which. Whether the statements from `since` to `n` − 1 were
|
|
352
|
+
* kept is not known, so they are neither counted as done nor offered again: the import is
|
|
353
|
+
* always `ended`, which rolls back what is open, and `done` is `since` − 1. Any statement that
|
|
354
|
+
* isn't known to be unable to roll back is taken for one that may have: one behind a comment
|
|
355
|
+
* too, and a COMMIT or a ROLLBACK that failed. No error's number makes such a failure
|
|
356
|
+
* `rolled-back`: a procedure can commit and then fail with a deadlock's.
|
|
357
|
+
*
|
|
358
|
+
* It is also the answer when the transaction is gone after a plain statement, and a statement
|
|
359
|
+
* from its first on may have committed on its way and begun another: a second START
|
|
360
|
+
* TRANSACTION, COMMIT AND CHAIN, ROLLBACK AND CHAIN, a CALL, an EXECUTE, whether it opened the
|
|
361
|
+
* transaction or ran inside it; or one of a kind that commits first that failed inside it, and
|
|
362
|
+
* left it open (see `uncommitted`). The server only undid the one open last, and doesn't say
|
|
363
|
+
* where that began: some of the statements from `since` may be committed, and what is
|
|
364
|
+
* committed must not run twice. Only a plain statement, a SET, and as the transaction's first
|
|
365
|
+
* BEGIN, START TRANSACTION or a statement of a kind that commits first (which commits what was
|
|
366
|
+
* before it) are known to have committed nothing in it. A SET can commit, but then no
|
|
367
|
+
* transaction is open after it.
|
|
368
|
+
*
|
|
369
|
+
* And when a statement of a kind that commits first fails, the transaction is gone after it,
|
|
370
|
+
* and its error is not one known to come only after that commit (see `committed`). That
|
|
371
|
+
* commit may be what failed, and the server undid the transaction; or the commit held, and
|
|
372
|
+
* the statement's own work failed after it. The server doesn't say which, and what a failed
|
|
373
|
+
* commit is answered with has no end: the engine's 1180, a replication hook's 3100, the waits
|
|
374
|
+
* for another session's FLUSH TABLES WITH READ LOCK (1205, and 1317 or MariaDB's 1969 when
|
|
375
|
+
* that wait is cut short), a server gone read-only (1290), 1105 from MySQL's commit and from
|
|
376
|
+
* MariaDB's Galera past its write-set limit, Galera's 1235 and 1047, the binary log's 1026
|
|
377
|
+
* and 1024, a class-40 SQLSTATE (a server in a cluster refuses a commit that another
|
|
378
|
+
* server's conflicts with), and whatever a plugin or a cluster adds. With a transaction open
|
|
379
|
+
* after such a statement, the answer is `unknown` too when its error is one of these known
|
|
380
|
+
* ones: class 40, 1180, 3100, 1205, 1317, 1969 and 1290.
|
|
381
|
+
*
|
|
382
|
+
* A statement the import refused itself and never sent (its error says `unsent`) changed
|
|
383
|
+
* nothing: its failure is `uncommitted` with the same `since` while a transaction is open, and
|
|
384
|
+
* `committed` while none is, whatever kind of statement it is. Never `rolled-back` or `unknown`,
|
|
385
|
+
* and the import waits for a choice.
|
|
386
|
+
*
|
|
387
|
+
* A rollback undoes rows and nothing of the session. When a statement it undid had changed how
|
|
388
|
+
* the session reads strings, or which database is selected, the statements from `since` would
|
|
389
|
+
* run a second time under what a later one set: split differently than they first ran, or into
|
|
390
|
+
* another database. The server then ends the import with the failure (`ended`) instead of
|
|
391
|
+
* waiting for a choice.
|
|
392
|
+
*
|
|
393
|
+
* It does the same when what the transaction held can't be sent again to do what it first did.
|
|
394
|
+
* Only a transaction that BEGIN, START TRANSACTION or a plain statement opened, and that held
|
|
395
|
+
* plain statements alone (INSERT, REPLACE, UPDATE, DELETE, SELECT, DO, USE, SAVEPOINT,
|
|
396
|
+
* RELEASE), is offered again. A SET is not plain: what it changed in the session (the character
|
|
397
|
+
* set, the time zone, a variable) stays changed after the rollback. Nor is a statement of a
|
|
398
|
+
* kind that commits first that opened it, nor a transaction that was open before the import's
|
|
399
|
+
* first statement, which nothing of the file opened.
|
|
400
|
+
*
|
|
401
|
+
* What a rerun still can't put back, for the dialog to say: rows the undone statements put
|
|
402
|
+
* into tables that have no transactions (MyISAM, Aria), which the server kept, and a user
|
|
403
|
+
* variable a statement other than SET assigned (`@n := @n + 1`, SELECT … INTO @n).
|
|
404
|
+
*/
|
|
405
|
+
export type ImportFailureState = 'uncommitted' | 'rolled-back' | 'committed' | 'unknown'
|
|
406
|
+
|
|
407
|
+
/** The connection went while the batch ran: the import is over, and what was open is undone. */
|
|
408
|
+
export interface ImportLostLine {
|
|
409
|
+
type: 'lost'
|
|
410
|
+
done: number
|
|
411
|
+
/** The number of the statement that was running: whether it finished is not known. */
|
|
412
|
+
doubt?: number
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/** The import was ended while the batch ran: its statement was stopped, and no other started. */
|
|
416
|
+
export interface ImportStoppedLine {
|
|
417
|
+
type: 'stopped'
|
|
418
|
+
done: number
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Where an import is, from the server's memory alone: asking runs nothing on the database.
|
|
423
|
+
* - `idle`: open, waiting for the next batch.
|
|
424
|
+
* - `running`: a batch is running, whether or not anyone still reads its answer. With `ending`,
|
|
425
|
+
* the server is closing the import, with or without a batch: ask again for how it ended.
|
|
426
|
+
* - `failed`: a statement was refused, and the import waits for a choice for `pauseLeftMs`.
|
|
427
|
+
* Never said of an import the server has begun to end: a choice would be refused.
|
|
428
|
+
* - `ended`: closed, by import-end, by a limit, or on a failure it could not go on after; a
|
|
429
|
+
* transaction left open was rolled back.
|
|
430
|
+
* - `lost`: the connection went.
|
|
431
|
+
*/
|
|
432
|
+
export interface ImportStatus {
|
|
433
|
+
state: 'idle' | 'running' | 'failed' | 'ended' | 'lost'
|
|
434
|
+
/**
|
|
435
|
+
* The number of the last statement done or left out, 0 before the first: the next batch
|
|
436
|
+
* starts after it.
|
|
437
|
+
*/
|
|
438
|
+
done: number
|
|
439
|
+
/** With `running`: the statement the server is waiting on, and for how long, in milliseconds. */
|
|
440
|
+
running?: { n: number; ms: number }
|
|
441
|
+
/**
|
|
442
|
+
* The statement that was refused, as its `failed` line said: with `failed`; with `running`
|
|
443
|
+
* while the batch it stopped still writes its last line, and while the import closes
|
|
444
|
+
* (`ending`); and with `ended` or `lost` when the import closed before a choice was made for
|
|
445
|
+
* it. Once the import has closed, the error is its numbers alone, with `ERROR 1062 (23000)`
|
|
446
|
+
* as its message: the server's words quote the file, and nothing of the file is kept. Its
|
|
447
|
+
* `unsent` stays.
|
|
448
|
+
*
|
|
449
|
+
* With `failed`, `ended` and `lost` it leaves the import where its `failed` line did, and the
|
|
450
|
+
* browser holds a status to that (with `running` the batch that met it may still be ending,
|
|
451
|
+
* and nothing is taken from it):
|
|
452
|
+
* - `since`, when there is one, is `n` at the most;
|
|
453
|
+
* - with `rolled-back` and `unknown`, `since` is there and `done` is `since` − 1;
|
|
454
|
+
* - otherwise `done` is `n` − 1;
|
|
455
|
+
* - `unknown` is never said with `failed`: the server has ended the import on it;
|
|
456
|
+
* - a statement that was never sent (`error.unsent`) is never `unknown` or `rolled-back`.
|
|
457
|
+
*/
|
|
458
|
+
failure?: { n: number; error: ImportError; state: ImportFailureState }
|
|
459
|
+
/** The file has a transaction open. */
|
|
460
|
+
inTransaction: boolean
|
|
461
|
+
/**
|
|
462
|
+
* Where the file's transaction began: with `inTransaction`; with a failure that is
|
|
463
|
+
* `rolled-back` or `unknown`, also once the import has `ended` on it; and with `rolledBack`
|
|
464
|
+
* when that is known.
|
|
465
|
+
*
|
|
466
|
+
* With `inTransaction` it is the statement that opened the one open now, or, when a statement
|
|
467
|
+
* since may have committed on its way and begun another (a second START TRANSACTION, a CALL,
|
|
468
|
+
* a CREATE that failed), the earliest that one can have begun at: some statements from there
|
|
469
|
+
* may be committed. After a `rolled-back` failure it is the first of those the server undid.
|
|
470
|
+
* After an `unknown` one it is where the statements begin of which it is not known whether
|
|
471
|
+
* they were kept. With `rolledBack` and no such failure, it is where the transaction the end
|
|
472
|
+
* rolled back began, and it is left out where that isn't known (see ImportEnded).
|
|
473
|
+
*/
|
|
474
|
+
since?: number
|
|
475
|
+
/**
|
|
476
|
+
* sql_mode has NO_BACKSLASH_ESCAPES, as the server last heard from the session: what the next
|
|
477
|
+
* batch is split under, and says in `nbe`. The browser takes it from here after a refusal or
|
|
478
|
+
* a cut stream, where a `mode` line may never have arrived.
|
|
479
|
+
*/
|
|
480
|
+
noBackslashEscapes: boolean
|
|
481
|
+
/** The same for ANSI_QUOTES, and `aq`. */
|
|
482
|
+
ansiQuotes: boolean
|
|
483
|
+
/** The character set the session reads statements in, as the server last heard: see ImportModeLine. */
|
|
484
|
+
characterSet: string
|
|
485
|
+
/** The database selected, as the server last heard; null with none. */
|
|
486
|
+
database: string | null
|
|
487
|
+
/**
|
|
488
|
+
* With `failed`: how long the choice still has, in milliseconds, as the server answers. The
|
|
489
|
+
* browser counts down from when the answer arrives: its clock is not the server's.
|
|
490
|
+
*/
|
|
491
|
+
pauseLeftMs?: number
|
|
492
|
+
/**
|
|
493
|
+
* With `running`: the server is ending the import, by import-end, by one of its limits, or on
|
|
494
|
+
* a failure it could not go on after. What runs is being stopped and what is open rolled
|
|
495
|
+
* back. No batch is taken from here on (409 `import-ended`) and no choice is waited for, so
|
|
496
|
+
* neither `idle` nor `failed` is said any more. The next answer that isn't this one is
|
|
497
|
+
* `ended` or `lost`, with how far the import got.
|
|
498
|
+
*/
|
|
499
|
+
ending?: true
|
|
500
|
+
/** With `ended`: a transaction was open at the end, and was rolled back (see `since`). */
|
|
501
|
+
rolledBack?: boolean
|
|
502
|
+
/** With `lost` and `ended`: the statement that was running, which may not have finished. */
|
|
503
|
+
doubt?: number
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* What import-end is sent: the import's id, from the dialog that started it; or `current`, from
|
|
508
|
+
* any other window of the same viewer, for their open import on the connection. An import whose
|
|
509
|
+
* dialog is gone (a tab that crashed, a page that was reloaded) is ended that way, and not left
|
|
510
|
+
* to hold its connection's locks until `idleMs` is over.
|
|
511
|
+
*
|
|
512
|
+
* Both end it the same, and are answered the same. With `current` and no import open, the
|
|
513
|
+
* answer is the 404 of an import nobody knows, also when one of the viewer's ended a moment
|
|
514
|
+
* ago: how that one ended is import-options' `current` to say.
|
|
515
|
+
*/
|
|
516
|
+
export type ImportEndRequest =
|
|
517
|
+
| { connection: string; import: string; current?: never }
|
|
518
|
+
| {
|
|
519
|
+
connection: string
|
|
520
|
+
/** `true`; 1 is taken for it too. */
|
|
521
|
+
current: true | 1
|
|
522
|
+
import?: never
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/** How an import ended: what ran is kept up to `done`, less a transaction left open. */
|
|
526
|
+
export interface ImportEnded {
|
|
527
|
+
/** The number of the last statement done. */
|
|
528
|
+
done: number
|
|
529
|
+
/** A transaction the file left open was rolled back, as the `mysql` client does at its end. */
|
|
530
|
+
rolledBack: boolean
|
|
531
|
+
/**
|
|
532
|
+
* With `rolledBack`: the number of that transaction's first statement, which with all after
|
|
533
|
+
* it up to `done` was undone. Left out when that isn't known: a statement in it may have
|
|
534
|
+
* committed and begun the one rolled back (a second START TRANSACTION, a CALL, a CREATE that
|
|
535
|
+
* failed), and then statements from where it seemed to begin may be kept.
|
|
536
|
+
*/
|
|
537
|
+
since?: number
|
|
538
|
+
/** The statement that was running and would not stop: whether it finished is not known. */
|
|
539
|
+
doubt?: number
|
|
540
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* What the browser app takes of an import, as `@otwdesign/db-core/import`: the batch's format,
|
|
3
|
+
* the protocol's limits and its types. An entry of its own, as `format` is, so the app's build
|
|
4
|
+
* finds it in the core's source and never in the server's entry. Nothing here is from Node.
|
|
5
|
+
*/
|
|
6
|
+
export * from './charsets.ts'
|
|
7
|
+
export * from './frames.ts'
|
|
8
|
+
export * from './protocol.ts'
|
package/src/index.ts
CHANGED
|
@@ -29,3 +29,27 @@ export type { SchemaChoice } from './backup/dump.ts'
|
|
|
29
29
|
export type { BackupHeader, BackupSummary } from './backup/format.ts'
|
|
30
30
|
export type { PgTool } from './backup/pg-tools.ts'
|
|
31
31
|
export type { RestorePlan, RestoreResult } from './backup/restore.ts'
|
|
32
|
+
// Types only: an import's code loads on the first import, and what the browser shares of it is
|
|
33
|
+
// `@otwdesign/db-core/import`.
|
|
34
|
+
export type {
|
|
35
|
+
ImportAcceptedLine,
|
|
36
|
+
ImportBatch,
|
|
37
|
+
ImportBeatLine,
|
|
38
|
+
ImportDatabase,
|
|
39
|
+
ImportDoneLine,
|
|
40
|
+
ImportEnded,
|
|
41
|
+
ImportEndRequest,
|
|
42
|
+
ImportFailedLine,
|
|
43
|
+
ImportFailureState,
|
|
44
|
+
ImportLimits,
|
|
45
|
+
ImportLine,
|
|
46
|
+
ImportLostLine,
|
|
47
|
+
ImportModeLine,
|
|
48
|
+
ImportOptions,
|
|
49
|
+
ImportRunQuery,
|
|
50
|
+
ImportStarted,
|
|
51
|
+
ImportStartRequest,
|
|
52
|
+
ImportStatementLine,
|
|
53
|
+
ImportStatus,
|
|
54
|
+
ImportStoppedLine,
|
|
55
|
+
} from './import/protocol.ts'
|