@remix-run/data-table 0.0.0 → 0.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/LICENSE +21 -0
- package/README.md +549 -2
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/lib/adapter.d.ts +550 -0
- package/dist/lib/adapter.d.ts.map +1 -0
- package/dist/lib/adapter.js +1 -0
- package/dist/lib/column.d.ts +193 -0
- package/dist/lib/column.d.ts.map +1 -0
- package/dist/lib/column.js +302 -0
- package/dist/lib/database/execution-context.d.ts +10 -0
- package/dist/lib/database/execution-context.d.ts.map +1 -0
- package/dist/lib/database/execution-context.js +1 -0
- package/dist/lib/database/helpers.d.ts +26 -0
- package/dist/lib/database/helpers.d.ts.map +1 -0
- package/dist/lib/database/helpers.js +116 -0
- package/dist/lib/database/query-execution.d.ts +7 -0
- package/dist/lib/database/query-execution.d.ts.map +1 -0
- package/dist/lib/database/query-execution.js +401 -0
- package/dist/lib/database/relations.d.ts +4 -0
- package/dist/lib/database/relations.d.ts.map +1 -0
- package/dist/lib/database/relations.js +207 -0
- package/dist/lib/database/write-lifecycle.d.ts +13 -0
- package/dist/lib/database/write-lifecycle.d.ts.map +1 -0
- package/dist/lib/database/write-lifecycle.js +279 -0
- package/dist/lib/database.d.ts +264 -0
- package/dist/lib/database.d.ts.map +1 -0
- package/dist/lib/database.js +319 -0
- package/dist/lib/errors.d.ts +59 -0
- package/dist/lib/errors.d.ts.map +1 -0
- package/dist/lib/errors.js +76 -0
- package/dist/lib/inflection.d.ts +3 -0
- package/dist/lib/inflection.d.ts.map +1 -0
- package/dist/lib/inflection.js +56 -0
- package/dist/lib/migrations/filename.d.ts +12 -0
- package/dist/lib/migrations/filename.d.ts.map +1 -0
- package/dist/lib/migrations/filename.js +20 -0
- package/dist/lib/migrations/helpers.d.ts +11 -0
- package/dist/lib/migrations/helpers.d.ts.map +1 -0
- package/dist/lib/migrations/helpers.js +77 -0
- package/dist/lib/migrations/journal-store.d.ts +15 -0
- package/dist/lib/migrations/journal-store.d.ts.map +1 -0
- package/dist/lib/migrations/journal-store.js +83 -0
- package/dist/lib/migrations/registry.d.ts +27 -0
- package/dist/lib/migrations/registry.d.ts.map +1 -0
- package/dist/lib/migrations/registry.js +51 -0
- package/dist/lib/migrations/runner.d.ts +20 -0
- package/dist/lib/migrations/runner.d.ts.map +1 -0
- package/dist/lib/migrations/runner.js +273 -0
- package/dist/lib/migrations/schema-api.d.ts +7 -0
- package/dist/lib/migrations/schema-api.d.ts.map +1 -0
- package/dist/lib/migrations/schema-api.js +326 -0
- package/dist/lib/migrations-node.d.ts +17 -0
- package/dist/lib/migrations-node.d.ts.map +1 -0
- package/dist/lib/migrations-node.js +65 -0
- package/dist/lib/migrations.d.ts +292 -0
- package/dist/lib/migrations.d.ts.map +1 -0
- package/dist/lib/migrations.js +38 -0
- package/dist/lib/operators.d.ts +154 -0
- package/dist/lib/operators.d.ts.map +1 -0
- package/dist/lib/operators.js +218 -0
- package/dist/lib/query.d.ts +159 -0
- package/dist/lib/query.d.ts.map +1 -0
- package/dist/lib/query.js +401 -0
- package/dist/lib/references.d.ts +41 -0
- package/dist/lib/references.d.ts.map +1 -0
- package/dist/lib/references.js +33 -0
- package/dist/lib/sql-helpers.d.ts +50 -0
- package/dist/lib/sql-helpers.d.ts.map +1 -0
- package/dist/lib/sql-helpers.js +111 -0
- package/dist/lib/sql.d.ts +45 -0
- package/dist/lib/sql.d.ts.map +1 -0
- package/dist/lib/sql.js +65 -0
- package/dist/lib/table.d.ts +569 -0
- package/dist/lib/table.d.ts.map +1 -0
- package/dist/lib/table.js +519 -0
- package/dist/lib/types.d.ts +4 -0
- package/dist/lib/types.d.ts.map +1 -0
- package/dist/lib/types.js +1 -0
- package/dist/migrations/node.d.ts +2 -0
- package/dist/migrations/node.d.ts.map +1 -0
- package/dist/migrations/node.js +1 -0
- package/dist/migrations.d.ts +8 -0
- package/dist/migrations.d.ts.map +1 -0
- package/dist/migrations.js +5 -0
- package/dist/operators.d.ts +3 -0
- package/dist/operators.d.ts.map +1 -0
- package/dist/operators.js +1 -0
- package/dist/sql-helpers.d.ts +3 -0
- package/dist/sql-helpers.d.ts.map +1 -0
- package/dist/sql-helpers.js +1 -0
- package/package.json +56 -7
- package/src/index.ts +198 -0
- package/src/lib/adapter.ts +653 -0
- package/src/lib/column.ts +384 -0
- package/src/lib/database/execution-context.ts +15 -0
- package/src/lib/database/helpers.ts +216 -0
- package/src/lib/database/query-execution.ts +638 -0
- package/src/lib/database/relations.ts +332 -0
- package/src/lib/database/write-lifecycle.ts +487 -0
- package/src/lib/database.ts +856 -0
- package/src/lib/errors.ts +119 -0
- package/src/lib/inflection.ts +69 -0
- package/src/lib/migrations/filename.ts +25 -0
- package/src/lib/migrations/helpers.ts +108 -0
- package/src/lib/migrations/journal-store.ts +122 -0
- package/src/lib/migrations/registry.ts +62 -0
- package/src/lib/migrations/runner.ts +374 -0
- package/src/lib/migrations/schema-api.ts +417 -0
- package/src/lib/migrations-node.ts +71 -0
- package/src/lib/migrations.ts +328 -0
- package/src/lib/operators.ts +436 -0
- package/src/lib/query.ts +958 -0
- package/src/lib/references.ts +78 -0
- package/src/lib/sql-helpers.ts +146 -0
- package/src/lib/sql.ts +84 -0
- package/src/lib/table.ts +1309 -0
- package/src/lib/types.ts +3 -0
- package/src/migrations/node.ts +1 -0
- package/src/migrations.ts +26 -0
- package/src/operators.ts +18 -0
- package/src/sql-helpers.ts +9 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { rawSql } from "../sql.js";
|
|
2
|
+
export function normalizeChecksum(migration) {
|
|
3
|
+
if (migration.checksum) {
|
|
4
|
+
return migration.checksum;
|
|
5
|
+
}
|
|
6
|
+
return migration.id + ':' + migration.name;
|
|
7
|
+
}
|
|
8
|
+
export async function ensureMigrationJournal(adapter, tableName) {
|
|
9
|
+
await adapter.migrate({
|
|
10
|
+
operation: {
|
|
11
|
+
kind: 'createTable',
|
|
12
|
+
table: { name: tableName },
|
|
13
|
+
ifNotExists: true,
|
|
14
|
+
columns: {
|
|
15
|
+
id: { type: 'varchar', length: 64, nullable: false, primaryKey: true },
|
|
16
|
+
name: { type: 'varchar', length: 255, nullable: false },
|
|
17
|
+
checksum: { type: 'varchar', length: 128, nullable: false },
|
|
18
|
+
batch: { type: 'integer', nullable: false },
|
|
19
|
+
applied_at: { type: 'timestamp', nullable: false, default: { kind: 'now' } },
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
export async function hasMigrationJournal(adapter, tableName) {
|
|
25
|
+
try {
|
|
26
|
+
await adapter.execute({
|
|
27
|
+
operation: {
|
|
28
|
+
kind: 'raw',
|
|
29
|
+
sql: rawSql('select 1 from ' + tableName + ' limit 1'),
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export async function loadJournalRows(adapter, tableName) {
|
|
39
|
+
let result = await adapter.execute({
|
|
40
|
+
operation: {
|
|
41
|
+
kind: 'raw',
|
|
42
|
+
sql: rawSql('select id, name, checksum, batch, applied_at from ' + tableName + ' order by id asc'),
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
let rows = result.rows ?? [];
|
|
46
|
+
return rows.map((row) => ({
|
|
47
|
+
id: String(row.id),
|
|
48
|
+
name: String(row.name),
|
|
49
|
+
checksum: String(row.checksum),
|
|
50
|
+
batch: Number(row.batch),
|
|
51
|
+
appliedAt: new Date(String(row.applied_at)),
|
|
52
|
+
}));
|
|
53
|
+
}
|
|
54
|
+
export async function insertJournalRow(adapter, tableName, row, transaction) {
|
|
55
|
+
await adapter.execute({
|
|
56
|
+
operation: {
|
|
57
|
+
kind: 'raw',
|
|
58
|
+
sql: rawSql('insert into ' + tableName + ' (id, name, checksum, batch) values (?, ?, ?, ?)', [
|
|
59
|
+
row.id,
|
|
60
|
+
row.name,
|
|
61
|
+
row.checksum,
|
|
62
|
+
row.batch,
|
|
63
|
+
]),
|
|
64
|
+
},
|
|
65
|
+
transaction,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
export async function deleteJournalRow(adapter, tableName, id, transaction) {
|
|
69
|
+
await adapter.execute({
|
|
70
|
+
operation: {
|
|
71
|
+
kind: 'raw',
|
|
72
|
+
sql: rawSql('delete from ' + tableName + ' where id = ?', [id]),
|
|
73
|
+
},
|
|
74
|
+
transaction,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
export function getBatch(rows) {
|
|
78
|
+
if (rows.length === 0) {
|
|
79
|
+
return 1;
|
|
80
|
+
}
|
|
81
|
+
let max = Math.max(...rows.map((row) => row.batch));
|
|
82
|
+
return max + 1;
|
|
83
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { MigrationDescriptor, MigrationRegistry } from '../migrations.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Returns a new array of migrations ordered by migration id.
|
|
4
|
+
* @param migrations Migration descriptors to sort.
|
|
5
|
+
* @returns A newly sorted migration descriptor array.
|
|
6
|
+
*/
|
|
7
|
+
export declare function sortMigrations(migrations: MigrationDescriptor[]): MigrationDescriptor[];
|
|
8
|
+
/**
|
|
9
|
+
* Resolves a migration source into a sorted migration list.
|
|
10
|
+
* @param input Migration list or registry.
|
|
11
|
+
* @returns A sorted migration descriptor list.
|
|
12
|
+
*/
|
|
13
|
+
export declare function resolveMigrations(input: MigrationDescriptor[] | MigrationRegistry): MigrationDescriptor[];
|
|
14
|
+
/**
|
|
15
|
+
* Creates an in-memory migration registry.
|
|
16
|
+
* @param initial Optional initial migration list.
|
|
17
|
+
* @returns A migration registry with duplicate-id protection.
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { createMigrationRegistry } from 'remix/data-table/migrations'
|
|
21
|
+
*
|
|
22
|
+
* let registry = createMigrationRegistry()
|
|
23
|
+
* registry.register({ id, name, migration })
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function createMigrationRegistry(initial?: MigrationDescriptor[]): MigrationRegistry;
|
|
27
|
+
//# sourceMappingURL=registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../../src/lib/migrations/registry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAE9E;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,UAAU,EAAE,mBAAmB,EAAE,GAAG,mBAAmB,EAAE,CAEvF;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,mBAAmB,EAAE,GAAG,iBAAiB,GAC/C,mBAAmB,EAAE,CAMvB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,GAAE,mBAAmB,EAAO,GAAG,iBAAiB,CAuB9F"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a new array of migrations ordered by migration id.
|
|
3
|
+
* @param migrations Migration descriptors to sort.
|
|
4
|
+
* @returns A newly sorted migration descriptor array.
|
|
5
|
+
*/
|
|
6
|
+
export function sortMigrations(migrations) {
|
|
7
|
+
return [...migrations].sort((left, right) => left.id.localeCompare(right.id));
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Resolves a migration source into a sorted migration list.
|
|
11
|
+
* @param input Migration list or registry.
|
|
12
|
+
* @returns A sorted migration descriptor list.
|
|
13
|
+
*/
|
|
14
|
+
export function resolveMigrations(input) {
|
|
15
|
+
if (Array.isArray(input)) {
|
|
16
|
+
return sortMigrations(input);
|
|
17
|
+
}
|
|
18
|
+
return input.list();
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Creates an in-memory migration registry.
|
|
22
|
+
* @param initial Optional initial migration list.
|
|
23
|
+
* @returns A migration registry with duplicate-id protection.
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* import { createMigrationRegistry } from 'remix/data-table/migrations'
|
|
27
|
+
*
|
|
28
|
+
* let registry = createMigrationRegistry()
|
|
29
|
+
* registry.register({ id, name, migration })
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export function createMigrationRegistry(initial = []) {
|
|
33
|
+
let migrations = new Map();
|
|
34
|
+
for (let migration of initial) {
|
|
35
|
+
if (migrations.has(migration.id)) {
|
|
36
|
+
throw new Error('Duplicate migration id: ' + migration.id);
|
|
37
|
+
}
|
|
38
|
+
migrations.set(migration.id, migration);
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
register(migration) {
|
|
42
|
+
if (migrations.has(migration.id)) {
|
|
43
|
+
throw new Error('Duplicate migration id: ' + migration.id);
|
|
44
|
+
}
|
|
45
|
+
migrations.set(migration.id, migration);
|
|
46
|
+
},
|
|
47
|
+
list() {
|
|
48
|
+
return sortMigrations(Array.from(migrations.values()));
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { DatabaseAdapter } from '../adapter.ts';
|
|
2
|
+
import type { MigrationDescriptor, MigrationRegistry, MigrationRunner, MigrationRunnerOptions } from '../migrations.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a migration runner for applying/reverting migrations against an adapter.
|
|
5
|
+
* @param adapter Database adapter used to compile and execute migration operations.
|
|
6
|
+
* @param migrations Migration descriptors or registry.
|
|
7
|
+
* @param options Optional runner configuration.
|
|
8
|
+
* @returns A migration runner instance.
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { createMigrationRunner } from 'remix/data-table/migrations'
|
|
12
|
+
*
|
|
13
|
+
* let runner = createMigrationRunner(adapter, migrations, {
|
|
14
|
+
* journalTable: 'app_migrations',
|
|
15
|
+
* })
|
|
16
|
+
* await runner.up()
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare function createMigrationRunner(adapter: DatabaseAdapter, migrations: MigrationDescriptor[] | MigrationRegistry, options?: MigrationRunnerOptions): MigrationRunner;
|
|
20
|
+
//# sourceMappingURL=runner.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runner.d.ts","sourceRoot":"","sources":["../../../src/lib/migrations/runner.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAoB,MAAM,eAAe,CAAA;AAEtE,OAAO,KAAK,EAIV,mBAAmB,EAGnB,iBAAiB,EACjB,eAAe,EACf,sBAAsB,EAGvB,MAAM,kBAAkB,CAAA;AAyRzB;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,eAAe,EACxB,UAAU,EAAE,mBAAmB,EAAE,GAAG,iBAAiB,EACrD,OAAO,GAAE,sBAA2B,GACnC,eAAe,CAwDjB"}
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { createDatabase, createDatabaseWithTransaction } from "../database.js";
|
|
2
|
+
import { deleteJournalRow, ensureMigrationJournal, getBatch, hasMigrationJournal, insertJournalRow, loadJournalRows, normalizeChecksum, } from "./journal-store.js";
|
|
3
|
+
import { resolveMigrations } from "./registry.js";
|
|
4
|
+
import { createMigrationSchema } from "./schema-api.js";
|
|
5
|
+
function assertStepOption(step) {
|
|
6
|
+
if (step === undefined) {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
if (!Number.isInteger(step) || step < 1) {
|
|
10
|
+
throw new Error('Invalid migration step option. Expected a positive integer.');
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function assertMigrateOptions(options) {
|
|
14
|
+
if (options.to !== undefined && options.step !== undefined) {
|
|
15
|
+
throw new Error('Cannot combine "to" and "step" migration options in the same run');
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function assertTargetOption(migrations, to) {
|
|
19
|
+
if (!to) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
let target = migrations.find((migration) => migration.id === to);
|
|
23
|
+
if (!target) {
|
|
24
|
+
throw new Error('Unknown migration target: ' + to);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function assertNoMigrationDrift(migrations, journal) {
|
|
28
|
+
let migrationMap = new Map(migrations.map((migration) => [migration.id, migration]));
|
|
29
|
+
for (let row of journal) {
|
|
30
|
+
let migration = migrationMap.get(row.id);
|
|
31
|
+
if (!migration) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
let expected = normalizeChecksum(migration);
|
|
35
|
+
if (expected !== row.checksum) {
|
|
36
|
+
throw new Error('Migration checksum drift detected for "' +
|
|
37
|
+
row.id +
|
|
38
|
+
'" (journal=' +
|
|
39
|
+
row.checksum +
|
|
40
|
+
', current=' +
|
|
41
|
+
expected +
|
|
42
|
+
')');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function createDryRunDatabase(adapter) {
|
|
47
|
+
let error = new Error('Cannot execute data operations while running migrations with dryRun');
|
|
48
|
+
let throwDryRunError = async () => {
|
|
49
|
+
throw error;
|
|
50
|
+
};
|
|
51
|
+
let dryRunAdapter = {
|
|
52
|
+
dialect: adapter.dialect,
|
|
53
|
+
capabilities: adapter.capabilities,
|
|
54
|
+
compileSql(operation) {
|
|
55
|
+
return adapter.compileSql(operation);
|
|
56
|
+
},
|
|
57
|
+
async hasTable(table) {
|
|
58
|
+
return adapter.hasTable(table);
|
|
59
|
+
},
|
|
60
|
+
async hasColumn(table, column) {
|
|
61
|
+
return adapter.hasColumn(table, column);
|
|
62
|
+
},
|
|
63
|
+
execute: throwDryRunError,
|
|
64
|
+
migrate: throwDryRunError,
|
|
65
|
+
beginTransaction: throwDryRunError,
|
|
66
|
+
commitTransaction: throwDryRunError,
|
|
67
|
+
rollbackTransaction: throwDryRunError,
|
|
68
|
+
createSavepoint: throwDryRunError,
|
|
69
|
+
rollbackToSavepoint: throwDryRunError,
|
|
70
|
+
releaseSavepoint: throwDryRunError,
|
|
71
|
+
};
|
|
72
|
+
return createDatabase(dryRunAdapter);
|
|
73
|
+
}
|
|
74
|
+
async function runMigrations(input) {
|
|
75
|
+
let adapter = input.adapter;
|
|
76
|
+
let migrations = input.migrations;
|
|
77
|
+
let journalTable = input.journalTable;
|
|
78
|
+
let dryRun = Boolean(input.options.dryRun);
|
|
79
|
+
let target = input.options.to;
|
|
80
|
+
let step = input.options.step;
|
|
81
|
+
assertMigrateOptions(input.options);
|
|
82
|
+
assertStepOption(step);
|
|
83
|
+
assertTargetOption(migrations, target);
|
|
84
|
+
let sql = [];
|
|
85
|
+
await adapter.acquireMigrationLock?.();
|
|
86
|
+
try {
|
|
87
|
+
let journal = [];
|
|
88
|
+
if (dryRun) {
|
|
89
|
+
let canReadJournal = await hasMigrationJournal(adapter, journalTable);
|
|
90
|
+
if (canReadJournal) {
|
|
91
|
+
journal = await loadJournalRows(adapter, journalTable);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
await ensureMigrationJournal(adapter, journalTable);
|
|
96
|
+
journal = await loadJournalRows(adapter, journalTable);
|
|
97
|
+
}
|
|
98
|
+
let appliedMap = new Map(journal.map((row) => [row.id, row]));
|
|
99
|
+
assertNoMigrationDrift(migrations, journal);
|
|
100
|
+
let toRun = [];
|
|
101
|
+
if (input.direction === 'up') {
|
|
102
|
+
for (let migration of migrations) {
|
|
103
|
+
if (!appliedMap.has(migration.id)) {
|
|
104
|
+
toRun.push(migration);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (target) {
|
|
108
|
+
toRun = toRun.filter((migration) => migration.id <= target);
|
|
109
|
+
}
|
|
110
|
+
if (step !== undefined) {
|
|
111
|
+
toRun = toRun.slice(0, step);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
let appliedMigrations = migrations
|
|
116
|
+
.filter((migration) => appliedMap.has(migration.id))
|
|
117
|
+
.reverse();
|
|
118
|
+
if (target) {
|
|
119
|
+
appliedMigrations = appliedMigrations.filter((migration) => migration.id >= target);
|
|
120
|
+
}
|
|
121
|
+
if (step !== undefined) {
|
|
122
|
+
appliedMigrations = appliedMigrations.slice(0, step);
|
|
123
|
+
}
|
|
124
|
+
toRun = appliedMigrations;
|
|
125
|
+
}
|
|
126
|
+
let applied = [];
|
|
127
|
+
let reverted = [];
|
|
128
|
+
let batch = getBatch(journal);
|
|
129
|
+
for (let migration of toRun) {
|
|
130
|
+
if (migration.migration.transaction === 'required' &&
|
|
131
|
+
!adapter.capabilities.transactionalDdl) {
|
|
132
|
+
throw new Error('Migration "' +
|
|
133
|
+
migration.id +
|
|
134
|
+
'" requires transactional DDL, but adapter does not support it');
|
|
135
|
+
}
|
|
136
|
+
let shouldUseTransaction = !dryRun &&
|
|
137
|
+
migration.migration.transaction !== 'none' &&
|
|
138
|
+
adapter.capabilities.transactionalDdl;
|
|
139
|
+
let token;
|
|
140
|
+
if (shouldUseTransaction) {
|
|
141
|
+
token = await adapter.beginTransaction();
|
|
142
|
+
}
|
|
143
|
+
let db = dryRun
|
|
144
|
+
? createDryRunDatabase(adapter)
|
|
145
|
+
: token
|
|
146
|
+
? createDatabaseWithTransaction(adapter, token)
|
|
147
|
+
: createDatabase(adapter);
|
|
148
|
+
let schema = createMigrationSchema(db, async (operation) => {
|
|
149
|
+
let compiled = adapter.compileSql(operation);
|
|
150
|
+
sql.push(...compiled);
|
|
151
|
+
if (!dryRun) {
|
|
152
|
+
await adapter.migrate({ operation, transaction: token });
|
|
153
|
+
}
|
|
154
|
+
}, { transaction: token });
|
|
155
|
+
let context = {
|
|
156
|
+
db,
|
|
157
|
+
schema,
|
|
158
|
+
};
|
|
159
|
+
try {
|
|
160
|
+
if (input.direction === 'up') {
|
|
161
|
+
await migration.migration.up(context);
|
|
162
|
+
if (!dryRun) {
|
|
163
|
+
await insertJournalRow(adapter, journalTable, {
|
|
164
|
+
id: migration.id,
|
|
165
|
+
name: migration.name,
|
|
166
|
+
checksum: normalizeChecksum(migration),
|
|
167
|
+
batch,
|
|
168
|
+
}, token);
|
|
169
|
+
}
|
|
170
|
+
applied.push({
|
|
171
|
+
id: migration.id,
|
|
172
|
+
name: migration.name,
|
|
173
|
+
status: 'applied',
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
await migration.migration.down(context);
|
|
178
|
+
if (!dryRun) {
|
|
179
|
+
await deleteJournalRow(adapter, journalTable, migration.id, token);
|
|
180
|
+
}
|
|
181
|
+
reverted.push({
|
|
182
|
+
id: migration.id,
|
|
183
|
+
name: migration.name,
|
|
184
|
+
status: 'pending',
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
if (token) {
|
|
188
|
+
await adapter.commitTransaction(token);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
if (token) {
|
|
193
|
+
await adapter.rollbackTransaction(token);
|
|
194
|
+
}
|
|
195
|
+
throw error;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return {
|
|
199
|
+
applied,
|
|
200
|
+
reverted,
|
|
201
|
+
sql,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
finally {
|
|
205
|
+
await adapter.releaseMigrationLock?.();
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Creates a migration runner for applying/reverting migrations against an adapter.
|
|
210
|
+
* @param adapter Database adapter used to compile and execute migration operations.
|
|
211
|
+
* @param migrations Migration descriptors or registry.
|
|
212
|
+
* @param options Optional runner configuration.
|
|
213
|
+
* @returns A migration runner instance.
|
|
214
|
+
* @example
|
|
215
|
+
* ```ts
|
|
216
|
+
* import { createMigrationRunner } from 'remix/data-table/migrations'
|
|
217
|
+
*
|
|
218
|
+
* let runner = createMigrationRunner(adapter, migrations, {
|
|
219
|
+
* journalTable: 'app_migrations',
|
|
220
|
+
* })
|
|
221
|
+
* await runner.up()
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
export function createMigrationRunner(adapter, migrations, options = {}) {
|
|
225
|
+
let journalTable = options.journalTable ?? 'data_table_migrations';
|
|
226
|
+
return {
|
|
227
|
+
async up(runOptions = {}) {
|
|
228
|
+
return runMigrations({
|
|
229
|
+
adapter,
|
|
230
|
+
migrations: resolveMigrations(migrations),
|
|
231
|
+
journalTable,
|
|
232
|
+
direction: 'up',
|
|
233
|
+
options: runOptions,
|
|
234
|
+
});
|
|
235
|
+
},
|
|
236
|
+
async down(runOptions = {}) {
|
|
237
|
+
return runMigrations({
|
|
238
|
+
adapter,
|
|
239
|
+
migrations: resolveMigrations(migrations),
|
|
240
|
+
journalTable,
|
|
241
|
+
direction: 'down',
|
|
242
|
+
options: runOptions,
|
|
243
|
+
});
|
|
244
|
+
},
|
|
245
|
+
async status() {
|
|
246
|
+
await ensureMigrationJournal(adapter, journalTable);
|
|
247
|
+
let journal = await loadJournalRows(adapter, journalTable);
|
|
248
|
+
let journalMap = new Map(journal.map((row) => [row.id, row]));
|
|
249
|
+
let sortedMigrations = resolveMigrations(migrations);
|
|
250
|
+
return sortedMigrations.map((migration) => {
|
|
251
|
+
let journalRow = journalMap.get(migration.id);
|
|
252
|
+
if (!journalRow) {
|
|
253
|
+
return {
|
|
254
|
+
id: migration.id,
|
|
255
|
+
name: migration.name,
|
|
256
|
+
status: 'pending',
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
let checksum = normalizeChecksum(migration);
|
|
260
|
+
return {
|
|
261
|
+
id: migration.id,
|
|
262
|
+
name: migration.name,
|
|
263
|
+
status: checksum === journalRow.checksum
|
|
264
|
+
? 'applied'
|
|
265
|
+
: 'drifted',
|
|
266
|
+
appliedAt: journalRow.appliedAt,
|
|
267
|
+
batch: journalRow.batch,
|
|
268
|
+
checksum: journalRow.checksum,
|
|
269
|
+
};
|
|
270
|
+
});
|
|
271
|
+
},
|
|
272
|
+
};
|
|
273
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Database } from '../database.ts';
|
|
2
|
+
import type { DataMigrationOperation, TransactionToken } from '../adapter.ts';
|
|
3
|
+
import type { MigrationSchema } from '../migrations.ts';
|
|
4
|
+
export declare function createMigrationSchema(db: Database, emit: (operation: DataMigrationOperation) => Promise<void>, options?: {
|
|
5
|
+
transaction?: TransactionToken;
|
|
6
|
+
}): MigrationSchema;
|
|
7
|
+
//# sourceMappingURL=schema-api.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema-api.d.ts","sourceRoot":"","sources":["../../../src/lib/migrations/schema-api.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AAC9C,OAAO,KAAK,EAKV,sBAAsB,EAItB,gBAAgB,EAEjB,MAAM,eAAe,CAAA;AAItB,OAAO,KAAK,EAKV,eAAe,EAGhB,MAAM,kBAAkB,CAAA;AA+PzB,wBAAgB,qBAAqB,CACnC,EAAE,EAAE,QAAQ,EACZ,IAAI,EAAE,CAAC,SAAS,EAAE,sBAAsB,KAAK,OAAO,CAAC,IAAI,CAAC,EAC1D,OAAO,CAAC,EAAE;IAAE,WAAW,CAAC,EAAE,gBAAgB,CAAA;CAAE,GAC3C,eAAe,CAqIjB"}
|