sqlmorpheus 0.2.3 → 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 +41 -68
- package/dist/index.mjs +41 -65
- 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;
|
|
@@ -187,7 +188,7 @@ var TableStructureGenerator = class {
|
|
|
187
188
|
} else {
|
|
188
189
|
const classTable = table;
|
|
189
190
|
structure = getTableStructure(classTable);
|
|
190
|
-
tables[classTable.name] =
|
|
191
|
+
tables[classTable.name] = structure;
|
|
191
192
|
}
|
|
192
193
|
for (const key in structure.columns) {
|
|
193
194
|
const entry = structure.columns[key];
|
|
@@ -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.
|
|
@@ -988,12 +991,15 @@ function TableClass(tableName, primaryKey) {
|
|
|
988
991
|
foreignKey.toTable = tableName;
|
|
989
992
|
}
|
|
990
993
|
}
|
|
991
|
-
|
|
992
|
-
|
|
994
|
+
const obj = new table();
|
|
995
|
+
for (const key in obj) {
|
|
996
|
+
const value = obj[key];
|
|
993
997
|
const existingEntry = tableStructure.columns[key];
|
|
994
998
|
const defaultEntry = TableObj.getColumnEntry(key, value);
|
|
995
999
|
if (!defaultEntry)
|
|
996
1000
|
continue;
|
|
1001
|
+
if (key == primaryKey)
|
|
1002
|
+
defaultEntry.isPrimaryKey = true;
|
|
997
1003
|
tableStructure.columns[key] = {
|
|
998
1004
|
...defaultEntry,
|
|
999
1005
|
...existingEntry
|
|
@@ -1525,7 +1531,7 @@ var MySqlDialect = class extends DefaultSql {
|
|
|
1525
1531
|
return `ALTER TABLE ${tableName} PRIMARY KEY;`;
|
|
1526
1532
|
}
|
|
1527
1533
|
async getForeignKeys(tableName) {
|
|
1528
|
-
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}';`);
|
|
1529
1535
|
return data.map((entry) => {
|
|
1530
1536
|
return {
|
|
1531
1537
|
fromTable: tableName,
|
|
@@ -1536,19 +1542,20 @@ var MySqlDialect = class extends DefaultSql {
|
|
|
1536
1542
|
});
|
|
1537
1543
|
}
|
|
1538
1544
|
async getTableNames() {
|
|
1539
|
-
const
|
|
1540
|
-
return
|
|
1545
|
+
const entries = await this.db.runGetStatement("SHOW TABLES");
|
|
1546
|
+
return entries.map((entry) => Object.values(entry)[0]);
|
|
1541
1547
|
}
|
|
1542
1548
|
async getColumnInformation(tableName) {
|
|
1543
1549
|
const data = await this.db.runGetStatement(`SHOW COLUMNS FROM ${tableName};`);
|
|
1544
1550
|
const output = {};
|
|
1545
1551
|
for (const entry of data) {
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
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"
|
|
1552
1559
|
};
|
|
1553
1560
|
}
|
|
1554
1561
|
return output;
|
|
@@ -1572,45 +1579,14 @@ function getDialect(db, dbInstructions) {
|
|
|
1572
1579
|
}
|
|
1573
1580
|
|
|
1574
1581
|
// src/index.ts
|
|
1575
|
-
async function
|
|
1576
|
-
Logger.setMode(dbInstructions.loggerMode);
|
|
1577
|
-
const dialect = getDialect(db, dbInstructions);
|
|
1578
|
-
const mm = new MigrationManager(dialect);
|
|
1579
|
-
const migration = await mm.generateSqlChanges(dbInstructions);
|
|
1580
|
-
if (!migration)
|
|
1581
|
-
return;
|
|
1582
|
-
const migrationHistoryManager = new MigrationHistoryManager(dbInstructions.configPath);
|
|
1583
|
-
migrationHistoryManager.createMigrationHistory(migration.fromVersion, dbInstructions.version, migration.changes, overwriteExisting);
|
|
1584
|
-
}
|
|
1585
|
-
async function runPreparedMigrations(db, dbInstructions) {
|
|
1586
|
-
Logger.setMode(dbInstructions.loggerMode);
|
|
1587
|
-
const historyManager = new MigrationHistoryManager(dbInstructions.configPath);
|
|
1588
|
-
const dialect = getDialect(db, dbInstructions);
|
|
1589
|
-
const fromVersion = await dialect.getVersion();
|
|
1590
|
-
const toVersion = dbInstructions.version;
|
|
1591
|
-
if (fromVersion == toVersion)
|
|
1592
|
-
return;
|
|
1593
|
-
await db.createBackup?.(`from_${fromVersion}_to_${dbInstructions.version}`);
|
|
1594
|
-
Logger.log(`Running migrations from ${fromVersion} to ${toVersion}`);
|
|
1595
|
-
for (let i = fromVersion ? fromVersion + 1 : toVersion; i <= toVersion; ++i) {
|
|
1596
|
-
const upChanges = historyManager.getUpMigration(i);
|
|
1597
|
-
Logger.debug(upChanges);
|
|
1598
|
-
await db.runMultipleWriteStatements(upChanges);
|
|
1599
|
-
dbInstructions.version = i;
|
|
1600
|
-
}
|
|
1601
|
-
await dialect.setVersion(toVersion);
|
|
1602
|
-
}
|
|
1603
|
-
async function prepareAndRunMigration(db, instructions, overwriteExisting) {
|
|
1604
|
-
Logger.setMode(instructions.loggerMode);
|
|
1605
|
-
await prepareMigration(db, instructions, overwriteExisting);
|
|
1606
|
-
await runPreparedMigrations(db, instructions);
|
|
1607
|
-
}
|
|
1608
|
-
async function runMigrationWithoutHistory(db, dbInstructions) {
|
|
1582
|
+
async function runMigration(db, dbInstructions) {
|
|
1609
1583
|
Logger.setMode(dbInstructions.loggerMode);
|
|
1610
1584
|
const dialect = getDialect(db, dbInstructions);
|
|
1611
1585
|
const mm = new MigrationManager(dialect);
|
|
1612
1586
|
const migration = await mm.generateSqlChanges(dbInstructions);
|
|
1613
1587
|
if (migration) {
|
|
1588
|
+
const migrationHistoryManager = new MigrationHistoryManager(dbInstructions.configPath);
|
|
1589
|
+
migrationHistoryManager.createMigrationHistory(migration.fromVersion, dbInstructions.version, migration.changes);
|
|
1614
1590
|
await db.createBackup?.(`from_${migration.fromVersion}_to_${dbInstructions.version}`);
|
|
1615
1591
|
await db.runMultipleWriteStatements(migration.changes.up);
|
|
1616
1592
|
}
|
|
@@ -1636,9 +1612,6 @@ async function rollback(db, dbInstructions, toVersion) {
|
|
|
1636
1612
|
ForeignKey,
|
|
1637
1613
|
TableClass,
|
|
1638
1614
|
TableObj,
|
|
1639
|
-
prepareAndRunMigration,
|
|
1640
|
-
prepareMigration,
|
|
1641
1615
|
rollback,
|
|
1642
|
-
|
|
1643
|
-
runPreparedMigrations
|
|
1616
|
+
runMigration
|
|
1644
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;
|
|
@@ -153,7 +157,7 @@ var TableStructureGenerator = class {
|
|
|
153
157
|
} else {
|
|
154
158
|
const classTable = table;
|
|
155
159
|
structure = getTableStructure(classTable);
|
|
156
|
-
tables[classTable.name] =
|
|
160
|
+
tables[classTable.name] = structure;
|
|
157
161
|
}
|
|
158
162
|
for (const key in structure.columns) {
|
|
159
163
|
const entry = structure.columns[key];
|
|
@@ -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.
|
|
@@ -954,12 +960,15 @@ function TableClass(tableName, primaryKey) {
|
|
|
954
960
|
foreignKey.toTable = tableName;
|
|
955
961
|
}
|
|
956
962
|
}
|
|
957
|
-
|
|
958
|
-
|
|
963
|
+
const obj = new table();
|
|
964
|
+
for (const key in obj) {
|
|
965
|
+
const value = obj[key];
|
|
959
966
|
const existingEntry = tableStructure.columns[key];
|
|
960
967
|
const defaultEntry = TableObj.getColumnEntry(key, value);
|
|
961
968
|
if (!defaultEntry)
|
|
962
969
|
continue;
|
|
970
|
+
if (key == primaryKey)
|
|
971
|
+
defaultEntry.isPrimaryKey = true;
|
|
963
972
|
tableStructure.columns[key] = {
|
|
964
973
|
...defaultEntry,
|
|
965
974
|
...existingEntry
|
|
@@ -1491,7 +1500,7 @@ var MySqlDialect = class extends DefaultSql {
|
|
|
1491
1500
|
return `ALTER TABLE ${tableName} PRIMARY KEY;`;
|
|
1492
1501
|
}
|
|
1493
1502
|
async getForeignKeys(tableName) {
|
|
1494
|
-
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}';`);
|
|
1495
1504
|
return data.map((entry) => {
|
|
1496
1505
|
return {
|
|
1497
1506
|
fromTable: tableName,
|
|
@@ -1502,19 +1511,20 @@ var MySqlDialect = class extends DefaultSql {
|
|
|
1502
1511
|
});
|
|
1503
1512
|
}
|
|
1504
1513
|
async getTableNames() {
|
|
1505
|
-
const
|
|
1506
|
-
return
|
|
1514
|
+
const entries = await this.db.runGetStatement("SHOW TABLES");
|
|
1515
|
+
return entries.map((entry) => Object.values(entry)[0]);
|
|
1507
1516
|
}
|
|
1508
1517
|
async getColumnInformation(tableName) {
|
|
1509
1518
|
const data = await this.db.runGetStatement(`SHOW COLUMNS FROM ${tableName};`);
|
|
1510
1519
|
const output = {};
|
|
1511
1520
|
for (const entry of data) {
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
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"
|
|
1518
1528
|
};
|
|
1519
1529
|
}
|
|
1520
1530
|
return output;
|
|
@@ -1538,45 +1548,14 @@ function getDialect(db, dbInstructions) {
|
|
|
1538
1548
|
}
|
|
1539
1549
|
|
|
1540
1550
|
// src/index.ts
|
|
1541
|
-
async function
|
|
1542
|
-
Logger.setMode(dbInstructions.loggerMode);
|
|
1543
|
-
const dialect = getDialect(db, dbInstructions);
|
|
1544
|
-
const mm = new MigrationManager(dialect);
|
|
1545
|
-
const migration = await mm.generateSqlChanges(dbInstructions);
|
|
1546
|
-
if (!migration)
|
|
1547
|
-
return;
|
|
1548
|
-
const migrationHistoryManager = new MigrationHistoryManager(dbInstructions.configPath);
|
|
1549
|
-
migrationHistoryManager.createMigrationHistory(migration.fromVersion, dbInstructions.version, migration.changes, overwriteExisting);
|
|
1550
|
-
}
|
|
1551
|
-
async function runPreparedMigrations(db, dbInstructions) {
|
|
1552
|
-
Logger.setMode(dbInstructions.loggerMode);
|
|
1553
|
-
const historyManager = new MigrationHistoryManager(dbInstructions.configPath);
|
|
1554
|
-
const dialect = getDialect(db, dbInstructions);
|
|
1555
|
-
const fromVersion = await dialect.getVersion();
|
|
1556
|
-
const toVersion = dbInstructions.version;
|
|
1557
|
-
if (fromVersion == toVersion)
|
|
1558
|
-
return;
|
|
1559
|
-
await db.createBackup?.(`from_${fromVersion}_to_${dbInstructions.version}`);
|
|
1560
|
-
Logger.log(`Running migrations from ${fromVersion} to ${toVersion}`);
|
|
1561
|
-
for (let i = fromVersion ? fromVersion + 1 : toVersion; i <= toVersion; ++i) {
|
|
1562
|
-
const upChanges = historyManager.getUpMigration(i);
|
|
1563
|
-
Logger.debug(upChanges);
|
|
1564
|
-
await db.runMultipleWriteStatements(upChanges);
|
|
1565
|
-
dbInstructions.version = i;
|
|
1566
|
-
}
|
|
1567
|
-
await dialect.setVersion(toVersion);
|
|
1568
|
-
}
|
|
1569
|
-
async function prepareAndRunMigration(db, instructions, overwriteExisting) {
|
|
1570
|
-
Logger.setMode(instructions.loggerMode);
|
|
1571
|
-
await prepareMigration(db, instructions, overwriteExisting);
|
|
1572
|
-
await runPreparedMigrations(db, instructions);
|
|
1573
|
-
}
|
|
1574
|
-
async function runMigrationWithoutHistory(db, dbInstructions) {
|
|
1551
|
+
async function runMigration(db, dbInstructions) {
|
|
1575
1552
|
Logger.setMode(dbInstructions.loggerMode);
|
|
1576
1553
|
const dialect = getDialect(db, dbInstructions);
|
|
1577
1554
|
const mm = new MigrationManager(dialect);
|
|
1578
1555
|
const migration = await mm.generateSqlChanges(dbInstructions);
|
|
1579
1556
|
if (migration) {
|
|
1557
|
+
const migrationHistoryManager = new MigrationHistoryManager(dbInstructions.configPath);
|
|
1558
|
+
migrationHistoryManager.createMigrationHistory(migration.fromVersion, dbInstructions.version, migration.changes);
|
|
1580
1559
|
await db.createBackup?.(`from_${migration.fromVersion}_to_${dbInstructions.version}`);
|
|
1581
1560
|
await db.runMultipleWriteStatements(migration.changes.up);
|
|
1582
1561
|
}
|
|
@@ -1601,9 +1580,6 @@ export {
|
|
|
1601
1580
|
ForeignKey,
|
|
1602
1581
|
TableClass,
|
|
1603
1582
|
TableObj,
|
|
1604
|
-
prepareAndRunMigration,
|
|
1605
|
-
prepareMigration,
|
|
1606
1583
|
rollback,
|
|
1607
|
-
|
|
1608
|
-
runPreparedMigrations
|
|
1584
|
+
runMigration
|
|
1609
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"
|