orez 0.2.27 → 0.2.29

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 (150) hide show
  1. package/package.json +3 -4
  2. package/src/admin/admin-data.test.ts +0 -348
  3. package/src/admin/http-proxy.ts +0 -252
  4. package/src/admin/log-store.ts +0 -192
  5. package/src/admin/server.ts +0 -471
  6. package/src/admin/ui.ts +0 -1322
  7. package/src/bench/proxy-throughput.bench.ts +0 -343
  8. package/src/bench/serial-mutations.bench.ts +0 -270
  9. package/src/browser.ts +0 -203
  10. package/src/cf-do/.wrangler/cache/cf.json +0 -1
  11. package/src/cf-do/.wrangler/state/v3/cache/miniflare-CacheObject/metadata.sqlite +0 -0
  12. package/src/cf-do/.wrangler/state/v3/cache/miniflare-CacheObject/metadata.sqlite-shm +0 -0
  13. package/src/cf-do/.wrangler/state/v3/cache/miniflare-CacheObject/metadata.sqlite-wal +0 -0
  14. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/0ffaabee41a60e04dd0eb7db3073f0a40139e6a97ccd26823967acb652b89a7b.sqlite +0 -0
  15. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/metadata.sqlite +0 -0
  16. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/metadata.sqlite-shm +0 -0
  17. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/metadata.sqlite-wal +0 -0
  18. package/src/cf-do/.wrangler/tmp/bundle-0z4CpE/middleware-insertion-facade.js +0 -11
  19. package/src/cf-do/.wrangler/tmp/bundle-0z4CpE/middleware-loader.entry.ts +0 -134
  20. package/src/cf-do/.wrangler/tmp/bundle-vYmw0E/middleware-insertion-facade.js +0 -11
  21. package/src/cf-do/.wrangler/tmp/bundle-vYmw0E/middleware-loader.entry.ts +0 -134
  22. package/src/cf-do/.wrangler/tmp/dev-cbILNo/worker.js +0 -1059
  23. package/src/cf-do/.wrangler/tmp/dev-cbILNo/worker.js.map +0 -8
  24. package/src/cf-do/.wrangler/tmp/dev-qbho19/worker.js +0 -1059
  25. package/src/cf-do/.wrangler/tmp/dev-qbho19/worker.js.map +0 -8
  26. package/src/cf-do/ARCHITECTURE.md +0 -93
  27. package/src/cf-do/CHAT_E2E.md +0 -213
  28. package/src/cf-do/watermark.test.ts +0 -103
  29. package/src/cf-do/watermark.ts +0 -118
  30. package/src/cf-do/worker.ts +0 -1041
  31. package/src/cf-do/wrangler.toml +0 -11
  32. package/src/cf-pglite/README.md +0 -19
  33. package/src/change-tracking.ts +0 -25
  34. package/src/child-process.test.ts +0 -147
  35. package/src/child-process.ts +0 -90
  36. package/src/cli-entry.ts +0 -72
  37. package/src/cli.test.ts +0 -40
  38. package/src/cli.ts +0 -1214
  39. package/src/config.ts +0 -150
  40. package/src/do-sql-tracking.test.ts +0 -19
  41. package/src/do-sql-tracking.ts +0 -19
  42. package/src/index.ts +0 -1215
  43. package/src/integration/integration.test.ts +0 -517
  44. package/src/integration/native-binary.guard.test.ts +0 -13
  45. package/src/integration/native-startup.test.ts +0 -44
  46. package/src/integration/replication-latency.test.ts +0 -428
  47. package/src/integration/restore-live-stress.test.ts +0 -433
  48. package/src/integration/restore-reset.test.ts +0 -400
  49. package/src/integration/restore.test.ts +0 -274
  50. package/src/integration/test-permissions.ts +0 -147
  51. package/src/load-config.ts +0 -46
  52. package/src/log.ts +0 -96
  53. package/src/mutex.ts +0 -47
  54. package/src/pg-proxy-browser.singledb.test.ts +0 -233
  55. package/src/pg-proxy-browser.ts +0 -2022
  56. package/src/pg-proxy-do-backend.test.ts +0 -3890
  57. package/src/pg-proxy-do-backend.ts +0 -7191
  58. package/src/pg-proxy.ts +0 -1087
  59. package/src/pg-sqlite-compiler/README.md +0 -53
  60. package/src/pg-sqlite-compiler/catalog/seed.ts +0 -524
  61. package/src/pg-sqlite-compiler/fixtures/pgsqlite/arithmetic.json +0 -307
  62. package/src/pg-sqlite-compiler/fixtures/pgsqlite/array.json +0 -377
  63. package/src/pg-sqlite-compiler/fixtures/pgsqlite/cast.json +0 -12
  64. package/src/pg-sqlite-compiler/fixtures/pgsqlite/catalog.json +0 -447
  65. package/src/pg-sqlite-compiler/fixtures/pgsqlite/create-table.json +0 -32
  66. package/src/pg-sqlite-compiler/fixtures/pgsqlite/datetime.json +0 -397
  67. package/src/pg-sqlite-compiler/fixtures/pgsqlite/enum.json +0 -337
  68. package/src/pg-sqlite-compiler/fixtures/pgsqlite/insert.json +0 -337
  69. package/src/pg-sqlite-compiler/fixtures/pgsqlite/json.json +0 -537
  70. package/src/pg-sqlite-compiler/fixtures/pgsqlite/misc.json +0 -1837
  71. package/src/pg-sqlite-compiler/index.ts +0 -73
  72. package/src/pg-sqlite-compiler/integration.test.ts +0 -136
  73. package/src/pg-sqlite-compiler/passes/ast-utils.ts +0 -113
  74. package/src/pg-sqlite-compiler/passes/catalog.ts +0 -65
  75. package/src/pg-sqlite-compiler/passes/datetime.ts +0 -74
  76. package/src/pg-sqlite-compiler/passes/index.ts +0 -49
  77. package/src/pg-sqlite-compiler/passes/types.ts +0 -156
  78. package/src/pg-sqlite-compiler/smoke.test.ts +0 -69
  79. package/src/pg-sqlite-compiler/test/catalog.test.ts +0 -171
  80. package/src/pg-sqlite-compiler/test/corpus.test.ts +0 -161
  81. package/src/pg-sqlite-compiler/test/datetime.oracle.test.ts +0 -102
  82. package/src/pg-sqlite-compiler/test/oracle.ts +0 -237
  83. package/src/pg-sqlite-compiler/test/types.test.ts +0 -109
  84. package/src/pg-sqlite-compiler/types.ts +0 -63
  85. package/src/pglite-ipc.test.ts +0 -116
  86. package/src/pglite-ipc.ts +0 -266
  87. package/src/pglite-manager.ts +0 -557
  88. package/src/pglite-web-proxy.test.ts +0 -57
  89. package/src/pglite-web-proxy.ts +0 -221
  90. package/src/pglite-web-worker.ts +0 -152
  91. package/src/pglite-worker-thread.ts +0 -253
  92. package/src/port.ts +0 -25
  93. package/src/process-title.ts +0 -9
  94. package/src/recovery.ts +0 -155
  95. package/src/replication/change-tracker.test.ts +0 -357
  96. package/src/replication/change-tracker.ts +0 -279
  97. package/src/replication/handler.test.ts +0 -511
  98. package/src/replication/handler.ts +0 -1190
  99. package/src/replication/pgoutput-encoder.test.ts +0 -697
  100. package/src/replication/pgoutput-encoder.ts +0 -373
  101. package/src/replication/tcp-replication.test.ts +0 -876
  102. package/src/replication/zero-compat.test.ts +0 -1150
  103. package/src/restore-stress.test.ts +0 -188
  104. package/src/s3-local.ts +0 -203
  105. package/src/shim/hooks.mjs +0 -120
  106. package/src/shim/register.mjs +0 -4
  107. package/src/sqlite-mode/apply-mode.ts +0 -224
  108. package/src/sqlite-mode/index.ts +0 -15
  109. package/src/sqlite-mode/native-binary.ts +0 -89
  110. package/src/sqlite-mode/package-resolve.ts +0 -17
  111. package/src/sqlite-mode/resolve-mode.ts +0 -80
  112. package/src/sqlite-mode/shim-template.ts +0 -159
  113. package/src/sqlite-mode/sqlite-mode.test.ts +0 -427
  114. package/src/sqlite-mode/types.ts +0 -30
  115. package/src/vite-plugin.ts +0 -67
  116. package/src/wasm-sqlite.test.ts +0 -537
  117. package/src/worker/browser-admin.ts +0 -52
  118. package/src/worker/browser-build-config.test.ts +0 -71
  119. package/src/worker/browser-build-config.ts +0 -109
  120. package/src/worker/browser-embed-admin.test.ts +0 -75
  121. package/src/worker/browser-embed.ts +0 -345
  122. package/src/worker/cf-patches.ts +0 -384
  123. package/src/worker/embed-integration.test.ts +0 -321
  124. package/src/worker/index.ts +0 -138
  125. package/src/worker/shims/fastify.test.ts +0 -255
  126. package/src/worker/shims/fastify.ts +0 -306
  127. package/src/worker/shims/http-service.test.ts +0 -355
  128. package/src/worker/shims/http-service.ts +0 -293
  129. package/src/worker/shims/node-stub.ts +0 -290
  130. package/src/worker/shims/oxfmt.ts +0 -3
  131. package/src/worker/shims/postgres-browser.ts +0 -59
  132. package/src/worker/shims/postgres-socket.test.ts +0 -576
  133. package/src/worker/shims/postgres-socket.ts +0 -310
  134. package/src/worker/shims/postgres.test.ts +0 -364
  135. package/src/worker/shims/postgres.ts +0 -1454
  136. package/src/worker/shims/sqlite-browser.test.ts +0 -233
  137. package/src/worker/shims/sqlite-browser.ts +0 -175
  138. package/src/worker/shims/sqlite.test.ts +0 -786
  139. package/src/worker/shims/sqlite.ts +0 -978
  140. package/src/worker/shims/stream-browser.ts +0 -15
  141. package/src/worker/shims/ws-browser.test.ts +0 -205
  142. package/src/worker/shims/ws-browser.ts +0 -248
  143. package/src/worker/shims/ws.test.ts +0 -288
  144. package/src/worker/shims/ws.ts +0 -467
  145. package/src/worker/shims/zero-process-env.ts +0 -11
  146. package/src/worker/types.ts +0 -75
  147. package/src/worker/worker-integration.test.ts +0 -223
  148. package/src/worker/worker.test.ts +0 -136
  149. package/src/worker/zero-cache-embed-cf.ts +0 -463
  150. package/src/worker/zero-cache-embed.ts +0 -277
