@fragno-dev/db 0.1.3 → 0.1.6
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/.turbo/turbo-build.log +43 -30
- package/CHANGELOG.md +19 -0
- package/dist/adapters/adapters.js +1 -0
- package/dist/adapters/drizzle/drizzle-adapter.d.ts +1 -1
- package/dist/adapters/drizzle/drizzle-adapter.d.ts.map +1 -1
- package/dist/adapters/drizzle/drizzle-adapter.js +9 -2
- package/dist/adapters/drizzle/drizzle-adapter.js.map +1 -1
- package/dist/adapters/drizzle/generate.js +1 -1
- package/dist/adapters/drizzle/generate.js.map +1 -1
- package/dist/adapters/kysely/kysely-adapter.d.ts +1 -1
- package/dist/adapters/kysely/kysely-adapter.d.ts.map +1 -1
- package/dist/adapters/kysely/kysely-adapter.js +47 -27
- package/dist/adapters/kysely/kysely-adapter.js.map +1 -1
- package/dist/adapters/kysely/kysely-query-compiler.js +2 -2
- package/dist/adapters/kysely/kysely-query-compiler.js.map +1 -1
- package/dist/adapters/kysely/kysely-query.js +2 -1
- package/dist/adapters/kysely/kysely-query.js.map +1 -1
- package/dist/adapters/kysely/kysely-uow-compiler.js +2 -2
- package/dist/adapters/kysely/kysely-uow-compiler.js.map +1 -1
- package/dist/adapters/kysely/migration/execute-base.js +128 -0
- package/dist/adapters/kysely/migration/execute-base.js.map +1 -0
- package/dist/adapters/kysely/migration/execute-factory.js +28 -0
- package/dist/adapters/kysely/migration/execute-factory.js.map +1 -0
- package/dist/adapters/kysely/migration/execute-mssql.js +112 -0
- package/dist/adapters/kysely/migration/execute-mssql.js.map +1 -0
- package/dist/adapters/kysely/migration/execute-mysql.js +93 -0
- package/dist/adapters/kysely/migration/execute-mysql.js.map +1 -0
- package/dist/adapters/kysely/migration/execute-postgres.js +104 -0
- package/dist/adapters/kysely/migration/execute-postgres.js.map +1 -0
- package/dist/adapters/kysely/migration/execute-sqlite.js +123 -0
- package/dist/adapters/kysely/migration/execute-sqlite.js.map +1 -0
- package/dist/adapters/kysely/migration/execute.js +23 -168
- package/dist/adapters/kysely/migration/execute.js.map +1 -1
- package/dist/migration-engine/shared.d.ts +24 -5
- package/dist/migration-engine/shared.d.ts.map +1 -1
- package/dist/migration-engine/shared.js.map +1 -1
- package/dist/query/query.d.ts +4 -4
- package/dist/query/query.d.ts.map +1 -1
- package/dist/query/unit-of-work.d.ts +22 -22
- package/dist/query/unit-of-work.d.ts.map +1 -1
- package/dist/schema/create.d.ts +41 -41
- package/dist/schema/create.d.ts.map +1 -1
- package/package.json +7 -2
- package/src/adapters/drizzle/drizzle-adapter-pglite.test.ts +1 -1
- package/src/adapters/drizzle/drizzle-adapter.ts +13 -3
- package/src/adapters/drizzle/generate.test.ts +97 -0
- package/src/adapters/drizzle/generate.ts +3 -2
- package/src/adapters/kysely/kysely-adapter.ts +64 -35
- package/src/adapters/kysely/kysely-query-compiler.ts +3 -1
- package/src/adapters/kysely/kysely-query.ts +3 -1
- package/src/adapters/kysely/kysely-uow-compiler.ts +4 -2
- package/src/adapters/kysely/migration/execute-base.ts +256 -0
- package/src/adapters/kysely/migration/execute-factory.ts +32 -0
- package/src/adapters/kysely/migration/execute-mssql.ts +250 -0
- package/src/adapters/kysely/migration/execute-mysql.ts +211 -0
- package/src/adapters/kysely/migration/execute-postgres.ts +234 -0
- package/src/adapters/kysely/migration/execute-sqlite.test.ts +1363 -0
- package/src/adapters/kysely/migration/execute-sqlite.ts +247 -0
- package/src/adapters/kysely/migration/execute.ts +33 -396
- package/src/adapters/kysely/migration/kysely-migrator.test.ts +84 -2
- package/src/migration-engine/shared.ts +29 -11
- package/tsdown.config.ts +1 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Kysely, PostgresDialect } from "kysely";
|
|
1
|
+
import { Kysely, PostgresDialect, SqliteDialect } from "kysely";
|
|
2
2
|
import { describe, expect, beforeAll, test } from "vitest";
|
|
3
3
|
import { KyselyAdapter } from "../kysely-adapter";
|
|
4
|
-
import { column, idColumn, schema } from "../../../schema/create";
|
|
4
|
+
import { column, idColumn, referenceColumn, schema } from "../../../schema/create";
|
|
5
5
|
|
|
6
6
|
describe("KyselyMigrator", () => {
|
|
7
7
|
const testSchema = schema((s) => {
|
|
@@ -177,3 +177,85 @@ describe("KyselyMigrator", () => {
|
|
|
177
177
|
).rejects.toThrow("schema only has version 4");
|
|
178
178
|
});
|
|
179
179
|
});
|
|
180
|
+
|
|
181
|
+
describe("KyselyMigrator - SQLite Foreign Key Merging", () => {
|
|
182
|
+
// Test the user's exact example schema
|
|
183
|
+
const userExampleSchema = schema((s) => {
|
|
184
|
+
return s
|
|
185
|
+
.addTable("users", (t) => {
|
|
186
|
+
return t.addColumn("id", idColumn());
|
|
187
|
+
})
|
|
188
|
+
.addTable("posts", (t) => {
|
|
189
|
+
return t.addColumn("id", idColumn()).addColumn("authorId", referenceColumn());
|
|
190
|
+
})
|
|
191
|
+
.addReference("author", {
|
|
192
|
+
type: "one",
|
|
193
|
+
from: { table: "posts", column: "authorId" },
|
|
194
|
+
to: { table: "users", column: "id" },
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
199
|
+
let db: Kysely<any>;
|
|
200
|
+
let adapter: KyselyAdapter;
|
|
201
|
+
|
|
202
|
+
beforeAll(async () => {
|
|
203
|
+
// Create a Kysely instance with SQLite dialect
|
|
204
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
205
|
+
db = new Kysely({ dialect: new SqliteDialect({} as any) });
|
|
206
|
+
adapter = new KyselyAdapter({ db, provider: "sqlite" });
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test("SQLite should merge foreign keys into create-table operations", async () => {
|
|
210
|
+
const migrator = adapter.createMigrationEngine(userExampleSchema, "test");
|
|
211
|
+
|
|
212
|
+
// Migrate from 0 -> 3 (all tables + FK in one batch)
|
|
213
|
+
const preparedMigration = await migrator.prepareMigrationTo(3, {
|
|
214
|
+
updateSettings: true,
|
|
215
|
+
fromVersion: 0,
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
expect(preparedMigration.operations.length).toBeGreaterThan(0);
|
|
219
|
+
const sql = preparedMigration.getSQL?.();
|
|
220
|
+
expect(sql).toBeDefined();
|
|
221
|
+
|
|
222
|
+
// The SQL should have PRAGMA defer_foreign_keys
|
|
223
|
+
expect(sql).toContain("PRAGMA defer_foreign_keys = ON");
|
|
224
|
+
|
|
225
|
+
// Should create users table
|
|
226
|
+
expect(sql).toContain('create table "users_test"');
|
|
227
|
+
|
|
228
|
+
// Should create posts table WITH inline foreign key constraint
|
|
229
|
+
expect(sql).toContain('create table "posts_test"');
|
|
230
|
+
expect(sql).toContain("foreign key");
|
|
231
|
+
expect(sql).toContain("authorId");
|
|
232
|
+
expect(sql).toContain('references "users_test"');
|
|
233
|
+
|
|
234
|
+
// Should NOT have a separate ALTER TABLE ADD FOREIGN KEY
|
|
235
|
+
// (SQLite doesn't support it, and we've merged it into create-table)
|
|
236
|
+
expect(sql).not.toMatch(/alter table.*add.*foreign key/i);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
test("SQLite FK merging full schema verification", async () => {
|
|
240
|
+
const migrator = adapter.createMigrationEngine(userExampleSchema, "test");
|
|
241
|
+
|
|
242
|
+
const preparedMigration = await migrator.prepareMigrationTo(3, {
|
|
243
|
+
updateSettings: true,
|
|
244
|
+
fromVersion: 0,
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
const sql = preparedMigration.getSQL?.();
|
|
248
|
+
expect(sql).toBeDefined();
|
|
249
|
+
|
|
250
|
+
// Verify the complete SQL snapshot
|
|
251
|
+
expect(sql).toMatchInlineSnapshot(`
|
|
252
|
+
"PRAGMA defer_foreign_keys = ON;
|
|
253
|
+
|
|
254
|
+
create table "users_test" ("id" text not null unique, "_internalId" integer not null primary key autoincrement, "_version" integer default 0 not null);
|
|
255
|
+
|
|
256
|
+
create table "posts_test" ("id" text not null unique, "authorId" integer not null, "_internalId" integer not null primary key autoincrement, "_version" integer default 0 not null, constraint "posts_users_author_fk" foreign key ("authorId") references "users_test" ("_internalId") on delete restrict on update restrict);
|
|
257
|
+
|
|
258
|
+
insert into "fragno_db_settings" ("id", "key", "value") values ('BflimUWc1NbCMMDD9SM3gQ', 'test.schema_version', '3');"
|
|
259
|
+
`);
|
|
260
|
+
});
|
|
261
|
+
});
|
|
@@ -5,6 +5,22 @@ export interface ForeignKeyInfo {
|
|
|
5
5
|
referencedColumns: string[];
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Provider-specific metadata that can be attached to operations during preprocessing.
|
|
10
|
+
* This allows providers to add additional context without polluting the core operation types.
|
|
11
|
+
*/
|
|
12
|
+
export interface MigrationOperationMetadata {
|
|
13
|
+
[key: string]: unknown;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* SQLite-specific metadata for create-table operations.
|
|
18
|
+
* Includes foreign keys that should be created inline with the table.
|
|
19
|
+
*/
|
|
20
|
+
export interface SqliteCreateTableMetadata extends MigrationOperationMetadata {
|
|
21
|
+
inlineForeignKeys?: ForeignKeyInfo[];
|
|
22
|
+
}
|
|
23
|
+
|
|
8
24
|
export interface ColumnInfo {
|
|
9
25
|
name: string;
|
|
10
26
|
type:
|
|
@@ -23,33 +39,35 @@ export interface ColumnInfo {
|
|
|
23
39
|
default?: { value: unknown } | { dbSpecial: "now" } | { runtime: "cuid" | "now" };
|
|
24
40
|
}
|
|
25
41
|
|
|
26
|
-
export type MigrationOperation
|
|
27
|
-
|
|
28
|
-
|
|
42
|
+
export type MigrationOperation<
|
|
43
|
+
TMeta extends MigrationOperationMetadata = MigrationOperationMetadata,
|
|
44
|
+
> =
|
|
45
|
+
| (TableOperation & { metadata?: TMeta })
|
|
46
|
+
| ({
|
|
29
47
|
// warning: not supported by SQLite
|
|
30
48
|
type: "add-foreign-key";
|
|
31
49
|
table: string;
|
|
32
50
|
value: ForeignKeyInfo;
|
|
33
|
-
}
|
|
34
|
-
| {
|
|
51
|
+
} & { metadata?: TMeta })
|
|
52
|
+
| ({
|
|
35
53
|
// warning: not supported by SQLite
|
|
36
54
|
type: "drop-foreign-key";
|
|
37
55
|
table: string;
|
|
38
56
|
name: string;
|
|
39
|
-
}
|
|
40
|
-
| {
|
|
57
|
+
} & { metadata?: TMeta })
|
|
58
|
+
| ({
|
|
41
59
|
type: "drop-index";
|
|
42
60
|
table: string;
|
|
43
61
|
name: string;
|
|
44
|
-
}
|
|
45
|
-
| {
|
|
62
|
+
} & { metadata?: TMeta })
|
|
63
|
+
| ({
|
|
46
64
|
type: "add-index";
|
|
47
65
|
table: string;
|
|
48
66
|
columns: string[];
|
|
49
67
|
name: string;
|
|
50
68
|
unique: boolean;
|
|
51
|
-
}
|
|
52
|
-
| CustomOperation;
|
|
69
|
+
} & { metadata?: TMeta })
|
|
70
|
+
| (CustomOperation & { metadata?: TMeta });
|
|
53
71
|
|
|
54
72
|
export type CustomOperation = {
|
|
55
73
|
type: "custom";
|
package/tsdown.config.ts
CHANGED