sqlmorpheus 0.1.8 → 0.1.9

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.mjs CHANGED
@@ -40,31 +40,36 @@ function getTableInfoFromMetadata(metadata) {
40
40
  // src/lib/TableStructureGenerator.ts
41
41
  var TableStructureGenerator = class {
42
42
  dialect;
43
- tables = {};
44
- constructor(dbContext, dialect) {
43
+ dbInstructions;
44
+ constructor(dbInstructions, dialect) {
45
45
  this.dialect = dialect;
46
- if (dbContext.tables.length) {
46
+ this.dbInstructions = dbInstructions;
47
+ }
48
+ generateTableStructure() {
49
+ if (this.dbInstructions.tables.length) {
47
50
  const tableObjects = {};
48
- for (const table of dbContext.tables) {
51
+ for (const table of this.dbInstructions.tables) {
49
52
  tableObjects[table.name] = { columns: new table(), tableInfo: getTableInfo(table) };
50
53
  }
51
- this.getExpectedStructure(tableObjects);
54
+ return this.getExpectedStructure(tableObjects);
52
55
  } else {
53
- const tableObjects = dbContext.tables;
54
- this.getExpectedStructure(tableObjects);
56
+ const tableObjects = this.dbInstructions.tables;
57
+ return this.getExpectedStructure(tableObjects);
55
58
  }
56
59
  }
57
60
  getExpectedStructure(tableObjects) {
61
+ const tables = {};
58
62
  for (const tableName in tableObjects) {
59
63
  const obj = tableObjects[tableName];
60
64
  const tableInfo = obj.tableInfo;
61
- this.tables[tableName] = {
65
+ tables[tableName] = {
62
66
  table: tableName,
63
67
  primaryKey: tableInfo?.primaryKey,
64
68
  columns: this.getColumns(obj.columns, tableInfo?.primaryKey),
65
69
  foreignKeys: tableInfo?.foreignKeys
66
70
  };
67
71
  }
72
+ return tables;
68
73
  }
69
74
  getColumns(obj, primaryKey) {
70
75
  const columns = [];
@@ -108,38 +113,30 @@ var TableStructureGenerator = class {
108
113
 
109
114
  // src/lib/exceptions/NotAllowedException.ts
110
115
  var NotAllowedException = class extends Error {
111
- constructor(version, tableName, type) {
112
- const information = `You have to enable "${type}" for version "${version}" and table "${tableName}"`;
113
- switch (type) {
114
- case "continueWithoutRollback":
115
- super(`Some changes can not be rolled back automatically. ${information}`);
116
- break;
117
- case "dropColumn":
118
- super(`Not allowed to drop column. ${information}`);
119
- break;
120
- case "dropTable":
121
- super(`Not allowed to drop table. ${information}`);
122
- break;
123
- case "recreateTable":
124
- super(`Not allowed to recreate table. ${information}`);
125
- break;
126
- case "alterForeignKey":
127
- super(`Not allowed to alter existing foreign key. ${information}`);
128
- break;
129
- case "alterPrimaryKey":
130
- super(`Not allowed to alter primary key. ${information}`);
131
- break;
132
- case "removeForeignKey":
133
- super(`Not allowed to remove existing foreign key. ${information}`);
134
- break;
116
+ constructor(entries) {
117
+ let message = "Some destructive changes need to be confirmed. In preMigrations() run:";
118
+ for (const entry of entries) {
119
+ message += `
120
+ migrations.allowMigration(${entry.version}, "${entry.tableName}", "${entry.type}");`;
135
121
  }
122
+ super(message);
136
123
  }
137
124
  };
138
125
 
139
126
  // src/lib/typings/Migrations.ts
140
127
  var Migrations = class {
141
- migrationData = {};
128
+ toVersion = 0;
142
129
  alwaysAllowed = {};
130
+ fromVersion = 0;
131
+ migrationData = {};
132
+ notAllowedChanges = [];
133
+ reset(dbInstructions, fromVersion) {
134
+ this.fromVersion = fromVersion;
135
+ this.migrationData = {};
136
+ this.toVersion = dbInstructions.version;
137
+ this.alwaysAllowed = dbInstructions.alwaysAllowedMigrations ?? {};
138
+ this.notAllowedChanges = [];
139
+ }
143
140
  getEntry(table) {
144
141
  const tableName = this.getTableName(table);
145
142
  if (!this.migrationData.hasOwnProperty(tableName)) {
@@ -154,30 +151,38 @@ var Migrations = class {
154
151
  getTableName(table) {
155
152
  return typeof table == "string" ? table : table.name;
156
153
  }
157
- alwaysAllow(...allowedMigrations) {
158
- for (const key of allowedMigrations) {
159
- this.alwaysAllowed[key] = true;
154
+ verifyMigration(tableName, type) {
155
+ if (!this.migrationData[tableName]?.allowedMigrations[type] && !this.alwaysAllowed[type])
156
+ this.notAllowedChanges.push({ version: this.toVersion, tableName, type });
157
+ }
158
+ checkIfAllowed(throwIfNotAllowed) {
159
+ const exception = new NotAllowedException(this.notAllowedChanges);
160
+ if (throwIfNotAllowed)
161
+ throw exception;
162
+ else {
163
+ console.error(exception.message);
160
164
  }
165
+ return !this.notAllowedChanges.length;
161
166
  }
162
167
  allowMigration(version, table, ...allowedMigrations) {
168
+ if (version < this.fromVersion || version > this.toVersion)
169
+ return;
163
170
  const entry = this.getEntry(table);
164
- if (!entry.allowedMigrations.hasOwnProperty(version))
165
- entry.allowedMigrations[version] = {};
166
171
  for (const key of allowedMigrations) {
167
- this.alwaysAllowed[key] = true;
172
+ entry.allowedMigrations[key] = true;
168
173
  }
169
174
  }
170
- throwIfNotAllowed(version, tableName, type) {
171
- if (!(this.migrationData[tableName]?.allowedMigrations[version] ?? this.alwaysAllowed)[type])
172
- throw new NotAllowedException(version, tableName, type);
173
- }
174
- renameTable(oldTableName, newTable) {
175
+ renameTable(version, oldTableName, newTable) {
176
+ if (version < this.fromVersion || version > this.toVersion)
177
+ return;
175
178
  const entry = this.getEntry(newTable);
176
179
  entry.recreate = true;
177
180
  if (!entry.oldTableName)
178
181
  entry.oldTableName = oldTableName;
179
182
  }
180
- renameColumn(table, oldColumn, newColumn) {
183
+ renameColumn(version, table, oldColumn, newColumn) {
184
+ if (version < this.fromVersion || version > this.toVersion)
185
+ return;
181
186
  const entry = this.getEntry(table);
182
187
  const existingColumnEntry = entry.renamedColumns.find((entry2) => entry2[entry2.length - 1] == oldColumn);
183
188
  if (existingColumnEntry)
@@ -185,10 +190,15 @@ var Migrations = class {
185
190
  else
186
191
  entry.renamedColumns.push([oldColumn, newColumn]);
187
192
  }
188
- recreateTable(table) {
193
+ recreateTableImp(table) {
189
194
  const entry = this.getEntry(table);
190
195
  entry.recreate = true;
191
196
  }
197
+ recreateTable(version, table) {
198
+ if (version < this.fromVersion || version > this.toVersion)
199
+ return;
200
+ this.recreateTableImp(table);
201
+ }
192
202
  getMigrationData() {
193
203
  return this.migrationData;
194
204
  }
@@ -203,7 +213,7 @@ var Migrations = class {
203
213
  }
204
214
  }
205
215
  getUpdatedColumnName(tableName, oldColumnName) {
206
- const columnNames = this.migrationData[tableName].renamedColumns.find((columns) => columns[0] == oldColumnName);
216
+ const columnNames = this.migrationData[tableName]?.renamedColumns.find((columns) => columns[0] == oldColumnName);
207
217
  return columnNames ? columnNames[columnNames.length - 1] : oldColumnName;
208
218
  }
209
219
  willBeRecreated(tableName) {
@@ -211,827 +221,836 @@ var Migrations = class {
211
221
  }
212
222
  };
213
223
 
214
- // src/lib/MigrationHistoryManager.ts
215
- import { existsSync, mkdirSync, writeFileSync } from "fs";
216
- import { readFileSync } from "fs";
217
- var FILENAME_UP_PREFIX = "up_to_";
218
- var FILENAME_DOWN_PREFIX = "down_to_";
219
- var MigrationHistoryManager = class {
220
- configPath;
221
- migrationsPath;
222
- constructor(configPath) {
223
- this.configPath = configPath;
224
- this.migrationsPath = `${configPath}/migrations`;
225
- mkdirSync(this.configPath, { recursive: true });
226
- mkdirSync(this.migrationsPath, { recursive: true });
224
+ // src/lib/MigrationManager.ts
225
+ var MigrationManager = class {
226
+ migrations = new Migrations();
227
+ newTables = {};
228
+ existingTables = [];
229
+ dialect;
230
+ constructor(dialect) {
231
+ this.dialect = dialect;
227
232
  }
228
233
  /**
229
- * Creates migration history files for upgrading and downgrading database schemas.
234
+ * Generates the SQL changes required to migrate a database from its current version
235
+ * to the target version defined in the database instructions. Handles the creation
236
+ * and deletion of tables, migration of foreign keys and columns, renaming of columns,
237
+ * and recreating tables, along with executing any pre- and post-migration custom logic.
230
238
  *
231
- * @param fromVersion - The current version number of the database.
232
- * @param toVersion - The target version number for the migration.
233
- * @param changes - An object containing the SQL changes for upgrading (`changes.up`) and downgrading (`changes.down`).
234
- * @param overwriteExisting - A flag that determines if existing migration files should be overwritten. Defaults to `false`.
239
+ * @return A promise that resolves to an object containing the SQL
240
+ * changes for the migration (`up` and `down` changes) or null if no migration is required.
241
+ * Throws an error in scenarios such as attempting to migrate to a lower version
242
+ * or an invalid starting version.
235
243
  */
236
- createMigrationHistory(fromVersion, toVersion, changes, overwriteExisting) {
237
- const upPath = `${this.migrationsPath}/${FILENAME_UP_PREFIX}${toVersion}.sql`;
238
- const downPath = `${this.migrationsPath}/${FILENAME_DOWN_PREFIX}${fromVersion}.sql`;
239
- if (existsSync(upPath) && !overwriteExisting)
240
- throw new Error(`Migration ${upPath} already exists!`);
241
- else if (existsSync(downPath) && !overwriteExisting)
242
- throw new Error(`Migration ${downPath} already exists!`);
243
- Logger.log(`Save migrations to ${this.migrationsPath}`);
244
- writeFileSync(`${this.migrationsPath}/${FILENAME_UP_PREFIX}${toVersion}.sql`, `-- From: Version ${fromVersion}
245
- -- To: Version ${toVersion}
246
- ${changes.up}`, { encoding: "utf-8" });
247
- writeFileSync(`${this.migrationsPath}/${FILENAME_DOWN_PREFIX}${fromVersion}.sql`, `-- From: Version ${toVersion}
248
- -- To: Version ${fromVersion}
249
- ${changes.down}`, { encoding: "utf-8" });
244
+ async generateSqlChanges(dbInstructions) {
245
+ const fromVersion = await this.dialect.getVersion();
246
+ const toVersion = dbInstructions.version;
247
+ if (toVersion <= 0)
248
+ throw new Error("Cannot migrate to version 0 or lower");
249
+ if (fromVersion == toVersion) {
250
+ Logger.log("Version has not changed. No migrations needed.");
251
+ return null;
252
+ }
253
+ if (fromVersion > toVersion)
254
+ throw new Error(`You cannot create new migrations with a lower version (from ${fromVersion} to ${toVersion})`);
255
+ const foreignKeyOffQuery = this.dialect.changeForeignKeysState(false);
256
+ const changes = {
257
+ up: foreignKeyOffQuery,
258
+ down: foreignKeyOffQuery
259
+ };
260
+ this.migrations.reset(dbInstructions, fromVersion);
261
+ const preSQL = dbInstructions.preMigration?.(
262
+ this.migrations,
263
+ fromVersion,
264
+ toVersion
265
+ );
266
+ if (preSQL) {
267
+ changes.up += `
268
+
269
+ -- Custom SQL from preMigration()
270
+ ${preSQL.up}`;
271
+ changes.down += `
272
+
273
+ -- Custom SQL from preMigration()
274
+ ${preSQL.down}`;
275
+ }
276
+ const tableStructureGenerator = new TableStructureGenerator(dbInstructions, this.dialect);
277
+ this.newTables = tableStructureGenerator.generateTableStructure();
278
+ if (fromVersion == 0) {
279
+ Logger.log(`Creating initial migration to version ${dbInstructions.version}`);
280
+ const initialChanges = await this.createAndDropTables();
281
+ changes.up += initialChanges.up;
282
+ changes.down += initialChanges.down;
283
+ } else {
284
+ Logger.log(`Creating migration SQL from version ${fromVersion} to ${toVersion}`);
285
+ this.existingTables = await this.dialect.getTableNames();
286
+ [
287
+ await this.createAndDropTables(),
288
+ await this.migrateForeignKeys(),
289
+ await this.migrateColumns(),
290
+ this.renameColumns(),
291
+ await this.recreateTables()
292
+ ].forEach((entry) => {
293
+ changes.up += entry.up;
294
+ changes.down += entry.down;
295
+ });
296
+ }
297
+ const postSql = dbInstructions.postMigration?.(fromVersion, toVersion);
298
+ if (postSql) {
299
+ changes.up += `
300
+
301
+ -- Custom SQL from postMigration()
302
+ ${postSql.up}`;
303
+ changes.down += `
304
+
305
+ -- Custom SQL from postMigration()
306
+ ${postSql.down}`;
307
+ }
308
+ const foreignKeyOnQuery = this.dialect.changeForeignKeysState(true);
309
+ changes.up += `
310
+
311
+ ${foreignKeyOnQuery}`;
312
+ changes.down += `
313
+
314
+ ${foreignKeyOnQuery}`;
315
+ if (!this.migrations.checkIfAllowed(dbInstructions.throwIfNotAllowed)) {
316
+ return null;
317
+ }
318
+ return {
319
+ fromVersion,
320
+ toVersion,
321
+ changes
322
+ };
250
323
  }
251
324
  /**
252
- * Retrieves the SQL migration script for upgrading to the specified version.
253
- * If no version is provided, it retrieves the migration script for the latest version in the history.
325
+ * Generates SQL change statements for creating and dropping tables based on the current table structure
326
+ * and existing table configurations in the database. The `up` changes include SQL statements for creating
327
+ * new tables, while the `down` changes include SQL statements for dropping the corresponding tables or
328
+ * recreating them if necessary.
254
329
  *
255
- * @param toVersion - The version of the migration script to retrieve. If not specified, the latest version is used.
256
- * @return The content of the SQL migration script as a string.
330
+ * @return {Promise<SqlChanges>} A promise that resolves to an object containing `up` and `down` SQL
331
+ * change statements for migrating the database schema.
257
332
  */
258
- getUpMigration(toVersion) {
259
- return readFileSync(`${this.migrationsPath}/${FILENAME_UP_PREFIX}${toVersion}.sql`, { encoding: "utf-8" });
333
+ async createAndDropTables() {
334
+ let changes = {
335
+ up: "\n\n-- Create tables\n",
336
+ down: "\n\n-- Create tables\n"
337
+ };
338
+ for (const tableName in this.newTables) {
339
+ if (this.tableDoesNotExists(tableName)) {
340
+ const structure = this.newTables[tableName];
341
+ changes.up += `${this.createTableSql(tableName, structure.columns, structure.foreignKeys)}
342
+ `;
343
+ changes.down += `${this.dialect.dropTable(tableName)}
344
+ `;
345
+ }
346
+ }
347
+ for (const tableName of this.existingTables) {
348
+ if (!this.newTables[tableName]) {
349
+ this.migrations.verifyMigration(tableName, "dropTable");
350
+ if (!this.dialect.canInspectForeignKeys || !this.dialect.canInspectPrimaryKey)
351
+ this.migrations.verifyMigration(tableName, "continueWithoutRollback");
352
+ changes.up += `${this.dialect.dropTable(tableName)}
353
+ `;
354
+ changes.down += this.dialect.canInspectForeignKeys && this.dialect.canInspectPrimaryKey ? this.createTableSql(tableName, await this.dialect.getColumnInformation(tableName), await this.dialect.getForeignKeys(tableName)) : "\n\n-- Cannot recreate foreign keys or load primary keys with this database! Table will not be recreated!\n";
355
+ }
356
+ }
357
+ return changes;
260
358
  }
261
359
  /**
262
- * Retrieves the SQL migration script for downgrading to the specified version.
360
+ * Generates an SQL string to create a table with the specified schema.
263
361
  *
264
- * @param version - The version number of the migration to retrieve.
265
- * @return The SQL script for the down migration.
362
+ * @param tableName - The name of the table to be created.
363
+ * @param columns - An array of objects defining the columns of the table, including their names, types
364
+ * default values, and whether they are primary keys.
365
+ * @param foreignKeys - An optional array of objects defining the foreign key constraints for the table,
366
+ * including the source column, target table, target column, action on update, and action on delete.
367
+ * @return The complete SQL string to create the table with the specified structure and constraints.
266
368
  */
267
- getDownMigration(version) {
268
- return readFileSync(`${this.migrationsPath}/${FILENAME_DOWN_PREFIX}${version}.sql`, { encoding: "utf-8" });
269
- }
270
- };
271
-
272
- // src/lib/dialects/DefaultSql.ts
273
- var MIGRATION_DATA_TABLE_NAME = "__sqlmorpheus_migrations";
274
- var DefaultSql = class {
275
- canAlterForeignKeys = false;
276
- canAlterPrimaryKey = false;
277
- canInspectForeignKeys = false;
278
- canInspectPrimaryKey = false;
279
- typeString = "TEXT";
280
- typeInt = "INTEGER";
281
- typeBigInt = "BIGINT";
282
- typeBoolean = "BOOLEAN";
283
- typeNull = "NULL";
284
- formatValueToSql(value) {
285
- return value.toString();
286
- }
287
- changeForeignKeysState(enabled) {
288
- return "";
289
- }
290
- async getForeignKeys(tableName, db) {
291
- throw new Error("Inspecting foreign keys is not supported!");
292
- }
293
- addForeignKey(fromTableName, foreignKey) {
294
- throw new Error("Adding foreign keys is not supported!");
295
- }
296
- removeForeignKey(tableName, foreignKeyName) {
297
- throw new Error("Foreign key removal is not supported!");
298
- }
299
- addPrimaryKey(tableName, columnName) {
300
- throw new Error("Adding foreign keys is not supported!");
301
- }
302
- removePrimaryKey(tableName, columnName) {
303
- throw new Error("Foreign key removal is not supported!");
304
- }
305
- foreignKey(column, foreignTable, foreignColumn, onUpdate, onDelete) {
306
- let query = `FOREIGN KEY (${column}) REFERENCES ${foreignTable} (${foreignColumn})`;
307
- if (onUpdate)
308
- query += ` ON UPDATE ${onUpdate}`;
309
- if (onDelete)
310
- query += ` ON DELETE ${onDelete}`;
311
- return query;
312
- }
313
- createTable(tableName, entries) {
314
- return `CREATE TABLE IF NOT EXISTS ${tableName} (
315
- ${entries.join(",\n ")}
316
- );`;
317
- }
318
- renameTable(tableName, newTableName) {
319
- return `ALTER TABLE ${tableName} RENAME TO ${newTableName};`;
320
- }
321
- dropTable(tableName) {
322
- return `DROP TABLE IF EXISTS ${tableName};`;
323
- }
324
- columnDefinition(columnName, type, defaultValue, isPrimaryKey) {
325
- const query = `${columnName} ${type} DEFAULT ${defaultValue}`;
326
- return isPrimaryKey ? `${query} PRIMARY KEY` : query;
369
+ createTableSql(tableName, columns, foreignKeys) {
370
+ const queryLines = [];
371
+ for (const columnInfo of columns) {
372
+ queryLines.push(this.dialect.columnDefinition(columnInfo.name, columnInfo.type, columnInfo.defaultValue, columnInfo.isPrimaryKey));
373
+ }
374
+ if (foreignKeys) {
375
+ for (const foreignKeyInfo of foreignKeys) {
376
+ queryLines.push(this.dialect.foreignKey(foreignKeyInfo.fromColumn, foreignKeyInfo.toTable, foreignKeyInfo.toColumn.toString(), foreignKeyInfo.onUpdate, foreignKeyInfo.onDelete));
377
+ }
378
+ }
379
+ return this.dialect.createTable(tableName, queryLines);
327
380
  }
328
- createColumn(columnTable, entry) {
329
- return `ALTER TABLE ${columnTable} ADD ${entry};`;
381
+ /**
382
+ * Checks if the specified table does not exist in the list of existing tables.
383
+ *
384
+ * @param tableName - The name of the table to check.
385
+ * @return Returns true if the table does not exist, otherwise false.
386
+ */
387
+ tableDoesNotExists(tableName) {
388
+ return this.existingTables.indexOf(tableName) == -1;
330
389
  }
331
- renameColumn(tableName, oldColumnName, newColumnName) {
332
- return `ALTER TABLE ${tableName} RENAME COLUMN ${newColumnName} TO ${oldColumnName};`;
390
+ /**
391
+ * Recreates database tables that have been flagged to be recreated in migration data.
392
+ * This method generated the necessary SQL code for backing up the current table data, dropping and recreating
393
+ * the tables, and reinserting the preserved data into the recreated tables.
394
+ */
395
+ async recreateTables() {
396
+ const changes = {
397
+ up: "\n\n-- Recreate tables\n",
398
+ down: "\n\n-- Recreate tables\n"
399
+ };
400
+ const migrationData = this.migrations.getMigrationData();
401
+ for (const tableName in migrationData) {
402
+ const migrationEntry = migrationData[tableName];
403
+ if (!migrationEntry.recreate)
404
+ continue;
405
+ this.migrations.verifyMigration(tableName, "recreateTable");
406
+ if (!this.dialect.canInspectForeignKeys || !this.dialect.canInspectPrimaryKey)
407
+ this.migrations.verifyMigration(tableName, "continueWithoutRollback");
408
+ const oldColumnList = await this.dialect.getColumnInformation(tableName);
409
+ const newColumnList = this.newTables[tableName].columns;
410
+ const moveableColumns = newColumnList.filter((entry) => oldColumnList.find((oldEntry) => oldEntry.name == entry.name) != void 0).map((entry) => entry.name);
411
+ const backupTableName = `${tableName}__backup`;
412
+ const insertQuery = this.dialect.insert(
413
+ backupTableName,
414
+ this.dialect.insertValues(moveableColumns, this.dialect.select(tableName, moveableColumns))
415
+ );
416
+ const moveDataQuery = insertQuery + "\n" + this.dialect.dropTable(tableName) + "\n" + this.dialect.renameTable(backupTableName, tableName);
417
+ const structure = this.newTables[tableName];
418
+ changes.up += this.createTableSql(backupTableName, structure.columns, structure.foreignKeys) + "\n" + moveDataQuery;
419
+ changes.down += this.dialect.canInspectForeignKeys && this.dialect.canInspectPrimaryKey ? this.createTableSql(backupTableName, await this.dialect.getColumnInformation(tableName), await this.dialect.getForeignKeys(tableName)) + "\n" + moveDataQuery : "\n\n-- Cannot recreate foreign keys or load primary keys with this database! Table will not be recreated!\n";
420
+ }
421
+ return changes;
333
422
  }
334
- copyColumn(tableName, oldColumnName, newColumnName) {
335
- return `UPDATE ${tableName} SET ${newColumnName} = ${oldColumnName};`;
336
- }
337
- dropColumn(tableName, columnName) {
338
- return `ALTER TABLE ${tableName} DROP COLUMN ${columnName};`;
339
- }
340
- select(tableName, select, where) {
341
- let query = `SELECT ${select.join(",")} FROM ${tableName}`;
342
- if (where)
343
- query += ` WHERE ${where}`;
344
- return `${query};`;
345
- }
346
- insert(tableName, content) {
347
- return `INSERT INTO ${tableName} ${content};`;
348
- }
349
- insertValues(keys, valueString) {
350
- return `(${keys}) ${valueString ?? `VALUES (${keys.map(() => "?").join(",")})`}`;
351
- }
352
- migrationTableQuery() {
353
- return this.createTable(MIGRATION_DATA_TABLE_NAME, [
354
- this.columnDefinition("version", this.typeInt, "0", false)
355
- ]);
356
- }
357
- async createMigrationTableIfNeeded(db) {
358
- await db.runMultipleWriteStatements(this.migrationTableQuery());
359
- }
360
- //TODO: untested
361
- async getVersion(db) {
362
- await this.createMigrationTableIfNeeded(db);
363
- const query = this.select(MIGRATION_DATA_TABLE_NAME, ["version"]);
364
- const data = await db.runGetStatement(query);
365
- return data.length ? data[0].version : 0;
366
- }
367
- //TODO: untested
368
- async setVersion(db, newVersion) {
369
- const query = `${this.migrationTableQuery()};
370
- INSERT INTO ${MIGRATION_DATA_TABLE_NAME} (version) VALUES ${newVersion} WHERE NOT EXISTS (SELECT 1 FROM ${MIGRATION_DATA_TABLE_NAME})
371
- UNION ALL
372
- UPDATE ${MIGRATION_DATA_TABLE_NAME} SET version = ${newVersion} WHERE EXISTS (SELECT 1 FROM ${MIGRATION_DATA_TABLE_NAME});
373
- `;
374
- await db.runMultipleWriteStatements(query);
375
- }
376
- };
377
-
378
- // src/lib/dialects/SqliteDialect.ts
379
- var SqliteDialect = class extends DefaultSql {
380
- typeBoolean = "INTEGER";
381
- typeBigInt = "INTEGER";
382
- canAlterForeignKeys = false;
383
- canAlterPrimaryKey = false;
384
- canInspectForeignKeys = true;
385
- canInspectPrimaryKey = true;
386
- changeForeignKeysState(enabled) {
387
- return `PRAGMA foreign_keys = ${enabled ? "ON" : "OFF"};`;
388
- }
389
- async getForeignKeys(tableName, db) {
390
- const data = await db.runGetStatement(`PRAGMA foreign_key_list(${tableName});`);
391
- return data.map((entry) => {
392
- return {
393
- fromTable: tableName,
394
- fromColumn: entry["from"],
395
- toTable: entry["table"],
396
- toColumn: entry["to"],
397
- on_update: entry["on_update"],
398
- on_delete: entry["on_delete"]
399
- };
400
- });
401
- }
402
- async getTableNames(db) {
403
- return (await db.runGetStatement("SELECT name FROM sqlite_master WHERE type = 'table'")).map((obj) => obj.name);
404
- }
405
- async getColumnInformation(tableName, db) {
406
- const data = await db.runGetStatement(`PRAGMA table_info(${tableName})`);
407
- return data.map((entry) => {
408
- return {
409
- name: entry["name"],
410
- type: entry["type"],
411
- defaultValue: entry["dflt_value"],
412
- isPrimaryKey: entry["pk"] == "1"
413
- };
414
- });
415
- }
416
- formatValueToSql(value) {
417
- switch (typeof value) {
418
- case "boolean":
419
- return value ? "1" : "0";
420
- default:
421
- return value.toString();
423
+ /**
424
+ * Migrates foreign key constraints for all defined tables.
425
+ */
426
+ async migrateForeignKeys() {
427
+ if (!this.dialect.canAlterForeignKeys)
428
+ return { up: "", down: "" };
429
+ let changes = {
430
+ up: "\n\n-- Foreign keys\n",
431
+ down: "\n\n-- Foreign keys\n"
432
+ };
433
+ for (const tableName in this.newTables) {
434
+ if (this.migrations.willBeRecreated(tableName) || this.tableDoesNotExists(tableName))
435
+ continue;
436
+ const structure = this.newTables[tableName];
437
+ const newForeignKeys = structure.foreignKeys ?? [];
438
+ const oldForeignKeys = await this.dialect.getForeignKeys(tableName);
439
+ let checkForNewForeignKeys = true;
440
+ for (const oldForeignKey of oldForeignKeys) {
441
+ const newForeignKey = newForeignKeys.find((entry) => entry.fromColumn == oldForeignKey.fromColumn);
442
+ if (!newForeignKey) {
443
+ Logger.log(`Foreign key ${oldForeignKey.toTable}.${oldForeignKey.toColumn} to ${oldForeignKey.toTable}.${oldForeignKey.toColumn} was removed!`);
444
+ this.migrations.verifyMigration(tableName, "removeForeignKey");
445
+ if (this.dialect.canAlterForeignKeys) {
446
+ changes.up += this.dialect.removeForeignKey(oldForeignKey.fromTable, oldForeignKey.fromColumn);
447
+ changes.down += this.dialect.addForeignKey(
448
+ oldForeignKey.fromTable,
449
+ this.dialect.foreignKey(oldForeignKey.fromColumn, oldForeignKey.toTable, oldForeignKey.toColumn, oldForeignKey.onUpdate, oldForeignKey.onDelete)
450
+ );
451
+ } else {
452
+ this.migrations.recreateTableImp(structure.table);
453
+ checkForNewForeignKeys = false;
454
+ }
455
+ } else {
456
+ if (!newForeignKey || oldForeignKey.toColumn != newForeignKey.toColumn || (oldForeignKey.onUpdate ?? "NO ACTION") != (newForeignKey.onUpdate ?? "NO ACTION") || (oldForeignKey.onDelete ?? "NO ACTION") != (newForeignKey.onDelete ?? "NO ACTION")) {
457
+ Logger.log(`Foreign key ${oldForeignKey.toTable}.${oldForeignKey.toColumn} to ${oldForeignKey.toTable}.${oldForeignKey.toColumn} was changed!`);
458
+ this.migrations.verifyMigration(tableName, "alterForeignKey");
459
+ if (this.dialect.canAlterForeignKeys) {
460
+ changes.up += this.dialect.removeForeignKey(oldForeignKey.fromTable, oldForeignKey.fromColumn) + this.dialect.addForeignKey(
461
+ newForeignKey.fromTable,
462
+ this.dialect.foreignKey(newForeignKey.fromColumn, newForeignKey.toTable, newForeignKey.toColumn, newForeignKey.onUpdate, newForeignKey.onDelete)
463
+ );
464
+ changes.down += this.dialect.addForeignKey(
465
+ oldForeignKey.fromTable,
466
+ this.dialect.foreignKey(oldForeignKey.fromColumn, oldForeignKey.toTable, oldForeignKey.toColumn, oldForeignKey.onUpdate, oldForeignKey.onDelete)
467
+ ) + this.dialect.removeForeignKey(newForeignKey.fromTable, newForeignKey.fromColumn);
468
+ } else {
469
+ this.migrations.recreateTableImp(structure.table);
470
+ checkForNewForeignKeys = false;
471
+ }
472
+ }
473
+ }
474
+ }
475
+ if (checkForNewForeignKeys) {
476
+ for (const newForeignKey of newForeignKeys) {
477
+ const oldForeignKey = oldForeignKeys.find((entry) => entry.fromColumn == newForeignKey.fromColumn);
478
+ if (oldForeignKey)
479
+ continue;
480
+ Logger.log(`Foreign key ${newForeignKey.toTable}.${newForeignKey.toColumn} to ${newForeignKey.toTable}.${newForeignKey.toColumn} is new!`);
481
+ if (this.dialect.canAlterForeignKeys) {
482
+ changes.up += this.dialect.addForeignKey(
483
+ newForeignKey.toTable,
484
+ this.dialect.foreignKey(newForeignKey.fromColumn, newForeignKey.toTable, newForeignKey.toColumn, newForeignKey.onUpdate, newForeignKey.onDelete)
485
+ );
486
+ changes.down += this.dialect.removeForeignKey(newForeignKey.fromTable, newForeignKey.fromColumn);
487
+ } else {
488
+ this.migrations.recreateTableImp(structure.table);
489
+ break;
490
+ }
491
+ }
492
+ }
422
493
  }
423
- }
424
- async getVersion(db) {
425
- const output = await db.runGetStatement(`PRAGMA user_version;`);
426
- return output[0].user_version;
427
- }
428
- async setVersion(db, newVersion) {
429
- await db.runMultipleWriteStatements(`PRAGMA user_version = ${newVersion};`);
430
- }
431
- };
432
-
433
- // src/lib/dialects/PostgresDialect.ts
434
- var PostgresDialect = class extends DefaultSql {
435
- canAlterForeignKeys = true;
436
- canAlterPrimaryKey = true;
437
- canInspectForeignKeys = false;
438
- canInspectPrimaryKey = false;
439
- changeForeignKeysState(enabled) {
440
- return `SET session_replication_role = '${enabled ? "replica" : "origin"}';`;
441
- }
442
- addForeignKey(fromTableName, foreignKey) {
443
- return `ALTER TABLE ${fromTableName} ADD CONSTRAINT ${fromTableName} ${foreignKey};`;
444
- }
445
- removeForeignKey(tableName, foreignKeyName) {
446
- return `ALTER TABLE ${tableName} DROP CONSTRAINT ${foreignKeyName};`;
447
- }
448
- async getTableNames(db) {
449
- return await db.runGetStatement("SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE';");
450
- }
451
- async getColumnInformation(tableName, db) {
452
- const data = await db.runGetStatement(`SELECT column_name, data_type, column_default FROM information_schema.columns WHERE table_schema = 'public' AND table_name = '${tableName}';`);
453
- return data.map((entry) => {
454
- return {
455
- name: entry["column_name"],
456
- type: entry["data_type"],
457
- defaultValue: entry["column_default"],
458
- isPrimaryKey: false
459
- };
460
- });
461
- }
462
- addPrimaryKey(tableName, columnName) {
463
- return `ALTER TABLE ${tableName} ADD PRIMARY KEY (${columnName});
464
- `;
465
- }
466
- removePrimaryKey(tableName, columnName) {
467
- return `ALTER TABLE ${tableName} DROP CONSTRAINT ${columnName};
468
- `;
469
- }
470
- };
471
-
472
- // src/lib/dialects/MsSqlDialect.ts
473
- var MsSqlDialect = class extends DefaultSql {
474
- canAlterForeignKeys = true;
475
- canAlterPrimaryKey = true;
476
- canInspectForeignKeys = true;
477
- canInspectPrimaryKey = false;
478
- changeForeignKeysState(enabled) {
479
- return `EXEC sp_MSforeachtable "${enabled ? "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all" : "ALTER TABLE ? NOCHECK CONSTRAINT all"}"`;
480
- }
481
- async getForeignKeys(tableName, db) {
482
- const data = await db.runGetStatement(`EXEC sp_fkeys @fktable_name = ${tableName};`);
483
- return data.map((entry) => {
484
- return {
485
- fromTable: entry[tableName],
486
- fromColumn: entry["FKCOLUMN_NAME"],
487
- toTable: entry["PKTABLE_NAME"],
488
- toColumn: entry["PKCOLUMN_NAME"]
489
- };
490
- });
491
- }
492
- addForeignKey(fromTableName, foreignKey) {
493
- return `ALTER TABLE ${fromTableName} ADD ${foreignKey};`;
494
- }
495
- removeForeignKey(tableName, foreignKeyName) {
496
- return `ALTER TABLE ${tableName} DROP CONSTRAINT ${foreignKeyName};`;
497
- }
498
- addPrimaryKey(tableName, columnName) {
499
- return `ALTER TABLE ${tableName} ADD CONSTRAINT ${tableName}_primary PRIMARY KEY (${columnName});`;
500
- }
501
- removePrimaryKey(tableName, columnName) {
502
- return `ALTER TABLE ${tableName} DROP CONSTRAINT ${columnName}_primary;`;
503
- }
504
- async getTableNames(db) {
505
- return await db.runGetStatement("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'");
506
- }
507
- async getColumnInformation(tableName, db) {
508
- const data = await db.runGetStatement(`SELECT COLUMN_NAME, DATA_TYPE, COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'${tableName};'`);
509
- return data.map((entry) => {
510
- return {
511
- name: entry["COLUMN_NAME"],
512
- type: entry["DATA_TYPE"],
513
- defaultValue: entry["COLUMN_DEFAULT"],
514
- isPrimaryKey: false
515
- };
516
- });
517
- }
518
- };
519
-
520
- // src/lib/dialects/MySqlDialect.ts
521
- var MySqlDialect = class extends DefaultSql {
522
- canAlterForeignKeys = true;
523
- canAlterPrimaryKey = true;
524
- canInspectForeignKeys = true;
525
- canInspectPrimaryKey = true;
526
- changeForeignKeysState(enabled) {
527
- return `SET FOREIGN_KEY_CHECKS ${enabled ? "1" : "0"}
528
- ;`;
529
- }
530
- async getForeignKeys(tableName, db) {
531
- const data = await 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}';`);
532
- return data.map((entry) => {
533
- return {
534
- fromTable: tableName,
535
- fromColumn: entry["COLUMN_NAME"],
536
- toTable: entry["REFERENCED_TABLE_NAME"],
537
- toColumn: entry["REFERENCED_COLUMN_NAME"]
538
- };
539
- });
540
- }
541
- addForeignKey(fromTableName, foreignKey) {
542
- return `ALTER TABLE ${fromTableName} ADD ${foreignKey};`;
543
- }
544
- removeForeignKey(tableName, foreignKeyName) {
545
- return `ALTER TABLE ${tableName} DROP FOREIGN KEY ${foreignKeyName};`;
546
- }
547
- addPrimaryKey(tableName, columnName) {
548
- return `ALTER TABLE ${tableName} PRIMARY KEY (${columnName});`;
549
- }
550
- removePrimaryKey(tableName, _columnName) {
551
- return `ALTER TABLE ${tableName} PRIMARY KEY;`;
552
- }
553
- async getTableNames(db) {
554
- return await db.runGetStatement("SHOW TABLES;");
555
- }
556
- async getColumnInformation(tableName, db) {
557
- const data = await db.runGetStatement(`SHOW COLUMNS FROM ${tableName};`);
558
- return data.map((entry) => {
559
- return {
560
- name: entry["Field"],
561
- type: entry["Type"],
562
- defaultValue: entry["Default"],
563
- isPrimaryKey: entry["Key"] == "PRI"
564
- };
565
- });
566
- }
567
- };
568
-
569
- // src/lib/getDialect.ts
570
- function getDialect(dbInstructions) {
571
- switch (dbInstructions.dialect) {
572
- case "Sqlite":
573
- return new SqliteDialect();
574
- case "Postgres":
575
- return new PostgresDialect();
576
- case "MsSql":
577
- return new MsSqlDialect();
578
- case "MySql":
579
- return new MySqlDialect();
580
- default:
581
- throw new Error(`Unknown dialect ${dbInstructions.dialect}`);
582
- }
583
- }
584
-
585
- // src/lib/MigrationManager.ts
586
- var MigrationManager = class {
587
- migrations = new Migrations();
588
- tableStructureGenerator;
589
- existingTables = [];
590
- db;
591
- dbInstructions;
592
- migrationHistoryManager;
593
- dialect;
594
- constructor(db, dbInstructions) {
595
- this.dialect = getDialect(dbInstructions);
596
- this.db = db;
597
- this.dbInstructions = dbInstructions;
598
- this.tableStructureGenerator = new TableStructureGenerator(dbInstructions, this.dialect);
599
- this.migrationHistoryManager = new MigrationHistoryManager(this.dbInstructions.configPath);
494
+ return changes;
600
495
  }
601
496
  /**
602
- * Generates the SQL changes required to migrate a database from its current version
603
- * to the target version defined in the database instructions. Handles the creation
604
- * and deletion of tables, migration of foreign keys and columns, renaming of columns,
605
- * and recreating tables, along with executing any pre- and post-migration custom logic.
606
- *
607
- * @return A promise that resolves to an object containing the SQL
608
- * changes for the migration (`up` and `down` changes) or null if no migration is required.
609
- * Throws an error in scenarios such as attempting to migrate to a lower version
610
- * or an invalid starting version.
497
+ * Checks all columns of all tables, creates them if they do not exist in the database and modifies them if needed.
498
+ * If types, default values or primary key change, the table is recreated
611
499
  */
612
- async getMigrateSql() {
613
- if (this.dbInstructions.version <= 0)
614
- throw new Error("Cannot migrate to version 0 or lower");
615
- const fromVersion = await this.dialect.getVersion(this.db);
616
- if (fromVersion == 0) {
617
- Logger.log(`Creating initial migration to version ${this.dbInstructions.version}`);
618
- return this.createAndDropTables();
619
- }
620
- if (fromVersion == this.dbInstructions.version) {
621
- Logger.log("Version has not changed. No migrations needed.");
622
- return null;
623
- }
624
- if (fromVersion > this.dbInstructions.version)
625
- throw new Error(`You cannot create new migrations with a lower version (from ${fromVersion} to ${this.dbInstructions.version})`);
626
- Logger.log(`Creating migration SQL from version ${fromVersion} to ${this.dbInstructions.version}`);
627
- const db = this.db;
628
- await db.createBackup?.(`from_${fromVersion}_to_${this.dbInstructions.version}`);
629
- this.existingTables = await this.dialect.getTableNames(this.db);
630
- const foreignKeyOffQuery = this.dialect.changeForeignKeysState(false);
500
+ async migrateColumns() {
631
501
  const changes = {
632
- up: foreignKeyOffQuery,
633
- down: foreignKeyOffQuery
502
+ up: "\n\n-- Migrate columns\n",
503
+ down: "\n\n-- Migrate columns\n"
634
504
  };
635
- const preSQL = this.dbInstructions.preMigration?.(
636
- this.migrations,
637
- fromVersion,
638
- this.dbInstructions.version
639
- );
640
- if (preSQL) {
641
- changes.up += `
642
-
643
- -- Custom SQL from preMigration()
644
- ${preSQL.up}`;
645
- changes.down += `
646
-
647
- -- Custom SQL from preMigration()
648
- ${preSQL.down}`;
649
- }
650
- [
651
- await this.createAndDropTables(),
652
- await this.migrateForeignKeys(),
653
- await this.migrateColumns(),
654
- this.renameColumns(),
655
- await this.recreateTables()
656
- ].forEach((entry) => {
657
- changes.up += entry.up;
658
- changes.down += entry.down;
659
- });
660
- const postSql = this.dbInstructions.postMigration?.(fromVersion, this.dbInstructions.version);
661
- if (postSql) {
662
- changes.up += `
663
-
664
- -- Custom SQL from postMigration()
665
- ${postSql.up}`;
666
- changes.down += `
667
-
668
- -- Custom SQL from postMigration()
669
- ${postSql.down}`;
505
+ for (const tableName in this.newTables) {
506
+ if (this.migrations.willBeRecreated(tableName) || this.tableDoesNotExists(tableName))
507
+ continue;
508
+ const newTableDefinition = this.newTables[tableName];
509
+ const oldColumnList = await this.dialect.getColumnInformation(tableName);
510
+ const oldPrimaryKey = this.dialect.canInspectPrimaryKey ? this.getPrimaryKeyColumn(oldColumnList) : false;
511
+ const newColumnList = newTableDefinition.columns;
512
+ const newPrimaryKey = newTableDefinition.primaryKey;
513
+ if (this.dialect.canInspectPrimaryKey && oldPrimaryKey != newPrimaryKey) {
514
+ Logger.log(`Primary key in ${tableName} was changed from ${oldPrimaryKey} to ${newPrimaryKey}!`);
515
+ this.migrations.verifyMigration(tableName, "alterPrimaryKey");
516
+ if (this.dialect.canAlterPrimaryKey) {
517
+ if (oldPrimaryKey) {
518
+ changes.up += this.dialect.removePrimaryKey(tableName, oldPrimaryKey);
519
+ changes.down += this.dialect.addPrimaryKey(tableName, oldPrimaryKey);
520
+ }
521
+ if (newPrimaryKey) {
522
+ changes.up += this.dialect.addPrimaryKey(tableName, newPrimaryKey.toString());
523
+ changes.down += this.dialect.removePrimaryKey(tableName, newPrimaryKey.toString());
524
+ }
525
+ } else
526
+ this.migrations.recreateTableImp(newTableDefinition.table);
527
+ continue;
528
+ }
529
+ for (const newColumn of newColumnList) {
530
+ const oldColumn = oldColumnList.find(
531
+ (entry) => this.migrations.getUpdatedColumnName(tableName, entry.name) == newColumn.name
532
+ );
533
+ if (oldColumn == void 0) {
534
+ changes.up += this.dialect.createColumn(tableName, this.dialect.columnDefinition(newColumn.name, newColumn.type, newColumn.defaultValue, newColumn.isPrimaryKey)) + "\n";
535
+ changes.down += this.dialect.dropColumn(tableName, newColumn.name) + "\n";
536
+ } else if (newColumn.type != oldColumn.type || newColumn.defaultValue != oldColumn.defaultValue) {
537
+ this.migrations.recreateTableImp(newTableDefinition.table);
538
+ }
539
+ }
540
+ for (const oldColumn of oldColumnList) {
541
+ const newColumn = newColumnList.find(
542
+ (entry) => entry.name == this.migrations.getUpdatedColumnName(tableName, oldColumn.name)
543
+ );
544
+ if (newColumn == void 0) {
545
+ this.migrations.verifyMigration(tableName, "dropColumn");
546
+ changes.up += this.dialect.dropColumn(tableName, oldColumn.name) + "\n";
547
+ changes.down += this.dialect.createColumn(tableName, this.dialect.columnDefinition(oldColumn.name, oldColumn.type, oldColumn.defaultValue, oldColumn.isPrimaryKey)) + "\n";
548
+ }
549
+ }
670
550
  }
671
- const foreignKeyOnQuery = this.dialect.changeForeignKeysState(true);
672
- changes.up += `
673
-
674
- ${foreignKeyOnQuery}`;
675
- changes.down += `
676
-
677
- ${foreignKeyOnQuery}`;
678
551
  return changes;
679
552
  }
680
553
  /**
681
- * Prepares the migration by generating the SQL changes and creating a migration history entry.
682
- * If no changes are needed, the method exits early.
554
+ * Retrieves the name of the primary key column from a list of column information.
683
555
  *
684
- * @param overwriteExisting - Optional flag indicating if overwriting existing changes is allowed.
685
- * @return - A promise that resolves once the migration preparation is complete.
556
+ * @param columnInfoList - An array of ColumnInfo objects containing details about each column.
557
+ * @return The name of the primary key column if found; otherwise, undefined.
686
558
  */
687
- async prepareMigration(overwriteExisting) {
688
- const changes = await this.getMigrateSql();
689
- if (!changes)
690
- return;
691
- const fromVersion = await this.dialect.getVersion(this.db);
692
- this.migrationHistoryManager.createMigrationHistory(fromVersion, this.dbInstructions.version, changes, overwriteExisting);
559
+ getPrimaryKeyColumn(columnInfoList) {
560
+ for (const columnInfo of columnInfoList) {
561
+ if (columnInfo.isPrimaryKey)
562
+ return columnInfo.name;
563
+ }
693
564
  }
694
565
  /**
695
- * Generates SQL change statements for creating and dropping tables based on the current table structure
696
- * and existing table configurations in the database. The `up` changes include SQL statements for creating
697
- * new tables, while the `down` changes include SQL statements for dropping the corresponding tables or
698
- * recreating them if necessary.
699
- *
700
- * @return {Promise<SqlChanges>} A promise that resolves to an object containing `up` and `down` SQL
701
- * change statements for migrating the database schema.
566
+ * Handles the renaming of columns in database tables by generating SQL changes for both applying and reverting the renames.
567
+ * Skips tables that are marked for recreation.
702
568
  */
703
- async createAndDropTables() {
704
- let changes = {
705
- up: "\n\n-- Create tables\n",
706
- down: "\n\n-- Create tables\n"
569
+ renameColumns() {
570
+ const changes = {
571
+ up: "\n\n-- Rename columns\n",
572
+ down: "\n\n-- Rename columns\n"
707
573
  };
708
- for (const tableName in this.tableStructureGenerator.tables) {
709
- if (this.tableDoesNotExists(tableName)) {
710
- const structure = this.tableStructureGenerator.tables[tableName];
711
- changes.up += `${this.createTableSql(tableName, structure.columns, structure.foreignKeys)}
712
- `;
713
- changes.down += `${this.dialect.dropTable(tableName)}
714
- `;
715
- }
716
- }
717
- for (const tableName of this.existingTables) {
718
- if (!this.tableStructureGenerator.tables[tableName]) {
719
- this.migrations.throwIfNotAllowed(this.dbInstructions.version, tableName, "dropTable");
720
- if (!this.dialect.canInspectForeignKeys || !this.dialect.canInspectPrimaryKey)
721
- this.migrations.throwIfNotAllowed(this.dbInstructions.version, tableName, "continueWithoutRollback");
722
- changes.up += `${this.dialect.dropTable(tableName)}
723
- `;
724
- changes.down += this.dialect.canInspectForeignKeys && this.dialect.canInspectPrimaryKey ? this.createTableSql(tableName, await this.dialect.getColumnInformation(tableName, this.db), await this.dialect.getForeignKeys(tableName, this.db)) : "\n\n-- Cannot recreate foreign keys or load primary keys with this database! Table will not be recreated!\n";
725
- }
574
+ const migrationData = this.migrations.getMigrationData();
575
+ for (const tableName in migrationData) {
576
+ if (migrationData[tableName].recreate)
577
+ continue;
578
+ this.migrations.loopRenamedColumns(tableName, (oldColumnName, newColumnName) => {
579
+ changes.up += this.dialect.renameColumn(tableName, oldColumnName, newColumnName);
580
+ changes.down += this.dialect.renameColumn(tableName, newColumnName, oldColumnName);
581
+ });
726
582
  }
727
583
  return changes;
728
584
  }
585
+ };
586
+
587
+ // src/lib/MigrationHistoryManager.ts
588
+ import { existsSync, mkdirSync, writeFileSync } from "fs";
589
+ import { readFileSync } from "fs";
590
+ var FILENAME_UP_PREFIX = "up_to_";
591
+ var FILENAME_DOWN_PREFIX = "down_to_";
592
+ var MigrationHistoryManager = class {
593
+ configPath;
594
+ migrationsPath;
595
+ constructor(configPath) {
596
+ this.configPath = configPath;
597
+ this.migrationsPath = `${configPath}/migrations`;
598
+ mkdirSync(this.configPath, { recursive: true });
599
+ mkdirSync(this.migrationsPath, { recursive: true });
600
+ }
729
601
  /**
730
- * Generates an SQL string to create a table with the specified schema.
602
+ * Creates migration history files for upgrading and downgrading database schemas.
731
603
  *
732
- * @param tableName - The name of the table to be created.
733
- * @param columns - An array of objects defining the columns of the table, including their names, types
734
- * default values, and whether they are primary keys.
735
- * @param foreignKeys - An optional array of objects defining the foreign key constraints for the table,
736
- * including the source column, target table, target column, action on update, and action on delete.
737
- * @return The complete SQL string to create the table with the specified structure and constraints.
604
+ * @param fromVersion - The current version number of the database.
605
+ * @param toVersion - The target version number for the migration.
606
+ * @param changes - An object containing the SQL changes for upgrading (`changes.up`) and downgrading (`changes.down`).
607
+ * @param overwriteExisting - A flag that determines if existing migration files should be overwritten. Defaults to `false`.
738
608
  */
739
- createTableSql(tableName, columns, foreignKeys) {
740
- const queryLines = [];
741
- for (const columnInfo of columns) {
742
- queryLines.push(this.dialect.columnDefinition(columnInfo.name, columnInfo.type, columnInfo.defaultValue, columnInfo.isPrimaryKey));
743
- }
744
- if (foreignKeys) {
745
- for (const foreignKeyInfo of foreignKeys) {
746
- queryLines.push(this.dialect.foreignKey(foreignKeyInfo.fromColumn, foreignKeyInfo.toTable, foreignKeyInfo.toColumn.toString(), foreignKeyInfo.onUpdate, foreignKeyInfo.onDelete));
747
- }
748
- }
749
- return this.dialect.createTable(tableName, queryLines);
609
+ createMigrationHistory(fromVersion, toVersion, changes, overwriteExisting) {
610
+ const upPath = `${this.migrationsPath}/${FILENAME_UP_PREFIX}${toVersion}.sql`;
611
+ const downPath = `${this.migrationsPath}/${FILENAME_DOWN_PREFIX}${fromVersion}.sql`;
612
+ if (existsSync(upPath) && !overwriteExisting)
613
+ throw new Error(`Migration ${upPath} already exists!`);
614
+ else if (existsSync(downPath) && !overwriteExisting)
615
+ throw new Error(`Migration ${downPath} already exists!`);
616
+ Logger.log(`Save migrations to ${this.migrationsPath}`);
617
+ writeFileSync(`${this.migrationsPath}/${FILENAME_UP_PREFIX}${toVersion}.sql`, `-- From: Version ${fromVersion}
618
+ -- To: Version ${toVersion}
619
+ ${changes.up}`, { encoding: "utf-8" });
620
+ writeFileSync(`${this.migrationsPath}/${FILENAME_DOWN_PREFIX}${fromVersion}.sql`, `-- From: Version ${toVersion}
621
+ -- To: Version ${fromVersion}
622
+ ${changes.down}`, { encoding: "utf-8" });
750
623
  }
751
624
  /**
752
- * Checks if the specified table does not exist in the list of existing tables.
625
+ * Retrieves the SQL migration script for upgrading to the specified version.
626
+ * If no version is provided, it retrieves the migration script for the latest version in the history.
753
627
  *
754
- * @param tableName - The name of the table to check.
755
- * @return Returns true if the table does not exist, otherwise false.
628
+ * @param toVersion - The version of the migration script to retrieve. If not specified, the latest version is used.
629
+ * @return The content of the SQL migration script as a string.
756
630
  */
757
- tableDoesNotExists(tableName) {
758
- return this.existingTables.indexOf(tableName) == -1;
631
+ getUpMigration(toVersion) {
632
+ return readFileSync(`${this.migrationsPath}/${FILENAME_UP_PREFIX}${toVersion}.sql`, { encoding: "utf-8" });
759
633
  }
760
634
  /**
761
- * Recreates database tables that have been flagged to be recreated in migration data.
762
- * This method generated the necessary SQL code for backing up the current table data, dropping and recreating
763
- * the tables, and reinserting the preserved data into the recreated tables.
635
+ * Retrieves the SQL migration script for downgrading to the specified version.
636
+ *
637
+ * @param version - The version number of the migration to retrieve.
638
+ * @return The SQL script for the down migration.
764
639
  */
765
- async recreateTables() {
766
- const changes = {
767
- up: "\n\n-- Recreate tables\n",
768
- down: "\n\n-- Recreate tables\n"
769
- };
770
- const migrationData = this.migrations.getMigrationData();
771
- for (const tableName in migrationData) {
772
- const migrationEntry = migrationData[tableName];
773
- if (!migrationEntry.recreate)
774
- continue;
775
- this.migrations.throwIfNotAllowed(this.dbInstructions.version, tableName, "recreateTable");
776
- if (!this.dialect.canInspectForeignKeys || !this.dialect.canInspectPrimaryKey)
777
- this.migrations.throwIfNotAllowed(this.dbInstructions.version, tableName, "continueWithoutRollback");
778
- const oldColumnList = await this.dialect.getColumnInformation(tableName, this.db);
779
- const newColumnList = this.tableStructureGenerator.tables[tableName].columns;
780
- const moveableColumns = newColumnList.filter((entry) => oldColumnList.find((oldEntry) => oldEntry.name == entry.name) != void 0).map((entry) => entry.name);
781
- const backupTableName = `${tableName}__backup`;
782
- const insertQuery = this.dialect.insert(
783
- backupTableName,
784
- this.dialect.insertValues(moveableColumns, this.dialect.select(tableName, moveableColumns))
785
- );
786
- const moveDataQuery = insertQuery + "\n" + this.dialect.dropTable(tableName) + "\n" + this.dialect.renameTable(backupTableName, tableName);
787
- const structure = this.tableStructureGenerator.tables[tableName];
788
- changes.up += this.createTableSql(backupTableName, structure.columns, structure.foreignKeys) + "\n" + moveDataQuery;
789
- changes.down += this.dialect.canInspectForeignKeys && this.dialect.canInspectPrimaryKey ? this.createTableSql(backupTableName, await this.dialect.getColumnInformation(tableName, this.db), await this.dialect.getForeignKeys(tableName, this.db)) + "\n" + moveDataQuery : "\n\n-- Cannot recreate foreign keys or load primary keys with this database! Table will not be recreated!\n";
790
- }
791
- return changes;
640
+ getDownMigration(version) {
641
+ return readFileSync(`${this.migrationsPath}/${FILENAME_DOWN_PREFIX}${version}.sql`, { encoding: "utf-8" });
792
642
  }
793
- /**
794
- * Migrates foreign key constraints for all defined tables.
795
- */
796
- async migrateForeignKeys() {
797
- if (!this.dialect.canAlterForeignKeys)
798
- return { up: "", down: "" };
799
- let changes = {
800
- up: "\n\n-- Foreign keys\n",
801
- down: "\n\n-- Foreign keys\n"
802
- };
803
- for (const tableName in this.tableStructureGenerator.tables) {
804
- if (this.migrations.willBeRecreated(tableName) || this.tableDoesNotExists(tableName))
805
- continue;
806
- const structure = this.tableStructureGenerator.tables[tableName];
807
- const newForeignKeys = structure.foreignKeys ?? [];
808
- const oldForeignKeys = await this.dialect.getForeignKeys(tableName, this.db);
809
- let checkForNewForeignKeys = true;
810
- for (const oldForeignKey of oldForeignKeys) {
811
- const newForeignKey = newForeignKeys.find((entry) => entry.fromColumn == oldForeignKey.fromColumn);
812
- if (!newForeignKey) {
813
- Logger.log(`Foreign key ${oldForeignKey.toTable}.${oldForeignKey.toColumn} to ${oldForeignKey.toTable}.${oldForeignKey.toColumn} was removed!`);
814
- this.migrations.throwIfNotAllowed(this.dbInstructions.version, tableName, "removeForeignKey");
815
- if (this.dialect.canAlterForeignKeys) {
816
- changes.up += this.dialect.removeForeignKey(oldForeignKey.fromTable, oldForeignKey.fromColumn);
817
- changes.down += this.dialect.addForeignKey(
818
- oldForeignKey.fromTable,
819
- this.dialect.foreignKey(oldForeignKey.fromColumn, oldForeignKey.toTable, oldForeignKey.toColumn, oldForeignKey.onUpdate, oldForeignKey.onDelete)
820
- );
821
- } else {
822
- this.migrations.recreateTable(structure.table);
823
- checkForNewForeignKeys = false;
824
- }
825
- } else {
826
- if (!newForeignKey || oldForeignKey.toColumn != newForeignKey.toColumn || (oldForeignKey.onUpdate ?? "NO ACTION") != (newForeignKey.onUpdate ?? "NO ACTION") || (oldForeignKey.onDelete ?? "NO ACTION") != (newForeignKey.onDelete ?? "NO ACTION")) {
827
- Logger.log(`Foreign key ${oldForeignKey.toTable}.${oldForeignKey.toColumn} to ${oldForeignKey.toTable}.${oldForeignKey.toColumn} was changed!`);
828
- this.migrations.throwIfNotAllowed(this.dbInstructions.version, tableName, "alterForeignKey");
829
- if (this.dialect.canAlterForeignKeys) {
830
- changes.up += this.dialect.removeForeignKey(oldForeignKey.fromTable, oldForeignKey.fromColumn) + this.dialect.addForeignKey(
831
- newForeignKey.fromTable,
832
- this.dialect.foreignKey(newForeignKey.fromColumn, newForeignKey.toTable, newForeignKey.toColumn, newForeignKey.onUpdate, newForeignKey.onDelete)
833
- );
834
- changes.down += this.dialect.addForeignKey(
835
- oldForeignKey.fromTable,
836
- this.dialect.foreignKey(oldForeignKey.fromColumn, oldForeignKey.toTable, oldForeignKey.toColumn, oldForeignKey.onUpdate, oldForeignKey.onDelete)
837
- ) + this.dialect.removeForeignKey(newForeignKey.fromTable, newForeignKey.fromColumn);
838
- } else {
839
- this.migrations.recreateTable(structure.table);
840
- checkForNewForeignKeys = false;
841
- }
842
- }
843
- }
844
- }
845
- if (checkForNewForeignKeys) {
846
- for (const newForeignKey of newForeignKeys) {
847
- const oldForeignKey = oldForeignKeys.find((entry) => entry.fromColumn == newForeignKey.fromColumn);
848
- if (oldForeignKey)
849
- continue;
850
- Logger.log(`Foreign key ${newForeignKey.toTable}.${newForeignKey.toColumn} to ${newForeignKey.toTable}.${newForeignKey.toColumn} is new!`);
851
- if (this.dialect.canAlterForeignKeys) {
852
- changes.up += this.dialect.addForeignKey(
853
- newForeignKey.toTable,
854
- this.dialect.foreignKey(newForeignKey.fromColumn, newForeignKey.toTable, newForeignKey.toColumn, newForeignKey.onUpdate, newForeignKey.onDelete)
855
- );
856
- changes.down += this.dialect.removeForeignKey(newForeignKey.fromTable, newForeignKey.fromColumn);
857
- } else {
858
- this.migrations.recreateTable(structure.table);
859
- break;
860
- }
861
- }
643
+ };
644
+
645
+ // src/lib/tableInfo/decorators/DbTable.ts
646
+ import "polyfill-symbol-metadata";
647
+ function DbTable(tableName, primaryKey) {
648
+ return (table, context) => {
649
+ Object.defineProperty(table, "name", { value: tableName, writable: false });
650
+ const tableInfo = context?.metadata ? getTableInfoFromMetadata(context.metadata) : getTableInfo(table);
651
+ tableInfo.primaryKey = primaryKey?.toString();
652
+ if (tableInfo.foreignKeys) {
653
+ for (const foreignKey of tableInfo.foreignKeys) {
654
+ foreignKey.fromTable = tableName;
862
655
  }
863
656
  }
864
- return changes;
657
+ };
658
+ }
659
+
660
+ // src/lib/tableInfo/decorators/ForeignKey.ts
661
+ import "polyfill-symbol-metadata";
662
+ function ForeignKey(toTable, toColumn, onDelete, onUpdate) {
663
+ return (table, context) => {
664
+ const metadata = context?.metadata ? getTableInfoFromMetadata(context.metadata) : getTableInfo(table);
665
+ if (!metadata.foreignKeys)
666
+ metadata.foreignKeys = [];
667
+ metadata.foreignKeys.push({
668
+ fromTable: "",
669
+ fromColumn: context.name ?? context,
670
+ //to stay compatible with old decorator structure (see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#decorator-metadata)
671
+ toTable: toTable.name,
672
+ toColumn: toColumn.toString(),
673
+ onDelete,
674
+ onUpdate
675
+ });
676
+ };
677
+ }
678
+
679
+ // src/lib/dialects/DefaultSql.ts
680
+ var MIGRATION_DATA_TABLE_NAME = "__sqlmorpheus_migrations";
681
+ var DefaultSql = class {
682
+ db;
683
+ canAlterForeignKeys = false;
684
+ canAlterPrimaryKey = false;
685
+ canInspectForeignKeys = false;
686
+ canInspectPrimaryKey = false;
687
+ typeString = "TEXT";
688
+ typeInt = "INTEGER";
689
+ typeBigInt = "BIGINT";
690
+ typeBoolean = "BOOLEAN";
691
+ typeNull = "NULL";
692
+ constructor(db) {
693
+ this.db = db;
694
+ }
695
+ formatValueToSql(value) {
696
+ return value.toString();
697
+ }
698
+ changeForeignKeysState(enabled) {
699
+ return "";
700
+ }
701
+ addForeignKey(fromTableName, foreignKey) {
702
+ throw new Error("Adding foreign keys is not supported!");
703
+ }
704
+ removeForeignKey(tableName, foreignKeyName) {
705
+ throw new Error("Foreign key removal is not supported!");
706
+ }
707
+ addPrimaryKey(tableName, columnName) {
708
+ throw new Error("Adding foreign keys is not supported!");
709
+ }
710
+ removePrimaryKey(tableName, columnName) {
711
+ throw new Error("Foreign key removal is not supported!");
712
+ }
713
+ foreignKey(column, foreignTable, foreignColumn, onUpdate, onDelete) {
714
+ let query = `FOREIGN KEY (${column}) REFERENCES ${foreignTable} (${foreignColumn})`;
715
+ if (onUpdate)
716
+ query += ` ON UPDATE ${onUpdate}`;
717
+ if (onDelete)
718
+ query += ` ON DELETE ${onDelete}`;
719
+ return query;
720
+ }
721
+ createTable(tableName, entries) {
722
+ return `CREATE TABLE IF NOT EXISTS ${tableName} (
723
+ ${entries.join(",\n ")}
724
+ );`;
725
+ }
726
+ renameTable(tableName, newTableName) {
727
+ return `ALTER TABLE ${tableName} RENAME TO ${newTableName};`;
728
+ }
729
+ dropTable(tableName) {
730
+ return `DROP TABLE IF EXISTS ${tableName};`;
731
+ }
732
+ columnDefinition(columnName, type, defaultValue, isPrimaryKey) {
733
+ const query = `${columnName} ${type} DEFAULT ${defaultValue}`;
734
+ return isPrimaryKey ? `${query} PRIMARY KEY` : query;
735
+ }
736
+ createColumn(columnTable, entry) {
737
+ return `ALTER TABLE ${columnTable} ADD ${entry};`;
738
+ }
739
+ renameColumn(tableName, oldColumnName, newColumnName) {
740
+ return `ALTER TABLE ${tableName} RENAME COLUMN ${oldColumnName} TO ${newColumnName};`;
741
+ }
742
+ copyColumn(tableName, oldColumnName, newColumnName) {
743
+ return `UPDATE ${tableName} SET ${newColumnName} = ${oldColumnName};`;
744
+ }
745
+ dropColumn(tableName, columnName) {
746
+ return `ALTER TABLE ${tableName} DROP COLUMN ${columnName};`;
747
+ }
748
+ select(tableName, select, where) {
749
+ let query = `SELECT ${select.join(",")} FROM ${tableName}`;
750
+ if (where)
751
+ query += ` WHERE ${where}`;
752
+ return `${query};`;
753
+ }
754
+ insert(tableName, content) {
755
+ return `INSERT INTO ${tableName} ${content};`;
756
+ }
757
+ insertValues(keys, valueString) {
758
+ return `(${keys}) ${valueString ?? `VALUES (${keys.map(() => "?").join(",")})`}`;
759
+ }
760
+ migrationTableQuery() {
761
+ return this.createTable(MIGRATION_DATA_TABLE_NAME, [
762
+ this.columnDefinition("version", this.typeInt, "0", false)
763
+ ]);
764
+ }
765
+ async getForeignKeys(tableName) {
766
+ throw new Error("Inspecting foreign keys is not supported!");
767
+ }
768
+ async createMigrationTableIfNeeded() {
769
+ await this.db.runMultipleWriteStatements(this.migrationTableQuery());
770
+ }
771
+ //TODO: untested
772
+ async getVersion() {
773
+ await this.createMigrationTableIfNeeded();
774
+ const query = this.select(MIGRATION_DATA_TABLE_NAME, ["version"]);
775
+ const data = await this.db.runGetStatement(query);
776
+ return data.length ? data[0].version : 0;
777
+ }
778
+ //TODO: untested
779
+ async setVersion(newVersion) {
780
+ const query = `${this.migrationTableQuery()};
781
+ INSERT INTO ${MIGRATION_DATA_TABLE_NAME} (version) VALUES ${newVersion} WHERE NOT EXISTS (SELECT 1 FROM ${MIGRATION_DATA_TABLE_NAME})
782
+ UNION ALL
783
+ UPDATE ${MIGRATION_DATA_TABLE_NAME} SET version = ${newVersion} WHERE EXISTS (SELECT 1 FROM ${MIGRATION_DATA_TABLE_NAME});
784
+ `;
785
+ await this.db.runMultipleWriteStatements(query);
786
+ }
787
+ };
788
+
789
+ // src/lib/dialects/SqliteDialect.ts
790
+ var SqliteDialect = class extends DefaultSql {
791
+ typeBoolean = "INTEGER";
792
+ typeBigInt = "INTEGER";
793
+ canAlterForeignKeys = false;
794
+ canAlterPrimaryKey = false;
795
+ canInspectForeignKeys = true;
796
+ canInspectPrimaryKey = true;
797
+ changeForeignKeysState(enabled) {
798
+ return `PRAGMA foreign_keys = ${enabled ? "ON" : "OFF"};`;
799
+ }
800
+ async getTableNames() {
801
+ return (await this.db.runGetStatement("SELECT name FROM sqlite_master WHERE type = 'table'")).map((obj) => obj.name);
802
+ }
803
+ async getColumnInformation(tableName) {
804
+ const data = await this.db.runGetStatement(`PRAGMA table_info(${tableName})`);
805
+ return data.map((entry) => {
806
+ return {
807
+ name: entry["name"],
808
+ type: entry["type"],
809
+ defaultValue: entry["dflt_value"],
810
+ isPrimaryKey: entry["pk"] == "1"
811
+ };
812
+ });
865
813
  }
866
- /**
867
- * Checks all columns of all tables, creates them if they do not exist in the database and modifies them if needed.
868
- * If types, default values or primary key change, the table is recreated
869
- */
870
- async migrateColumns() {
871
- const changes = {
872
- up: "\n\n-- Migrate columns\n",
873
- down: "\n\n-- Migrate columns\n"
874
- };
875
- for (const tableName in this.tableStructureGenerator.tables) {
876
- if (this.migrations.willBeRecreated(tableName) || this.tableDoesNotExists(tableName))
877
- continue;
878
- const newTableDefinition = this.tableStructureGenerator.tables[tableName];
879
- const oldColumnList = await this.dialect.getColumnInformation(tableName, this.db);
880
- const oldPrimaryKey = this.dialect.canInspectPrimaryKey ? this.getPrimaryKeyColumn(oldColumnList) : false;
881
- const newColumnList = newTableDefinition.columns;
882
- const newPrimaryKey = newTableDefinition.primaryKey;
883
- if (this.dialect.canInspectPrimaryKey && oldPrimaryKey != newPrimaryKey) {
884
- Logger.log(`Primary key in ${tableName} was changed from ${oldPrimaryKey} to ${newPrimaryKey}!`);
885
- this.migrations.throwIfNotAllowed(this.dbInstructions.version, tableName, "alterPrimaryKey");
886
- if (this.dialect.canAlterPrimaryKey) {
887
- if (oldPrimaryKey) {
888
- changes.up += this.dialect.removePrimaryKey(tableName, oldPrimaryKey);
889
- changes.down += this.dialect.addPrimaryKey(tableName, oldPrimaryKey);
890
- }
891
- if (newPrimaryKey) {
892
- changes.up += this.dialect.addPrimaryKey(tableName, newPrimaryKey.toString());
893
- changes.down += this.dialect.removePrimaryKey(tableName, newPrimaryKey.toString());
894
- }
895
- } else
896
- this.migrations.recreateTable(newTableDefinition.table);
897
- continue;
898
- }
899
- for (const newColumn of newColumnList) {
900
- const oldColumn = oldColumnList.find((entry) => entry.name == newColumn.name);
901
- if (oldColumn == void 0) {
902
- changes.up += this.dialect.createColumn(tableName, this.dialect.columnDefinition(newColumn.name, newColumn.type, newColumn.defaultValue, newColumn.isPrimaryKey)) + "\n";
903
- changes.down += this.dialect.dropColumn(tableName, newColumn.name) + "\n";
904
- } else if (newColumn.type != oldColumn.type || newColumn.defaultValue != oldColumn.defaultValue) {
905
- this.migrations.recreateTable(newTableDefinition.table);
906
- }
907
- }
908
- for (const oldColumn of oldColumnList) {
909
- const newColumn = newColumnList.find((entry) => entry.name == oldColumn.name);
910
- if (newColumn == void 0) {
911
- this.migrations.throwIfNotAllowed(this.dbInstructions.version, tableName, "dropColumn");
912
- changes.up += this.dialect.dropColumn(tableName, oldColumn.name) + "\n";
913
- changes.down += this.dialect.createColumn(tableName, this.dialect.columnDefinition(oldColumn.name, oldColumn.type, oldColumn.defaultValue, oldColumn.isPrimaryKey)) + "\n";
914
- }
915
- }
814
+ formatValueToSql(value) {
815
+ switch (typeof value) {
816
+ case "boolean":
817
+ return value ? "1" : "0";
818
+ default:
819
+ return value.toString();
916
820
  }
917
- return changes;
918
821
  }
919
- /**
920
- * Retrieves the name of the primary key column from a list of column information.
921
- *
922
- * @param columnInfoList - An array of ColumnInfo objects containing details about each column.
923
- * @return The name of the primary key column if found; otherwise, undefined.
924
- */
925
- getPrimaryKeyColumn(columnInfoList) {
926
- for (const columnInfo of columnInfoList) {
927
- if (columnInfo.isPrimaryKey)
928
- return columnInfo.name;
929
- }
822
+ async getForeignKeys(tableName) {
823
+ const data = await this.db.runGetStatement(`PRAGMA foreign_key_list(${tableName});`);
824
+ return data.map((entry) => {
825
+ return {
826
+ fromTable: tableName,
827
+ fromColumn: entry["from"],
828
+ toTable: entry["table"],
829
+ toColumn: entry["to"],
830
+ on_update: entry["on_update"],
831
+ on_delete: entry["on_delete"]
832
+ };
833
+ });
930
834
  }
931
- /**
932
- * Handles the renaming of columns in database tables by generating SQL changes for both applying and reverting the renames.
933
- * Skips tables that are marked for recreation.
934
- */
935
- renameColumns() {
936
- const changes = {
937
- up: "\n\n-- Rename columns\n",
938
- down: "\n\n-- Rename columns\n"
939
- };
940
- const migrationData = this.migrations.getMigrationData();
941
- for (const tableName in migrationData) {
942
- if (migrationData[tableName].recreate)
943
- continue;
944
- this.migrations.loopRenamedColumns(tableName, (oldColumnName, newColumnName) => {
945
- changes.up += this.dialect.renameColumn(tableName, oldColumnName, newColumnName);
946
- changes.down += this.dialect.renameColumn(tableName, newColumnName, oldColumnName);
947
- });
948
- }
949
- return changes;
835
+ async getVersion() {
836
+ const output = await this.db.runGetStatement(`PRAGMA user_version;`);
837
+ return output[0].user_version;
838
+ }
839
+ async setVersion(newVersion) {
840
+ await this.db.runMultipleWriteStatements(`PRAGMA user_version = ${newVersion};`);
950
841
  }
951
842
  };
952
843
 
953
- // src/lib/tableInfo/decorators/DbTable.ts
954
- import "polyfill-symbol-metadata";
955
- function DbTable(tableName, primaryKey) {
956
- return (table, context) => {
957
- Object.defineProperty(table, "name", { value: tableName, writable: false });
958
- const tableInfo = context?.metadata ? getTableInfoFromMetadata(context.metadata) : getTableInfo(table);
959
- tableInfo.primaryKey = primaryKey?.toString();
960
- if (tableInfo.foreignKeys) {
961
- for (const foreignKey of tableInfo.foreignKeys) {
962
- foreignKey.fromTable = tableName;
963
- }
964
- }
965
- };
966
- }
844
+ // src/lib/dialects/PostgresDialect.ts
845
+ var PostgresDialect = class extends DefaultSql {
846
+ canAlterForeignKeys = true;
847
+ canAlterPrimaryKey = true;
848
+ canInspectForeignKeys = false;
849
+ canInspectPrimaryKey = false;
850
+ changeForeignKeysState(enabled) {
851
+ return `SET session_replication_role = '${enabled ? "replica" : "origin"}';`;
852
+ }
853
+ addForeignKey(fromTableName, foreignKey) {
854
+ return `ALTER TABLE ${fromTableName} ADD CONSTRAINT ${fromTableName} ${foreignKey};`;
855
+ }
856
+ removeForeignKey(tableName, foreignKeyName) {
857
+ return `ALTER TABLE ${tableName} DROP CONSTRAINT ${foreignKeyName};`;
858
+ }
859
+ async getTableNames() {
860
+ return await this.db.runGetStatement("SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE';");
861
+ }
862
+ async getColumnInformation(tableName) {
863
+ const data = await this.db.runGetStatement(`SELECT column_name, data_type, column_default FROM information_schema.columns WHERE table_schema = 'public' AND table_name = '${tableName}';`);
864
+ return data.map((entry) => {
865
+ return {
866
+ name: entry["column_name"],
867
+ type: entry["data_type"],
868
+ defaultValue: entry["column_default"],
869
+ isPrimaryKey: false
870
+ };
871
+ });
872
+ }
873
+ addPrimaryKey(tableName, columnName) {
874
+ return `ALTER TABLE ${tableName} ADD PRIMARY KEY (${columnName});
875
+ `;
876
+ }
877
+ removePrimaryKey(tableName, columnName) {
878
+ return `ALTER TABLE ${tableName} DROP CONSTRAINT ${columnName};
879
+ `;
880
+ }
881
+ };
967
882
 
968
- // src/lib/tableInfo/decorators/ForeignKey.ts
969
- import "polyfill-symbol-metadata";
970
- function ForeignKey(toTable, toColumn, onDelete, onUpdate) {
971
- return (table, context) => {
972
- const metadata = context?.metadata ? getTableInfoFromMetadata(context.metadata) : getTableInfo(table);
973
- if (!metadata.foreignKeys)
974
- metadata.foreignKeys = [];
975
- metadata.foreignKeys.push({
976
- fromTable: "",
977
- fromColumn: context.name ?? context,
978
- //to stay compatible with old decorator structure (see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#decorator-metadata)
979
- toTable: toTable.name,
980
- toColumn: toColumn.toString(),
981
- onDelete,
982
- onUpdate
883
+ // src/lib/dialects/MsSqlDialect.ts
884
+ var MsSqlDialect = class extends DefaultSql {
885
+ canAlterForeignKeys = true;
886
+ canAlterPrimaryKey = true;
887
+ canInspectForeignKeys = true;
888
+ canInspectPrimaryKey = false;
889
+ changeForeignKeysState(enabled) {
890
+ return `EXEC sp_MSforeachtable "${enabled ? "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all" : "ALTER TABLE ? NOCHECK CONSTRAINT all"}"`;
891
+ }
892
+ addForeignKey(fromTableName, foreignKey) {
893
+ return `ALTER TABLE ${fromTableName} ADD ${foreignKey};`;
894
+ }
895
+ removeForeignKey(tableName, foreignKeyName) {
896
+ return `ALTER TABLE ${tableName} DROP CONSTRAINT ${foreignKeyName};`;
897
+ }
898
+ addPrimaryKey(tableName, columnName) {
899
+ return `ALTER TABLE ${tableName} ADD CONSTRAINT ${tableName}_primary PRIMARY KEY (${columnName});`;
900
+ }
901
+ removePrimaryKey(tableName, columnName) {
902
+ return `ALTER TABLE ${tableName} DROP CONSTRAINT ${columnName}_primary;`;
903
+ }
904
+ async getForeignKeys(tableName) {
905
+ const data = await this.db.runGetStatement(`EXEC sp_fkeys @fktable_name = ${tableName};`);
906
+ return data.map((entry) => {
907
+ return {
908
+ fromTable: entry[tableName],
909
+ fromColumn: entry["FKCOLUMN_NAME"],
910
+ toTable: entry["PKTABLE_NAME"],
911
+ toColumn: entry["PKCOLUMN_NAME"]
912
+ };
983
913
  });
984
- };
914
+ }
915
+ async getTableNames() {
916
+ return await this.db.runGetStatement("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'");
917
+ }
918
+ async getColumnInformation(tableName) {
919
+ const data = await this.db.runGetStatement(`SELECT COLUMN_NAME, DATA_TYPE, COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'${tableName};'`);
920
+ return data.map((entry) => {
921
+ return {
922
+ name: entry["COLUMN_NAME"],
923
+ type: entry["DATA_TYPE"],
924
+ defaultValue: entry["COLUMN_DEFAULT"],
925
+ isPrimaryKey: false
926
+ };
927
+ });
928
+ }
929
+ };
930
+
931
+ // src/lib/dialects/MySqlDialect.ts
932
+ var MySqlDialect = class extends DefaultSql {
933
+ canAlterForeignKeys = true;
934
+ canAlterPrimaryKey = true;
935
+ canInspectForeignKeys = true;
936
+ canInspectPrimaryKey = true;
937
+ changeForeignKeysState(enabled) {
938
+ return `SET FOREIGN_KEY_CHECKS ${enabled ? "1" : "0"}
939
+ ;`;
940
+ }
941
+ addForeignKey(fromTableName, foreignKey) {
942
+ return `ALTER TABLE ${fromTableName} ADD ${foreignKey};`;
943
+ }
944
+ removeForeignKey(tableName, foreignKeyName) {
945
+ return `ALTER TABLE ${tableName} DROP FOREIGN KEY ${foreignKeyName};`;
946
+ }
947
+ addPrimaryKey(tableName, columnName) {
948
+ return `ALTER TABLE ${tableName} PRIMARY KEY (${columnName});`;
949
+ }
950
+ removePrimaryKey(tableName, _columnName) {
951
+ return `ALTER TABLE ${tableName} PRIMARY KEY;`;
952
+ }
953
+ async getForeignKeys(tableName) {
954
+ 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}';`);
955
+ return data.map((entry) => {
956
+ return {
957
+ fromTable: tableName,
958
+ fromColumn: entry["COLUMN_NAME"],
959
+ toTable: entry["REFERENCED_TABLE_NAME"],
960
+ toColumn: entry["REFERENCED_COLUMN_NAME"]
961
+ };
962
+ });
963
+ }
964
+ async getTableNames() {
965
+ return await this.db.runGetStatement("SHOW TABLES;");
966
+ }
967
+ async getColumnInformation(tableName) {
968
+ const data = await this.db.runGetStatement(`SHOW COLUMNS FROM ${tableName};`);
969
+ return data.map((entry) => {
970
+ return {
971
+ name: entry["Field"],
972
+ type: entry["Type"],
973
+ defaultValue: entry["Default"],
974
+ isPrimaryKey: entry["Key"] == "PRI"
975
+ };
976
+ });
977
+ }
978
+ };
979
+
980
+ // src/lib/getDialect.ts
981
+ function getDialect(db, dbInstructions) {
982
+ switch (dbInstructions.dialect) {
983
+ case "Sqlite":
984
+ return new SqliteDialect(db);
985
+ case "Postgres":
986
+ return new PostgresDialect(db);
987
+ case "MsSql":
988
+ return new MsSqlDialect(db);
989
+ case "MySql":
990
+ return new MySqlDialect(db);
991
+ default:
992
+ throw new Error(`Unknown dialect ${dbInstructions.dialect}`);
993
+ }
985
994
  }
986
995
 
987
996
  // src/index.ts
988
- async function prepareMigration(db, instructions, overwriteExisting) {
989
- Logger.setMode(instructions.loggerMode);
990
- const mm = new MigrationManager(db, instructions);
991
- await mm.prepareMigration(overwriteExisting);
997
+ async function prepareMigration(db, dbInstructions, overwriteExisting) {
998
+ Logger.setMode(dbInstructions.loggerMode);
999
+ const dialect = getDialect(db, dbInstructions);
1000
+ const mm = new MigrationManager(dialect);
1001
+ const migration = await mm.generateSqlChanges(dbInstructions);
1002
+ if (!migration)
1003
+ return;
1004
+ const migrationHistoryManager = new MigrationHistoryManager(dbInstructions.configPath);
1005
+ migrationHistoryManager.createMigrationHistory(migration.fromVersion, dbInstructions.version, migration.changes, overwriteExisting);
992
1006
  }
993
- async function runPreparedMigrations(db, instructions) {
994
- Logger.setMode(instructions.loggerMode);
995
- const migrationHistoryManager = new MigrationHistoryManager(instructions.configPath);
996
- const dialect = getDialect(instructions);
997
- const fromVersion = await dialect.getVersion(db);
998
- const toVersion = instructions.version;
1007
+ async function runPreparedMigrations(db, dbInstructions) {
1008
+ Logger.setMode(dbInstructions.loggerMode);
1009
+ const historyManager = new MigrationHistoryManager(dbInstructions.configPath);
1010
+ const dialect = getDialect(db, dbInstructions);
1011
+ const fromVersion = await dialect.getVersion();
1012
+ const toVersion = dbInstructions.version;
999
1013
  if (fromVersion == toVersion)
1000
1014
  return;
1015
+ await db.createBackup?.(`from_${fromVersion}_to_${dbInstructions.version}`);
1001
1016
  Logger.log(`Running migrations from ${fromVersion} to ${toVersion}`);
1002
1017
  for (let i = fromVersion ? fromVersion + 1 : toVersion; i <= toVersion; ++i) {
1003
- const upChanges = migrationHistoryManager.getUpMigration(i);
1018
+ const upChanges = historyManager.getUpMigration(i);
1004
1019
  Logger.debug(upChanges);
1005
1020
  await db.runMultipleWriteStatements(upChanges);
1006
- instructions.version = i;
1021
+ dbInstructions.version = i;
1007
1022
  }
1008
- await dialect.setVersion(db, toVersion);
1023
+ await dialect.setVersion(toVersion);
1009
1024
  }
1010
1025
  async function prepareAndRunMigration(db, instructions, overwriteExisting) {
1011
1026
  Logger.setMode(instructions.loggerMode);
1012
1027
  await prepareMigration(db, instructions, overwriteExisting);
1013
1028
  await runPreparedMigrations(db, instructions);
1014
1029
  }
1015
- async function runMigrationWithoutHistory(db, instructions) {
1016
- Logger.setMode(instructions.loggerMode);
1017
- const mm = new MigrationManager(db, instructions);
1018
- const changes = await mm.getMigrateSql();
1019
- if (changes)
1020
- await db.runMultipleWriteStatements(changes.up);
1030
+ async function runMigrationWithoutHistory(db, dbInstructions) {
1031
+ Logger.setMode(dbInstructions.loggerMode);
1032
+ const dialect = getDialect(db, dbInstructions);
1033
+ const mm = new MigrationManager(dialect);
1034
+ const migration = await mm.generateSqlChanges(dbInstructions);
1035
+ if (migration) {
1036
+ await db.createBackup?.(`from_${migration.fromVersion}_to_${dbInstructions.version}`);
1037
+ await db.runMultipleWriteStatements(migration.changes.up);
1038
+ }
1021
1039
  }
1022
- async function rollback(db, instructions, toVersion) {
1023
- Logger.setMode(instructions.loggerMode);
1024
- const migrationHistoryManager = new MigrationHistoryManager(instructions.configPath);
1025
- const dialect = getDialect(instructions);
1026
- const fromVersion = await dialect.getVersion(db);
1040
+ async function rollback(db, dbInstructions, toVersion) {
1041
+ Logger.setMode(dbInstructions.loggerMode);
1042
+ const dialect = getDialect(db, dbInstructions);
1043
+ const fromVersion = await dialect.getVersion();
1044
+ const historyManager = new MigrationHistoryManager(dbInstructions.configPath);
1045
+ await db.createBackup?.(`from_${fromVersion}_to_${dbInstructions.version}`);
1027
1046
  Logger.log(`Rolling back from ${fromVersion} to ${toVersion}`);
1028
1047
  for (let i = fromVersion - 1; i >= toVersion; --i) {
1029
- const upChanges = migrationHistoryManager.getDownMigration(i);
1048
+ const upChanges = historyManager.getDownMigration(i);
1030
1049
  Logger.debug(upChanges);
1031
1050
  await db.runMultipleWriteStatements(upChanges);
1032
- instructions.version = i;
1051
+ dbInstructions.version = i;
1033
1052
  }
1034
- await dialect.setVersion(db, toVersion);
1053
+ await dialect.setVersion(toVersion);
1035
1054
  }
1036
1055
  export {
1037
1056
  DbTable,