@rudderjs/database 1.1.0 → 1.2.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 +70 -0
- package/dist/db.d.ts +21 -3
- package/dist/db.d.ts.map +1 -1
- package/dist/db.js +27 -5
- package/dist/db.js.map +1 -1
- package/dist/index.d.ts +14 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +23 -4
- package/dist/index.js.map +1 -1
- package/dist/native/adapter.d.ts +202 -0
- package/dist/native/adapter.d.ts.map +1 -0
- package/dist/native/adapter.js +440 -0
- package/dist/native/adapter.js.map +1 -0
- package/dist/native/compiler.d.ts +371 -0
- package/dist/native/compiler.d.ts.map +1 -0
- package/dist/native/compiler.js +978 -0
- package/dist/native/compiler.js.map +1 -0
- package/dist/native/dialect-mysql.d.ts +26 -0
- package/dist/native/dialect-mysql.d.ts.map +1 -0
- package/dist/native/dialect-mysql.js +188 -0
- package/dist/native/dialect-mysql.js.map +1 -0
- package/dist/native/dialect-pg.d.ts +26 -0
- package/dist/native/dialect-pg.d.ts.map +1 -0
- package/dist/native/dialect-pg.js +192 -0
- package/dist/native/dialect-pg.js.map +1 -0
- package/dist/native/dialect.d.ts +255 -0
- package/dist/native/dialect.d.ts.map +1 -0
- package/dist/native/dialect.js +237 -0
- package/dist/native/dialect.js.map +1 -0
- package/dist/native/driver.d.ts +37 -0
- package/dist/native/driver.d.ts.map +1 -0
- package/dist/native/driver.js +19 -0
- package/dist/native/driver.js.map +1 -0
- package/dist/native/drivers/better-sqlite3.d.ts +56 -0
- package/dist/native/drivers/better-sqlite3.d.ts.map +1 -0
- package/dist/native/drivers/better-sqlite3.js +171 -0
- package/dist/native/drivers/better-sqlite3.js.map +1 -0
- package/dist/native/drivers/mysql.d.ts +30 -0
- package/dist/native/drivers/mysql.d.ts.map +1 -0
- package/dist/native/drivers/mysql.js +176 -0
- package/dist/native/drivers/mysql.js.map +1 -0
- package/dist/native/drivers/postgres.d.ts +57 -0
- package/dist/native/drivers/postgres.d.ts.map +1 -0
- package/dist/native/drivers/postgres.js +155 -0
- package/dist/native/drivers/postgres.js.map +1 -0
- package/dist/native/errors.d.ts +43 -0
- package/dist/native/errors.d.ts.map +1 -0
- package/dist/native/errors.js +64 -0
- package/dist/native/errors.js.map +1 -0
- package/dist/native/index.d.ts +27 -0
- package/dist/native/index.d.ts.map +1 -0
- package/dist/native/index.js +55 -0
- package/dist/native/index.js.map +1 -0
- package/dist/native/isolation.d.ts +14 -0
- package/dist/native/isolation.d.ts.map +1 -0
- package/dist/native/isolation.js +37 -0
- package/dist/native/isolation.js.map +1 -0
- package/dist/native/query-builder.d.ts +303 -0
- package/dist/native/query-builder.d.ts.map +1 -0
- package/dist/native/query-builder.js +984 -0
- package/dist/native/query-builder.js.map +1 -0
- package/dist/native/replica-picker.d.ts +22 -0
- package/dist/native/replica-picker.d.ts.map +1 -0
- package/dist/native/replica-picker.js +65 -0
- package/dist/native/replica-picker.js.map +1 -0
- package/dist/native/schema/alter-blueprint.d.ts +37 -0
- package/dist/native/schema/alter-blueprint.d.ts.map +1 -0
- package/dist/native/schema/alter-blueprint.js +56 -0
- package/dist/native/schema/alter-blueprint.js.map +1 -0
- package/dist/native/schema/blueprint.d.ts +151 -0
- package/dist/native/schema/blueprint.d.ts.map +1 -0
- package/dist/native/schema/blueprint.js +286 -0
- package/dist/native/schema/blueprint.js.map +1 -0
- package/dist/native/schema/column.d.ts +168 -0
- package/dist/native/schema/column.d.ts.map +1 -0
- package/dist/native/schema/column.js +190 -0
- package/dist/native/schema/column.js.map +1 -0
- package/dist/native/schema/ddl-compiler.d.ts +34 -0
- package/dist/native/schema/ddl-compiler.d.ts.map +1 -0
- package/dist/native/schema/ddl-compiler.js +352 -0
- package/dist/native/schema/ddl-compiler.js.map +1 -0
- package/dist/native/schema/inspect.d.ts +67 -0
- package/dist/native/schema/inspect.d.ts.map +1 -0
- package/dist/native/schema/inspect.js +312 -0
- package/dist/native/schema/inspect.js.map +1 -0
- package/dist/native/schema/introspect.d.ts +34 -0
- package/dist/native/schema/introspect.d.ts.map +1 -0
- package/dist/native/schema/introspect.js +101 -0
- package/dist/native/schema/introspect.js.map +1 -0
- package/dist/native/schema/migration.d.ts +8 -0
- package/dist/native/schema/migration.d.ts.map +1 -0
- package/dist/native/schema/migration.js +19 -0
- package/dist/native/schema/migration.js.map +1 -0
- package/dist/native/schema/migrator.d.ts +144 -0
- package/dist/native/schema/migrator.d.ts.map +1 -0
- package/dist/native/schema/migrator.js +239 -0
- package/dist/native/schema/migrator.js.map +1 -0
- package/dist/native/schema/rebuild.d.ts +11 -0
- package/dist/native/schema/rebuild.d.ts.map +1 -0
- package/dist/native/schema/rebuild.js +92 -0
- package/dist/native/schema/rebuild.js.map +1 -0
- package/dist/native/schema/schema-builder.d.ts +46 -0
- package/dist/native/schema/schema-builder.d.ts.map +1 -0
- package/dist/native/schema/schema-builder.js +153 -0
- package/dist/native/schema/schema-builder.js.map +1 -0
- package/dist/native/schema/schema-facade.d.ts +63 -0
- package/dist/native/schema/schema-facade.d.ts.map +1 -0
- package/dist/native/schema/schema-facade.js +124 -0
- package/dist/native/schema/schema-facade.js.map +1 -0
- package/dist/native/schema/schema-types.d.ts +27 -0
- package/dist/native/schema/schema-types.d.ts.map +1 -0
- package/dist/native/schema/schema-types.js +52 -0
- package/dist/native/schema/schema-types.js.map +1 -0
- package/dist/native/schema/types-generator.d.ts +73 -0
- package/dist/native/schema/types-generator.d.ts.map +1 -0
- package/dist/native/schema/types-generator.js +181 -0
- package/dist/native/schema/types-generator.js.map +1 -0
- package/dist/registry-bridge.d.ts +24 -4
- package/dist/registry-bridge.d.ts.map +1 -1
- package/dist/registry-bridge.js +20 -0
- package/dist/registry-bridge.js.map +1 -1
- package/dist/sticky.d.ts +22 -0
- package/dist/sticky.d.ts.map +1 -0
- package/dist/sticky.js +61 -0
- package/dist/sticky.js.map +1 -0
- package/package.json +32 -2
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import type { QueryBuilder, OrmAdapter } from '@rudderjs/contracts';
|
|
2
|
+
import { Migration } from './migration.js';
|
|
3
|
+
import type { SchemaBuilder } from './schema-builder.js';
|
|
4
|
+
/** The minimal connection surface the migrator needs. `NativeAdapter` satisfies it. */
|
|
5
|
+
export interface MigratorAdapter {
|
|
6
|
+
schemaBuilder(): SchemaBuilder;
|
|
7
|
+
query<T>(table: string): QueryBuilder<T>;
|
|
8
|
+
/**
|
|
9
|
+
* Run `fn` inside a transaction, passing it a transaction-scoped adapter whose
|
|
10
|
+
* `schemaBuilder()` / `query()` execute on the transaction. The migrator wraps
|
|
11
|
+
* each batch's up()s / down()s in this so a mid-batch failure rolls back the
|
|
12
|
+
* WHOLE batch — DDL and the `migrations` state-table writes commit atomically.
|
|
13
|
+
*
|
|
14
|
+
* The callback param mirrors the `OrmAdapter` contract (the scoped adapter the
|
|
15
|
+
* concrete `transaction()` hands back) so `NativeAdapter` stays structurally
|
|
16
|
+
* assignable to `MigratorAdapter`. The migrator narrows it to a {@link
|
|
17
|
+
* ScopedMigratorAdapter} internally — the scoped adapter is in fact a full
|
|
18
|
+
* native adapter that also exposes `schemaBuilder()`.
|
|
19
|
+
*/
|
|
20
|
+
transaction<T>(fn: (tx: OrmAdapter) => Promise<T>): Promise<T>;
|
|
21
|
+
/**
|
|
22
|
+
* A recording {@link SchemaBuilder} for `migrate --pretend`: statements are
|
|
23
|
+
* captured (via `record`) instead of executed. Optional — an adapter without it
|
|
24
|
+
* makes `--pretend` throw a clear error rather than silently executing.
|
|
25
|
+
*/
|
|
26
|
+
pretendSchemaBuilder?(record: (sql: string, bindings: readonly unknown[]) => void): SchemaBuilder;
|
|
27
|
+
}
|
|
28
|
+
/** A migration file resolved to its name (filename sans extension) + instance. */
|
|
29
|
+
export interface LoadedMigration {
|
|
30
|
+
name: string;
|
|
31
|
+
migration: Migration;
|
|
32
|
+
}
|
|
33
|
+
/** One row of `migrate:status` output. */
|
|
34
|
+
export interface MigrationStatus {
|
|
35
|
+
name: string;
|
|
36
|
+
ran: boolean;
|
|
37
|
+
batch: number | null;
|
|
38
|
+
}
|
|
39
|
+
/** The SQL a single migration's `up()` would emit, captured under `--pretend`. */
|
|
40
|
+
export interface PretendedMigration {
|
|
41
|
+
name: string;
|
|
42
|
+
statements: string[];
|
|
43
|
+
}
|
|
44
|
+
/** Outcome of a `run()`. */
|
|
45
|
+
export interface RunResult {
|
|
46
|
+
batch: number;
|
|
47
|
+
applied: string[];
|
|
48
|
+
/** Present only under `{ pretend: true }` — the captured (un-executed) SQL per
|
|
49
|
+
* pending migration. `applied` is empty in that case (nothing ran). */
|
|
50
|
+
pretended?: PretendedMigration[];
|
|
51
|
+
}
|
|
52
|
+
/** Knobs for {@link Migrator.run} (Laravel `migrate` flag parity). */
|
|
53
|
+
export interface RunOptions {
|
|
54
|
+
/** `--step`: record each pending migration in its OWN batch (so they roll back
|
|
55
|
+
* one at a time) instead of grouping the whole run into a single batch. */
|
|
56
|
+
step?: boolean;
|
|
57
|
+
/** `--pretend`: capture the SQL each pending migration would emit without
|
|
58
|
+
* executing anything or writing the `migrations` state table. */
|
|
59
|
+
pretend?: boolean;
|
|
60
|
+
}
|
|
61
|
+
/** Outcome of a `rollback()`. */
|
|
62
|
+
export interface RollbackResult {
|
|
63
|
+
/** The batch number that was rolled back (`0` if nothing to roll back). For a
|
|
64
|
+
* `--step`/`--batch` rollback this is the highest batch touched. */
|
|
65
|
+
batch: number;
|
|
66
|
+
reverted: string[];
|
|
67
|
+
}
|
|
68
|
+
/** Knobs for {@link Migrator.rollback} (Laravel `migrate:rollback` flag parity). */
|
|
69
|
+
export interface RollbackOptions {
|
|
70
|
+
/** `--step=N`: roll back the last N applied migrations (across batches), each
|
|
71
|
+
* reverted in reverse apply order. */
|
|
72
|
+
step?: number;
|
|
73
|
+
/** `--batch=N`: roll back every migration recorded in batch N. */
|
|
74
|
+
batch?: number;
|
|
75
|
+
}
|
|
76
|
+
interface MigrationRow {
|
|
77
|
+
id: number;
|
|
78
|
+
migration: string;
|
|
79
|
+
batch: number;
|
|
80
|
+
}
|
|
81
|
+
export declare class Migrator {
|
|
82
|
+
private readonly adapter;
|
|
83
|
+
private readonly schema;
|
|
84
|
+
constructor(adapter: MigratorAdapter);
|
|
85
|
+
/** Whether the `migrations` state table exists yet. */
|
|
86
|
+
installed(): Promise<boolean>;
|
|
87
|
+
/** Create the `migrations` state table if it's missing. Idempotent. */
|
|
88
|
+
ensureTable(): Promise<void>;
|
|
89
|
+
/** Names of applied migrations, in apply order. */
|
|
90
|
+
ran(): Promise<string[]>;
|
|
91
|
+
/** The batch number a fresh `run()` would use (`max(batch) + 1`, or 1). */
|
|
92
|
+
nextBatch(): Promise<number>;
|
|
93
|
+
/**
|
|
94
|
+
* Apply every not-yet-run migration (in `name` order) inside a single new
|
|
95
|
+
* batch. `onApply` streams progress to the caller (the CLI prints it). Returns
|
|
96
|
+
* the batch number and the applied names.
|
|
97
|
+
*/
|
|
98
|
+
run(migrations: LoadedMigration[], onApply?: (name: string) => void, opts?: RunOptions): Promise<RunResult>;
|
|
99
|
+
/** Dry-run: bind each pending migration's `up()` to a recording schema builder
|
|
100
|
+
* and collect the DDL it would emit. No transaction, no state-table writes. */
|
|
101
|
+
private pretend;
|
|
102
|
+
/** ran/pending state for every known migration, plus its batch when applied. */
|
|
103
|
+
status(migrations: LoadedMigration[]): Promise<MigrationStatus[]>;
|
|
104
|
+
/** The highest recorded batch number, or `0` when nothing has been applied. */
|
|
105
|
+
lastBatch(): Promise<number>;
|
|
106
|
+
/** Rows recorded in `batch`, newest-applied first (id DESC) — i.e. the order
|
|
107
|
+
* their `down()` methods should run to unwind the batch. */
|
|
108
|
+
migrationsInBatch(batch: number): Promise<MigrationRow[]>;
|
|
109
|
+
/**
|
|
110
|
+
* Revert the LAST batch: run each of its migrations' `down()` in reverse apply
|
|
111
|
+
* order (id DESC) and delete their `migrations` rows. The whole batch runs in
|
|
112
|
+
* one transaction, so a `down()` that throws leaves the state table untouched.
|
|
113
|
+
* Returns the rolled-back batch number and the reverted names. A no-op (batch
|
|
114
|
+
* `0`) when nothing has been applied.
|
|
115
|
+
*/
|
|
116
|
+
rollback(migrations: LoadedMigration[], onRevert?: (name: string) => void, opts?: RollbackOptions): Promise<RollbackResult>;
|
|
117
|
+
/**
|
|
118
|
+
* Revert EVERY applied migration (all batches), `down()` in full reverse order
|
|
119
|
+
* (id DESC across batches), in a single transaction. Used by `migrate:refresh`.
|
|
120
|
+
* Returns the reverted names.
|
|
121
|
+
*/
|
|
122
|
+
rollbackAll(migrations: LoadedMigration[]): Promise<string[]>;
|
|
123
|
+
/** Shared unwind path for `rollback` / `rollbackAll`: run `down()` for each row
|
|
124
|
+
* (already in reverse order) inside one transaction and delete its state row. */
|
|
125
|
+
private revertRows;
|
|
126
|
+
/**
|
|
127
|
+
* Drop every user table (used by `migrate:fresh`) — including the
|
|
128
|
+
* `migrations` state table, so the next `run()` rebuilds from a clean slate.
|
|
129
|
+
* Delegates to the dialect-aware {@link SchemaBuilder.dropAllTables}
|
|
130
|
+
* (`information_schema` + FK-safe drops on pg/mysql, `sqlite_master` on
|
|
131
|
+
* sqlite). Historically this read the SQLite catalog directly, which made
|
|
132
|
+
* `migrate:fresh` throw on pg/mysql native connections.
|
|
133
|
+
*/
|
|
134
|
+
dropAllTables(): Promise<void>;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Load migration files from a directory: each `*.{ts,js,mts,mjs}` whose default
|
|
138
|
+
* export is a {@link Migration} subclass, instantiated and sorted by filename
|
|
139
|
+
* (timestamp-prefixed names sort chronologically). A file without a valid
|
|
140
|
+
* default export throws — a silently-skipped migration is worse than a loud one.
|
|
141
|
+
*/
|
|
142
|
+
export declare function discoverMigrations(dir: string): Promise<LoadedMigration[]>;
|
|
143
|
+
export {};
|
|
144
|
+
//# sourceMappingURL=migrator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrator.d.ts","sourceRoot":"","sources":["../../../src/native/sch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AAiB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC;IAIzD;;;;;;OAMG;IACG,QAAQ,CACZ,UAAU,EAAE,eAAe,EAAE,EAC7B,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,EACjC,IAAI,GAAE,eAAoB,GACzB,OAAO,CAAC,cAAc,CAAC;IAwB1B;;;;OAIG;IACG,WAAW,CAAC,UAAU,EAAE,eAAe,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAMnE;sFACkF;YACpE,UAAU;IA6BxB;;;;;;;OAOG;IACG,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;CAGrC;AAED;;;;;GAKG;AACH,wBAAsB,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC,CA0BhF"}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// ─── Migrator (runner + state) ─────────────────────────────
|
|
2
|
+
//
|
|
3
|
+
// Node-capable: drives migration files against a connection and tracks applied
|
|
4
|
+
// migrations in a `migrations` table (Laravel's model: `id`, `migration`,
|
|
5
|
+
// `batch`). Phase 7.2 ships forward `migrate` + `status`; `rollback`/`refresh`
|
|
6
|
+
// (which reverse a batch via `down()`) and transactional batches land in 7.5 —
|
|
7
|
+
// the `batch` column is recorded now so rollback has the grouping it needs.
|
|
8
|
+
//
|
|
9
|
+
// The runner is decoupled from the concrete adapter: it needs only a
|
|
10
|
+
// `schemaBuilder()` (DDL) and `query()` (state-table CRUD), both of which
|
|
11
|
+
// `NativeAdapter` provides. That keeps it unit-testable against an in-memory
|
|
12
|
+
// SQLite adapter, and reusable by RN/WASM drivers later.
|
|
13
|
+
import { readdirSync } from 'node:fs';
|
|
14
|
+
import { join } from 'node:path';
|
|
15
|
+
import { pathToFileURL } from 'node:url';
|
|
16
|
+
import { Migration } from './migration.js';
|
|
17
|
+
import { withSchema } from './schema-facade.js';
|
|
18
|
+
import { NativeOrmError } from '../errors.js';
|
|
19
|
+
const TABLE = 'migrations';
|
|
20
|
+
export class Migrator {
|
|
21
|
+
adapter;
|
|
22
|
+
schema;
|
|
23
|
+
constructor(adapter) {
|
|
24
|
+
this.adapter = adapter;
|
|
25
|
+
this.schema = adapter.schemaBuilder();
|
|
26
|
+
}
|
|
27
|
+
/** Whether the `migrations` state table exists yet. */
|
|
28
|
+
installed() {
|
|
29
|
+
return this.schema.hasTable(TABLE);
|
|
30
|
+
}
|
|
31
|
+
/** Create the `migrations` state table if it's missing. Idempotent. */
|
|
32
|
+
async ensureTable() {
|
|
33
|
+
if (await this.installed())
|
|
34
|
+
return;
|
|
35
|
+
await this.schema.create(TABLE, (t) => {
|
|
36
|
+
t.id();
|
|
37
|
+
t.string('migration').unique();
|
|
38
|
+
t.integer('batch');
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
/** Names of applied migrations, in apply order. */
|
|
42
|
+
async ran() {
|
|
43
|
+
if (!(await this.installed()))
|
|
44
|
+
return [];
|
|
45
|
+
const rows = await this.adapter.query(TABLE).orderBy('id', 'ASC').get();
|
|
46
|
+
return rows.map(r => r.migration);
|
|
47
|
+
}
|
|
48
|
+
/** The batch number a fresh `run()` would use (`max(batch) + 1`, or 1). */
|
|
49
|
+
async nextBatch() {
|
|
50
|
+
if (!(await this.installed()))
|
|
51
|
+
return 1;
|
|
52
|
+
const rows = await this.adapter.query(TABLE).get();
|
|
53
|
+
return rows.reduce((max, r) => Math.max(max, r.batch), 0) + 1;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Apply every not-yet-run migration (in `name` order) inside a single new
|
|
57
|
+
* batch. `onApply` streams progress to the caller (the CLI prints it). Returns
|
|
58
|
+
* the batch number and the applied names.
|
|
59
|
+
*/
|
|
60
|
+
async run(migrations, onApply, opts = {}) {
|
|
61
|
+
// --pretend: capture each pending migration's SQL without touching the DB or
|
|
62
|
+
// the state table. Doesn't need (or create) the migrations table.
|
|
63
|
+
if (opts.pretend)
|
|
64
|
+
return this.pretend(migrations);
|
|
65
|
+
await this.ensureTable();
|
|
66
|
+
const done = new Set(await this.ran());
|
|
67
|
+
const pending = migrations.filter(m => !done.has(m.name));
|
|
68
|
+
const startBatch = await this.nextBatch();
|
|
69
|
+
if (pending.length === 0)
|
|
70
|
+
return { batch: startBatch, applied: [] };
|
|
71
|
+
// One transaction for the whole run: if any up() throws, every DDL change and
|
|
72
|
+
// `migrations` row recorded so far rolls back atomically. Use the TX-scoped
|
|
73
|
+
// adapter for both the schema builder and the state-table writes. Under
|
|
74
|
+
// --step each migration takes its own incrementing batch number (so it can be
|
|
75
|
+
// rolled back individually); otherwise they share `startBatch`.
|
|
76
|
+
await this.adapter.transaction(async (txAdapter) => {
|
|
77
|
+
const tx = txAdapter;
|
|
78
|
+
const schema = tx.schemaBuilder();
|
|
79
|
+
let offset = 0;
|
|
80
|
+
for (const { name, migration } of pending) {
|
|
81
|
+
const batch = opts.step ? startBatch + offset : startBatch;
|
|
82
|
+
await withSchema(schema, () => migration.up());
|
|
83
|
+
await tx.query(TABLE).create({ migration: name, batch });
|
|
84
|
+
onApply?.(name);
|
|
85
|
+
offset++;
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
return { batch: startBatch, applied: pending.map(m => m.name) };
|
|
89
|
+
}
|
|
90
|
+
/** Dry-run: bind each pending migration's `up()` to a recording schema builder
|
|
91
|
+
* and collect the DDL it would emit. No transaction, no state-table writes. */
|
|
92
|
+
async pretend(migrations) {
|
|
93
|
+
if (!this.adapter.pretendSchemaBuilder) {
|
|
94
|
+
throw new NativeOrmError('NATIVE_PRETEND_UNSUPPORTED', `[RudderJS ORM native] --pretend is not supported by this adapter (no pretendSchemaBuilder()).`);
|
|
95
|
+
}
|
|
96
|
+
const done = new Set(await this.ran());
|
|
97
|
+
const pending = migrations.filter(m => !done.has(m.name));
|
|
98
|
+
const pretended = [];
|
|
99
|
+
for (const { name, migration } of pending) {
|
|
100
|
+
const statements = [];
|
|
101
|
+
const schema = this.adapter.pretendSchemaBuilder((sql) => statements.push(sql));
|
|
102
|
+
await withSchema(schema, () => migration.up(), { pretend: true });
|
|
103
|
+
pretended.push({ name, statements });
|
|
104
|
+
}
|
|
105
|
+
return { batch: await this.nextBatch(), applied: [], pretended };
|
|
106
|
+
}
|
|
107
|
+
/** ran/pending state for every known migration, plus its batch when applied. */
|
|
108
|
+
async status(migrations) {
|
|
109
|
+
const rows = (await this.installed())
|
|
110
|
+
? await this.adapter.query(TABLE).get()
|
|
111
|
+
: [];
|
|
112
|
+
const byName = new Map(rows.map(r => [r.migration, r.batch]));
|
|
113
|
+
return migrations.map(m => ({
|
|
114
|
+
name: m.name,
|
|
115
|
+
ran: byName.has(m.name),
|
|
116
|
+
batch: byName.get(m.name) ?? null,
|
|
117
|
+
}));
|
|
118
|
+
}
|
|
119
|
+
/** The highest recorded batch number, or `0` when nothing has been applied. */
|
|
120
|
+
async lastBatch() {
|
|
121
|
+
if (!(await this.installed()))
|
|
122
|
+
return 0;
|
|
123
|
+
const rows = await this.adapter.query(TABLE).get();
|
|
124
|
+
return rows.reduce((max, r) => Math.max(max, r.batch), 0);
|
|
125
|
+
}
|
|
126
|
+
/** Rows recorded in `batch`, newest-applied first (id DESC) — i.e. the order
|
|
127
|
+
* their `down()` methods should run to unwind the batch. */
|
|
128
|
+
migrationsInBatch(batch) {
|
|
129
|
+
return this.adapter.query(TABLE).where('batch', batch).orderBy('id', 'DESC').get();
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Revert the LAST batch: run each of its migrations' `down()` in reverse apply
|
|
133
|
+
* order (id DESC) and delete their `migrations` rows. The whole batch runs in
|
|
134
|
+
* one transaction, so a `down()` that throws leaves the state table untouched.
|
|
135
|
+
* Returns the rolled-back batch number and the reverted names. A no-op (batch
|
|
136
|
+
* `0`) when nothing has been applied.
|
|
137
|
+
*/
|
|
138
|
+
async rollback(migrations, onRevert, opts = {}) {
|
|
139
|
+
// --batch=N: revert every migration recorded in that batch.
|
|
140
|
+
if (opts.batch !== undefined) {
|
|
141
|
+
const rows = await this.migrationsInBatch(opts.batch);
|
|
142
|
+
const reverted = await this.revertRows(rows, migrations, onRevert);
|
|
143
|
+
return { batch: opts.batch, reverted };
|
|
144
|
+
}
|
|
145
|
+
// --step=N: revert the last N applied migrations, across batches, newest first.
|
|
146
|
+
if (opts.step !== undefined && opts.step > 0) {
|
|
147
|
+
if (!(await this.installed()))
|
|
148
|
+
return { batch: 0, reverted: [] };
|
|
149
|
+
const all = await this.adapter.query(TABLE).orderBy('id', 'DESC').get();
|
|
150
|
+
const rows = all.slice(0, opts.step);
|
|
151
|
+
const batch = rows.reduce((max, r) => Math.max(max, r.batch), 0);
|
|
152
|
+
const reverted = await this.revertRows(rows, migrations, onRevert);
|
|
153
|
+
return { batch, reverted };
|
|
154
|
+
}
|
|
155
|
+
// Default: revert the last batch.
|
|
156
|
+
const batch = await this.lastBatch();
|
|
157
|
+
if (batch === 0)
|
|
158
|
+
return { batch: 0, reverted: [] };
|
|
159
|
+
const rows = await this.migrationsInBatch(batch);
|
|
160
|
+
const reverted = await this.revertRows(rows, migrations, onRevert);
|
|
161
|
+
return { batch, reverted };
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Revert EVERY applied migration (all batches), `down()` in full reverse order
|
|
165
|
+
* (id DESC across batches), in a single transaction. Used by `migrate:refresh`.
|
|
166
|
+
* Returns the reverted names.
|
|
167
|
+
*/
|
|
168
|
+
async rollbackAll(migrations) {
|
|
169
|
+
if (!(await this.installed()))
|
|
170
|
+
return [];
|
|
171
|
+
const rows = await this.adapter.query(TABLE).orderBy('id', 'DESC').get();
|
|
172
|
+
return this.revertRows(rows, migrations);
|
|
173
|
+
}
|
|
174
|
+
/** Shared unwind path for `rollback` / `rollbackAll`: run `down()` for each row
|
|
175
|
+
* (already in reverse order) inside one transaction and delete its state row. */
|
|
176
|
+
async revertRows(rows, migrations, onRevert) {
|
|
177
|
+
if (rows.length === 0)
|
|
178
|
+
return [];
|
|
179
|
+
const byName = new Map(migrations.map(m => [m.name, m.migration]));
|
|
180
|
+
const reverted = [];
|
|
181
|
+
await this.adapter.transaction(async (txAdapter) => {
|
|
182
|
+
const tx = txAdapter;
|
|
183
|
+
const schema = tx.schemaBuilder();
|
|
184
|
+
for (const row of rows) {
|
|
185
|
+
const migration = byName.get(row.migration);
|
|
186
|
+
if (!migration) {
|
|
187
|
+
throw new NativeOrmError('NATIVE_BAD_MIGRATION', `[RudderJS ORM native] Cannot roll back "${row.migration}": its migration file ` +
|
|
188
|
+
`was not found in database/migrations. Restore the file or remove its row from the migrations table.`);
|
|
189
|
+
}
|
|
190
|
+
await withSchema(schema, () => migration.down());
|
|
191
|
+
await tx.query(TABLE).delete(row.id);
|
|
192
|
+
reverted.push(row.migration);
|
|
193
|
+
onRevert?.(row.migration);
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
return reverted;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Drop every user table (used by `migrate:fresh`) — including the
|
|
200
|
+
* `migrations` state table, so the next `run()` rebuilds from a clean slate.
|
|
201
|
+
* Delegates to the dialect-aware {@link SchemaBuilder.dropAllTables}
|
|
202
|
+
* (`information_schema` + FK-safe drops on pg/mysql, `sqlite_master` on
|
|
203
|
+
* sqlite). Historically this read the SQLite catalog directly, which made
|
|
204
|
+
* `migrate:fresh` throw on pg/mysql native connections.
|
|
205
|
+
*/
|
|
206
|
+
async dropAllTables() {
|
|
207
|
+
await this.schema.dropAllTables();
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Load migration files from a directory: each `*.{ts,js,mts,mjs}` whose default
|
|
212
|
+
* export is a {@link Migration} subclass, instantiated and sorted by filename
|
|
213
|
+
* (timestamp-prefixed names sort chronologically). A file without a valid
|
|
214
|
+
* default export throws — a silently-skipped migration is worse than a loud one.
|
|
215
|
+
*/
|
|
216
|
+
export async function discoverMigrations(dir) {
|
|
217
|
+
let entries;
|
|
218
|
+
try {
|
|
219
|
+
entries = readdirSync(dir);
|
|
220
|
+
}
|
|
221
|
+
catch {
|
|
222
|
+
return []; // no migrations directory yet
|
|
223
|
+
}
|
|
224
|
+
const files = entries
|
|
225
|
+
.filter(f => /\.(ts|js|mts|mjs)$/.test(f) && !/\.d\.ts$/.test(f))
|
|
226
|
+
.sort();
|
|
227
|
+
const loaded = [];
|
|
228
|
+
for (const file of files) {
|
|
229
|
+
const name = file.replace(/\.(ts|js|mts|mjs)$/, '');
|
|
230
|
+
const mod = await import(pathToFileURL(join(dir, file)).href);
|
|
231
|
+
const Cls = mod.default;
|
|
232
|
+
if (typeof Cls !== 'function' || !(Cls.prototype instanceof Migration)) {
|
|
233
|
+
throw new NativeOrmError('NATIVE_BAD_MIGRATION', `[RudderJS ORM native] ${file} must default-export a class extending Migration.`);
|
|
234
|
+
}
|
|
235
|
+
loaded.push({ name, migration: new Cls() });
|
|
236
|
+
}
|
|
237
|
+
return loaded;
|
|
238
|
+
}
|
|
239
|
+
//# sourceMappingURL=migrator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrator.js","sourceRoot":"","sources":["../../../src/native/schema/migrator.ts"],"names":[],"mappings":"AAAA,8DAA8D;AAC9D,EAAE;AACF,+EAA+E;AAC/E,0EAA0E;AAC1E,+EAA+E;AAC/E,+EAA+E;AAC/E,4EAA4E;AAC5E,EAAE;AACF,qEAAqE;AACrE,0EAA0E;AAC1E,6EAA6E;AAC7E,yDAAyD;AAEzD,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AACrC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAChC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAExC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAA;AAE/C,OAAO,EAAE,cAAc,EAAE,MAAM,cAA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'../dialect.js';
|
|
3
|
+
import type { AlterBlueprint } from './alter-blueprint.js';
|
|
4
|
+
/**
|
|
5
|
+
* Rebuild `blueprint.table` applying its `change()` column definitions, via the
|
|
6
|
+
* SQLite shadow-table dance. Assumes `change()` is the only op in the call (the
|
|
7
|
+
* caller routes here only when a change is present; this re-checks and throws on
|
|
8
|
+
* a mixed call).
|
|
9
|
+
*/
|
|
10
|
+
export declare function rebuildTable(executor: Executor, dialect: Dialect, blueprint: AlterBlueprint): Promise<void>;
|
|
11
|
+
//# sourceMappingURL=rebuild.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rebuild.d.ts","sourceRoot":"","sources":["../../../src/native/schema/rebuild.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AAC5C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAE5C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAiB1D;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,QAAQ,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CA4DjH"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// ─── SQLite table rebuild (Schema.table column change()) ───
|
|
2
|
+
//
|
|
3
|
+
// Node-capable (runs via an {@link Executor}). SQLite can't change a column's
|
|
4
|
+
// type/constraints in place, so `t.<type>('col').change()` triggers the
|
|
5
|
+
// canonical 12-step rebuild: create a shadow table with the new column set, copy
|
|
6
|
+
// the data across, drop the original, rename the shadow into its place, then
|
|
7
|
+
// recreate the user indexes. This is the single biggest piece of `Schema.table`
|
|
8
|
+
// (parent plan Part 4) and the reason it's its own phase (7.4b).
|
|
9
|
+
//
|
|
10
|
+
// v1 scope — **`change()` must be the only operation in its `Schema.table` call.**
|
|
11
|
+
// Mixing it with add/drop/rename/index in one call throws; split those into a
|
|
12
|
+
// separate `table()` (the native-ALTER path handles them). That keeps the shadow
|
|
13
|
+
// reconstruction faithful: every non-changed column and the primary key are
|
|
14
|
+
// preserved exactly, only the changed columns' definitions are swapped, and all
|
|
15
|
+
// rows copy 1:1. Changing a primary-key column is rejected (the PK is preserved
|
|
16
|
+
// verbatim). Atomicity comes from the migrator's per-batch transaction (7.5);
|
|
17
|
+
// run standalone, the steps execute sequentially.
|
|
18
|
+
import { NativeOrmError } from '../errors.js';
|
|
19
|
+
import { compileColumnSpec } from './ddl-compiler.js';
|
|
20
|
+
import { readColumns, readIndexSql, isAutoincrement } from './introspect.js';
|
|
21
|
+
/** Render one preserved (introspected) column to its shadow-table spec. The
|
|
22
|
+
* single INTEGER rowid primary key is re-emitted inline (with AUTOINCREMENT
|
|
23
|
+
* when the original used it); everything else keeps its type/null/default. */
|
|
24
|
+
function preservedColumnSpec(col, dialect, singleIntPk, auto) {
|
|
25
|
+
if (singleIntPk && col.pk === 1) {
|
|
26
|
+
return `${dialect.quoteId(col.name)} INTEGER PRIMARY KEY${auto ? ' AUTOINCREMENT' : ''}`;
|
|
27
|
+
}
|
|
28
|
+
const parts = [dialect.quoteId(col.name), col.type || 'TEXT'];
|
|
29
|
+
if (col.notNull)
|
|
30
|
+
parts.push('NOT NULL');
|
|
31
|
+
if (col.dflt !== null)
|
|
32
|
+
parts.push(`DEFAULT ${col.dflt}`);
|
|
33
|
+
return parts.join(' ');
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Rebuild `blueprint.table` applying its `change()` column definitions, via the
|
|
37
|
+
* SQLite shadow-table dance. Assumes `change()` is the only op in the call (the
|
|
38
|
+
* caller routes here only when a change is present; this re-checks and throws on
|
|
39
|
+
* a mixed call).
|
|
40
|
+
*/
|
|
41
|
+
export async function rebuildTable(executor, dialect, blueprint) {
|
|
42
|
+
const table = blueprint.table;
|
|
43
|
+
const changes = blueprint.columns.filter(c => c.change);
|
|
44
|
+
const others = blueprint.columns.filter(c => !c.change);
|
|
45
|
+
if (others.length || blueprint.droppedColumns.length || blueprint.renamedColumns.length || blueprint.indexes.length || blueprint.droppedIndexes.length) {
|
|
46
|
+
throw new NativeOrmError('NATIVE_DDL_CHANGE_COMBINED', `[RudderJS ORM native] change() must be the only operation in a Schema.table('${table}', …) call (v1). ` +
|
|
47
|
+
`Move adds / drops / renames / index changes into a separate table() call.`);
|
|
48
|
+
}
|
|
49
|
+
const current = await readColumns(executor, dialect, table);
|
|
50
|
+
if (current.length === 0) {
|
|
51
|
+
throw new NativeOrmError('NATIVE_DDL_NO_TABLE', `[RudderJS ORM native] Cannot alter "${table}" — it has no columns or does not exist.`);
|
|
52
|
+
}
|
|
53
|
+
const byName = new Map(current.map(c => [c.name, c]));
|
|
54
|
+
const changeByName = new Map();
|
|
55
|
+
for (const def of changes) {
|
|
56
|
+
const existing = byName.get(def.name);
|
|
57
|
+
if (!existing) {
|
|
58
|
+
throw new NativeOrmError('NATIVE_DDL_CHANGE_MISSING', `[RudderJS ORM native] Cannot change column "${def.name}" — no such column on "${table}".`);
|
|
59
|
+
}
|
|
60
|
+
if (existing.pk > 0) {
|
|
61
|
+
throw new NativeOrmError('NATIVE_DDL_CHANGE_PK', `[RudderJS ORM native] Changing a primary-key column ("${def.name}") is not supported.`);
|
|
62
|
+
}
|
|
63
|
+
if (def.primary || def.autoIncrement) {
|
|
64
|
+
throw new NativeOrmError('NATIVE_DDL_CHANGE_TO_PK', `[RudderJS ORM native] change() cannot turn "${def.name}" into a primary key.`);
|
|
65
|
+
}
|
|
66
|
+
changeByName.set(def.name, def);
|
|
67
|
+
}
|
|
68
|
+
const auto = await isAutoincrement(executor, table);
|
|
69
|
+
const indexSqls = await readIndexSql(executor, table);
|
|
70
|
+
const pkCols = current.filter(c => c.pk > 0).sort((a, b) => a.pk - b.pk);
|
|
71
|
+
const singleIntPk = pkCols.length === 1 && (pkCols[0]?.type.toUpperCase() === 'INTEGER');
|
|
72
|
+
// New column set: same order, changed columns swapped for their new spec.
|
|
73
|
+
const colLines = current.map((col) => {
|
|
74
|
+
const changed = changeByName.get(col.name);
|
|
75
|
+
return changed ? compileColumnSpec(changed, dialect) : preservedColumnSpec(col, dialect, singleIntPk, auto);
|
|
76
|
+
});
|
|
77
|
+
if (pkCols.length && !singleIntPk) {
|
|
78
|
+
colLines.push(`PRIMARY KEY (${pkCols.map(c => dialect.quoteId(c.name)).join(', ')})`);
|
|
79
|
+
}
|
|
80
|
+
const shadow = `__rudder_new_${table}`;
|
|
81
|
+
const colNames = current.map(c => dialect.quoteId(c.name)).join(', ');
|
|
82
|
+
// The rebuild dance. (No renames/drops in v1, so the shadow has the same
|
|
83
|
+
// column names as the original — a straight 1:1 copy.)
|
|
84
|
+
await executor.execute(`CREATE TABLE ${dialect.quoteId(shadow)} (\n ${colLines.join(',\n ')}\n)`, []);
|
|
85
|
+
await executor.execute(`INSERT INTO ${dialect.quoteId(shadow)} (${colNames}) SELECT ${colNames} FROM ${dialect.quoteId(table)}`, []);
|
|
86
|
+
await executor.execute(`DROP TABLE ${dialect.quoteId(table)}`, []);
|
|
87
|
+
await executor.execute(`ALTER TABLE ${dialect.quoteId(shadow)} RENAME TO ${dialect.quoteId(table)}`, []);
|
|
88
|
+
for (const sql of indexSqls) {
|
|
89
|
+
await executor.execute(sql, []);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=rebuild.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rebuild.js","sourceRoot":"","sources":["../../../src/native/schema/rebuild.ts"],"names":[],"mappings":"AAAA,8DAA8D;AAC9D,EAAE;AACF,8EAA8E;AAC9E,wEAAwE;AACxE,iFAAiF;AACjF,6EAA6E;AAC7E,gFAAgF;AAChF,iEAAiE;AACjE,EAAE;AACF,mFAAmF;AACnF,8EAA8E;AAC9E,iFAAiF;AACjF,4EAA4E;AAC5E,gFAAgF;AAChF,gFAAgF;AAChF,8EAA8E;AAC9E,kDAAk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from './alter-blueprint.js';
|
|
5
|
+
export declare class SchemaBuilder {
|
|
6
|
+
private readonly executor;
|
|
7
|
+
private readonly dialect;
|
|
8
|
+
constructor(executor: Executor, dialect: Dialect);
|
|
9
|
+
/** `CREATE TABLE` (+ any indexes) from a `Blueprint` callback. */
|
|
10
|
+
create(table: string, build: (table: Blueprint) => void): Promise<void>;
|
|
11
|
+
/** `ALTER TABLE` — add/drop/rename/change columns + add/drop indexes and
|
|
12
|
+
* foreign keys via a callback. A `.change()` (column type/constraint
|
|
13
|
+
* change) is a native `ALTER COLUMN` / `MODIFY` on pg/mysql (7.4b); SQLite
|
|
14
|
+
* can't change a column in place, so it routes through the table-rebuild
|
|
15
|
+
* dance instead (which requires `change()` to be the only op in the call). */
|
|
16
|
+
table(table: string, build: (table: AlterBlueprint) => void): Promise<void>;
|
|
17
|
+
/** Rename a table. */
|
|
18
|
+
rename(from: string, to: string): Promise<void>;
|
|
19
|
+
/** `DROP TABLE`. */
|
|
20
|
+
drop(table: string): Promise<void>;
|
|
21
|
+
/** `DROP TABLE IF EXISTS`. */
|
|
22
|
+
dropIfExists(table: string): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Names of every user table (catalog lookup) — includes the `migrations`
|
|
25
|
+
* state table, excludes the engine's internal tables (`sqlite_*`).
|
|
26
|
+
* `information_schema` on pg/mysql, `sqlite_master` on sqlite.
|
|
27
|
+
*/
|
|
28
|
+
allTables(): Promise<string[]>;
|
|
29
|
+
/**
|
|
30
|
+
* Drop every user table — the `migrate:fresh` sweep (Laravel's
|
|
31
|
+
* `Schema::dropAllTables()` analog). Dialect-aware so foreign keys can't
|
|
32
|
+
* block it: pg drops with `CASCADE`, mysql disables `FOREIGN_KEY_CHECKS`
|
|
33
|
+
* for the batch (restored after, even on failure), sqlite drops in catalog
|
|
34
|
+
* order exactly as before.
|
|
35
|
+
*/
|
|
36
|
+
dropAllTables(): Promise<void>;
|
|
37
|
+
/** Whether `table` exists (catalog lookup). */
|
|
38
|
+
hasTable(table: string): Promise<boolean>;
|
|
39
|
+
/** Whether `table` has a `column` (catalog lookup). */
|
|
40
|
+
hasColumn(table: string, column: string): Promise<boolean>;
|
|
41
|
+
/** The SQL expression naming the active schema/database for dialects that
|
|
42
|
+
* introspect via `information_schema`, or `null` for the sqlite path. */
|
|
43
|
+
private currentSchemaSql;
|
|
44
|
+
private requireSqlite;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=schema-builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema-builder.d.ts","sourceRoot":"","sources":["../../../src/native/schema/schema-builder.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AAC5C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAE5C,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAIrD,qBAAa,aAAa;IAEtB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,OAAO;gBADP,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAG,OAAO;IAGpC,kEAAkE;IAC5D,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,KAAK,EAAE,SAAS,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAQ7E;;;;mFAI+E;IACzE,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,KAAK,EAAE,cAAc,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAYjF,sBAAsB;IAChB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAKrD,oBAAoB;IACd,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;I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