@spinajs/orm
Advanced tools
Comparing version 1.2.83 to 1.2.84
@@ -110,2 +110,6 @@ import { Op } from './enums'; | ||
/** | ||
* Should run migration on startup | ||
*/ | ||
OnStartup?: boolean; | ||
/** | ||
* Migration table name, if not set default is spinajs_orm_migrations | ||
@@ -112,0 +116,0 @@ */ |
@@ -21,3 +21,3 @@ import { Configuration } from '@spinajs/configuration'; | ||
*/ | ||
migrateUp(name?: string): Promise<void>; | ||
migrateUp(name?: string, force?: boolean): Promise<void>; | ||
/** | ||
@@ -29,3 +29,3 @@ * | ||
*/ | ||
migrateDown(name?: string): Promise<void>; | ||
migrateDown(name?: string, force?: boolean): Promise<void>; | ||
/** | ||
@@ -61,3 +61,2 @@ * This function is exposed mainly for unit testing purposes. It reloads table information for models | ||
private executeAvaibleMigrations; | ||
private prepareMigrations; | ||
} |
@@ -65,4 +65,3 @@ "use strict"; | ||
*/ | ||
async migrateUp(name) { | ||
await this.prepareMigrations(); | ||
async migrateUp(name, force = true) { | ||
this.Log.info('DB migration UP started ...'); | ||
@@ -89,3 +88,3 @@ await this.executeAvaibleMigrations(name, async (migration, driver) => { | ||
} | ||
}, false); | ||
}, false, force); | ||
this.Log.info('DB migration ended ...'); | ||
@@ -99,4 +98,3 @@ } | ||
*/ | ||
async migrateDown(name) { | ||
await this.prepareMigrations(); | ||
async migrateDown(name, force = true) { | ||
this.Log.info('DB migration DOWN started ...'); | ||
@@ -122,3 +120,3 @@ await this.executeAvaibleMigrations(name, async (migration, driver) => { | ||
} | ||
}, true); | ||
}, true, force); | ||
this.Log.info('DB migration ended ...'); | ||
@@ -153,3 +151,2 @@ } | ||
async resolveAsync() { | ||
const migrateOnStartup = this.Configuration.get('db.Migration.Startup', false); | ||
await this.createConnections(); | ||
@@ -163,5 +160,3 @@ // add all registered migrations via DI | ||
}); | ||
if (migrateOnStartup) { | ||
await this.migrateUp(); | ||
} | ||
await this.migrateUp(undefined, false); | ||
await this.reloadTableInfo(); | ||
@@ -253,4 +248,4 @@ this.applyModelMixins(); | ||
} | ||
async executeAvaibleMigrations(name, callback, down) { | ||
var _a, _b; | ||
async executeAvaibleMigrations(name, callback, down, force) { | ||
var _a, _b, _c; | ||
const toMigrate = name ? this.Migrations.filter((m) => m.name === name) : this.Migrations; | ||
@@ -278,20 +273,17 @@ let migrations = toMigrate | ||
const cn = this.Connections.get(md.Connection); | ||
if (!cn) { | ||
this.Log.warn(`Connection ${md.Connection} not exists for migration ${m.name} at file ${m.file}`); | ||
continue; | ||
} | ||
const migrationTableName = (_b = (_a = cn.Options.Migration) === null || _a === void 0 ? void 0 : _a.Table) !== null && _b !== void 0 ? _b : MIGRATION_TABLE_NAME; | ||
const exists = await cn.select().from(migrationTableName).where({ Migration: m.name }).orderByDescending('CreatedAt').first(); | ||
if (!exists) { | ||
const migration = await this.Container.resolve(m.type, [cn]); | ||
this.Log.info(`Setting up migration ${m.name} from file ${m.file} created at ${m.created} mode: ${down ? 'migrate down' : 'migrate up'}`); | ||
await callback(migration, cn); | ||
if (!((_c = cn.Options.Migration) === null || _c === void 0 ? void 0 : _c.OnStartup)) { | ||
if (!force) { | ||
continue; | ||
} | ||
} | ||
} | ||
} | ||
async prepareMigrations() { | ||
var _a, _b; | ||
for (const [_, connection] of this.Connections) { | ||
const migrationTableName = (_b = (_a = connection.Options.Migration) === null || _a === void 0 ? void 0 : _a.Table) !== null && _b !== void 0 ? _b : MIGRATION_TABLE_NAME; | ||
// if there is no info on migraiton table | ||
const migrationTableExists = await connection.schema().tableExists(migrationTableName, connection.Options.Database); | ||
const migrationTableExists = await cn.schema().tableExists(migrationTableName, cn.Options.Database); | ||
if (!migrationTableExists) { | ||
this.Log.info(`No migration table in database, recreating migration information ...`); | ||
await connection.schema().createTable(migrationTableName, (table) => { | ||
await cn.schema().createTable(migrationTableName, (table) => { | ||
table.string('Migration').unique().notNull(); | ||
@@ -301,2 +293,8 @@ table.dateTime('CreatedAt').notNull(); | ||
} | ||
const exists = await cn.select().from(migrationTableName).where({ Migration: m.name }).orderByDescending('CreatedAt').first(); | ||
if (!exists) { | ||
const migration = await this.Container.resolve(m.type, [cn]); | ||
this.Log.info(`Setting up migration ${m.name} from file ${m.file} created at ${m.created} mode: ${down ? 'migrate down' : 'migrate up'}`); | ||
await callback(migration, cn); | ||
} | ||
} | ||
@@ -303,0 +301,0 @@ } |
{ | ||
"name": "@spinajs/orm", | ||
"version": "1.2.83", | ||
"version": "1.2.84", | ||
"description": "framework orm module", | ||
@@ -54,3 +54,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "e2502149a863bdfc453a0b75ccc497f21d8ebef5" | ||
"gitHead": "f8e47e53317001c2023be382e1362c4b591c1561" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6209
354102