add previousDefinition in migration options

This commit is contained in:
Pierre Noël 2021-02-16 17:43:41 +01:00
parent ddd4b37fb2
commit 9950bbe4c0
5 changed files with 39 additions and 19 deletions

View File

@ -4,7 +4,11 @@ const _ = require('lodash');
const { singular } = require('pluralize');
const { contentTypes: contentTypesUtils } = require('strapi-utils');
const { storeDefinition, getColumnsWhereDefinitionChanged } = require('./utils/store-definition');
const {
getDefinitionFromStore,
storeDefinition,
getColumnsWhereDefinitionChanged,
} = require('./utils/store-definition');
const { getManyRelations } = require('./utils/associations');
const migrateSchemas = async ({ ORM, loadedModel, definition, connection, model }, context) => {
@ -397,10 +401,14 @@ const createOrUpdateTable = async ({ table, attributes, definition, ORM, model }
};
module.exports = async ({ ORM, loadedModel, definition, connection, model }) => {
const previousDefinitionRow = await getDefinitionFromStore(definition, ORM);
const previousDefinition = JSON.parse(_.get(previousDefinitionRow, 'value', null));
// run migrations
await strapi.db.migrations.run(migrateSchemas, {
ORM,
loadedModel,
previousDefinition,
definition,
connection,
model,

View File

@ -4,12 +4,9 @@ const _ = require('lodash');
const { contentTypes: contentTypesUtils } = require('strapi-utils');
const { PUBLISHED_AT_ATTRIBUTE } = contentTypesUtils.constants;
const { getDefinitionFromStore } = require('../utils/store-definition');
const getDraftAndPublishMigrationWay = async ({ definition, ORM }) => {
const previousDefRow = await getDefinitionFromStore(definition, ORM);
const previousDef = JSON.parse(_.get(previousDefRow, 'value', null));
const previousDraftAndPublish = contentTypesUtils.hasDraftAndPublish(previousDef);
const getDraftAndPublishMigrationWay = async ({ definition, previousDefinition }) => {
const previousDraftAndPublish = contentTypesUtils.hasDraftAndPublish(previousDefinition);
const actualDraftAndPublish = contentTypesUtils.hasDraftAndPublish(definition);
if (previousDraftAndPublish === actualDraftAndPublish) {
@ -23,8 +20,8 @@ const getDraftAndPublishMigrationWay = async ({ definition, ORM }) => {
}
};
const before = async ({ definition, ORM }, context) => {
const way = await getDraftAndPublishMigrationWay({ definition, ORM });
const before = async ({ definition, previousDefinition, ORM }, context) => {
const way = await getDraftAndPublishMigrationWay({ definition, previousDefinition });
if (way === 'disable') {
const publishedAtColumnExists = await ORM.knex.schema.hasColumn(
@ -50,8 +47,8 @@ const before = async ({ definition, ORM }, context) => {
}
};
const after = async ({ definition, ORM }) => {
const way = await getDraftAndPublishMigrationWay({ definition, ORM });
const after = async ({ definition, previousDefinition, ORM }) => {
const way = await getDraftAndPublishMigrationWay({ definition, previousDefinition });
if (way === 'enable') {
const now = new Date();

View File

@ -4,15 +4,12 @@ const _ = require('lodash');
const { contentTypes: contentTypesUtils } = require('strapi-utils');
const { PUBLISHED_AT_ATTRIBUTE } = contentTypesUtils.constants;
const { getDefinitionFromStore } = require('../utils/store-definition');
const getDraftAndPublishMigrationWay = async (definition, ORM) => {
const previousDefRow = await getDefinitionFromStore(definition, ORM);
const previousDef = JSON.parse(_.get(previousDefRow, 'value', null));
const previousDraftAndPublish = contentTypesUtils.hasDraftAndPublish(previousDef);
const getDraftAndPublishMigrationWay = async ({ definition, previousDefinition }) => {
const previousDraftAndPublish = contentTypesUtils.hasDraftAndPublish(previousDefinition);
const actualDraftAndPublish = contentTypesUtils.hasDraftAndPublish(definition);
if (!previousDefRow || previousDraftAndPublish === actualDraftAndPublish) {
if (!previousDefinition || previousDraftAndPublish === actualDraftAndPublish) {
return 'none';
}
if (!previousDraftAndPublish && actualDraftAndPublish) {
@ -23,8 +20,8 @@ const getDraftAndPublishMigrationWay = async (definition, ORM) => {
}
};
const migrateDraftAndPublish = async ({ definition, model, ORM }) => {
let way = await getDraftAndPublishMigrationWay(definition, ORM);
const migrateDraftAndPublish = async ({ definition, previousDefinition, model }) => {
let way = await getDraftAndPublishMigrationWay({ definition, previousDefinition });
if (way === 'enable') {
const createdAtCol = _.get(definition, 'timestamps.createdAt', 'createdAt');

View File

@ -8,7 +8,11 @@ const utils = require('./utils');
const populateQueries = require('./utils/populate-queries');
const relations = require('./relations');
const { findComponentByGlobalId } = require('./utils/helpers');
const { didDefinitionChange, storeDefinition } = require('./utils/store-definition');
const {
didDefinitionChange,
storeDefinition,
getDefinitionFromStore,
} = require('./utils/store-definition');
const {
PUBLISHED_AT_ATTRIBUTE,
@ -342,9 +346,13 @@ module.exports = async ({ models, target }, ctx) => {
const modelInstance = target[model];
const definitionDidChange = await didDefinitionChange(definition, instance);
const previousDefinitionRow = await getDefinitionFromStore(definition, instance);
const previousDefinition = JSON.parse(_.get(previousDefinitionRow, 'value', null));
// run migrations
await strapi.db.migrations.run(migrateSchema, {
definition,
previousDefinition,
model: modelInstance,
ORM: instance,
});

View File

@ -0,0 +1,10 @@
'use strict';
//
// const before = ({ definition, ORM }) => {
// const previousDefRow = await getDefinitionFromStore(definition, ORM);
//
// };
//
// module.exports = {
// before,
// };