sqlmorpheus 0.1.5 → 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/dist/index.js +65 -50
- package/dist/index.mjs +65 -50
- package/package.json +39 -39
package/dist/index.js
CHANGED
|
@@ -115,7 +115,7 @@ var TableStructureGenerator = class {
|
|
|
115
115
|
columnData.defaultValue = value === null ? this.dialect.typeNull : `"${this.dialect.formatValueToSql(value)}"`;
|
|
116
116
|
break;
|
|
117
117
|
case "number":
|
|
118
|
-
columnData.type = this.dialect.
|
|
118
|
+
columnData.type = this.dialect.typeInt;
|
|
119
119
|
columnData.defaultValue = value === null ? this.dialect.typeNull : this.dialect.formatValueToSql(value);
|
|
120
120
|
break;
|
|
121
121
|
case "boolean":
|
|
@@ -254,36 +254,15 @@ var MigrationHistoryManager = class {
|
|
|
254
254
|
(0, import_node_fs.mkdirSync)(this.configPath, { recursive: true });
|
|
255
255
|
(0, import_node_fs.mkdirSync)(this.migrationsPath, { recursive: true });
|
|
256
256
|
}
|
|
257
|
-
/**
|
|
258
|
-
* Retrieves the last recorded history version from the configuration path.
|
|
259
|
-
* If the version is not already cached in memory, this method reads it from a file on disk.
|
|
260
|
-
*
|
|
261
|
-
* @return The last version number, or 0 if the version file does not exist or cannot be parsed.
|
|
262
|
-
*/
|
|
263
|
-
getLastHistoryVersion() {
|
|
264
|
-
if (this.lastVersion)
|
|
265
|
-
return this.lastVersion;
|
|
266
|
-
const path = `${this.configPath}/last_version.txt`;
|
|
267
|
-
this.lastVersion = (0, import_node_fs.existsSync)(path) ? parseInt((0, import_fs.readFileSync)(`${this.configPath}/last_version.txt`, { encoding: "utf-8" })) ?? 0 : 0;
|
|
268
|
-
return this.lastVersion;
|
|
269
|
-
}
|
|
270
|
-
/**
|
|
271
|
-
* Updates the "last history version" information by writing the given version to a file.
|
|
272
|
-
*
|
|
273
|
-
* @param version - The version number to set as the last history version.
|
|
274
|
-
*/
|
|
275
|
-
setLastHistoryVersion(version) {
|
|
276
|
-
(0, import_node_fs.writeFileSync)(`${this.configPath}/last_version.txt`, version.toString(), { encoding: "utf-8" });
|
|
277
|
-
}
|
|
278
257
|
/**
|
|
279
258
|
* Creates migration history files for upgrading and downgrading database schemas.
|
|
280
259
|
*
|
|
260
|
+
* @param fromVersion - The current version number of the database.
|
|
281
261
|
* @param toVersion - The target version number for the migration.
|
|
282
262
|
* @param changes - An object containing the SQL changes for upgrading (`changes.up`) and downgrading (`changes.down`).
|
|
283
263
|
* @param overwriteExisting - A flag that determines if existing migration files should be overwritten. Defaults to `false`.
|
|
284
264
|
*/
|
|
285
|
-
createMigrationHistory(toVersion, changes, overwriteExisting) {
|
|
286
|
-
const fromVersion = this.getLastHistoryVersion();
|
|
265
|
+
createMigrationHistory(fromVersion, toVersion, changes, overwriteExisting) {
|
|
287
266
|
const upPath = `${this.migrationsPath}/${FILENAME_UP_PREFIX}${toVersion}.sql`;
|
|
288
267
|
const downPath = `${this.migrationsPath}/${FILENAME_DOWN_PREFIX}${fromVersion}.sql`;
|
|
289
268
|
if ((0, import_node_fs.existsSync)(upPath) && !overwriteExisting)
|
|
@@ -306,7 +285,7 @@ ${changes.down}`, { encoding: "utf-8" });
|
|
|
306
285
|
* @return The content of the SQL migration script as a string.
|
|
307
286
|
*/
|
|
308
287
|
getUpMigration(toVersion) {
|
|
309
|
-
return (0, import_fs.readFileSync)(`${this.migrationsPath}/${FILENAME_UP_PREFIX}${toVersion
|
|
288
|
+
return (0, import_fs.readFileSync)(`${this.migrationsPath}/${FILENAME_UP_PREFIX}${toVersion}.sql`, { encoding: "utf-8" });
|
|
310
289
|
}
|
|
311
290
|
/**
|
|
312
291
|
* Retrieves the SQL migration script for downgrading to the specified version.
|
|
@@ -320,13 +299,14 @@ ${changes.down}`, { encoding: "utf-8" });
|
|
|
320
299
|
};
|
|
321
300
|
|
|
322
301
|
// src/lib/dialects/DefaultSql.ts
|
|
302
|
+
var MIGRATION_DATA_TABLE_NAME = "__sqlmorpheus_migrations";
|
|
323
303
|
var DefaultSql = class {
|
|
324
304
|
canAlterForeignKeys = false;
|
|
325
305
|
canAlterPrimaryKey = false;
|
|
326
306
|
canInspectForeignKeys = false;
|
|
327
307
|
canInspectPrimaryKey = false;
|
|
328
308
|
typeString = "TEXT";
|
|
329
|
-
|
|
309
|
+
typeInt = "INTEGER";
|
|
330
310
|
typeBoolean = "BOOLEAN";
|
|
331
311
|
typeNull = "NULL";
|
|
332
312
|
formatValueToSql(value) {
|
|
@@ -369,8 +349,8 @@ var DefaultSql = class {
|
|
|
369
349
|
dropTable(tableName) {
|
|
370
350
|
return `DROP TABLE IF EXISTS ${tableName};`;
|
|
371
351
|
}
|
|
372
|
-
columnDefinition(
|
|
373
|
-
const query = `${
|
|
352
|
+
columnDefinition(columnName, type, defaultValue, isPrimaryKey) {
|
|
353
|
+
const query = `${columnName} ${type} DEFAULT ${defaultValue}`;
|
|
374
354
|
return isPrimaryKey ? `${query} PRIMARY KEY` : query;
|
|
375
355
|
}
|
|
376
356
|
createColumn(columnTable, entry) {
|
|
@@ -397,6 +377,30 @@ var DefaultSql = class {
|
|
|
397
377
|
insertValues(keys, valueString) {
|
|
398
378
|
return `(${keys}) ${valueString ?? `VALUES (${keys.map(() => "?").join(",")})`}`;
|
|
399
379
|
}
|
|
380
|
+
migrationTableQuery() {
|
|
381
|
+
return this.createTable(MIGRATION_DATA_TABLE_NAME, [
|
|
382
|
+
this.columnDefinition("version", this.typeInt, "0", false)
|
|
383
|
+
]);
|
|
384
|
+
}
|
|
385
|
+
async createMigrationTableIfNeeded(db) {
|
|
386
|
+
await db.runMultipleWriteStatements(this.migrationTableQuery());
|
|
387
|
+
}
|
|
388
|
+
//TODO: untested
|
|
389
|
+
async getVersion(db) {
|
|
390
|
+
await this.createMigrationTableIfNeeded(db);
|
|
391
|
+
const query = this.select(MIGRATION_DATA_TABLE_NAME, ["version"]);
|
|
392
|
+
const data = await db.runGetStatement(query);
|
|
393
|
+
return data.length ? data[0].version : 0;
|
|
394
|
+
}
|
|
395
|
+
//TODO: untested
|
|
396
|
+
async setVersion(db, newVersion) {
|
|
397
|
+
const query = `${this.migrationTableQuery()};
|
|
398
|
+
INSERT INTO ${MIGRATION_DATA_TABLE_NAME} (version) VALUES ${newVersion} WHERE NOT EXISTS (SELECT 1 FROM ${MIGRATION_DATA_TABLE_NAME})
|
|
399
|
+
UNION ALL
|
|
400
|
+
UPDATE ${MIGRATION_DATA_TABLE_NAME} SET version = ${newVersion} WHERE EXISTS (SELECT 1 FROM ${MIGRATION_DATA_TABLE_NAME});
|
|
401
|
+
`;
|
|
402
|
+
await db.runMultipleWriteStatements(query);
|
|
403
|
+
}
|
|
400
404
|
};
|
|
401
405
|
|
|
402
406
|
// src/lib/dialects/SqliteDialect.ts
|
|
@@ -444,6 +448,13 @@ var SqliteDialect = class extends DefaultSql {
|
|
|
444
448
|
return value.toString();
|
|
445
449
|
}
|
|
446
450
|
}
|
|
451
|
+
async getVersion(db) {
|
|
452
|
+
const output = await db.runGetStatement(`PRAGMA user_version;`);
|
|
453
|
+
return output[0].user_version;
|
|
454
|
+
}
|
|
455
|
+
async setVersion(db, newVersion) {
|
|
456
|
+
await db.runMultipleWriteStatements(`PRAGMA user_version = ${newVersion};`);
|
|
457
|
+
}
|
|
447
458
|
};
|
|
448
459
|
|
|
449
460
|
// src/lib/dialects/PostgresDialect.ts
|
|
@@ -582,6 +593,22 @@ var MySqlDialect = class extends DefaultSql {
|
|
|
582
593
|
}
|
|
583
594
|
};
|
|
584
595
|
|
|
596
|
+
// src/lib/getDialect.ts
|
|
597
|
+
function getDialect(dbInstructions) {
|
|
598
|
+
switch (dbInstructions.dialect) {
|
|
599
|
+
case "Sqlite":
|
|
600
|
+
return new SqliteDialect();
|
|
601
|
+
case "Postgres":
|
|
602
|
+
return new PostgresDialect();
|
|
603
|
+
case "MsSql":
|
|
604
|
+
return new MsSqlDialect();
|
|
605
|
+
case "MySql":
|
|
606
|
+
return new MySqlDialect();
|
|
607
|
+
default:
|
|
608
|
+
throw new Error(`Unknown dialect ${dbInstructions.dialect}`);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
585
612
|
// src/lib/MigrationManager.ts
|
|
586
613
|
var MigrationManager = class {
|
|
587
614
|
migrations = new Migrations();
|
|
@@ -592,22 +619,7 @@ var MigrationManager = class {
|
|
|
592
619
|
migrationHistoryManager;
|
|
593
620
|
dialect;
|
|
594
621
|
constructor(db, dbInstructions) {
|
|
595
|
-
|
|
596
|
-
case "Sqlite":
|
|
597
|
-
this.dialect = new SqliteDialect();
|
|
598
|
-
break;
|
|
599
|
-
case "Postgres":
|
|
600
|
-
this.dialect = new PostgresDialect();
|
|
601
|
-
break;
|
|
602
|
-
case "MsSql":
|
|
603
|
-
this.dialect = new MsSqlDialect();
|
|
604
|
-
break;
|
|
605
|
-
case "MySql":
|
|
606
|
-
this.dialect = new MySqlDialect();
|
|
607
|
-
break;
|
|
608
|
-
default:
|
|
609
|
-
throw new Error(`Unknown dialect ${dbInstructions.dialect}`);
|
|
610
|
-
}
|
|
622
|
+
this.dialect = getDialect(dbInstructions);
|
|
611
623
|
this.db = db;
|
|
612
624
|
this.dbInstructions = dbInstructions;
|
|
613
625
|
this.tableStructureGenerator = new TableStructureGenerator(dbInstructions, this.dialect);
|
|
@@ -627,7 +639,7 @@ var MigrationManager = class {
|
|
|
627
639
|
async getMigrateSql() {
|
|
628
640
|
if (this.dbInstructions.version <= 0)
|
|
629
641
|
throw new Error("Cannot migrate to version 0 or lower");
|
|
630
|
-
const fromVersion = this.
|
|
642
|
+
const fromVersion = await this.dialect.getVersion(this.db);
|
|
631
643
|
if (fromVersion == 0) {
|
|
632
644
|
Logger.log(`Creating initial migration to version ${this.dbInstructions.version}`);
|
|
633
645
|
return this.createAndDropTables();
|
|
@@ -703,7 +715,8 @@ ${foreignKeyOnQuery}`;
|
|
|
703
715
|
const changes = await this.getMigrateSql();
|
|
704
716
|
if (!changes)
|
|
705
717
|
return;
|
|
706
|
-
this.
|
|
718
|
+
const fromVersion = await this.dialect.getVersion(this.db);
|
|
719
|
+
this.migrationHistoryManager.createMigrationHistory(fromVersion, this.dbInstructions.version, changes, overwriteExisting);
|
|
707
720
|
}
|
|
708
721
|
/**
|
|
709
722
|
* Generates SQL change statements for creating and dropping tables based on the current table structure
|
|
@@ -1007,7 +1020,8 @@ async function prepareMigration(db, instructions, overwriteExisting) {
|
|
|
1007
1020
|
async function runPreparedMigrations(db, instructions) {
|
|
1008
1021
|
Logger.setMode(instructions.loggerMode);
|
|
1009
1022
|
const migrationHistoryManager = new MigrationHistoryManager(instructions.configPath);
|
|
1010
|
-
const
|
|
1023
|
+
const dialect = getDialect(instructions);
|
|
1024
|
+
const fromVersion = await dialect.getVersion(db);
|
|
1011
1025
|
const toVersion = instructions.version;
|
|
1012
1026
|
if (fromVersion == toVersion)
|
|
1013
1027
|
return;
|
|
@@ -1018,7 +1032,7 @@ async function runPreparedMigrations(db, instructions) {
|
|
|
1018
1032
|
await db.runMultipleWriteStatements(upChanges);
|
|
1019
1033
|
instructions.version = i;
|
|
1020
1034
|
}
|
|
1021
|
-
|
|
1035
|
+
await dialect.setVersion(db, toVersion);
|
|
1022
1036
|
}
|
|
1023
1037
|
async function prepareAndRunMigration(db, instructions, overwriteExisting) {
|
|
1024
1038
|
Logger.setMode(instructions.loggerMode);
|
|
@@ -1035,7 +1049,8 @@ async function runMigrationWithoutHistory(db, instructions) {
|
|
|
1035
1049
|
async function rollback(db, instructions, toVersion) {
|
|
1036
1050
|
Logger.setMode(instructions.loggerMode);
|
|
1037
1051
|
const migrationHistoryManager = new MigrationHistoryManager(instructions.configPath);
|
|
1038
|
-
const
|
|
1052
|
+
const dialect = getDialect(instructions);
|
|
1053
|
+
const fromVersion = await dialect.getVersion(db);
|
|
1039
1054
|
Logger.log(`Rolling back from ${fromVersion} to ${toVersion}`);
|
|
1040
1055
|
for (let i = fromVersion - 1; i >= toVersion; --i) {
|
|
1041
1056
|
const upChanges = migrationHistoryManager.getDownMigration(i);
|
|
@@ -1043,7 +1058,7 @@ async function rollback(db, instructions, toVersion) {
|
|
|
1043
1058
|
await db.runMultipleWriteStatements(upChanges);
|
|
1044
1059
|
instructions.version = i;
|
|
1045
1060
|
}
|
|
1046
|
-
|
|
1061
|
+
await dialect.setVersion(db, toVersion);
|
|
1047
1062
|
}
|
|
1048
1063
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1049
1064
|
0 && (module.exports = {
|
package/dist/index.mjs
CHANGED
|
@@ -83,7 +83,7 @@ var TableStructureGenerator = class {
|
|
|
83
83
|
columnData.defaultValue = value === null ? this.dialect.typeNull : `"${this.dialect.formatValueToSql(value)}"`;
|
|
84
84
|
break;
|
|
85
85
|
case "number":
|
|
86
|
-
columnData.type = this.dialect.
|
|
86
|
+
columnData.type = this.dialect.typeInt;
|
|
87
87
|
columnData.defaultValue = value === null ? this.dialect.typeNull : this.dialect.formatValueToSql(value);
|
|
88
88
|
break;
|
|
89
89
|
case "boolean":
|
|
@@ -222,36 +222,15 @@ var MigrationHistoryManager = class {
|
|
|
222
222
|
mkdirSync(this.configPath, { recursive: true });
|
|
223
223
|
mkdirSync(this.migrationsPath, { recursive: true });
|
|
224
224
|
}
|
|
225
|
-
/**
|
|
226
|
-
* Retrieves the last recorded history version from the configuration path.
|
|
227
|
-
* If the version is not already cached in memory, this method reads it from a file on disk.
|
|
228
|
-
*
|
|
229
|
-
* @return The last version number, or 0 if the version file does not exist or cannot be parsed.
|
|
230
|
-
*/
|
|
231
|
-
getLastHistoryVersion() {
|
|
232
|
-
if (this.lastVersion)
|
|
233
|
-
return this.lastVersion;
|
|
234
|
-
const path = `${this.configPath}/last_version.txt`;
|
|
235
|
-
this.lastVersion = existsSync(path) ? parseInt(readFileSync(`${this.configPath}/last_version.txt`, { encoding: "utf-8" })) ?? 0 : 0;
|
|
236
|
-
return this.lastVersion;
|
|
237
|
-
}
|
|
238
|
-
/**
|
|
239
|
-
* Updates the "last history version" information by writing the given version to a file.
|
|
240
|
-
*
|
|
241
|
-
* @param version - The version number to set as the last history version.
|
|
242
|
-
*/
|
|
243
|
-
setLastHistoryVersion(version) {
|
|
244
|
-
writeFileSync(`${this.configPath}/last_version.txt`, version.toString(), { encoding: "utf-8" });
|
|
245
|
-
}
|
|
246
225
|
/**
|
|
247
226
|
* Creates migration history files for upgrading and downgrading database schemas.
|
|
248
227
|
*
|
|
228
|
+
* @param fromVersion - The current version number of the database.
|
|
249
229
|
* @param toVersion - The target version number for the migration.
|
|
250
230
|
* @param changes - An object containing the SQL changes for upgrading (`changes.up`) and downgrading (`changes.down`).
|
|
251
231
|
* @param overwriteExisting - A flag that determines if existing migration files should be overwritten. Defaults to `false`.
|
|
252
232
|
*/
|
|
253
|
-
createMigrationHistory(toVersion, changes, overwriteExisting) {
|
|
254
|
-
const fromVersion = this.getLastHistoryVersion();
|
|
233
|
+
createMigrationHistory(fromVersion, toVersion, changes, overwriteExisting) {
|
|
255
234
|
const upPath = `${this.migrationsPath}/${FILENAME_UP_PREFIX}${toVersion}.sql`;
|
|
256
235
|
const downPath = `${this.migrationsPath}/${FILENAME_DOWN_PREFIX}${fromVersion}.sql`;
|
|
257
236
|
if (existsSync(upPath) && !overwriteExisting)
|
|
@@ -274,7 +253,7 @@ ${changes.down}`, { encoding: "utf-8" });
|
|
|
274
253
|
* @return The content of the SQL migration script as a string.
|
|
275
254
|
*/
|
|
276
255
|
getUpMigration(toVersion) {
|
|
277
|
-
return readFileSync(`${this.migrationsPath}/${FILENAME_UP_PREFIX}${toVersion
|
|
256
|
+
return readFileSync(`${this.migrationsPath}/${FILENAME_UP_PREFIX}${toVersion}.sql`, { encoding: "utf-8" });
|
|
278
257
|
}
|
|
279
258
|
/**
|
|
280
259
|
* Retrieves the SQL migration script for downgrading to the specified version.
|
|
@@ -288,13 +267,14 @@ ${changes.down}`, { encoding: "utf-8" });
|
|
|
288
267
|
};
|
|
289
268
|
|
|
290
269
|
// src/lib/dialects/DefaultSql.ts
|
|
270
|
+
var MIGRATION_DATA_TABLE_NAME = "__sqlmorpheus_migrations";
|
|
291
271
|
var DefaultSql = class {
|
|
292
272
|
canAlterForeignKeys = false;
|
|
293
273
|
canAlterPrimaryKey = false;
|
|
294
274
|
canInspectForeignKeys = false;
|
|
295
275
|
canInspectPrimaryKey = false;
|
|
296
276
|
typeString = "TEXT";
|
|
297
|
-
|
|
277
|
+
typeInt = "INTEGER";
|
|
298
278
|
typeBoolean = "BOOLEAN";
|
|
299
279
|
typeNull = "NULL";
|
|
300
280
|
formatValueToSql(value) {
|
|
@@ -337,8 +317,8 @@ var DefaultSql = class {
|
|
|
337
317
|
dropTable(tableName) {
|
|
338
318
|
return `DROP TABLE IF EXISTS ${tableName};`;
|
|
339
319
|
}
|
|
340
|
-
columnDefinition(
|
|
341
|
-
const query = `${
|
|
320
|
+
columnDefinition(columnName, type, defaultValue, isPrimaryKey) {
|
|
321
|
+
const query = `${columnName} ${type} DEFAULT ${defaultValue}`;
|
|
342
322
|
return isPrimaryKey ? `${query} PRIMARY KEY` : query;
|
|
343
323
|
}
|
|
344
324
|
createColumn(columnTable, entry) {
|
|
@@ -365,6 +345,30 @@ var DefaultSql = class {
|
|
|
365
345
|
insertValues(keys, valueString) {
|
|
366
346
|
return `(${keys}) ${valueString ?? `VALUES (${keys.map(() => "?").join(",")})`}`;
|
|
367
347
|
}
|
|
348
|
+
migrationTableQuery() {
|
|
349
|
+
return this.createTable(MIGRATION_DATA_TABLE_NAME, [
|
|
350
|
+
this.columnDefinition("version", this.typeInt, "0", false)
|
|
351
|
+
]);
|
|
352
|
+
}
|
|
353
|
+
async createMigrationTableIfNeeded(db) {
|
|
354
|
+
await db.runMultipleWriteStatements(this.migrationTableQuery());
|
|
355
|
+
}
|
|
356
|
+
//TODO: untested
|
|
357
|
+
async getVersion(db) {
|
|
358
|
+
await this.createMigrationTableIfNeeded(db);
|
|
359
|
+
const query = this.select(MIGRATION_DATA_TABLE_NAME, ["version"]);
|
|
360
|
+
const data = await db.runGetStatement(query);
|
|
361
|
+
return data.length ? data[0].version : 0;
|
|
362
|
+
}
|
|
363
|
+
//TODO: untested
|
|
364
|
+
async setVersion(db, newVersion) {
|
|
365
|
+
const query = `${this.migrationTableQuery()};
|
|
366
|
+
INSERT INTO ${MIGRATION_DATA_TABLE_NAME} (version) VALUES ${newVersion} WHERE NOT EXISTS (SELECT 1 FROM ${MIGRATION_DATA_TABLE_NAME})
|
|
367
|
+
UNION ALL
|
|
368
|
+
UPDATE ${MIGRATION_DATA_TABLE_NAME} SET version = ${newVersion} WHERE EXISTS (SELECT 1 FROM ${MIGRATION_DATA_TABLE_NAME});
|
|
369
|
+
`;
|
|
370
|
+
await db.runMultipleWriteStatements(query);
|
|
371
|
+
}
|
|
368
372
|
};
|
|
369
373
|
|
|
370
374
|
// src/lib/dialects/SqliteDialect.ts
|
|
@@ -412,6 +416,13 @@ var SqliteDialect = class extends DefaultSql {
|
|
|
412
416
|
return value.toString();
|
|
413
417
|
}
|
|
414
418
|
}
|
|
419
|
+
async getVersion(db) {
|
|
420
|
+
const output = await db.runGetStatement(`PRAGMA user_version;`);
|
|
421
|
+
return output[0].user_version;
|
|
422
|
+
}
|
|
423
|
+
async setVersion(db, newVersion) {
|
|
424
|
+
await db.runMultipleWriteStatements(`PRAGMA user_version = ${newVersion};`);
|
|
425
|
+
}
|
|
415
426
|
};
|
|
416
427
|
|
|
417
428
|
// src/lib/dialects/PostgresDialect.ts
|
|
@@ -550,6 +561,22 @@ var MySqlDialect = class extends DefaultSql {
|
|
|
550
561
|
}
|
|
551
562
|
};
|
|
552
563
|
|
|
564
|
+
// src/lib/getDialect.ts
|
|
565
|
+
function getDialect(dbInstructions) {
|
|
566
|
+
switch (dbInstructions.dialect) {
|
|
567
|
+
case "Sqlite":
|
|
568
|
+
return new SqliteDialect();
|
|
569
|
+
case "Postgres":
|
|
570
|
+
return new PostgresDialect();
|
|
571
|
+
case "MsSql":
|
|
572
|
+
return new MsSqlDialect();
|
|
573
|
+
case "MySql":
|
|
574
|
+
return new MySqlDialect();
|
|
575
|
+
default:
|
|
576
|
+
throw new Error(`Unknown dialect ${dbInstructions.dialect}`);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
|
|
553
580
|
// src/lib/MigrationManager.ts
|
|
554
581
|
var MigrationManager = class {
|
|
555
582
|
migrations = new Migrations();
|
|
@@ -560,22 +587,7 @@ var MigrationManager = class {
|
|
|
560
587
|
migrationHistoryManager;
|
|
561
588
|
dialect;
|
|
562
589
|
constructor(db, dbInstructions) {
|
|
563
|
-
|
|
564
|
-
case "Sqlite":
|
|
565
|
-
this.dialect = new SqliteDialect();
|
|
566
|
-
break;
|
|
567
|
-
case "Postgres":
|
|
568
|
-
this.dialect = new PostgresDialect();
|
|
569
|
-
break;
|
|
570
|
-
case "MsSql":
|
|
571
|
-
this.dialect = new MsSqlDialect();
|
|
572
|
-
break;
|
|
573
|
-
case "MySql":
|
|
574
|
-
this.dialect = new MySqlDialect();
|
|
575
|
-
break;
|
|
576
|
-
default:
|
|
577
|
-
throw new Error(`Unknown dialect ${dbInstructions.dialect}`);
|
|
578
|
-
}
|
|
590
|
+
this.dialect = getDialect(dbInstructions);
|
|
579
591
|
this.db = db;
|
|
580
592
|
this.dbInstructions = dbInstructions;
|
|
581
593
|
this.tableStructureGenerator = new TableStructureGenerator(dbInstructions, this.dialect);
|
|
@@ -595,7 +607,7 @@ var MigrationManager = class {
|
|
|
595
607
|
async getMigrateSql() {
|
|
596
608
|
if (this.dbInstructions.version <= 0)
|
|
597
609
|
throw new Error("Cannot migrate to version 0 or lower");
|
|
598
|
-
const fromVersion = this.
|
|
610
|
+
const fromVersion = await this.dialect.getVersion(this.db);
|
|
599
611
|
if (fromVersion == 0) {
|
|
600
612
|
Logger.log(`Creating initial migration to version ${this.dbInstructions.version}`);
|
|
601
613
|
return this.createAndDropTables();
|
|
@@ -671,7 +683,8 @@ ${foreignKeyOnQuery}`;
|
|
|
671
683
|
const changes = await this.getMigrateSql();
|
|
672
684
|
if (!changes)
|
|
673
685
|
return;
|
|
674
|
-
this.
|
|
686
|
+
const fromVersion = await this.dialect.getVersion(this.db);
|
|
687
|
+
this.migrationHistoryManager.createMigrationHistory(fromVersion, this.dbInstructions.version, changes, overwriteExisting);
|
|
675
688
|
}
|
|
676
689
|
/**
|
|
677
690
|
* Generates SQL change statements for creating and dropping tables based on the current table structure
|
|
@@ -975,7 +988,8 @@ async function prepareMigration(db, instructions, overwriteExisting) {
|
|
|
975
988
|
async function runPreparedMigrations(db, instructions) {
|
|
976
989
|
Logger.setMode(instructions.loggerMode);
|
|
977
990
|
const migrationHistoryManager = new MigrationHistoryManager(instructions.configPath);
|
|
978
|
-
const
|
|
991
|
+
const dialect = getDialect(instructions);
|
|
992
|
+
const fromVersion = await dialect.getVersion(db);
|
|
979
993
|
const toVersion = instructions.version;
|
|
980
994
|
if (fromVersion == toVersion)
|
|
981
995
|
return;
|
|
@@ -986,7 +1000,7 @@ async function runPreparedMigrations(db, instructions) {
|
|
|
986
1000
|
await db.runMultipleWriteStatements(upChanges);
|
|
987
1001
|
instructions.version = i;
|
|
988
1002
|
}
|
|
989
|
-
|
|
1003
|
+
await dialect.setVersion(db, toVersion);
|
|
990
1004
|
}
|
|
991
1005
|
async function prepareAndRunMigration(db, instructions, overwriteExisting) {
|
|
992
1006
|
Logger.setMode(instructions.loggerMode);
|
|
@@ -1003,7 +1017,8 @@ async function runMigrationWithoutHistory(db, instructions) {
|
|
|
1003
1017
|
async function rollback(db, instructions, toVersion) {
|
|
1004
1018
|
Logger.setMode(instructions.loggerMode);
|
|
1005
1019
|
const migrationHistoryManager = new MigrationHistoryManager(instructions.configPath);
|
|
1006
|
-
const
|
|
1020
|
+
const dialect = getDialect(instructions);
|
|
1021
|
+
const fromVersion = await dialect.getVersion(db);
|
|
1007
1022
|
Logger.log(`Rolling back from ${fromVersion} to ${toVersion}`);
|
|
1008
1023
|
for (let i = fromVersion - 1; i >= toVersion; --i) {
|
|
1009
1024
|
const upChanges = migrationHistoryManager.getDownMigration(i);
|
|
@@ -1011,7 +1026,7 @@ async function rollback(db, instructions, toVersion) {
|
|
|
1011
1026
|
await db.runMultipleWriteStatements(upChanges);
|
|
1012
1027
|
instructions.version = i;
|
|
1013
1028
|
}
|
|
1014
|
-
|
|
1029
|
+
await dialect.setVersion(db, toVersion);
|
|
1015
1030
|
}
|
|
1016
1031
|
export {
|
|
1017
1032
|
DbTable,
|
package/package.json
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "sqlmorpheus",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"author": "JodliDev",
|
|
5
|
-
"keywords": [],
|
|
6
|
-
"license": "MIT",
|
|
7
|
-
"description": "",
|
|
8
|
-
"repository": {
|
|
9
|
-
"type": "git",
|
|
10
|
-
"url": "git://github.com/JodliDev/SqlMorpheus.git"
|
|
11
|
-
},
|
|
12
|
-
"bugs": {
|
|
13
|
-
"url": "https://github.com/JodliDev/SqlMorpheus/issues"
|
|
14
|
-
},
|
|
15
|
-
"homepage": "https://github.com/JodliDev/SqlMorpheus#readme",
|
|
16
|
-
"main": "dist/index.js",
|
|
17
|
-
"types": "dist/index.d.ts",
|
|
18
|
-
"files": [
|
|
19
|
-
"/dist"
|
|
20
|
-
],
|
|
21
|
-
"scripts": {
|
|
22
|
-
"build": "tsup",
|
|
23
|
-
"dev": "tsup --watch",
|
|
24
|
-
"test": "vitest run",
|
|
25
|
-
"test:watch": "vitest",
|
|
26
|
-
"
|
|
27
|
-
},
|
|
28
|
-
"devDependencies": {
|
|
29
|
-
"@types/better-sqlite3": "^7.6.13",
|
|
30
|
-
"@types/node": "^24.0.13",
|
|
31
|
-
"better-sqlite3": "^12.2.0",
|
|
32
|
-
"tsup": "^8.5.0",
|
|
33
|
-
"typescript": "^5.8.3",
|
|
34
|
-
"vitest": "^3.2.4"
|
|
35
|
-
},
|
|
36
|
-
"dependencies": {
|
|
37
|
-
"polyfill-symbol-metadata": "^0.0.2"
|
|
38
|
-
}
|
|
39
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "sqlmorpheus",
|
|
3
|
+
"version": "0.1.6",
|
|
4
|
+
"author": "JodliDev",
|
|
5
|
+
"keywords": [],
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"description": "",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git://github.com/JodliDev/SqlMorpheus.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/JodliDev/SqlMorpheus/issues"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://github.com/JodliDev/SqlMorpheus#readme",
|
|
16
|
+
"main": "dist/index.js",
|
|
17
|
+
"types": "dist/index.d.ts",
|
|
18
|
+
"files": [
|
|
19
|
+
"/dist"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsup",
|
|
23
|
+
"dev": "tsup --watch",
|
|
24
|
+
"test": "vitest run",
|
|
25
|
+
"test:watch": "vitest",
|
|
26
|
+
"run_publish": "npm publish"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/better-sqlite3": "^7.6.13",
|
|
30
|
+
"@types/node": "^24.0.13",
|
|
31
|
+
"better-sqlite3": "^12.2.0",
|
|
32
|
+
"tsup": "^8.5.0",
|
|
33
|
+
"typescript": "^5.8.3",
|
|
34
|
+
"vitest": "^3.2.4"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"polyfill-symbol-metadata": "^0.0.2"
|
|
38
|
+
}
|
|
39
|
+
}
|