mirror of
https://github.com/strapi/strapi.git
synced 2025-12-28 15:44:59 +00:00
Add migration manager in database
This commit is contained in:
parent
443ace7c62
commit
1592417ed3
@ -398,13 +398,16 @@ const createOrUpdateTable = async ({ table, attributes, definition, ORM, model }
|
||||
}
|
||||
};
|
||||
|
||||
const migrationRunner = createMigrationRunner(migrateSchemas, {
|
||||
hooks: [draftPublishMigration],
|
||||
});
|
||||
|
||||
module.exports = async ({ ORM, loadedModel, definition, connection, model }) => {
|
||||
await strapi.db.migrations.register(draftPublishMigration);
|
||||
// run migrations
|
||||
await migrationRunner.run({ ORM, loadedModel, definition, connection, model });
|
||||
await strapi.db.migrations.runMigration(migrateSchemas, {
|
||||
ORM,
|
||||
loadedModel,
|
||||
definition,
|
||||
connection,
|
||||
model,
|
||||
});
|
||||
|
||||
// store new definitions
|
||||
await storeDefinition(definition, ORM);
|
||||
|
||||
@ -1,33 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const _ = require('lodash');
|
||||
|
||||
const createMigrationRunner = (migrationFunction, { hooks = [] } = {}, context = {}) => {
|
||||
const beforeHook = async (options, context) => {
|
||||
for (const migration of hooks) {
|
||||
if (_.isFunction(migration.before)) {
|
||||
await migration.before(options, context);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const afterHook = async (options, context) => {
|
||||
for (const migration of hooks.slice(0).reverse()) {
|
||||
if (_.isFunction(migration.after)) {
|
||||
await migration.after(options, context);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const run = async options => {
|
||||
await beforeHook(options, context);
|
||||
await migrationFunction(options, context);
|
||||
await afterHook(options, context);
|
||||
};
|
||||
|
||||
return {
|
||||
run,
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = createMigrationRunner;
|
||||
@ -6,6 +6,7 @@ const { createQuery } = require('./queries');
|
||||
const createConnectorRegistry = require('./connector-registry');
|
||||
const constants = require('./constants');
|
||||
const { validateModelSchemas } = require('./validation');
|
||||
const createMigrationManager = require('./migration-manager');
|
||||
|
||||
class DatabaseManager {
|
||||
constructor(strapi) {
|
||||
@ -20,6 +21,8 @@ class DatabaseManager {
|
||||
|
||||
this.queries = new Map();
|
||||
this.models = new Map();
|
||||
|
||||
this.migrations = createMigrationManager(this);
|
||||
}
|
||||
|
||||
async initialize() {
|
||||
|
||||
48
packages/strapi-database/lib/migration-manager.js
Normal file
48
packages/strapi-database/lib/migration-manager.js
Normal file
@ -0,0 +1,48 @@
|
||||
'use strict';
|
||||
const debug = require('debug')('strapi-database:migration');
|
||||
const { isFunction } = require('lodash/fp');
|
||||
|
||||
class MigrationManager {
|
||||
constructor(db) {
|
||||
debug('Initialize migration manager');
|
||||
this.db = db;
|
||||
this.migrations = [];
|
||||
}
|
||||
|
||||
register(migration) {
|
||||
debug('Register migration');
|
||||
// validateMigration
|
||||
this.migrations.push(migration);
|
||||
}
|
||||
|
||||
async runMigration(fn, options, context = {}) {
|
||||
debug('Run migration');
|
||||
await this.runBefore(options, context);
|
||||
await await fn(options, context);
|
||||
await this.runAfter(options, context);
|
||||
}
|
||||
|
||||
async runBefore(options, context) {
|
||||
debug('Run before migrations');
|
||||
|
||||
for (const migration of this.migrations) {
|
||||
if (isFunction(migration.before)) {
|
||||
await migration.before(options, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async runAfter(options, context) {
|
||||
debug('Run after migrations');
|
||||
|
||||
for (const migration of this.migrations.slice(0).reverse()) {
|
||||
if (isFunction(migration.after)) {
|
||||
await migration.after(options, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = strapi => {
|
||||
return new MigrationManager(strapi);
|
||||
};
|
||||
@ -59,23 +59,14 @@ module.exports = () => {
|
||||
}
|
||||
});
|
||||
|
||||
// strapi.database.migrations.push({
|
||||
// before() {
|
||||
// // if model had i18N but doesn't anymore
|
||||
// // on enable
|
||||
// // -> set locale to default locale
|
||||
// // -> init localizations json
|
||||
// // -> init strapiId
|
||||
// // on disabled
|
||||
// // -> delete data not in default locale
|
||||
// // -> remove default locale ?
|
||||
// // needed operations
|
||||
// },
|
||||
// after() {
|
||||
// // delete la data
|
||||
// // deleteColumn('locale');
|
||||
// },
|
||||
// });
|
||||
strapi.db.migrations.register({
|
||||
before() {
|
||||
console.log('before migration');
|
||||
},
|
||||
after() {
|
||||
console.log('after migration');
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -335,9 +335,9 @@ class Strapi {
|
||||
this.models['core_store'] = coreStoreModel(this.config);
|
||||
this.models['strapi_webhooks'] = webhookModel(this.config);
|
||||
|
||||
await this.runRegisterFunctions();
|
||||
|
||||
this.db = createDatabaseManager(this);
|
||||
|
||||
await this.runRegisterFunctions();
|
||||
await this.db.initialize();
|
||||
|
||||
this.store = createCoreStore({
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
const debug = require('debug')('strapi');
|
||||
const debug = require('debug')('strapi:webhook');
|
||||
const _ = require('lodash');
|
||||
const fetch = require('node-fetch');
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
const debug = require('debug')('strapi');
|
||||
const debug = require('debug')('strapi:worker-queue');
|
||||
|
||||
module.exports = class WorkerQueue {
|
||||
constructor({ logger, concurrency = 5 } = {}) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user