70 lines
2.7 KiB
JavaScript
Raw Normal View History

2021-02-18 18:42:28 +01:00
'use strict';
const { migrate } = require('./migrate');
const { areScalarAttrsOnly } = require('./utils');
2021-02-25 17:40:14 +01:00
2021-03-02 16:42:17 +01:00
const TMP_TABLE_NAME = '__tmp__i18n_field_migration';
const batchInsertInTmpTable = async ({ updatesInfo }, { transacting: trx }) => {
2021-03-02 16:42:17 +01:00
const tmpEntries = [];
updatesInfo.forEach(({ entriesIdsToUpdate, attributesValues }) => {
entriesIdsToUpdate.forEach(id => {
tmpEntries.push({ id, ...attributesValues });
});
});
await trx.batchInsert(TMP_TABLE_NAME, tmpEntries, 100);
2021-03-01 11:26:44 +01:00
};
const updateFromTmpTable = async ({ model, attrsToMigrate }, { transacting: trx }) => {
2021-03-02 16:42:17 +01:00
const collectionName = model.collectionName;
if (model.client === 'pg') {
const substitutes = attrsToMigrate.map(() => '?? = ??.??').join(',');
2021-03-11 10:41:35 +01:00
const bindings = [collectionName];
attrsToMigrate.forEach(attr => bindings.push(attr, TMP_TABLE_NAME, attr));
2021-03-02 16:42:17 +01:00
bindings.push(TMP_TABLE_NAME, collectionName, TMP_TABLE_NAME);
await trx.raw(`UPDATE ?? SET ${substitutes} FROM ?? WHERE ??.id = ??.id;`, bindings);
} else if (model.client === 'mysql') {
const substitutes = attrsToMigrate.map(() => '??.?? = ??.??').join(',');
2021-03-11 10:41:35 +01:00
const bindings = [collectionName, TMP_TABLE_NAME, collectionName, TMP_TABLE_NAME];
attrsToMigrate.forEach(attr => bindings.push(collectionName, attr, TMP_TABLE_NAME, attr));
2021-03-02 16:42:17 +01:00
await trx.raw(`UPDATE ?? JOIN ?? ON ??.id = ??.id SET ${substitutes};`, bindings);
}
};
2021-02-25 17:40:14 +01:00
const createTmpTable = async ({ ORM, attrsToMigrate, model }) => {
const columnsToCopy = ['id', ...attrsToMigrate];
2021-03-19 12:12:51 +01:00
await deleteTmpTable({ ORM });
2021-02-25 17:40:14 +01:00
await ORM.knex.raw(`CREATE TABLE ?? AS ??`, [
TMP_TABLE_NAME,
ORM.knex
.select(columnsToCopy)
.from(model.collectionName)
.whereRaw('?', 0),
]);
2021-03-02 16:42:17 +01:00
};
2021-02-25 17:40:14 +01:00
2021-03-02 16:42:17 +01:00
const deleteTmpTable = ({ ORM }) => ORM.knex.schema.dropTableIfExists(TMP_TABLE_NAME);
const migrateForBookshelf = async ({ ORM, model, attrsToMigrate }) => {
const onlyScalarAttrs = areScalarAttrsOnly({ model, attributes: attrsToMigrate });
2021-03-18 18:27:15 +01:00
// optimize migration for pg and mysql when there are only scalar attributes to migrate
if (onlyScalarAttrs && ['pg', 'mysql'].includes(model.client)) {
2021-03-19 10:21:56 +01:00
// create table outside of the transaction because mysql doesn't accept the creation inside
await createTmpTable({ ORM, attrsToMigrate, model });
await ORM.knex.transaction(async transacting => {
await migrate({ model, attrsToMigrate }, { migrateFn: batchInsertInTmpTable, transacting });
await updateFromTmpTable({ model, attrsToMigrate }, { transacting });
2021-03-18 18:27:15 +01:00
});
await deleteTmpTable({ ORM });
} else {
await ORM.knex.transaction(async transacting => {
await migrate({ model, attrsToMigrate }, { transacting });
2021-03-18 18:27:15 +01:00
});
2021-02-25 17:40:14 +01:00
}
};
2021-03-02 17:09:35 +01:00
module.exports = migrateForBookshelf;