@guren/orm 2.9.0 → 2.10.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/dist/index.d.ts +5 -0
- package/dist/index.js +74 -28
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -961,6 +961,11 @@ interface MigrationStatusEntry {
|
|
|
961
961
|
name: string;
|
|
962
962
|
applied: boolean;
|
|
963
963
|
appliedAt: Date | null;
|
|
964
|
+
/**
|
|
965
|
+
* True for a tracker row no local folder carries: applied to this database,
|
|
966
|
+
* then deleted from disk (or migrated by another checkout). Absent otherwise.
|
|
967
|
+
*/
|
|
968
|
+
orphaned?: boolean;
|
|
964
969
|
}
|
|
965
970
|
//#endregion
|
|
966
971
|
//#region src/seeder.d.ts
|
package/dist/index.js
CHANGED
|
@@ -2550,40 +2550,82 @@ function listLocalMigrations(migrationsFolder) {
|
|
|
2550
2550
|
if (!existsSync(migrationsFolder)) return [];
|
|
2551
2551
|
return readdirSync(migrationsFolder, { withFileTypes: true }).filter((entry) => entry.isDirectory() && existsSync(resolve(migrationsFolder, entry.name, "migration.sql"))).map((entry) => ({ name: entry.name })).sort((a, b) => a.name.localeCompare(b.name));
|
|
2552
2552
|
}
|
|
2553
|
+
function parseAppliedAt(row) {
|
|
2554
|
+
if (!row?.appliedAt) return null;
|
|
2555
|
+
const parsed = row.appliedAt instanceof Date ? row.appliedAt : new Date(row.appliedAt);
|
|
2556
|
+
return Number.isNaN(parsed.getTime()) ? null : parsed;
|
|
2557
|
+
}
|
|
2553
2558
|
/**
|
|
2554
2559
|
* Joins local migration folders with the migrator's tracker rows. Drizzle
|
|
2555
2560
|
* decides pending migrations by name membership, so status uses that rule.
|
|
2561
|
+
* The same rule makes the migrator skip a row with no folder, whatever that
|
|
2562
|
+
* row's migration created, so those rows come back too, marked `orphaned`.
|
|
2556
2563
|
*/
|
|
2557
2564
|
function buildMigrationStatus(localMigrations, appliedRows) {
|
|
2558
|
-
const
|
|
2559
|
-
for (const row of appliedRows) if (row.name)
|
|
2560
|
-
|
|
2561
|
-
const row =
|
|
2562
|
-
|
|
2563
|
-
if (row?.appliedAt) {
|
|
2564
|
-
const parsed = row.appliedAt instanceof Date ? row.appliedAt : new Date(row.appliedAt);
|
|
2565
|
-
appliedAt = Number.isNaN(parsed.getTime()) ? null : parsed;
|
|
2566
|
-
}
|
|
2565
|
+
const unmatchedRows = /* @__PURE__ */ new Map();
|
|
2566
|
+
for (const row of appliedRows) if (row.name) unmatchedRows.set(row.name, row);
|
|
2567
|
+
const entries = localMigrations.map((migration) => {
|
|
2568
|
+
const row = unmatchedRows.get(migration.name);
|
|
2569
|
+
unmatchedRows.delete(migration.name);
|
|
2567
2570
|
return {
|
|
2568
2571
|
name: migration.name,
|
|
2569
2572
|
applied: row !== void 0,
|
|
2570
|
-
appliedAt
|
|
2573
|
+
appliedAt: parseAppliedAt(row)
|
|
2571
2574
|
};
|
|
2572
2575
|
});
|
|
2576
|
+
if (unmatchedRows.size === 0) return entries;
|
|
2577
|
+
const orphaned = [...unmatchedRows].map(([name, row]) => ({
|
|
2578
|
+
name,
|
|
2579
|
+
applied: true,
|
|
2580
|
+
appliedAt: parseAppliedAt(row),
|
|
2581
|
+
orphaned: true
|
|
2582
|
+
}));
|
|
2583
|
+
return [...entries, ...orphaned].sort((a, b) => a.name.localeCompare(b.name));
|
|
2573
2584
|
}
|
|
2574
2585
|
/**
|
|
2575
|
-
*
|
|
2576
|
-
*
|
|
2577
|
-
*
|
|
2586
|
+
* Where the migrations folder and the tracker disagree, in both directions.
|
|
2587
|
+
* Best-effort by contract: the names exist to be logged, and a tracker this
|
|
2588
|
+
* driver cannot read costs the names, not the boot.
|
|
2578
2589
|
*/
|
|
2579
|
-
async function
|
|
2590
|
+
async function readMigrationDrift(migrationsFolder, readApplied) {
|
|
2580
2591
|
try {
|
|
2581
|
-
|
|
2592
|
+
const status = buildMigrationStatus(listLocalMigrations(migrationsFolder), await readApplied());
|
|
2593
|
+
return {
|
|
2594
|
+
pending: status.filter((entry) => !entry.applied).map((entry) => entry.name),
|
|
2595
|
+
orphaned: status.filter((entry) => entry.orphaned).map((entry) => entry.name)
|
|
2596
|
+
};
|
|
2582
2597
|
} catch {
|
|
2583
|
-
return
|
|
2598
|
+
return {
|
|
2599
|
+
pending: [],
|
|
2600
|
+
orphaned: []
|
|
2601
|
+
};
|
|
2584
2602
|
}
|
|
2585
2603
|
}
|
|
2586
2604
|
/**
|
|
2605
|
+
* One migrator run, reported on both sides. The tracker is read before the
|
|
2606
|
+
* migrator writes (afterwards every row is applied), and orphans are named
|
|
2607
|
+
* before it runs, since they are the likeliest reason it is about to fail.
|
|
2608
|
+
* Without `readApplied` nothing is read or reported: a reset's re-apply.
|
|
2609
|
+
*/
|
|
2610
|
+
async function migrateAndReport(migrationsFolder, { readApplied, migrate }) {
|
|
2611
|
+
const drift = readApplied ? await readMigrationDrift(migrationsFolder, readApplied) : {
|
|
2612
|
+
pending: [],
|
|
2613
|
+
orphaned: []
|
|
2614
|
+
};
|
|
2615
|
+
reportOrphanedMigrations(drift.orphaned, migrationsFolder);
|
|
2616
|
+
await migrate();
|
|
2617
|
+
reportAppliedMigrations(drift.pending, migrationsFolder);
|
|
2618
|
+
}
|
|
2619
|
+
/**
|
|
2620
|
+
* Warns on every boot while the tracker holds rows no folder carries, such as a
|
|
2621
|
+
* generator's migration a dev server applied before `git clean` removed it. Its
|
|
2622
|
+
* tables stay behind, and a later migration creating them again fails.
|
|
2623
|
+
*/
|
|
2624
|
+
function reportOrphanedMigrations(orphaned, migrationsFolder) {
|
|
2625
|
+
if (orphaned.length === 0) return;
|
|
2626
|
+
console.warn(`[guren/orm] ${orphaned.length} migration(s) applied to this database have no folder in ${migrationsFolder}: ${orphaned.join(", ")}.\n[guren/orm] Whatever they created is still in the database. \`bun run db:status\` lists them as orphaned.`);
|
|
2627
|
+
}
|
|
2628
|
+
/**
|
|
2587
2629
|
* Names what a run applied, so a migration nobody added on purpose — an orphan
|
|
2588
2630
|
* folder a generator left behind on a branch that was deleted, say — does not
|
|
2589
2631
|
* reach the database in silence. Silent when the run applied nothing: an
|
|
@@ -2773,9 +2815,10 @@ function createPostgresDatabase(options) {
|
|
|
2773
2815
|
client: migrationClient,
|
|
2774
2816
|
...relations ? { relations } : {}
|
|
2775
2817
|
});
|
|
2776
|
-
|
|
2777
|
-
|
|
2778
|
-
|
|
2818
|
+
await migrateAndReport(resolvedMigrationsFolder, {
|
|
2819
|
+
readApplied: report ? () => readAppliedMigrations(migrationClient) : void 0,
|
|
2820
|
+
migrate: () => migrate(db, { migrationsFolder: resolvedMigrationsFolder })
|
|
2821
|
+
});
|
|
2779
2822
|
} finally {
|
|
2780
2823
|
await migrationClient.end({ timeout: 0 });
|
|
2781
2824
|
}
|
|
@@ -2934,9 +2977,10 @@ function createMySqlDatabase(options) {
|
|
|
2934
2977
|
client: migrationClient,
|
|
2935
2978
|
...relations ? { relations } : {}
|
|
2936
2979
|
});
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
|
|
2980
|
+
await migrateAndReport(resolvedMigrationsFolder, {
|
|
2981
|
+
readApplied: report ? () => readAppliedMigrations(migrationDb) : void 0,
|
|
2982
|
+
migrate: () => migrate(migrationDb, { migrationsFolder: resolvedMigrationsFolder })
|
|
2983
|
+
});
|
|
2940
2984
|
} finally {
|
|
2941
2985
|
await closePool(migrationClient);
|
|
2942
2986
|
}
|
|
@@ -3166,10 +3210,11 @@ function createSqliteDatabase(options) {
|
|
|
3166
3210
|
const summary = inspectMigrationsFolder(resolvedMigrationsFolder);
|
|
3167
3211
|
if (summary.migrationsFound === 0) return noMigrationsToRun(summary);
|
|
3168
3212
|
const db = await database.get();
|
|
3169
|
-
const pending = report ? await pendingMigrationNames(resolvedMigrationsFolder, readAppliedMigrations) : [];
|
|
3170
3213
|
const { migrate } = await import("drizzle-orm/bun-sqlite/migrator");
|
|
3171
|
-
await
|
|
3172
|
-
|
|
3214
|
+
await migrateAndReport(resolvedMigrationsFolder, {
|
|
3215
|
+
readApplied: report ? readAppliedMigrations : void 0,
|
|
3216
|
+
migrate: () => migrate(db, { migrationsFolder: resolvedMigrationsFolder })
|
|
3217
|
+
});
|
|
3173
3218
|
return summary;
|
|
3174
3219
|
} catch (error) {
|
|
3175
3220
|
throw migrationFailure(error);
|
|
@@ -3318,9 +3363,10 @@ function createAwsDataApiDatabase(options) {
|
|
|
3318
3363
|
if (summary.migrationsFound === 0) return noMigrationsToRun(summary);
|
|
3319
3364
|
const { migrate } = await loadAwsDataApiModules();
|
|
3320
3365
|
await withAdminDb(async (db) => {
|
|
3321
|
-
|
|
3322
|
-
|
|
3323
|
-
|
|
3366
|
+
await migrateAndReport(resolvedMigrationsFolder, {
|
|
3367
|
+
readApplied: report ? () => readAppliedMigrations(db) : void 0,
|
|
3368
|
+
migrate: () => migrate(db, { migrationsFolder: resolvedMigrationsFolder })
|
|
3369
|
+
});
|
|
3324
3370
|
});
|
|
3325
3371
|
return summary;
|
|
3326
3372
|
} catch (error) {
|