@fabrika/app 0.9.0-dev.8 → 0.9.0-dev.9
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabrika/app",
|
|
3
|
-
"version": "0.9.0-dev.
|
|
3
|
+
"version": "0.9.0-dev.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -109,14 +109,14 @@
|
|
|
109
109
|
"test": "bun test --path-ignore-patterns='src/react/**' && bun test --isolate src/react"
|
|
110
110
|
},
|
|
111
111
|
"dependencies": {
|
|
112
|
-
"@fabrika/auth": "0.9.0-dev.
|
|
113
|
-
"@fabrika/auth-core": "0.9.0-dev.
|
|
114
|
-
"@fabrika/db": "0.9.0-dev.
|
|
115
|
-
"@fabrika/email": "0.9.0-dev.
|
|
116
|
-
"@fabrika/jwks": "0.9.0-dev.
|
|
117
|
-
"@fabrika/platform": "0.9.0-dev.
|
|
118
|
-
"@fabrika/platform-node": "0.9.0-dev.
|
|
119
|
-
"@fabrika/proxy-core": "0.9.0-dev.
|
|
112
|
+
"@fabrika/auth": "0.9.0-dev.9",
|
|
113
|
+
"@fabrika/auth-core": "0.9.0-dev.9",
|
|
114
|
+
"@fabrika/db": "0.9.0-dev.9",
|
|
115
|
+
"@fabrika/email": "0.9.0-dev.9",
|
|
116
|
+
"@fabrika/jwks": "0.9.0-dev.9",
|
|
117
|
+
"@fabrika/platform": "0.9.0-dev.9",
|
|
118
|
+
"@fabrika/platform-node": "0.9.0-dev.9",
|
|
119
|
+
"@fabrika/proxy-core": "0.9.0-dev.9",
|
|
120
120
|
"altcha-lib": "2.6.0",
|
|
121
121
|
"jose": "^5.9.0"
|
|
122
122
|
},
|
package/skill/SKILL.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: fabrika-app
|
|
3
|
-
version: 0.9.0-dev.
|
|
3
|
+
version: 0.9.0-dev.9
|
|
4
4
|
description: >
|
|
5
5
|
How to write and change a Fabrika application: src/vocabulary.ts, db/migrations/, src/db/tables.ts, the
|
|
6
6
|
slices' endpoints.ts with their handlers, fabrika.contract.ts, jobs, files, webhooks, share links, connectors
|
|
@@ -30,6 +30,7 @@ below. When it does not reach far enough, declare the step off it. A declared st
|
|
|
30
30
|
| `fabrika.contract.ts` | `app.contract((c) => ({ api: c.rpc('/api', { … }), … }))`: mounts the groups |
|
|
31
31
|
| `src/app.ts` | `export default serve(contract, { database: scoped(tables) })` and nothing else |
|
|
32
32
|
| `web/` | the SPA: React over `@fabrika/app/react` and `@fabrika/ui`, never a server module |
|
|
33
|
+
| `tools/` | a developer's tools, such as a command line; the application never loads them |
|
|
33
34
|
|
|
34
35
|
## The paved road
|
|
35
36
|
|
|
@@ -256,7 +257,10 @@ Which promise the types, the checker, boot, the runtime or the approver keeps, a
|
|
|
256
257
|
- An endpoint never sends e-mail; it has no `ctx.mail`. It records an outbox row, declares `kicks: ['<job>']` and calls `ctx.kick('<job>')`, which
|
|
257
258
|
starts that job once its transaction committed. The job declares `mailer` and sends with `ctx.mail`, under a key
|
|
258
259
|
derived from the row, and sends again only where `ctx.mail.mayRetry(error)` allows it (`references/jobs.md`).
|
|
259
|
-
- No `as` (except `as const`), `any`, `@ts-ignore`, `@ts-expect-error`, `process.env` or `Bun.env`.
|
|
260
|
+
- No `as` (except `as const`), `any`, `@ts-ignore`, `@ts-expect-error`, `process.env` or `Bun.env`. A developer's tool
|
|
261
|
+
under `tools/` (a command line that calls a deployed environment with a service key) may read the environment and
|
|
262
|
+
spawn a process; it still has no `as`, `any` or key literal, the application never imports it, and `tools/` holds
|
|
263
|
+
no `package.json`. Its tests run in the platform build, so they test its logic, without a keychain or a network.
|
|
260
264
|
- Never log an error object, a request, headers or the environment.
|
|
261
265
|
- Never edit a generated file (`src/db/schema.generated.ts`, the generated tests, `fabrika/entry.generated.ts`,
|
|
262
266
|
`fabrika/migrations.generated.ts`, `fabrika/connect/`) or a committed migration. Regenerate the file; write a new migration.
|
|
@@ -19,20 +19,20 @@ the SPA is added with it.
|
|
|
19
19
|
"test": "bun test --isolate"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@fabrika/app": "0.9.0-dev.
|
|
23
|
-
"@fabrika/db": "0.9.0-dev.
|
|
24
|
-
"@fabrika/provider-zerops": "0.9.0-dev.
|
|
25
|
-
"@fabrika/proxy-core": "0.9.0-dev.
|
|
26
|
-
"@fabrika/ui": "0.9.0-dev.
|
|
22
|
+
"@fabrika/app": "0.9.0-dev.9",
|
|
23
|
+
"@fabrika/db": "0.9.0-dev.9",
|
|
24
|
+
"@fabrika/provider-zerops": "0.9.0-dev.9",
|
|
25
|
+
"@fabrika/proxy-core": "0.9.0-dev.9",
|
|
26
|
+
"@fabrika/ui": "0.9.0-dev.9",
|
|
27
27
|
"kysely": "0.29.6",
|
|
28
28
|
"react": "^19.0.0",
|
|
29
29
|
"react-dom": "^19.0.0",
|
|
30
30
|
"zod": "^3.25.76"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@fabrika/check": "0.9.0-dev.
|
|
34
|
-
"@fabrika/cli": "0.9.0-dev.
|
|
35
|
-
"@fabrika/platform": "0.9.0-dev.
|
|
33
|
+
"@fabrika/check": "0.9.0-dev.9",
|
|
34
|
+
"@fabrika/cli": "0.9.0-dev.9",
|
|
35
|
+
"@fabrika/platform": "0.9.0-dev.9",
|
|
36
36
|
"@happy-dom/global-registrator": "^20.11.2",
|
|
37
37
|
"@types/bun": "^1.4.3",
|
|
38
38
|
"@types/react": "^19.0.0",
|
|
@@ -8,33 +8,37 @@ architecture, and a refusal's allowed form is the one fix that passes. There is
|
|
|
8
8
|
|
|
9
9
|
The refusals agents meet most, and their fix:
|
|
10
10
|
|
|
11
|
-
| Rule | Means | Fix
|
|
12
|
-
| ------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
13
|
-
| `literal` | the shell (contract, vocabulary, tables, groups, schemas) holds a computed value or call | literal objects, `z.*` calls and imported literal consts; move logic into `.handle(…)`
|
|
14
|
-
| `contract` | the contract does not compile (a reserved role, an unknown action, a wrong field) | the message names the field
|
|
15
|
-
| `package-replacement` | `package.json` names the app under `@fabrika/*`, or a dependency is `workspace:*` | another name; a registry range such as `^0.0.4`
|
|
16
|
-
| `generated-test-hash` | a generated test is missing, edited or stale | `bunx fabrika db test-scaffold`, and commit what it writes
|
|
17
|
-
| `endpoint-fixture` | an endpoint's input cannot be sampled, a fixture or seed names no endpoint, a member, invitation or link field no endpoint that reads, cancels or revokes one, a file names no file field or a path outside the application, or a refusal is `validation` | a literal in `src/__tests__/endpoint-access.fixtures.ts` (`references/tests.md`)
|
|
18
|
-
| `raw-sql` | a driver's `sql` (imported from `bun`, Kysely, `postgres`) | the handles, or `sql: 'read'` and `ctx.sql`
|
|
19
|
-
| `forbidden-import` | a driver, an S3 client, `jose`, `node:http`, `child_process` | the handles, a file column with `ctx.files`, a connector
|
|
20
|
-
| `forbidden-global` | `Bun.sql`, `Bun.serve`, `Bun.spawn`; `globalThis`, `window`, `self`, `global` or `Bun` in a server module | the handles; state lives in a table, not in a global; `crypto.randomUUID()` for an id
|
|
21
|
-
| `env-read` | `process.env`, `Bun.env` | `defineDeps(build)`, which receives the application's variables
|
|
22
|
-
| `client-identity` | an input field `userId`, `ownerId`, `createdBy`, `tenantId`, `workspaceId` | the access form's `from`, `ctx.principal.id`, or `createdBy` in the policy
|
|
23
|
-
| `type-assertion` | `as T` (other than `as const`), `<T>x` | fix the type; `fromRow` parses a row into a schema's type
|
|
24
|
-
| `sensitive-log` | a log of an error object, a request, headers or the environment | log a short fixed message
|
|
25
|
-
| `migration-name` | a migration not named `YYYYMMDDHHMMSS_slug.sql` (UTC) | `bunx fabrika db new <slug>`
|
|
26
|
-
| `
|
|
27
|
-
| `
|
|
28
|
-
| `
|
|
29
|
-
| `
|
|
30
|
-
| `
|
|
31
|
-
| `
|
|
32
|
-
| `
|
|
33
|
-
| `
|
|
34
|
-
| `
|
|
35
|
-
| `
|
|
36
|
-
| `cloudflare-
|
|
37
|
-
| `cloudflare-
|
|
11
|
+
| Rule | Means | Fix |
|
|
12
|
+
| ------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
|
|
13
|
+
| `literal` | the shell (contract, vocabulary, tables, groups, schemas) holds a computed value or call | literal objects, `z.*` calls and imported literal consts; move logic into `.handle(…)` |
|
|
14
|
+
| `contract` | the contract does not compile (a reserved role, an unknown action, a wrong field) | the message names the field |
|
|
15
|
+
| `package-replacement` | `package.json` names the app under `@fabrika/*`, or a dependency is `workspace:*` | another name; a registry range such as `^0.0.4` |
|
|
16
|
+
| `generated-test-hash` | a generated test is missing, edited or stale | `bunx fabrika db test-scaffold`, and commit what it writes |
|
|
17
|
+
| `endpoint-fixture` | an endpoint's input cannot be sampled, a fixture or seed names no endpoint, a member, invitation or link field no endpoint that reads, cancels or revokes one, a file names no file field or a path outside the application, or a refusal is `validation` | a literal in `src/__tests__/endpoint-access.fixtures.ts` (`references/tests.md`) |
|
|
18
|
+
| `raw-sql` | a driver's `sql` (imported from `bun`, Kysely, `postgres`) | the handles, or `sql: 'read'` and `ctx.sql` |
|
|
19
|
+
| `forbidden-import` | a driver, an S3 client, `jose`, `node:http`, `child_process` | the handles, a file column with `ctx.files`, a connector |
|
|
20
|
+
| `forbidden-global` | `Bun.sql`, `Bun.serve`, `Bun.spawn`; `globalThis`, `window`, `self`, `global` or `Bun` in a server module | the handles; state lives in a table, not in a global; `crypto.randomUUID()` for an id |
|
|
21
|
+
| `env-read` | `process.env`, `Bun.env` | `defineDeps(build)`, which receives the application's variables |
|
|
22
|
+
| `client-identity` | an input field `userId`, `ownerId`, `createdBy`, `tenantId`, `workspaceId` | the access form's `from`, `ctx.principal.id`, or `createdBy` in the policy |
|
|
23
|
+
| `type-assertion` | `as T` (other than `as const`), `<T>x` | fix the type; `fromRow` parses a row into a schema's type |
|
|
24
|
+
| `sensitive-log` | a log of an error object, a request, headers or the environment | log a short fixed message |
|
|
25
|
+
| `migration-name` | a migration not named `YYYYMMDDHHMMSS_slug.sql` (UTC) | `bunx fabrika db new <slug>` |
|
|
26
|
+
| `migration-edited` | a migration already on the main branch, edited (even a comment), renamed or deleted | restore the file; put the change in a new migration (`bunx fabrika db new <slug>`) |
|
|
27
|
+
| `test-only-import` | `@fabrika/app/testing`, `@fabrika/app/react/testing`, `@fabrika/db/testing` or `@fabrika/db/testing-sqlite` outside a test file | import them only from `*.test.ts` or `__tests__/` |
|
|
28
|
+
| `spa-server-import` | the SPA loads a server module | the SPA imports `@fabrika/app/client`, the contract, `src/schemas.ts` and `src/shared/` |
|
|
29
|
+
| `shared-import` | a module under `src/shared/` loads a slice, `src/db/`, `@fabrika/*` or a builtin | a shared module imports `zod/v4` and other shared modules only |
|
|
30
|
+
| `tool-import` | a module outside `tools/` loads a developer's tool under `tools/` | move what both need under `src/` (`src/shared/`) and import it from the tool |
|
|
31
|
+
| `skipped-test` | `.skip`, `.only`, `.todo` | run the test |
|
|
32
|
+
| `file-column-migration` | a file column without `REFERENCES fabrika_files (id)` or without an index it leads | both, in a new migration (`references/table-and-migration.md`) |
|
|
33
|
+
| `on-conflict-index` | `insert(…, { onConflict: { columns } })` whose columns are no unique index the migrations create | a `UNIQUE` constraint or a `CREATE UNIQUE INDEX` without `WHERE` on exactly those columns |
|
|
34
|
+
| `cross-app-fetch` | a `fetch` of an absolute URL under no origin a site declares in `egress` | `egress: ['https://…']` on the site that calls it (`references/endpoint.md`) |
|
|
35
|
+
| `url-endpoint` | `ctx.url('<id>')` names no procedure of the contract | the endpoint id `<mount>.<group>.<procedure>` |
|
|
36
|
+
| `cloudflare-scan` | on Cloudflare, a file kind with `scan: true` | the kind without `scan`, and no public procedure that takes its files |
|
|
37
|
+
| `cloudflare-job-segment` | on Cloudflare, a job whose `limits.timeoutMs` exceeds one 14-minute segment | at most 14 minutes: claim a bounded batch per run, and the next run takes the rest |
|
|
38
|
+
| `cloudflare-current-timestamp` | on Cloudflare, `DEFAULT CURRENT_TIMESTAMP` in a migration: SQLite stores `YYYY-MM-DD HH:MM:SS`, which every read of the table refuses | `DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))`, in a new migration |
|
|
39
|
+
| `cloudflare-framework-name` | on Cloudflare, a `fabrika_*` name in a migration, quoted or not | your own tables, and `REFERENCES fabrika_files (id)` for a file column |
|
|
40
|
+
| `migration-refused` | a migration statement Control refuses (a function, an object or a form the platform does not admit) | a new migration that does without it; a key in a SQLite backfill: the UUID expression in `table-and-migration.md` |
|
|
41
|
+
| `backup-schema` | on Cloudflare, a schema the backup cannot copy: a key back into its own table that a `CHECK` reads, or that is `NOT NULL` | a nullable key no `CHECK` names, or no such key; a new migration, before any further one deploys |
|
|
38
42
|
|
|
39
43
|
The hints:
|
|
40
44
|
|
|
@@ -171,6 +171,16 @@ database enforces foreign keys.
|
|
|
171
171
|
generated tests write its first value.
|
|
172
172
|
- No transaction control (`BEGIN`, `COMMIT`, `SAVEPOINT`) and no Postgres syntax (`timestamptz`, `::` casts,
|
|
173
173
|
`GENERATED ALWAYS AS IDENTITY`, `CREATE TYPE`).
|
|
174
|
+
- SQLite has no `gen_random_uuid()`. A backfill that inserts rows gives each one a key with this expression, a UUID v4
|
|
175
|
+
that `z.uuid()` accepts (`hex(randomblob(16))` alone is not one). It is random, not time-ordered like the UUIDv7 an
|
|
176
|
+
insert of the data layer stamps, so nothing may depend on the order of these keys.
|
|
177
|
+
|
|
178
|
+
```sql
|
|
179
|
+
INSERT INTO deal_tags (id, deal_id, tag)
|
|
180
|
+
SELECT lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)), 2) || '-'
|
|
181
|
+
|| substr('89ab', 1 + (random() & 3), 1) || substr(hex(randomblob(2)), 2) || '-' || hex(randomblob(6))), id, label
|
|
182
|
+
FROM deals WHERE label IS NOT NULL;
|
|
183
|
+
```
|
|
174
184
|
|
|
175
185
|
## Database code is a declared deviation
|
|
176
186
|
|
package/src/cloudflare/import.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
import {
|
|
7
7
|
decodeOpsDumpLine,
|
|
8
|
+
describeOpsDumpSchema,
|
|
8
9
|
encodeOpsDumpRow,
|
|
9
10
|
foldOpsTableHash,
|
|
10
11
|
OPS_DATA_REFUSED_STATUS,
|
|
@@ -24,11 +25,7 @@ import {
|
|
|
24
25
|
type OpsDumpManifest,
|
|
25
26
|
opsDumpRowidSql,
|
|
26
27
|
type OpsDumpSchema,
|
|
27
|
-
type OpsDumpSchemaColumn,
|
|
28
28
|
type OpsDumpSchemaEntry,
|
|
29
|
-
type OpsDumpSchemaForeignKey,
|
|
30
|
-
type OpsDumpSchemaIndex,
|
|
31
|
-
type OpsDumpSchemaTable,
|
|
32
29
|
type OpsDumpSource,
|
|
33
30
|
type OpsDumpTable,
|
|
34
31
|
type OpsDumpTablePlan,
|
|
@@ -761,200 +758,9 @@ function sameDdl(left: readonly OpsDumpSchemaEntry[], right: readonly OpsDumpSch
|
|
|
761
758
|
return left.length === right.length && canonical(left) === canonical(right)
|
|
762
759
|
}
|
|
763
760
|
|
|
764
|
-
/**
|
|
765
|
-
* The schema `planOpsDump` reads, from the PRAGMAs workerd admits, of every table a dump holds. An `INITIALLY DEFERRED`
|
|
766
|
-
* clause is read from the table's SQL outside its quotes and comments, and marks every foreign key of its table; the
|
|
767
|
-
* names a `CHECK` reads come from the same SQL (`checkNamesOf`).
|
|
768
|
-
*/
|
|
761
|
+
/** The schema `planOpsDump` reads, of every table a dump holds (`describeOpsDumpSchema`). */
|
|
769
762
|
export function describeDumpSchema(storage: SqliteStorage): OpsDumpSchema {
|
|
770
|
-
|
|
771
|
-
for (const row of storage.sql.exec('PRAGMA table_list').toArray()) {
|
|
772
|
-
const name = text(row['name'])
|
|
773
|
-
const type = text(row['type'])
|
|
774
|
-
if (text(row['schema']) !== 'main' || type === 'view' || !dumped(name)) {
|
|
775
|
-
continue
|
|
776
|
-
}
|
|
777
|
-
const virtual = type === 'virtual'
|
|
778
|
-
const sql = storage.sql.exec(`SELECT sql FROM sqlite_schema WHERE type = 'table' AND name = ?`, name).toArray()[0]?.['sql']
|
|
779
|
-
const clauses = typeof sql === 'string' ? unquotedSql(sql) : null
|
|
780
|
-
tables.push({
|
|
781
|
-
name,
|
|
782
|
-
virtual,
|
|
783
|
-
withoutRowid: flag(row['wr']),
|
|
784
|
-
checkNames: typeof sql === 'string' ? checkNamesOf(sql) : null,
|
|
785
|
-
// A virtual table is refused whatever it holds, and its module may not be loaded to describe it.
|
|
786
|
-
columns: virtual ? [] : describeColumns(storage, name),
|
|
787
|
-
indexes: virtual ? [] : describeIndexes(storage, name),
|
|
788
|
-
foreignKeys: virtual ? [] : describeForeignKeys(storage, name, clauses === null || /\binitially\s+deferred\b/i.test(clauses)),
|
|
789
|
-
})
|
|
790
|
-
}
|
|
791
|
-
return { tables }
|
|
792
|
-
}
|
|
793
|
-
|
|
794
|
-
function describeColumns(storage: SqliteStorage, table: string): OpsDumpSchemaColumn[] {
|
|
795
|
-
return storage.sql.exec(`PRAGMA table_xinfo(${literal(table)})`).toArray().map((row) => {
|
|
796
|
-
const hidden = integer(row['hidden'])
|
|
797
|
-
return {
|
|
798
|
-
name: text(row['name']),
|
|
799
|
-
type: text(row['type']),
|
|
800
|
-
notNull: flag(row['notnull']),
|
|
801
|
-
primaryKey: integer(row['pk']),
|
|
802
|
-
generated: hidden === 2 || hidden === 3,
|
|
803
|
-
}
|
|
804
|
-
})
|
|
805
|
-
}
|
|
806
|
-
|
|
807
|
-
function describeIndexes(storage: SqliteStorage, table: string): OpsDumpSchemaIndex[] {
|
|
808
|
-
return storage.sql.exec(`PRAGMA index_list(${literal(table)})`).toArray().map((row) => {
|
|
809
|
-
const origin = row['origin']
|
|
810
|
-
if (origin !== 'c' && origin !== 'u' && origin !== 'pk') {
|
|
811
|
-
throw new Error('PRAGMA index_list answered an origin SQLite does not write')
|
|
812
|
-
}
|
|
813
|
-
const columns = storage.sql.exec(`PRAGMA index_xinfo(${literal(text(row['name']))})`).toArray()
|
|
814
|
-
.filter((column) => flag(column['key']))
|
|
815
|
-
.sort((left, right) => integer(left['seqno']) - integer(right['seqno']))
|
|
816
|
-
.map((column) => typeof column['name'] === 'string' ? column['name'] : null)
|
|
817
|
-
return { unique: flag(row['unique']), origin, partial: flag(row['partial']), columns }
|
|
818
|
-
})
|
|
819
|
-
}
|
|
820
|
-
|
|
821
|
-
function describeForeignKeys(storage: SqliteStorage, table: string, initiallyDeferred: boolean): OpsDumpSchemaForeignKey[] {
|
|
822
|
-
const keys = new Map<number, { parent: string; rows: { seq: number; from: string; to: string | null }[] }>()
|
|
823
|
-
for (const row of storage.sql.exec(`PRAGMA foreign_key_list(${literal(table)})`).toArray()) {
|
|
824
|
-
const id = integer(row['id'])
|
|
825
|
-
const to = row['to']
|
|
826
|
-
const entry = { seq: integer(row['seq']), from: text(row['from']), to: typeof to === 'string' ? to : null }
|
|
827
|
-
const key = keys.get(id)
|
|
828
|
-
if (key === undefined) {
|
|
829
|
-
keys.set(id, { parent: text(row['table']), rows: [entry] })
|
|
830
|
-
} else {
|
|
831
|
-
key.rows.push(entry)
|
|
832
|
-
}
|
|
833
|
-
}
|
|
834
|
-
return [...keys.entries()].sort(([left], [right]) => left - right).map(([, key]) => {
|
|
835
|
-
const rows = key.rows.sort((left, right) => left.seq - right.seq)
|
|
836
|
-
const references = rows.flatMap((row) => row.to === null ? [] : [row.to])
|
|
837
|
-
if (references.length !== 0 && references.length !== rows.length) {
|
|
838
|
-
throw new Error('PRAGMA foreign_key_list named the parent columns of a key only in part')
|
|
839
|
-
}
|
|
840
|
-
return { parent: key.parent, columns: rows.map((row) => row.from), references: references.length === 0 ? null : references, initiallyDeferred }
|
|
841
|
-
})
|
|
842
|
-
}
|
|
843
|
-
|
|
844
|
-
type SqlToken =
|
|
845
|
-
| { readonly kind: 'name'; readonly text: string; readonly quoted: boolean }
|
|
846
|
-
| { readonly kind: 'open' | 'close' | 'other' }
|
|
847
|
-
|
|
848
|
-
const WORD_CHAR = /[A-Za-z0-9_$\u0080-\uffff]/
|
|
849
|
-
|
|
850
|
-
/**
|
|
851
|
-
* The names the `CHECK` clauses of a table's `sql` read: every word and quoted identifier between a `CHECK` keyword's
|
|
852
|
-
* parentheses, ASCII-lowercase, keywords and numbers included, so it lists at least every column a `CHECK` reads. A
|
|
853
|
-
* string literal and a comment name nothing; a double-quoted string, which SQLite may read as either, counts as a name.
|
|
854
|
-
* `null` when the text does not scan (an unclosed quote, comment or parenthesis), which counts as naming every column.
|
|
855
|
-
*/
|
|
856
|
-
export function checkNamesOf(sql: string): string[] | null {
|
|
857
|
-
const tokens = sqlTokens(sql)
|
|
858
|
-
if (tokens === null) return null
|
|
859
|
-
const names: string[] = []
|
|
860
|
-
for (let index = 0; index < tokens.length; index++) {
|
|
861
|
-
const token = tokens[index]
|
|
862
|
-
if (token?.kind !== 'name' || token.quoted || asciiLower(token.text) !== 'check' || tokens[index + 1]?.kind !== 'open') continue
|
|
863
|
-
let depth = 0
|
|
864
|
-
let end = index + 1
|
|
865
|
-
for (; end < tokens.length; end++) {
|
|
866
|
-
const inner = tokens[end]
|
|
867
|
-
if (inner?.kind === 'open') depth++
|
|
868
|
-
else if (inner?.kind === 'close' && --depth === 0) break
|
|
869
|
-
else if (inner?.kind === 'name') names.push(asciiLower(inner.text))
|
|
870
|
-
}
|
|
871
|
-
if (end === tokens.length) return null
|
|
872
|
-
index = end
|
|
873
|
-
}
|
|
874
|
-
return names
|
|
875
|
-
}
|
|
876
|
-
|
|
877
|
-
/** `sql` as words, quoted identifiers and parentheses; string literals, comments and whitespace dropped. */
|
|
878
|
-
function sqlTokens(sql: string): SqlToken[] | null {
|
|
879
|
-
const tokens: SqlToken[] = []
|
|
880
|
-
let index = 0
|
|
881
|
-
while (index < sql.length) {
|
|
882
|
-
const char = sql.charAt(index)
|
|
883
|
-
if (sql.startsWith('--', index)) {
|
|
884
|
-
const end = sql.indexOf('\n', index)
|
|
885
|
-
index = end < 0 ? sql.length : end + 1
|
|
886
|
-
} else if (sql.startsWith('/*', index)) {
|
|
887
|
-
const end = sql.indexOf('*/', index + 2)
|
|
888
|
-
if (end < 0) return null
|
|
889
|
-
index = end + 2
|
|
890
|
-
} else if (char === "'" || char === '"' || char === '`' || char === '[') {
|
|
891
|
-
const close = char === '[' ? ']' : char
|
|
892
|
-
let end = index + 1
|
|
893
|
-
let text = ''
|
|
894
|
-
for (;;) {
|
|
895
|
-
const next = sql.indexOf(close, end)
|
|
896
|
-
if (next < 0) return null
|
|
897
|
-
text += sql.slice(end, next)
|
|
898
|
-
if (close !== ']' && sql.charAt(next + 1) === close) {
|
|
899
|
-
text += close
|
|
900
|
-
end = next + 2
|
|
901
|
-
continue
|
|
902
|
-
}
|
|
903
|
-
end = next + 1
|
|
904
|
-
break
|
|
905
|
-
}
|
|
906
|
-
if (char !== "'") tokens.push({ kind: 'name', text, quoted: true })
|
|
907
|
-
index = end
|
|
908
|
-
} else if (WORD_CHAR.test(char)) {
|
|
909
|
-
let end = index + 1
|
|
910
|
-
while (end < sql.length && WORD_CHAR.test(sql.charAt(end))) end++
|
|
911
|
-
tokens.push({ kind: 'name', text: sql.slice(index, end), quoted: false })
|
|
912
|
-
index = end
|
|
913
|
-
} else {
|
|
914
|
-
if (char === '(') tokens.push({ kind: 'open' })
|
|
915
|
-
else if (char === ')') tokens.push({ kind: 'close' })
|
|
916
|
-
else if (!/\s/.test(char)) tokens.push({ kind: 'other' })
|
|
917
|
-
index++
|
|
918
|
-
}
|
|
919
|
-
}
|
|
920
|
-
return tokens
|
|
921
|
-
}
|
|
922
|
-
|
|
923
|
-
/** SQLite compares identifiers case-insensitively in ASCII only. */
|
|
924
|
-
function asciiLower(value: string): string {
|
|
925
|
-
return value.replace(/[A-Z]/g, (letter) => letter.toLowerCase())
|
|
926
|
-
}
|
|
927
|
-
|
|
928
|
-
/** `sql` with its string literals, quoted identifiers and comments blanked, so a keyword search finds only clauses. */
|
|
929
|
-
function unquotedSql(sql: string): string {
|
|
930
|
-
let out = ''
|
|
931
|
-
let index = 0
|
|
932
|
-
while (index < sql.length) {
|
|
933
|
-
const char = sql.charAt(index)
|
|
934
|
-
const close = char === "'" ? "'" : char === '"' ? '"' : char === '`' ? '`' : char === '[' ? ']' : null
|
|
935
|
-
if (close !== null) {
|
|
936
|
-
let end = index + 1
|
|
937
|
-
for (;;) {
|
|
938
|
-
end = sql.indexOf(close, end)
|
|
939
|
-
if (end < 0 || close === ']' || sql.charAt(end + 1) !== close) break
|
|
940
|
-
end += 2
|
|
941
|
-
}
|
|
942
|
-
index = end < 0 ? sql.length : end + 1
|
|
943
|
-
out += ' '
|
|
944
|
-
} else if (sql.startsWith('--', index)) {
|
|
945
|
-
const end = sql.indexOf('\n', index)
|
|
946
|
-
index = end < 0 ? sql.length : end + 1
|
|
947
|
-
out += ' '
|
|
948
|
-
} else if (sql.startsWith('/*', index)) {
|
|
949
|
-
const end = sql.indexOf('*/', index + 2)
|
|
950
|
-
index = end < 0 ? sql.length : end + 2
|
|
951
|
-
out += ' '
|
|
952
|
-
} else {
|
|
953
|
-
out += char
|
|
954
|
-
index++
|
|
955
|
-
}
|
|
956
|
-
}
|
|
957
|
-
return out
|
|
763
|
+
return describeOpsDumpSchema((query, ...bindings) => storage.sql.exec(query, ...bindings).toArray())
|
|
958
764
|
}
|
|
959
765
|
|
|
960
766
|
/** The import marker, parsed strictly; `null` when the object holds none. A marker that does not parse is a corrupt object. */
|
|
@@ -1056,10 +862,6 @@ function quote(identifier: string): string {
|
|
|
1056
862
|
return `"${identifier.replaceAll('"', '""')}"`
|
|
1057
863
|
}
|
|
1058
864
|
|
|
1059
|
-
function literal(value: string): string {
|
|
1060
|
-
return `'${value.replaceAll("'", "''")}'`
|
|
1061
|
-
}
|
|
1062
|
-
|
|
1063
865
|
/** Code-unit order, which no locale changes; the migration runner orders files the same way. */
|
|
1064
866
|
function compareNames(left: string, right: string): number {
|
|
1065
867
|
return left < right ? -1 : left > right ? 1 : 0
|
|
@@ -1084,14 +886,3 @@ function text(value: SqliteValue | undefined): string {
|
|
|
1084
886
|
}
|
|
1085
887
|
return value
|
|
1086
888
|
}
|
|
1087
|
-
|
|
1088
|
-
function integer(value: SqliteValue | undefined): number {
|
|
1089
|
-
if (typeof value !== 'number' || !Number.isSafeInteger(value)) {
|
|
1090
|
-
throw new Error('SQLite answered a schema number that is not an integer')
|
|
1091
|
-
}
|
|
1092
|
-
return value
|
|
1093
|
-
}
|
|
1094
|
-
|
|
1095
|
-
function flag(value: SqliteValue | undefined): boolean {
|
|
1096
|
-
return integer(value) !== 0
|
|
1097
|
-
}
|
|
@@ -43,16 +43,5 @@ export declare function importInProgress(storage: {
|
|
|
43
43
|
* generation and mode it was admitted under still hold when it commits.
|
|
44
44
|
*/
|
|
45
45
|
export declare function importStep(target: ImportTarget, barrier: MaintenanceBarrier, step: OpsImportRequest): Promise<Response>;
|
|
46
|
-
/**
|
|
47
|
-
* The schema `planOpsDump` reads, from the PRAGMAs workerd admits, of every table a dump holds. An `INITIALLY DEFERRED`
|
|
48
|
-
* clause is read from the table's SQL outside its quotes and comments, and marks every foreign key of its table; the
|
|
49
|
-
* names a `CHECK` reads come from the same SQL (`checkNamesOf`).
|
|
50
|
-
*/
|
|
46
|
+
/** The schema `planOpsDump` reads, of every table a dump holds (`describeOpsDumpSchema`). */
|
|
51
47
|
export declare function describeDumpSchema(storage: SqliteStorage): OpsDumpSchema;
|
|
52
|
-
/**
|
|
53
|
-
* The names the `CHECK` clauses of a table's `sql` read: every word and quoted identifier between a `CHECK` keyword's
|
|
54
|
-
* parentheses, ASCII-lowercase, keywords and numbers included, so it lists at least every column a `CHECK` reads. A
|
|
55
|
-
* string literal and a comment name nothing; a double-quoted string, which SQLite may read as either, counts as a name.
|
|
56
|
-
* `null` when the text does not scan (an unclosed quote, comment or parenthesis), which counts as naming every column.
|
|
57
|
-
*/
|
|
58
|
-
export declare function checkNamesOf(sql: string): string[] | null;
|