sqlmorpheus 0.2.4 → 0.2.5
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.mts +2 -5
- package/dist/index.d.ts +2 -5
- package/dist/index.js +35 -65
- package/dist/index.mjs +35 -62
- package/package.json +3 -1
package/dist/index.d.mts
CHANGED
|
@@ -347,10 +347,7 @@ declare function ForeignKey<TOther extends TableClassInterface>(toTable: Class<T
|
|
|
347
347
|
onUpdate?: ForeignKeyActions;
|
|
348
348
|
}): (table: any, context: any) => void;
|
|
349
349
|
|
|
350
|
-
declare function
|
|
351
|
-
declare function runPreparedMigrations(db: DatabaseAccess, dbInstructions: DatabaseInstructions): Promise<void>;
|
|
352
|
-
declare function prepareAndRunMigration(db: DatabaseAccess, instructions: DatabaseInstructions, overwriteExisting?: boolean): Promise<void>;
|
|
353
|
-
declare function runMigrationWithoutHistory(db: DatabaseAccess, dbInstructions: DatabaseInstructions): Promise<void>;
|
|
350
|
+
declare function runMigration(db: DatabaseAccess, dbInstructions: DatabaseInstructions): Promise<void>;
|
|
354
351
|
declare function rollback(db: DatabaseAccess, dbInstructions: DatabaseInstructions, toVersion: number): Promise<void>;
|
|
355
352
|
|
|
356
|
-
export { DataType, type DatabaseAccess, type DatabaseInstructions, ForeignKey, type PublicMigrations, type SqlChanges, TableClass, TableObj,
|
|
353
|
+
export { DataType, type DatabaseAccess, type DatabaseInstructions, ForeignKey, type PublicMigrations, type SqlChanges, TableClass, TableObj, rollback, runMigration };
|
package/dist/index.d.ts
CHANGED
|
@@ -347,10 +347,7 @@ declare function ForeignKey<TOther extends TableClassInterface>(toTable: Class<T
|
|
|
347
347
|
onUpdate?: ForeignKeyActions;
|
|
348
348
|
}): (table: any, context: any) => void;
|
|
349
349
|
|
|
350
|
-
declare function
|
|
351
|
-
declare function runPreparedMigrations(db: DatabaseAccess, dbInstructions: DatabaseInstructions): Promise<void>;
|
|
352
|
-
declare function prepareAndRunMigration(db: DatabaseAccess, instructions: DatabaseInstructions, overwriteExisting?: boolean): Promise<void>;
|
|
353
|
-
declare function runMigrationWithoutHistory(db: DatabaseAccess, dbInstructions: DatabaseInstructions): Promise<void>;
|
|
350
|
+
declare function runMigration(db: DatabaseAccess, dbInstructions: DatabaseInstructions): Promise<void>;
|
|
354
351
|
declare function rollback(db: DatabaseAccess, dbInstructions: DatabaseInstructions, toVersion: number): Promise<void>;
|
|
355
352
|
|
|
356
|
-
export { DataType, type DatabaseAccess, type DatabaseInstructions, ForeignKey, type PublicMigrations, type SqlChanges, TableClass, TableObj,
|
|
353
|
+
export { DataType, type DatabaseAccess, type DatabaseInstructions, ForeignKey, type PublicMigrations, type SqlChanges, TableClass, TableObj, rollback, runMigration };
|
package/dist/index.js
CHANGED
|
@@ -24,11 +24,8 @@ __export(index_exports, {
|
|
|
24
24
|
ForeignKey: () => ForeignKey,
|
|
25
25
|
TableClass: () => TableClass,
|
|
26
26
|
TableObj: () => TableObj,
|
|
27
|
-
prepareAndRunMigration: () => prepareAndRunMigration,
|
|
28
|
-
prepareMigration: () => prepareMigration,
|
|
29
27
|
rollback: () => rollback,
|
|
30
|
-
|
|
31
|
-
runPreparedMigrations: () => runPreparedMigrations
|
|
28
|
+
runMigration: () => runMigration
|
|
32
29
|
});
|
|
33
30
|
module.exports = __toCommonJS(index_exports);
|
|
34
31
|
|
|
@@ -101,6 +98,7 @@ var TableObj = class _TableObj {
|
|
|
101
98
|
}
|
|
102
99
|
constructor(tableName, defaultValues) {
|
|
103
100
|
this.tableName = tableName;
|
|
101
|
+
this.tableStructure.table = tableName;
|
|
104
102
|
for (const key in defaultValues) {
|
|
105
103
|
const value = defaultValues[key];
|
|
106
104
|
const columnEntry = _TableObj.getColumnEntry(key, value);
|
|
@@ -110,6 +108,9 @@ var TableObj = class _TableObj {
|
|
|
110
108
|
}
|
|
111
109
|
}
|
|
112
110
|
primaryKey(key) {
|
|
111
|
+
if (this.tableStructure.primaryKey) {
|
|
112
|
+
this.tableStructure.columns[this.tableStructure.primaryKey].isPrimaryKey = false;
|
|
113
|
+
}
|
|
113
114
|
this.tableStructure.primaryKey = key.toString();
|
|
114
115
|
this.tableStructure.columns[key.toString()].isPrimaryKey = true;
|
|
115
116
|
return this;
|
|
@@ -510,6 +511,7 @@ var Migrations = class {
|
|
|
510
511
|
const entry = this.getEntry(table);
|
|
511
512
|
entry.recreate = true;
|
|
512
513
|
Logger.log(`Table ${this.getTableName(table)} will be recreated!`);
|
|
514
|
+
this.allowMigration(version, table, "recreateTable");
|
|
513
515
|
}
|
|
514
516
|
};
|
|
515
517
|
|
|
@@ -609,17 +611,19 @@ ${foreignKeyOnQuery}`;
|
|
|
609
611
|
changes.down += `
|
|
610
612
|
|
|
611
613
|
${foreignKeyOnQuery}`;
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
614
|
+
if (fromVersion != 0) {
|
|
615
|
+
const exception = this.migrations.verifyRenamingTasks(this.newTables) ?? this.migrations.verifyAllowedMigrations();
|
|
616
|
+
if (exception) {
|
|
617
|
+
Logger.debug(`
|
|
615
618
|
Canceled changes:
|
|
616
619
|
${changes.up}
|
|
617
620
|
`);
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
621
|
+
if (dbInstructions.throwIfNotAllowed)
|
|
622
|
+
throw exception;
|
|
623
|
+
else {
|
|
624
|
+
console.error(exception.message);
|
|
625
|
+
return null;
|
|
626
|
+
}
|
|
623
627
|
}
|
|
624
628
|
}
|
|
625
629
|
return {
|
|
@@ -858,9 +862,13 @@ ${changes.up}
|
|
|
858
862
|
this.migrations.internalRecreate(newTableDefinition.table, "Dialect does not support altering primary keys");
|
|
859
863
|
continue;
|
|
860
864
|
}
|
|
865
|
+
const renamedColumns = this.migrations.getMigrationData()[tableName]?.renamedColumns;
|
|
861
866
|
for (const key in newColumnList) {
|
|
862
867
|
const newColumn = newColumnList[key];
|
|
863
868
|
const oldColumn = oldColumnNewNameIndex[newColumn.name];
|
|
869
|
+
const willBeRenamed = renamedColumns?.find((entry) => entry.newName);
|
|
870
|
+
if (willBeRenamed)
|
|
871
|
+
continue;
|
|
864
872
|
if (oldColumn == void 0) {
|
|
865
873
|
changes.up += this.dialect.createColumn(tableName, this.dialect.columnDefinition(newColumn.name, newColumn.type, newColumn.isPrimaryKey, newColumn.defaultValue)) + "\n";
|
|
866
874
|
changes.down += this.dialect.dropColumn(tableName, newColumn.name) + "\n";
|
|
@@ -946,12 +954,7 @@ var MigrationHistoryManager = class {
|
|
|
946
954
|
else if ((0, import_node_fs.existsSync)(downPath) && !overwriteExisting)
|
|
947
955
|
throw new Error(`Migration ${downPath} already exists!`);
|
|
948
956
|
Logger.log(`Save migrations to ${this.migrationsPath}`);
|
|
949
|
-
|
|
950
|
-
-- To: Version ${toVersion}
|
|
951
|
-
${changes.up}`, { encoding: "utf-8" });
|
|
952
|
-
(0, import_node_fs.writeFileSync)(`${this.migrationsPath}/${FILENAME_DOWN_PREFIX}${fromVersion}.sql`, `-- From: Version ${toVersion}
|
|
953
|
-
-- To: Version ${fromVersion}
|
|
954
|
-
${changes.down}`, { encoding: "utf-8" });
|
|
957
|
+
return changes;
|
|
955
958
|
}
|
|
956
959
|
/**
|
|
957
960
|
* Retrieves the SQL migration script for upgrading to the specified version.
|
|
@@ -1528,7 +1531,7 @@ var MySqlDialect = class extends DefaultSql {
|
|
|
1528
1531
|
return `ALTER TABLE ${tableName} PRIMARY KEY;`;
|
|
1529
1532
|
}
|
|
1530
1533
|
async getForeignKeys(tableName) {
|
|
1531
|
-
const data = await this.db.runGetStatement(`SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME,
|
|
1534
|
+
const data = await this.db.runGetStatement(`SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME,REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = (SELECT DATABASE()) AND REFERENCED_TABLE_NAME = '${tableName}';`);
|
|
1532
1535
|
return data.map((entry) => {
|
|
1533
1536
|
return {
|
|
1534
1537
|
fromTable: tableName,
|
|
@@ -1539,19 +1542,20 @@ var MySqlDialect = class extends DefaultSql {
|
|
|
1539
1542
|
});
|
|
1540
1543
|
}
|
|
1541
1544
|
async getTableNames() {
|
|
1542
|
-
const
|
|
1543
|
-
return
|
|
1545
|
+
const entries = await this.db.runGetStatement("SHOW TABLES");
|
|
1546
|
+
return entries.map((entry) => Object.values(entry)[0]);
|
|
1544
1547
|
}
|
|
1545
1548
|
async getColumnInformation(tableName) {
|
|
1546
1549
|
const data = await this.db.runGetStatement(`SHOW COLUMNS FROM ${tableName};`);
|
|
1547
1550
|
const output = {};
|
|
1548
1551
|
for (const entry of data) {
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1552
|
+
const lengthMatch = entry["Type"].match(/^\w+\((\d+)\)/);
|
|
1553
|
+
output[entry["Field"]] = {
|
|
1554
|
+
name: entry["Field"],
|
|
1555
|
+
type: entry["Type"],
|
|
1556
|
+
maxLength: lengthMatch ? parseInt(lengthMatch[1]) ?? 0 : 0,
|
|
1557
|
+
defaultValue: entry["Default"],
|
|
1558
|
+
isPrimaryKey: entry["Key"] == "PRI"
|
|
1555
1559
|
};
|
|
1556
1560
|
}
|
|
1557
1561
|
return output;
|
|
@@ -1575,45 +1579,14 @@ function getDialect(db, dbInstructions) {
|
|
|
1575
1579
|
}
|
|
1576
1580
|
|
|
1577
1581
|
// src/index.ts
|
|
1578
|
-
async function
|
|
1579
|
-
Logger.setMode(dbInstructions.loggerMode);
|
|
1580
|
-
const dialect = getDialect(db, dbInstructions);
|
|
1581
|
-
const mm = new MigrationManager(dialect);
|
|
1582
|
-
const migration = await mm.generateSqlChanges(dbInstructions);
|
|
1583
|
-
if (!migration)
|
|
1584
|
-
return;
|
|
1585
|
-
const migrationHistoryManager = new MigrationHistoryManager(dbInstructions.configPath);
|
|
1586
|
-
migrationHistoryManager.createMigrationHistory(migration.fromVersion, dbInstructions.version, migration.changes, overwriteExisting);
|
|
1587
|
-
}
|
|
1588
|
-
async function runPreparedMigrations(db, dbInstructions) {
|
|
1589
|
-
Logger.setMode(dbInstructions.loggerMode);
|
|
1590
|
-
const historyManager = new MigrationHistoryManager(dbInstructions.configPath);
|
|
1591
|
-
const dialect = getDialect(db, dbInstructions);
|
|
1592
|
-
const fromVersion = await dialect.getVersion();
|
|
1593
|
-
const toVersion = dbInstructions.version;
|
|
1594
|
-
if (fromVersion == toVersion)
|
|
1595
|
-
return;
|
|
1596
|
-
await db.createBackup?.(`from_${fromVersion}_to_${dbInstructions.version}`);
|
|
1597
|
-
Logger.log(`Running migrations from ${fromVersion} to ${toVersion}`);
|
|
1598
|
-
for (let i = fromVersion ? fromVersion + 1 : toVersion; i <= toVersion; ++i) {
|
|
1599
|
-
const upChanges = historyManager.getUpMigration(i);
|
|
1600
|
-
Logger.debug(upChanges);
|
|
1601
|
-
await db.runMultipleWriteStatements(upChanges);
|
|
1602
|
-
dbInstructions.version = i;
|
|
1603
|
-
}
|
|
1604
|
-
await dialect.setVersion(toVersion);
|
|
1605
|
-
}
|
|
1606
|
-
async function prepareAndRunMigration(db, instructions, overwriteExisting) {
|
|
1607
|
-
Logger.setMode(instructions.loggerMode);
|
|
1608
|
-
await prepareMigration(db, instructions, overwriteExisting);
|
|
1609
|
-
await runPreparedMigrations(db, instructions);
|
|
1610
|
-
}
|
|
1611
|
-
async function runMigrationWithoutHistory(db, dbInstructions) {
|
|
1582
|
+
async function runMigration(db, dbInstructions) {
|
|
1612
1583
|
Logger.setMode(dbInstructions.loggerMode);
|
|
1613
1584
|
const dialect = getDialect(db, dbInstructions);
|
|
1614
1585
|
const mm = new MigrationManager(dialect);
|
|
1615
1586
|
const migration = await mm.generateSqlChanges(dbInstructions);
|
|
1616
1587
|
if (migration) {
|
|
1588
|
+
const migrationHistoryManager = new MigrationHistoryManager(dbInstructions.configPath);
|
|
1589
|
+
migrationHistoryManager.createMigrationHistory(migration.fromVersion, dbInstructions.version, migration.changes);
|
|
1617
1590
|
await db.createBackup?.(`from_${migration.fromVersion}_to_${dbInstructions.version}`);
|
|
1618
1591
|
await db.runMultipleWriteStatements(migration.changes.up);
|
|
1619
1592
|
}
|
|
@@ -1639,9 +1612,6 @@ async function rollback(db, dbInstructions, toVersion) {
|
|
|
1639
1612
|
ForeignKey,
|
|
1640
1613
|
TableClass,
|
|
1641
1614
|
TableObj,
|
|
1642
|
-
prepareAndRunMigration,
|
|
1643
|
-
prepareMigration,
|
|
1644
1615
|
rollback,
|
|
1645
|
-
|
|
1646
|
-
runPreparedMigrations
|
|
1616
|
+
runMigration
|
|
1647
1617
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -67,6 +67,7 @@ var TableObj = class _TableObj {
|
|
|
67
67
|
}
|
|
68
68
|
constructor(tableName, defaultValues) {
|
|
69
69
|
this.tableName = tableName;
|
|
70
|
+
this.tableStructure.table = tableName;
|
|
70
71
|
for (const key in defaultValues) {
|
|
71
72
|
const value = defaultValues[key];
|
|
72
73
|
const columnEntry = _TableObj.getColumnEntry(key, value);
|
|
@@ -76,6 +77,9 @@ var TableObj = class _TableObj {
|
|
|
76
77
|
}
|
|
77
78
|
}
|
|
78
79
|
primaryKey(key) {
|
|
80
|
+
if (this.tableStructure.primaryKey) {
|
|
81
|
+
this.tableStructure.columns[this.tableStructure.primaryKey].isPrimaryKey = false;
|
|
82
|
+
}
|
|
79
83
|
this.tableStructure.primaryKey = key.toString();
|
|
80
84
|
this.tableStructure.columns[key.toString()].isPrimaryKey = true;
|
|
81
85
|
return this;
|
|
@@ -476,6 +480,7 @@ var Migrations = class {
|
|
|
476
480
|
const entry = this.getEntry(table);
|
|
477
481
|
entry.recreate = true;
|
|
478
482
|
Logger.log(`Table ${this.getTableName(table)} will be recreated!`);
|
|
483
|
+
this.allowMigration(version, table, "recreateTable");
|
|
479
484
|
}
|
|
480
485
|
};
|
|
481
486
|
|
|
@@ -575,17 +580,19 @@ ${foreignKeyOnQuery}`;
|
|
|
575
580
|
changes.down += `
|
|
576
581
|
|
|
577
582
|
${foreignKeyOnQuery}`;
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
583
|
+
if (fromVersion != 0) {
|
|
584
|
+
const exception = this.migrations.verifyRenamingTasks(this.newTables) ?? this.migrations.verifyAllowedMigrations();
|
|
585
|
+
if (exception) {
|
|
586
|
+
Logger.debug(`
|
|
581
587
|
Canceled changes:
|
|
582
588
|
${changes.up}
|
|
583
589
|
`);
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
590
|
+
if (dbInstructions.throwIfNotAllowed)
|
|
591
|
+
throw exception;
|
|
592
|
+
else {
|
|
593
|
+
console.error(exception.message);
|
|
594
|
+
return null;
|
|
595
|
+
}
|
|
589
596
|
}
|
|
590
597
|
}
|
|
591
598
|
return {
|
|
@@ -824,9 +831,13 @@ ${changes.up}
|
|
|
824
831
|
this.migrations.internalRecreate(newTableDefinition.table, "Dialect does not support altering primary keys");
|
|
825
832
|
continue;
|
|
826
833
|
}
|
|
834
|
+
const renamedColumns = this.migrations.getMigrationData()[tableName]?.renamedColumns;
|
|
827
835
|
for (const key in newColumnList) {
|
|
828
836
|
const newColumn = newColumnList[key];
|
|
829
837
|
const oldColumn = oldColumnNewNameIndex[newColumn.name];
|
|
838
|
+
const willBeRenamed = renamedColumns?.find((entry) => entry.newName);
|
|
839
|
+
if (willBeRenamed)
|
|
840
|
+
continue;
|
|
830
841
|
if (oldColumn == void 0) {
|
|
831
842
|
changes.up += this.dialect.createColumn(tableName, this.dialect.columnDefinition(newColumn.name, newColumn.type, newColumn.isPrimaryKey, newColumn.defaultValue)) + "\n";
|
|
832
843
|
changes.down += this.dialect.dropColumn(tableName, newColumn.name) + "\n";
|
|
@@ -883,7 +894,7 @@ ${changes.up}
|
|
|
883
894
|
};
|
|
884
895
|
|
|
885
896
|
// src/lib/MigrationHistoryManager.ts
|
|
886
|
-
import { existsSync, mkdirSync
|
|
897
|
+
import { existsSync, mkdirSync } from "fs";
|
|
887
898
|
import { readFileSync } from "fs";
|
|
888
899
|
var FILENAME_UP_PREFIX = "up_to_";
|
|
889
900
|
var FILENAME_DOWN_PREFIX = "down_to_";
|
|
@@ -912,12 +923,7 @@ var MigrationHistoryManager = class {
|
|
|
912
923
|
else if (existsSync(downPath) && !overwriteExisting)
|
|
913
924
|
throw new Error(`Migration ${downPath} already exists!`);
|
|
914
925
|
Logger.log(`Save migrations to ${this.migrationsPath}`);
|
|
915
|
-
|
|
916
|
-
-- To: Version ${toVersion}
|
|
917
|
-
${changes.up}`, { encoding: "utf-8" });
|
|
918
|
-
writeFileSync(`${this.migrationsPath}/${FILENAME_DOWN_PREFIX}${fromVersion}.sql`, `-- From: Version ${toVersion}
|
|
919
|
-
-- To: Version ${fromVersion}
|
|
920
|
-
${changes.down}`, { encoding: "utf-8" });
|
|
926
|
+
return changes;
|
|
921
927
|
}
|
|
922
928
|
/**
|
|
923
929
|
* Retrieves the SQL migration script for upgrading to the specified version.
|
|
@@ -1494,7 +1500,7 @@ var MySqlDialect = class extends DefaultSql {
|
|
|
1494
1500
|
return `ALTER TABLE ${tableName} PRIMARY KEY;`;
|
|
1495
1501
|
}
|
|
1496
1502
|
async getForeignKeys(tableName) {
|
|
1497
|
-
const data = await this.db.runGetStatement(`SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME,
|
|
1503
|
+
const data = await this.db.runGetStatement(`SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME,REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = (SELECT DATABASE()) AND REFERENCED_TABLE_NAME = '${tableName}';`);
|
|
1498
1504
|
return data.map((entry) => {
|
|
1499
1505
|
return {
|
|
1500
1506
|
fromTable: tableName,
|
|
@@ -1505,19 +1511,20 @@ var MySqlDialect = class extends DefaultSql {
|
|
|
1505
1511
|
});
|
|
1506
1512
|
}
|
|
1507
1513
|
async getTableNames() {
|
|
1508
|
-
const
|
|
1509
|
-
return
|
|
1514
|
+
const entries = await this.db.runGetStatement("SHOW TABLES");
|
|
1515
|
+
return entries.map((entry) => Object.values(entry)[0]);
|
|
1510
1516
|
}
|
|
1511
1517
|
async getColumnInformation(tableName) {
|
|
1512
1518
|
const data = await this.db.runGetStatement(`SHOW COLUMNS FROM ${tableName};`);
|
|
1513
1519
|
const output = {};
|
|
1514
1520
|
for (const entry of data) {
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
+
const lengthMatch = entry["Type"].match(/^\w+\((\d+)\)/);
|
|
1522
|
+
output[entry["Field"]] = {
|
|
1523
|
+
name: entry["Field"],
|
|
1524
|
+
type: entry["Type"],
|
|
1525
|
+
maxLength: lengthMatch ? parseInt(lengthMatch[1]) ?? 0 : 0,
|
|
1526
|
+
defaultValue: entry["Default"],
|
|
1527
|
+
isPrimaryKey: entry["Key"] == "PRI"
|
|
1521
1528
|
};
|
|
1522
1529
|
}
|
|
1523
1530
|
return output;
|
|
@@ -1541,45 +1548,14 @@ function getDialect(db, dbInstructions) {
|
|
|
1541
1548
|
}
|
|
1542
1549
|
|
|
1543
1550
|
// src/index.ts
|
|
1544
|
-
async function
|
|
1545
|
-
Logger.setMode(dbInstructions.loggerMode);
|
|
1546
|
-
const dialect = getDialect(db, dbInstructions);
|
|
1547
|
-
const mm = new MigrationManager(dialect);
|
|
1548
|
-
const migration = await mm.generateSqlChanges(dbInstructions);
|
|
1549
|
-
if (!migration)
|
|
1550
|
-
return;
|
|
1551
|
-
const migrationHistoryManager = new MigrationHistoryManager(dbInstructions.configPath);
|
|
1552
|
-
migrationHistoryManager.createMigrationHistory(migration.fromVersion, dbInstructions.version, migration.changes, overwriteExisting);
|
|
1553
|
-
}
|
|
1554
|
-
async function runPreparedMigrations(db, dbInstructions) {
|
|
1555
|
-
Logger.setMode(dbInstructions.loggerMode);
|
|
1556
|
-
const historyManager = new MigrationHistoryManager(dbInstructions.configPath);
|
|
1557
|
-
const dialect = getDialect(db, dbInstructions);
|
|
1558
|
-
const fromVersion = await dialect.getVersion();
|
|
1559
|
-
const toVersion = dbInstructions.version;
|
|
1560
|
-
if (fromVersion == toVersion)
|
|
1561
|
-
return;
|
|
1562
|
-
await db.createBackup?.(`from_${fromVersion}_to_${dbInstructions.version}`);
|
|
1563
|
-
Logger.log(`Running migrations from ${fromVersion} to ${toVersion}`);
|
|
1564
|
-
for (let i = fromVersion ? fromVersion + 1 : toVersion; i <= toVersion; ++i) {
|
|
1565
|
-
const upChanges = historyManager.getUpMigration(i);
|
|
1566
|
-
Logger.debug(upChanges);
|
|
1567
|
-
await db.runMultipleWriteStatements(upChanges);
|
|
1568
|
-
dbInstructions.version = i;
|
|
1569
|
-
}
|
|
1570
|
-
await dialect.setVersion(toVersion);
|
|
1571
|
-
}
|
|
1572
|
-
async function prepareAndRunMigration(db, instructions, overwriteExisting) {
|
|
1573
|
-
Logger.setMode(instructions.loggerMode);
|
|
1574
|
-
await prepareMigration(db, instructions, overwriteExisting);
|
|
1575
|
-
await runPreparedMigrations(db, instructions);
|
|
1576
|
-
}
|
|
1577
|
-
async function runMigrationWithoutHistory(db, dbInstructions) {
|
|
1551
|
+
async function runMigration(db, dbInstructions) {
|
|
1578
1552
|
Logger.setMode(dbInstructions.loggerMode);
|
|
1579
1553
|
const dialect = getDialect(db, dbInstructions);
|
|
1580
1554
|
const mm = new MigrationManager(dialect);
|
|
1581
1555
|
const migration = await mm.generateSqlChanges(dbInstructions);
|
|
1582
1556
|
if (migration) {
|
|
1557
|
+
const migrationHistoryManager = new MigrationHistoryManager(dbInstructions.configPath);
|
|
1558
|
+
migrationHistoryManager.createMigrationHistory(migration.fromVersion, dbInstructions.version, migration.changes);
|
|
1583
1559
|
await db.createBackup?.(`from_${migration.fromVersion}_to_${dbInstructions.version}`);
|
|
1584
1560
|
await db.runMultipleWriteStatements(migration.changes.up);
|
|
1585
1561
|
}
|
|
@@ -1604,9 +1580,6 @@ export {
|
|
|
1604
1580
|
ForeignKey,
|
|
1605
1581
|
TableClass,
|
|
1606
1582
|
TableObj,
|
|
1607
|
-
prepareAndRunMigration,
|
|
1608
|
-
prepareMigration,
|
|
1609
1583
|
rollback,
|
|
1610
|
-
|
|
1611
|
-
runPreparedMigrations
|
|
1584
|
+
runMigration
|
|
1612
1585
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sqlmorpheus",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"author": "JodliDev",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"license": "MIT",
|
|
@@ -26,9 +26,11 @@
|
|
|
26
26
|
"build_and_publish": "npm run build && npm publish"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
+
"@testcontainers/mysql": "^11.7.1",
|
|
29
30
|
"@types/better-sqlite3": "^7.6.13",
|
|
30
31
|
"@types/node": "^24.0.13",
|
|
31
32
|
"better-sqlite3": "^12.2.0",
|
|
33
|
+
"mysql2": "^3.15.2",
|
|
32
34
|
"tsup": "^8.5.0",
|
|
33
35
|
"typescript": "^5.8.3",
|
|
34
36
|
"vitest": "^3.2.4"
|