@@ -1,53 +0,0 @@
1
- # pg-sqlite-compiler
2
-
3
- PostgreSQL SQL → SQLite SQL compiler. Single pass over the libpg_query AST,
4
- emitting via pgsql-deparser with SQLite-specific overrides.
5
-
6
- ## Architecture
7
-
8
- ```
9
- PG SQL ──► parseSync() (libpg-query WASM, real PG parser)
10
-
11
- PG AST (RawStmt[])
12
-
13
- passes[] (one visitor per concern)
14
-
15
- PG AST (mutated)
16
-
17
- emit() (pgsql-deparser + SQLite overrides)
18
-
19
- SQLite SQL
20
- ```
21
-
22
- ## Passes
23
-
24
- Each pass is a focused visitor for one PG → SQLite concern:
25
-
26
- - `passes/datetime.ts` — NOW(), CURRENT_TIMESTAMP, EXTRACT, DATE_TRUNC, INTERVAL
27
- - `passes/array.ts` — ARRAY[…], @>, <@, unnest, array literals
28
- - `passes/cast.ts` — `::type`, CAST chains, PG → SQLite type names
29
- - `passes/json.ts` — `->`, `->>`, jsonb_set/get/path
30
- - `passes/create_table.ts` — type mappings, BIGSERIAL → INTEGER, defaults
31
- - `passes/insert.ts` — ON CONFLICT semantics, RETURNING (mostly native)
32
- - `passes/catalog.ts` — pg_class / pg_attribute / information_schema rewrites
33
-
34
- ## Testing
35
-
36
- Two layers:
37
-
38
- 1. **Snapshot tests** (`test/*.test.ts`) — for each pass, fixed (input, expected
39
- output) pairs. Fast, deterministic, tracked in git.
40
-
41
- 2. **Oracle tests** (`test/oracle.test.ts`) — spawn pgsqlite, send query to it
42
- via PG wire, send same query through our compiler + bun:sqlite, compare
43
- result sets. Validates semantic equivalence, not text identity. Only runs
44
- when pgsqlite binary is available (`scripts/pgsqlite/ensure.ts`).
45
-
46
- The pgsqlite binary itself is NOT shipped. It's a dev-time / CI oracle only.
47
-
48
- ## Why not just use pgsqlite?
49
-
50
- pgsqlite is a Rust server (tokio + rusqlite); doesn't run in Cloudflare workerd.
51
- We need PG → SQLite translation as a pure-TS library that compiles into a CF
52
- Durable Object alongside zero-cache. So we reimplement the translation in TS,
53
- using pgsqlite as our quality oracle.
@@ -1,524 +0,0 @@
1
- /**
2
- * Catalog seed — populates `_orez_catalog__*` tables in a DO SQLite database.
3
- *
4
- * The catalog pass rewrites `pg_catalog.pg_class`, `information_schema.columns`,
5
- * etc. references to point at flat `_orez_catalog__*` table names. Here we
6
- * create those tables and fill them with rows synthesized from SQLite's
7
- * introspection (`sqlite_master`, `PRAGMA table_info(...)`).
8
- *
9
- * Tables seeded (every entry in the catalog pass's recognized-name set —
10
- * keeping them in sync ensures "no such table" errors don't surface a
11
- * confusing `_orez_catalog__pg_index`-style internal name):
12
- *
13
- * pg_namespace public + pg_catalog + information_schema
14
- * pg_type ~20 built-in OIDs
15
- * pg_class one row per user table
16
- * pg_attribute one row per column
17
- * pg_attrdef per-column defaults
18
- * pg_constraint empty stub (queryable, no rows)
19
- * pg_index empty stub (sqlite_master indexes could be
20
- * synthesized here in a follow-up)
21
- * pg_proc empty stub
22
- * pg_trigger empty stub
23
- * pg_inherits empty stub
24
- * pg_depend, pg_description,
25
- * pg_enum, pg_roles, pg_user,
26
- * pg_settings empty stubs (queryable)
27
- * pg_publication one row per supplied publication name
28
- * pg_publication_tables cross-product when publications set
29
- * pg_publication_rel empty stub
30
- * pg_replication_slots empty (orez has its own change tracker)
31
- * pg_stat_replication empty stub
32
- * pg_subscription empty stub
33
- * pg_extension empty stub
34
- * pg_sequence empty stub
35
- * pg_views empty stub
36
- * pg_tables one row per user table
37
- * pg_collation, pg_am,
38
- * pg_operator, pg_cast,
39
- * pg_language, pg_statistic,
40
- * pg_locks empty stubs
41
- * information_schema_columns one row per column
42
- * information_schema_tables one row per user table
43
- *
44
- * Idempotent and transactional — wrapped in BEGIN/COMMIT so readers never
45
- * observe a half-built catalog mid-rebuild. Safe to call on every DO init.
46
- *
47
- * Used in tests via `buildCatalogTables(db, opts?)`. In the DO runtime, the
48
- * same function runs against `ctx.storage.sql` (better-sqlite3-compatible).
49
- */
50
-
51
- export interface SqliteLike {
52
- exec(sql: string): unknown
53
- prepare(sql: string): {
54
- all(...args: unknown[]): unknown[]
55
- run(...args: unknown[]): unknown
56
- }
57
- }
58
-
59
- export interface SeedOptions {
60
- /** publication names to expose in pg_publication. */
61
- publications?: readonly string[]
62
- }
63
-
64
- const NAMESPACE_OID_PUBLIC = 2200
65
- const NAMESPACE_OID_PG_CATALOG = 11
66
- const NAMESPACE_OID_INFO_SCHEMA = 99
67
- const TABLE_OID_BASE = 50_000
68
-
69
- function hashName(name: string, base: number): number {
70
- let h = 0
71
- for (let i = 0; i < name.length; i++) {
72
- h = ((h * 33) ^ name.charCodeAt(i)) >>> 0
73
- }
74
- return base + (h % 1_000_000)
75
- }
76
-
77
- /** PG type OID lookup for the small set of types we expose. */
78
- const PG_TYPE_OIDS: Record<string, number> = {
79
- bool: 16,
80
- bytea: 17,
81
- int8: 20,
82
- int2: 21,
83
- int4: 23,
84
- text: 25,
85
- oid: 26,
86
- name: 19,
87
- float4: 700,
88
- float8: 701,
89
- varchar: 1043,
90
- date: 1082,
91
- time: 1083,
92
- timestamp: 1114,
93
- timestamptz: 1184,
94
- interval: 1186,
95
- numeric: 1700,
96
- json: 114,
97
- jsonb: 3802,
98
- uuid: 2950,
99
- regclass: 2205,
100
- regtype: 2206,
101
- regproc: 24,
102
- }
103
-
104
- /** Map SQLite affinity → PG type name we'll report (best-effort). */
105
- function sqliteToPgType(decltype: string | null): string {
106
- if (!decltype) return 'text'
107
- const lower = decltype.toLowerCase()
108
- if (lower.includes('int')) return 'int4'
109
- if (lower.includes('char') || lower.includes('clob') || lower === 'text') return 'text'
110
- if (lower.includes('real') || lower.includes('floa') || lower.includes('doub'))
111
- return 'float8'
112
- if (lower.includes('blob')) return 'bytea'
113
- if (lower === 'numeric' || lower.includes('dec')) return 'numeric'
114
- return 'text'
115
- }
116
-
117
- interface PragmaCol {
118
- cid: number
119
- name: string
120
- type: string | null
121
- notnull: number
122
- dflt_value: string | null
123
- pk: number
124
- }
125
-
126
- /**
127
- * Seed catalog tables. Wrapped in a transaction so readers never observe a
128
- * partial rebuild. Tables created as `_orez_catalog__pg_class` etc. The
129
- * catalog pass rewrites `pg_catalog.pg_class` → `_orez_catalog__pg_class`.
130
- */
131
- export function buildCatalogTables(db: SqliteLike, opts: SeedOptions = {}): void {
132
- // De-dupe publication names so we don't violate the UNIQUE constraint
133
- const publications = [...new Set(opts.publications ?? [])]
134
-
135
- db.exec('BEGIN IMMEDIATE;')
136
- try {
137
- seedPgNamespace(db)
138
- seedPgType(db)
139
- const tables = readUserTables(db)
140
- seedPgClass(db, tables)
141
- seedPgAttributeAndDefaults(db, tables)
142
- seedPgPublication(db, publications, tables)
143
- seedSimpleStubs(db)
144
- seedInformationSchema(db, tables)
145
- db.exec('COMMIT;')
146
- } catch (err) {
147
- try {
148
- db.exec('ROLLBACK;')
149
- } catch {}
150
- throw err
151
- }
152
- }
153
-
154
- function readUserTables(db: SqliteLike): { name: string; type: string }[] {
155
- return db
156
- .prepare(
157
- "SELECT name, type FROM sqlite_master WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' AND name NOT LIKE '_orez_catalog__%'"
158
- )
159
- .all() as { name: string; type: string }[]
160
- }
161
-
162
- function quoteIdent(name: string): string {
163
- return `"${name.replace(/"/g, '""')}"`
164
- }
165
-
166
- function readColumns(db: SqliteLike, table: string): PragmaCol[] {
167
- return db.prepare(`PRAGMA table_info(${quoteIdent(table)})`).all() as PragmaCol[]
168
- }
169
-
170
- function seedPgNamespace(db: SqliteLike): void {
171
- db.exec(`
172
- DROP TABLE IF EXISTS _orez_catalog__pg_namespace;
173
- CREATE TABLE _orez_catalog__pg_namespace (
174
- oid INTEGER PRIMARY KEY,
175
- nspname TEXT NOT NULL,
176
- nspowner INTEGER NOT NULL DEFAULT 10,
177
- nspacl TEXT
178
- );
179
- `)
180
- const insert = db.prepare(
181
- 'INSERT INTO _orez_catalog__pg_namespace (oid, nspname) VALUES (?, ?)'
182
- )
183
- insert.run(NAMESPACE_OID_PUBLIC, 'public')
184
- insert.run(NAMESPACE_OID_PG_CATALOG, 'pg_catalog')
185
- insert.run(NAMESPACE_OID_INFO_SCHEMA, 'information_schema')
186
- }
187
-
188
- function seedPgType(db: SqliteLike): void {
189
- db.exec(`
190
- DROP TABLE IF EXISTS _orez_catalog__pg_type;
191
- CREATE TABLE _orez_catalog__pg_type (
192
- oid INTEGER PRIMARY KEY,
193
- typname TEXT NOT NULL,
194
- typnamespace INTEGER NOT NULL DEFAULT ${NAMESPACE_OID_PG_CATALOG},
195
- typlen INTEGER NOT NULL DEFAULT -1,
196
- typtype TEXT NOT NULL DEFAULT 'b',
197
- typbasetype INTEGER NOT NULL DEFAULT 0,
198
- typelem INTEGER NOT NULL DEFAULT 0,
199
- typcategory TEXT,
200
- typnotnull INTEGER NOT NULL DEFAULT 0
201
- );
202
- `)
203
- const insert = db.prepare(
204
- 'INSERT INTO _orez_catalog__pg_type (oid, typname) VALUES (?, ?)'
205
- )
206
- for (const [name, oid] of Object.entries(PG_TYPE_OIDS)) insert.run(oid, name)
207
- }
208
-
209
- function seedPgClass(db: SqliteLike, tables: { name: string; type: string }[]): void {
210
- db.exec(`
211
- DROP TABLE IF EXISTS _orez_catalog__pg_class;
212
- CREATE TABLE _orez_catalog__pg_class (
213
- oid INTEGER PRIMARY KEY,
214
- relname TEXT NOT NULL,
215
- relnamespace INTEGER NOT NULL DEFAULT ${NAMESPACE_OID_PUBLIC},
216
- relkind TEXT NOT NULL DEFAULT 'r',
217
- relowner INTEGER NOT NULL DEFAULT 10,
218
- relhasindex INTEGER NOT NULL DEFAULT 0,
219
- relhasrules INTEGER NOT NULL DEFAULT 0,
220
- relhastriggers INTEGER NOT NULL DEFAULT 0,
221
- relhassubclass INTEGER NOT NULL DEFAULT 0,
222
- relpersistence TEXT NOT NULL DEFAULT 'p',
223
- reltuples INTEGER NOT NULL DEFAULT 0
224
- );
225
- `)
226
- if (tables.length === 0) return
227
- const insert = db.prepare(
228
- 'INSERT INTO _orez_catalog__pg_class (oid, relname, relnamespace, relkind) VALUES (?, ?, ?, ?)'
229
- )
230
- for (const t of tables) {
231
- const oid = hashName(t.name, TABLE_OID_BASE)
232
- const kind = t.type === 'view' ? 'v' : 'r'
233
- insert.run(oid, t.name, NAMESPACE_OID_PUBLIC, kind)
234
- }
235
- }
236
-
237
- function seedPgAttributeAndDefaults(
238
- db: SqliteLike,
239
- tables: { name: string; type: string }[]
240
- ): void {
241
- db.exec(`
242
- DROP TABLE IF EXISTS _orez_catalog__pg_attribute;
243
- CREATE TABLE _orez_catalog__pg_attribute (
244
- attrelid INTEGER NOT NULL,
245
- attname TEXT NOT NULL,
246
- attnum INTEGER NOT NULL,
247
- atttypid INTEGER NOT NULL DEFAULT 25,
248
- attlen INTEGER NOT NULL DEFAULT -1,
249
- attnotnull INTEGER NOT NULL DEFAULT 0,
250
- atthasdef INTEGER NOT NULL DEFAULT 0,
251
- attisdropped INTEGER NOT NULL DEFAULT 0,
252
- atttypmod INTEGER NOT NULL DEFAULT -1,
253
- PRIMARY KEY (attrelid, attnum)
254
- );
255
-
256
- DROP TABLE IF EXISTS _orez_catalog__pg_attrdef;
257
- CREATE TABLE _orez_catalog__pg_attrdef (
258
- oid INTEGER PRIMARY KEY,
259
- adrelid INTEGER NOT NULL,
260
- adnum INTEGER NOT NULL,
261
- adbin TEXT,
262
- adsrc TEXT
263
- );
264
- `)
265
- const attrInsert = db.prepare(
266
- 'INSERT INTO _orez_catalog__pg_attribute (attrelid, attname, attnum, atttypid, attnotnull, atthasdef) VALUES (?, ?, ?, ?, ?, ?)'
267
- )
268
- const defInsert = db.prepare(
269
- 'INSERT INTO _orez_catalog__pg_attrdef (oid, adrelid, adnum, adbin, adsrc) VALUES (?, ?, ?, ?, ?)'
270
- )
271
- let defOid = 100_000
272
- for (const t of tables) {
273
- const cols = readColumns(db, t.name)
274
- if (cols.length === 0) continue
275
- const oid = hashName(t.name, TABLE_OID_BASE)
276
- for (const c of cols) {
277
- const typname = sqliteToPgType(c.type)
278
- const typoid = PG_TYPE_OIDS[typname] ?? PG_TYPE_OIDS.text
279
- const hasdef = c.dflt_value !== null ? 1 : 0
280
- attrInsert.run(oid, c.name, c.cid + 1, typoid, c.notnull, hasdef)
281
- if (c.dflt_value !== null) {
282
- defInsert.run(defOid++, oid, c.cid + 1, c.dflt_value, c.dflt_value)
283
- }
284
- }
285
- }
286
- }
287
-
288
- function seedPgPublication(
289
- db: SqliteLike,
290
- publications: readonly string[],
291
- tables: { name: string; type: string }[]
292
- ): void {
293
- db.exec(`
294
- DROP TABLE IF EXISTS _orez_catalog__pg_publication;
295
- CREATE TABLE _orez_catalog__pg_publication (
296
- oid INTEGER PRIMARY KEY,
297
- pubname TEXT NOT NULL UNIQUE,
298
- pubowner INTEGER NOT NULL DEFAULT 10,
299
- puballtables INTEGER NOT NULL DEFAULT 0,
300
- pubinsert INTEGER NOT NULL DEFAULT 1,
301
- pubupdate INTEGER NOT NULL DEFAULT 1,
302
- pubdelete INTEGER NOT NULL DEFAULT 1,
303
- pubtruncate INTEGER NOT NULL DEFAULT 1
304
- );
305
-
306
- DROP TABLE IF EXISTS _orez_catalog__pg_publication_tables;
307
- CREATE TABLE _orez_catalog__pg_publication_tables (
308
- pubname TEXT NOT NULL,
309
- schemaname TEXT NOT NULL,
310
- tablename TEXT NOT NULL,
311
- PRIMARY KEY (pubname, schemaname, tablename)
312
- );
313
-
314
- DROP TABLE IF EXISTS _orez_catalog__pg_publication_rel;
315
- CREATE TABLE _orez_catalog__pg_publication_rel (
316
- oid INTEGER PRIMARY KEY,
317
- prpubid INTEGER NOT NULL,
318
- prrelid INTEGER NOT NULL
319
- );
320
- `)
321
- if (publications.length === 0) return
322
-
323
- const pubInsert = db.prepare(
324
- 'INSERT INTO _orez_catalog__pg_publication (oid, pubname) VALUES (?, ?)'
325
- )
326
- const ptInsert = db.prepare(
327
- 'INSERT INTO _orez_catalog__pg_publication_tables (pubname, schemaname, tablename) VALUES (?, ?, ?)'
328
- )
329
- const relInsert = db.prepare(
330
- 'INSERT INTO _orez_catalog__pg_publication_rel (oid, prpubid, prrelid) VALUES (?, ?, ?)'
331
- )
332
- let relOid = 200_000
333
- for (const p of publications) {
334
- const pubOid = hashName(p, 1_000_000)
335
- pubInsert.run(pubOid, p)
336
- for (const t of tables) {
337
- ptInsert.run(p, 'public', t.name)
338
- relInsert.run(relOid++, pubOid, hashName(t.name, TABLE_OID_BASE))
339
- }
340
- }
341
- }
342
-
343
- /**
344
- * Empty-but-queryable stubs for catalog tables that the pass recognizes but
345
- * we don't actively populate. Created with their PG-compatible columns so
346
- * SELECT-from-them returns 0 rows cleanly instead of throwing.
347
- */
348
- function seedSimpleStubs(db: SqliteLike): void {
349
- const stubs: { name: string; columns: string }[] = [
350
- {
351
- name: 'pg_constraint',
352
- columns:
353
- 'oid INTEGER PRIMARY KEY, conname TEXT NOT NULL, connamespace INTEGER, contype TEXT, conrelid INTEGER, conindid INTEGER, confrelid INTEGER, conkey TEXT, confkey TEXT',
354
- },
355
- {
356
- name: 'pg_index',
357
- columns:
358
- 'indexrelid INTEGER PRIMARY KEY, indrelid INTEGER NOT NULL, indnatts INTEGER, indisunique INTEGER DEFAULT 0, indisprimary INTEGER DEFAULT 0, indkey TEXT',
359
- },
360
- {
361
- name: 'pg_proc',
362
- columns:
363
- 'oid INTEGER PRIMARY KEY, proname TEXT NOT NULL, pronamespace INTEGER, prorettype INTEGER, proargtypes TEXT',
364
- },
365
- {
366
- name: 'pg_trigger',
367
- columns:
368
- 'oid INTEGER PRIMARY KEY, tgrelid INTEGER NOT NULL, tgname TEXT NOT NULL, tgfoid INTEGER, tgenabled TEXT',
369
- },
370
- {
371
- name: 'pg_inherits',
372
- columns: 'inhrelid INTEGER NOT NULL, inhparent INTEGER NOT NULL, inhseqno INTEGER',
373
- },
374
- {
375
- name: 'pg_depend',
376
- columns:
377
- 'classid INTEGER, objid INTEGER, objsubid INTEGER, refclassid INTEGER, refobjid INTEGER, refobjsubid INTEGER, deptype TEXT',
378
- },
379
- {
380
- name: 'pg_description',
381
- columns: 'objoid INTEGER, classoid INTEGER, objsubid INTEGER, description TEXT',
382
- },
383
- {
384
- name: 'pg_enum',
385
- columns:
386
- 'oid INTEGER PRIMARY KEY, enumtypid INTEGER NOT NULL, enumsortorder REAL, enumlabel TEXT NOT NULL',
387
- },
388
- {
389
- name: 'pg_extension',
390
- columns:
391
- 'oid INTEGER PRIMARY KEY, extname TEXT NOT NULL, extowner INTEGER, extnamespace INTEGER, extversion TEXT',
392
- },
393
- {
394
- name: 'pg_sequence',
395
- columns:
396
- 'seqrelid INTEGER PRIMARY KEY, seqtypid INTEGER, seqstart INTEGER, seqincrement INTEGER, seqmax INTEGER, seqmin INTEGER, seqcache INTEGER, seqcycle INTEGER',
397
- },
398
- {
399
- name: 'pg_views',
400
- columns: 'schemaname TEXT, viewname TEXT, viewowner TEXT, definition TEXT',
401
- },
402
- { name: 'pg_collation', columns: 'oid INTEGER PRIMARY KEY, collname TEXT NOT NULL' },
403
- {
404
- name: 'pg_am',
405
- columns: 'oid INTEGER PRIMARY KEY, amname TEXT NOT NULL, amtype TEXT',
406
- },
407
- { name: 'pg_operator', columns: 'oid INTEGER PRIMARY KEY, oprname TEXT NOT NULL' },
408
- {
409
- name: 'pg_cast',
410
- columns:
411
- 'oid INTEGER PRIMARY KEY, castsource INTEGER, casttarget INTEGER, castfunc INTEGER, castcontext TEXT, castmethod TEXT',
412
- },
413
- { name: 'pg_language', columns: 'oid INTEGER PRIMARY KEY, lanname TEXT NOT NULL' },
414
- {
415
- name: 'pg_statistic',
416
- columns:
417
- 'starelid INTEGER, staattnum INTEGER, stainherit INTEGER, stanullfrac REAL',
418
- },
419
- {
420
- name: 'pg_locks',
421
- columns:
422
- 'locktype TEXT, database INTEGER, relation INTEGER, page INTEGER, tuple INTEGER, transactionid INTEGER, mode TEXT, granted INTEGER',
423
- },
424
- {
425
- name: 'pg_replication_slots',
426
- columns:
427
- "slot_name TEXT PRIMARY KEY, plugin TEXT, slot_type TEXT NOT NULL DEFAULT 'logical', database TEXT, active INTEGER NOT NULL DEFAULT 0, restart_lsn TEXT, confirmed_flush_lsn TEXT",
428
- },
429
- {
430
- name: 'pg_stat_replication',
431
- columns:
432
- 'pid INTEGER, usesysid INTEGER, usename TEXT, application_name TEXT, client_addr TEXT, state TEXT',
433
- },
434
- {
435
- name: 'pg_subscription',
436
- columns:
437
- 'oid INTEGER PRIMARY KEY, subname TEXT NOT NULL, subenabled INTEGER, subconninfo TEXT, subslotname TEXT',
438
- },
439
- {
440
- name: 'pg_database',
441
- columns: 'oid INTEGER PRIMARY KEY, datname TEXT NOT NULL',
442
- },
443
- { name: 'pg_roles', columns: 'oid INTEGER PRIMARY KEY, rolname TEXT NOT NULL' },
444
- { name: 'pg_user', columns: 'usename TEXT PRIMARY KEY, usesysid INTEGER' },
445
- {
446
- name: 'pg_settings',
447
- columns:
448
- 'name TEXT PRIMARY KEY, setting TEXT, category TEXT, short_desc TEXT, context TEXT, vartype TEXT',
449
- },
450
- ]
451
- for (const { name, columns } of stubs) {
452
- db.exec(
453
- `DROP TABLE IF EXISTS _orez_catalog__${name}; CREATE TABLE _orez_catalog__${name} (${columns});`
454
- )
455
- }
456
- // pg_database — single row "main"
457
- const datInsert = db.prepare(
458
- 'INSERT INTO _orez_catalog__pg_database (oid, datname) VALUES (?, ?)'
459
- )
460
- datInsert.run(1, 'main')
461
- }
462
-
463
- function seedInformationSchema(
464
- db: SqliteLike,
465
- tables: { name: string; type: string }[]
466
- ): void {
467
- db.exec(`
468
- DROP TABLE IF EXISTS _orez_catalog__information_schema_columns;
469
- CREATE TABLE _orez_catalog__information_schema_columns (
470
- table_catalog TEXT NOT NULL DEFAULT 'main',
471
- table_schema TEXT NOT NULL DEFAULT 'public',
472
- table_name TEXT NOT NULL,
473
- column_name TEXT NOT NULL,
474
- ordinal_position INTEGER NOT NULL,
475
- column_default TEXT,
476
- is_nullable TEXT NOT NULL DEFAULT 'YES',
477
- data_type TEXT NOT NULL,
478
- character_maximum_length INTEGER,
479
- numeric_precision INTEGER,
480
- numeric_scale INTEGER,
481
- udt_name TEXT
482
- );
483
-
484
- DROP TABLE IF EXISTS _orez_catalog__information_schema_tables;
485
- CREATE TABLE _orez_catalog__information_schema_tables (
486
- table_catalog TEXT NOT NULL DEFAULT 'main',
487
- table_schema TEXT NOT NULL DEFAULT 'public',
488
- table_name TEXT NOT NULL,
489
- table_type TEXT NOT NULL DEFAULT 'BASE TABLE',
490
- PRIMARY KEY (table_schema, table_name)
491
- );
492
- `)
493
- const colInsert = db.prepare(
494
- 'INSERT INTO _orez_catalog__information_schema_columns ' +
495
- '(table_catalog, table_schema, table_name, column_name, ordinal_position, column_default, is_nullable, data_type, udt_name) ' +
496
- 'VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)'
497
- )
498
- const tblInsert = db.prepare(
499
- 'INSERT INTO _orez_catalog__information_schema_tables (table_catalog, table_schema, table_name, table_type) VALUES (?, ?, ?, ?)'
500
- )
501
- for (const t of tables) {
502
- tblInsert.run('main', 'public', t.name, t.type === 'view' ? 'VIEW' : 'BASE TABLE')
503
- const cols = readColumns(db, t.name)
504
- for (const c of cols) {
505
- const typname = sqliteToPgType(c.type)
506
- const isNullable = c.notnull ? 'NO' : 'YES'
507
- // dflt_value from PRAGMA is already a SQL expression as-written (e.g.
508
- // `'foo'` for a string default, `CURRENT_TIMESTAMP` bare for a keyword
509
- // default). Pass through unchanged — PG's column_default reports the
510
- // original expression text.
511
- colInsert.run(
512
- 'main',
513
- 'public',
514
- t.name,
515
- c.name,
516
- c.cid + 1,
517
- c.dflt_value,
518
- isNullable,
519
- typname,
520
- typname
521
- )
522
- }
523
- }
524
- }