@guren/orm 2.7.1 → 2.8.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.js +123 -54
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2348,6 +2348,28 @@ function buildMigrationStatus(localMigrations, appliedRows) {
|
|
|
2348
2348
|
};
|
|
2349
2349
|
});
|
|
2350
2350
|
}
|
|
2351
|
+
/**
|
|
2352
|
+
* Local migrations the tracker has no row for, in the order the migrator will
|
|
2353
|
+
* apply them. Best-effort by contract: the names exist to be logged, and a
|
|
2354
|
+
* tracker this driver cannot read costs the names, not the boot.
|
|
2355
|
+
*/
|
|
2356
|
+
async function pendingMigrationNames(migrationsFolder, readApplied) {
|
|
2357
|
+
try {
|
|
2358
|
+
return buildMigrationStatus(listLocalMigrations(migrationsFolder), await readApplied()).filter((entry) => !entry.applied).map((entry) => entry.name);
|
|
2359
|
+
} catch {
|
|
2360
|
+
return [];
|
|
2361
|
+
}
|
|
2362
|
+
}
|
|
2363
|
+
/**
|
|
2364
|
+
* Names what a run applied, so a migration nobody added on purpose — an orphan
|
|
2365
|
+
* folder a generator left behind on a branch that was deleted, say — does not
|
|
2366
|
+
* reach the database in silence. Silent when the run applied nothing: an
|
|
2367
|
+
* up-to-date database boots on every restart, and a line there is one nobody reads.
|
|
2368
|
+
*/
|
|
2369
|
+
function reportAppliedMigrations(applied, migrationsFolder) {
|
|
2370
|
+
if (applied.length === 0) return;
|
|
2371
|
+
console.info(`[guren/orm] Applied ${applied.length} migration(s) from ${migrationsFolder}: ${applied.join(", ")}`);
|
|
2372
|
+
}
|
|
2351
2373
|
//#endregion
|
|
2352
2374
|
//#region src/seeder.ts
|
|
2353
2375
|
const SUPPORTED_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
@@ -2492,7 +2514,26 @@ function createPostgresDatabase(options) {
|
|
|
2492
2514
|
if (!resolved) throw new Error("DATABASE_URL is not set and no connection string was provided.");
|
|
2493
2515
|
return resolved;
|
|
2494
2516
|
}
|
|
2517
|
+
let reapplyingAfterReset = false;
|
|
2518
|
+
/**
|
|
2519
|
+
* Tracker rows over an open client. Only a missing tracker means "nothing
|
|
2520
|
+
* applied": a denied SELECT, a broken schema, or an unreachable server must
|
|
2521
|
+
* not read as all-pending.
|
|
2522
|
+
*/
|
|
2523
|
+
async function readAppliedMigrations(client) {
|
|
2524
|
+
try {
|
|
2525
|
+
return (await client.unsafe("SELECT name FROM drizzle.__drizzle_migrations")).map((row) => ({
|
|
2526
|
+
name: row.name,
|
|
2527
|
+
appliedAt: null
|
|
2528
|
+
}));
|
|
2529
|
+
} catch (error) {
|
|
2530
|
+
if (isMissingTrackerTable(error, "postgres")) return [];
|
|
2531
|
+
throw error;
|
|
2532
|
+
}
|
|
2533
|
+
}
|
|
2495
2534
|
const migrations = singleFlight(async () => {
|
|
2535
|
+
const report = !reapplyingAfterReset;
|
|
2536
|
+
reapplyingAfterReset = false;
|
|
2496
2537
|
let endpoint;
|
|
2497
2538
|
try {
|
|
2498
2539
|
const summary = inspectMigrationsFolder(resolvedMigrationsFolder);
|
|
@@ -2505,10 +2546,13 @@ function createPostgresDatabase(options) {
|
|
|
2505
2546
|
...clientOptions
|
|
2506
2547
|
});
|
|
2507
2548
|
try {
|
|
2508
|
-
|
|
2549
|
+
const db = drizzle({
|
|
2509
2550
|
client: migrationClient,
|
|
2510
2551
|
...relations ? { relations } : {}
|
|
2511
|
-
})
|
|
2552
|
+
});
|
|
2553
|
+
const pending = report ? await pendingMigrationNames(resolvedMigrationsFolder, () => readAppliedMigrations(migrationClient)) : [];
|
|
2554
|
+
await migrate(db, { migrationsFolder: resolvedMigrationsFolder });
|
|
2555
|
+
reportAppliedMigrations(pending, resolvedMigrationsFolder);
|
|
2512
2556
|
} finally {
|
|
2513
2557
|
await migrationClient.end({ timeout: 0 });
|
|
2514
2558
|
}
|
|
@@ -2580,24 +2624,13 @@ function createPostgresDatabase(options) {
|
|
|
2580
2624
|
await adminClient.unsafe("DROP SCHEMA IF EXISTS drizzle CASCADE");
|
|
2581
2625
|
});
|
|
2582
2626
|
migrations.reset();
|
|
2627
|
+
reapplyingAfterReset = true;
|
|
2583
2628
|
return migrations.get();
|
|
2584
2629
|
}
|
|
2585
2630
|
async function migrationStatus() {
|
|
2586
2631
|
const localMigrations = listLocalMigrations(resolvedMigrationsFolder);
|
|
2587
2632
|
if (localMigrations.length === 0) return [];
|
|
2588
|
-
return buildMigrationStatus(localMigrations, await withAdminClient(
|
|
2589
|
-
try {
|
|
2590
|
-
return (await adminClient.unsafe("SELECT name FROM drizzle.__drizzle_migrations")).map((row) => {
|
|
2591
|
-
return {
|
|
2592
|
-
name: row.name,
|
|
2593
|
-
appliedAt: null
|
|
2594
|
-
};
|
|
2595
|
-
});
|
|
2596
|
-
} catch (error) {
|
|
2597
|
-
if (isMissingTrackerTable(error, "postgres")) return [];
|
|
2598
|
-
throw error;
|
|
2599
|
-
}
|
|
2600
|
-
}));
|
|
2633
|
+
return buildMigrationStatus(localMigrations, await withAdminClient(readAppliedMigrations));
|
|
2601
2634
|
}
|
|
2602
2635
|
return {
|
|
2603
2636
|
getDatabase: database.get,
|
|
@@ -2640,7 +2673,28 @@ function createMySqlDatabase(options) {
|
|
|
2640
2673
|
if (!resolved) throw new Error("DATABASE_URL is not set and no connection string was provided.");
|
|
2641
2674
|
return resolved;
|
|
2642
2675
|
}
|
|
2676
|
+
let reapplyingAfterReset = false;
|
|
2677
|
+
/**
|
|
2678
|
+
* Tracker rows over an open handle. Only a missing tracker means "nothing
|
|
2679
|
+
* applied": a denied SELECT or an unreachable server must not read as
|
|
2680
|
+
* all-pending.
|
|
2681
|
+
*/
|
|
2682
|
+
async function readAppliedMigrations(adminDb) {
|
|
2683
|
+
const { sql } = await import("drizzle-orm");
|
|
2684
|
+
try {
|
|
2685
|
+
const [rows] = await adminDb.execute(sql.raw("SELECT name, applied_at FROM __drizzle_migrations"));
|
|
2686
|
+
return rows.map((row) => ({
|
|
2687
|
+
name: row.name,
|
|
2688
|
+
appliedAt: row.applied_at
|
|
2689
|
+
}));
|
|
2690
|
+
} catch (error) {
|
|
2691
|
+
if (isMissingTrackerTable(error, "mysql")) return [];
|
|
2692
|
+
throw error;
|
|
2693
|
+
}
|
|
2694
|
+
}
|
|
2643
2695
|
const migrations = singleFlight(async () => {
|
|
2696
|
+
const report = !reapplyingAfterReset;
|
|
2697
|
+
reapplyingAfterReset = false;
|
|
2644
2698
|
let endpoint;
|
|
2645
2699
|
try {
|
|
2646
2700
|
const summary = inspectMigrationsFolder(resolvedMigrationsFolder);
|
|
@@ -2653,10 +2707,13 @@ function createMySqlDatabase(options) {
|
|
|
2653
2707
|
...clientOptions
|
|
2654
2708
|
});
|
|
2655
2709
|
try {
|
|
2656
|
-
|
|
2710
|
+
const migrationDb = drizzle({
|
|
2657
2711
|
client: migrationClient,
|
|
2658
2712
|
...relations ? { relations } : {}
|
|
2659
|
-
})
|
|
2713
|
+
});
|
|
2714
|
+
const pending = report ? await pendingMigrationNames(resolvedMigrationsFolder, () => readAppliedMigrations(migrationDb)) : [];
|
|
2715
|
+
await migrate(migrationDb, { migrationsFolder: resolvedMigrationsFolder });
|
|
2716
|
+
reportAppliedMigrations(pending, resolvedMigrationsFolder);
|
|
2660
2717
|
} finally {
|
|
2661
2718
|
await closePool(migrationClient);
|
|
2662
2719
|
}
|
|
@@ -2737,24 +2794,13 @@ function createMySqlDatabase(options) {
|
|
|
2737
2794
|
}
|
|
2738
2795
|
});
|
|
2739
2796
|
migrations.reset();
|
|
2797
|
+
reapplyingAfterReset = true;
|
|
2740
2798
|
return migrations.get();
|
|
2741
2799
|
}
|
|
2742
2800
|
async function migrationStatus() {
|
|
2743
2801
|
const localMigrations = listLocalMigrations(resolvedMigrationsFolder);
|
|
2744
2802
|
if (localMigrations.length === 0) return [];
|
|
2745
|
-
|
|
2746
|
-
return buildMigrationStatus(localMigrations, await withAdminDb(async (adminDb) => {
|
|
2747
|
-
try {
|
|
2748
|
-
const [rows] = await adminDb.execute(sql.raw("SELECT name, applied_at FROM __drizzle_migrations"));
|
|
2749
|
-
return rows.map((row) => ({
|
|
2750
|
-
name: row.name,
|
|
2751
|
-
appliedAt: row.applied_at
|
|
2752
|
-
}));
|
|
2753
|
-
} catch (error) {
|
|
2754
|
-
if (isMissingTrackerTable(error, "mysql")) return [];
|
|
2755
|
-
throw error;
|
|
2756
|
-
}
|
|
2757
|
-
}));
|
|
2803
|
+
return buildMigrationStatus(localMigrations, await withAdminDb(readAppliedMigrations));
|
|
2758
2804
|
}
|
|
2759
2805
|
return {
|
|
2760
2806
|
getDatabase: database.get,
|
|
@@ -2877,13 +2923,30 @@ function createSqliteDatabase(options) {
|
|
|
2877
2923
|
if (key) releaseActiveConnection(key, closeDatabase);
|
|
2878
2924
|
}
|
|
2879
2925
|
}
|
|
2926
|
+
let reapplyingAfterReset = false;
|
|
2927
|
+
/** Tracker rows over the open handle. Only a missing tracker means "nothing applied"; a drifted column must not read as all-pending. */
|
|
2928
|
+
function readAppliedMigrations() {
|
|
2929
|
+
try {
|
|
2930
|
+
return (sqliteClient?.query("SELECT name, applied_at FROM __drizzle_migrations").all() ?? []).map((row) => ({
|
|
2931
|
+
name: row.name,
|
|
2932
|
+
appliedAt: row.applied_at
|
|
2933
|
+
}));
|
|
2934
|
+
} catch (error) {
|
|
2935
|
+
if (!isMissingTrackerTable(error, "sqlite")) throw error;
|
|
2936
|
+
return [];
|
|
2937
|
+
}
|
|
2938
|
+
}
|
|
2880
2939
|
const migrations = singleFlight(async () => {
|
|
2940
|
+
const report = !reapplyingAfterReset;
|
|
2941
|
+
reapplyingAfterReset = false;
|
|
2881
2942
|
try {
|
|
2882
2943
|
const summary = inspectMigrationsFolder(resolvedMigrationsFolder);
|
|
2883
2944
|
if (summary.migrationsFound === 0) return noMigrationsToRun(summary);
|
|
2884
2945
|
const db = await database.get();
|
|
2946
|
+
const pending = report ? await pendingMigrationNames(resolvedMigrationsFolder, readAppliedMigrations) : [];
|
|
2885
2947
|
const { migrate } = await import("drizzle-orm/bun-sqlite/migrator");
|
|
2886
2948
|
await migrate(db, { migrationsFolder: resolvedMigrationsFolder });
|
|
2949
|
+
reportAppliedMigrations(pending, resolvedMigrationsFolder);
|
|
2887
2950
|
return summary;
|
|
2888
2951
|
} catch (error) {
|
|
2889
2952
|
throw migrationFailure(error);
|
|
@@ -2910,22 +2973,14 @@ function createSqliteDatabase(options) {
|
|
|
2910
2973
|
sqliteClient.exec("PRAGMA foreign_keys = ON;");
|
|
2911
2974
|
}
|
|
2912
2975
|
migrations.reset();
|
|
2976
|
+
reapplyingAfterReset = true;
|
|
2913
2977
|
return migrations.get();
|
|
2914
2978
|
},
|
|
2915
2979
|
async migrationStatus() {
|
|
2916
2980
|
const localMigrations = listLocalMigrations(resolvedMigrationsFolder);
|
|
2917
2981
|
if (localMigrations.length === 0) return [];
|
|
2918
2982
|
await database.get();
|
|
2919
|
-
|
|
2920
|
-
try {
|
|
2921
|
-
appliedRows = (sqliteClient?.query("SELECT name, applied_at FROM __drizzle_migrations").all() ?? []).map((row) => ({
|
|
2922
|
-
name: row.name,
|
|
2923
|
-
appliedAt: row.applied_at
|
|
2924
|
-
}));
|
|
2925
|
-
} catch (error) {
|
|
2926
|
-
if (!isMissingTrackerTable(error, "sqlite")) throw error;
|
|
2927
|
-
}
|
|
2928
|
-
return buildMigrationStatus(localMigrations, appliedRows);
|
|
2983
|
+
return buildMigrationStatus(localMigrations, readAppliedMigrations());
|
|
2929
2984
|
},
|
|
2930
2985
|
async configureOrm() {
|
|
2931
2986
|
const db = await database.get();
|
|
@@ -3031,12 +3086,19 @@ function createAwsDataApiDatabase(options) {
|
|
|
3031
3086
|
...relations ? { relations } : {}
|
|
3032
3087
|
};
|
|
3033
3088
|
}
|
|
3089
|
+
let reapplyingAfterReset = false;
|
|
3034
3090
|
const migrations = singleFlight(async () => {
|
|
3091
|
+
const report = !reapplyingAfterReset;
|
|
3092
|
+
reapplyingAfterReset = false;
|
|
3035
3093
|
try {
|
|
3036
3094
|
const summary = inspectMigrationsFolder(resolvedMigrationsFolder);
|
|
3037
3095
|
if (summary.migrationsFound === 0) return noMigrationsToRun(summary);
|
|
3038
3096
|
const { migrate } = await loadAwsDataApiModules();
|
|
3039
|
-
await withAdminDb((db) =>
|
|
3097
|
+
await withAdminDb(async (db) => {
|
|
3098
|
+
const pending = report ? await pendingMigrationNames(resolvedMigrationsFolder, () => readAppliedMigrations(db)) : [];
|
|
3099
|
+
await migrate(db, { migrationsFolder: resolvedMigrationsFolder });
|
|
3100
|
+
reportAppliedMigrations(pending, resolvedMigrationsFolder);
|
|
3101
|
+
});
|
|
3040
3102
|
return summary;
|
|
3041
3103
|
} catch (error) {
|
|
3042
3104
|
const reason = error instanceof Error ? error.message : String(error);
|
|
@@ -3090,24 +3152,31 @@ function createAwsDataApiDatabase(options) {
|
|
|
3090
3152
|
await adminDb.execute(sql.raw("DROP SCHEMA IF EXISTS drizzle CASCADE"));
|
|
3091
3153
|
});
|
|
3092
3154
|
migrations.reset();
|
|
3155
|
+
reapplyingAfterReset = true;
|
|
3093
3156
|
return migrations.get();
|
|
3094
3157
|
}
|
|
3158
|
+
/**
|
|
3159
|
+
* Tracker rows over an open handle. Only a missing tracker table means
|
|
3160
|
+
* "nothing applied". Anything else (IAM denial, network) must surface:
|
|
3161
|
+
* reporting it as all-pending could prompt a re-run of applied migrations.
|
|
3162
|
+
*/
|
|
3163
|
+
async function readAppliedMigrations(adminDb) {
|
|
3164
|
+
const { sql } = await import("drizzle-orm");
|
|
3165
|
+
try {
|
|
3166
|
+
const result = await adminDb.execute(sql.raw("SELECT name FROM drizzle.__drizzle_migrations"));
|
|
3167
|
+
return (Array.isArray(result) ? result : result.rows ?? []).map((row) => ({
|
|
3168
|
+
name: row.name,
|
|
3169
|
+
appliedAt: null
|
|
3170
|
+
}));
|
|
3171
|
+
} catch (error) {
|
|
3172
|
+
if (isMissingTrackerTableError(error)) return [];
|
|
3173
|
+
throw error;
|
|
3174
|
+
}
|
|
3175
|
+
}
|
|
3095
3176
|
async function migrationStatus() {
|
|
3096
3177
|
const localMigrations = listLocalMigrations(resolvedMigrationsFolder);
|
|
3097
3178
|
if (localMigrations.length === 0) return [];
|
|
3098
|
-
|
|
3099
|
-
return buildMigrationStatus(localMigrations, await withAdminDb(async (adminDb) => {
|
|
3100
|
-
try {
|
|
3101
|
-
const result = await adminDb.execute(sql.raw("SELECT name FROM drizzle.__drizzle_migrations"));
|
|
3102
|
-
return (Array.isArray(result) ? result : result.rows ?? []).map((row) => ({
|
|
3103
|
-
name: row.name,
|
|
3104
|
-
appliedAt: null
|
|
3105
|
-
}));
|
|
3106
|
-
} catch (error) {
|
|
3107
|
-
if (isMissingTrackerTableError(error)) return [];
|
|
3108
|
-
throw error;
|
|
3109
|
-
}
|
|
3110
|
-
}));
|
|
3179
|
+
return buildMigrationStatus(localMigrations, await withAdminDb(readAppliedMigrations));
|
|
3111
3180
|
}
|
|
3112
3181
|
return {
|
|
3113
3182
|
getDatabase: databaseHandle.get,
|