@guren/orm 2.13.0 → 2.13.1
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/dist/index.js +28 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -3610,6 +3610,22 @@ function assertNotConnectionUri(dbPath, source) {
|
|
|
3610
3610
|
if (!CONNECTION_URI.test(dbPath)) return;
|
|
3611
3611
|
throw new Error(`createSqliteDatabase() received a connection URI where it expects a file path: ${dbPath} (from ${source}). Left alone it would be created as a directory tree and migrated into silently. Pass a path such as "./data/guren.db", or ":memory:".`);
|
|
3612
3612
|
}
|
|
3613
|
+
/**
|
|
3614
|
+
* Migrations run with enforcement off, so a rebuild's copy step or a data
|
|
3615
|
+
* migration can leave rows whose parent is gone. drizzle owns the COMMIT, so
|
|
3616
|
+
* this can only report them afterwards, as SQLite's own ALTER procedure would.
|
|
3617
|
+
*/
|
|
3618
|
+
function reportForeignKeyViolations(handle) {
|
|
3619
|
+
const rows = handle.all(sql.raw("PRAGMA foreign_key_check;"));
|
|
3620
|
+
if (rows.length === 0) return;
|
|
3621
|
+
const counts = /* @__PURE__ */ new Map();
|
|
3622
|
+
for (const { table, parent } of rows) {
|
|
3623
|
+
const key = `${table} -> ${parent}`;
|
|
3624
|
+
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
3625
|
+
}
|
|
3626
|
+
const summary = [...counts].map(([key, count]) => `${key} (${count})`).join(", ");
|
|
3627
|
+
console.warn(`[guren/orm] The migrations just applied left ${rows.length} row(s) whose foreign key names no parent row: ${summary}.\n[guren/orm] Foreign keys are not enforced while migrations run. \`PRAGMA foreign_key_check\` lists the rows.`);
|
|
3628
|
+
}
|
|
3613
3629
|
function createSqliteDatabase(options) {
|
|
3614
3630
|
const { migrationsFolder, filename, seedersFolder, relations } = options;
|
|
3615
3631
|
const resolvedMigrationsFolder = migrationsFolder instanceof URL ? fileURLToPath(migrationsFolder) : resolve(String(migrationsFolder));
|
|
@@ -3646,6 +3662,7 @@ function createSqliteDatabase(options) {
|
|
|
3646
3662
|
const { drizzle } = await import("drizzle-orm/bun-sqlite");
|
|
3647
3663
|
const sqlite = new Database(target.kind === "memory" ? ":memory:" : dbFile ?? dbPath);
|
|
3648
3664
|
sqlite.exec("PRAGMA journal_mode = WAL;");
|
|
3665
|
+
sqlite.exec("PRAGMA foreign_keys = ON;");
|
|
3649
3666
|
sqliteClient = sqlite;
|
|
3650
3667
|
const handle = drizzle({
|
|
3651
3668
|
client: sqlite,
|
|
@@ -3690,7 +3707,17 @@ function createSqliteDatabase(options) {
|
|
|
3690
3707
|
const { migrate } = await import("drizzle-orm/bun-sqlite/migrator");
|
|
3691
3708
|
await migrateAndReport(resolvedMigrationsFolder, {
|
|
3692
3709
|
readApplied: report ? readAppliedMigrations : void 0,
|
|
3693
|
-
migrate: () =>
|
|
3710
|
+
migrate: () => {
|
|
3711
|
+
const handle = db;
|
|
3712
|
+
const appliedBefore = readAppliedMigrations().length;
|
|
3713
|
+
handle.run(sql.raw("PRAGMA foreign_keys = OFF;"));
|
|
3714
|
+
try {
|
|
3715
|
+
migrate(db, { migrationsFolder: resolvedMigrationsFolder });
|
|
3716
|
+
} finally {
|
|
3717
|
+
handle.run(sql.raw("PRAGMA foreign_keys = ON;"));
|
|
3718
|
+
}
|
|
3719
|
+
if (readAppliedMigrations().length > appliedBefore) reportForeignKeyViolations(handle);
|
|
3720
|
+
}
|
|
3694
3721
|
});
|
|
3695
3722
|
return summary;
|
|
3696
3723
|
} catch (error) {
|