mirror of
https://github.com/strapi/strapi.git
synced 2025-09-26 08:52:26 +00:00
Merge pull request #13350 from strapi/ML-folder/transactions
Ml folder/transactions
This commit is contained in:
commit
a9f53dc53b
@ -58,6 +58,10 @@ class Database {
|
|||||||
return schema ? trx.schema.withSchema(schema) : trx.schema;
|
return schema ? trx.schema.withSchema(schema) : trx.schema;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
transaction() {
|
||||||
|
return this.connection.transaction();
|
||||||
|
}
|
||||||
|
|
||||||
queryBuilder(uid) {
|
queryBuilder(uid) {
|
||||||
return this.entityManager.createQueryBuilder(uid);
|
return this.entityManager.createQueryBuilder(uid);
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,8 @@ const createQueryBuilder = (uid, db) => {
|
|||||||
populate: null,
|
populate: null,
|
||||||
limit: null,
|
limit: null,
|
||||||
offset: null,
|
offset: null,
|
||||||
|
transaction: null,
|
||||||
|
forUpdate: false,
|
||||||
orderBy: [],
|
orderBy: [],
|
||||||
groupBy: [],
|
groupBy: [],
|
||||||
};
|
};
|
||||||
@ -115,6 +117,16 @@ const createQueryBuilder = (uid, db) => {
|
|||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
transacting(transaction) {
|
||||||
|
state.transaction = transaction;
|
||||||
|
return this;
|
||||||
|
},
|
||||||
|
|
||||||
|
forUpdate() {
|
||||||
|
state.forUpdate = true;
|
||||||
|
return this;
|
||||||
|
},
|
||||||
|
|
||||||
init(params = {}) {
|
init(params = {}) {
|
||||||
const { _q, filters, where, select, limit, offset, orderBy, groupBy, populate } = params;
|
const { _q, filters, where, select, limit, offset, orderBy, groupBy, populate } = params;
|
||||||
|
|
||||||
@ -308,6 +320,14 @@ const createQueryBuilder = (uid, db) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (state.transaction) {
|
||||||
|
qb.transacting(state.transaction);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state.forUpdate) {
|
||||||
|
qb.forUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
if (state.limit) {
|
if (state.limit) {
|
||||||
qb.limit(state.limit);
|
qb.limit(state.limit);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const { joinBy } = require('@strapi/utils');
|
||||||
const { getService } = require('../utils');
|
const { getService } = require('../utils');
|
||||||
const { ACTIONS, FOLDER_MODEL_UID, FILE_MODEL_UID } = require('../constants');
|
const { ACTIONS, FOLDER_MODEL_UID, FILE_MODEL_UID } = require('../constants');
|
||||||
const {
|
const {
|
||||||
@ -43,7 +44,7 @@ module.exports = {
|
|||||||
async moveMany(ctx) {
|
async moveMany(ctx) {
|
||||||
const { body } = ctx.request;
|
const { body } = ctx.request;
|
||||||
const {
|
const {
|
||||||
state: { userAbility, user },
|
state: { userAbility },
|
||||||
} = ctx;
|
} = ctx;
|
||||||
|
|
||||||
const pmFolder = strapi.admin.services.permission.createPermissionsManager({
|
const pmFolder = strapi.admin.services.permission.createPermissionsManager({
|
||||||
@ -60,26 +61,137 @@ module.exports = {
|
|||||||
await validateMoveManyFoldersFiles(body);
|
await validateMoveManyFoldersFiles(body);
|
||||||
const { folderIds = [], fileIds = [], destinationFolderId } = body;
|
const { folderIds = [], fileIds = [], destinationFolderId } = body;
|
||||||
|
|
||||||
const uploadService = getService('upload');
|
const trx = await strapi.db.transaction();
|
||||||
const folderService = getService('folder');
|
try {
|
||||||
|
// fetch folders
|
||||||
|
const existingFolders = await strapi.db
|
||||||
|
.queryBuilder(FOLDER_MODEL_UID)
|
||||||
|
.select(['id', 'uid', 'path'])
|
||||||
|
.where({ id: { $in: folderIds } })
|
||||||
|
.transacting(trx)
|
||||||
|
.forUpdate()
|
||||||
|
.execute();
|
||||||
|
|
||||||
const updatedFolders = [];
|
// fetch files
|
||||||
// updates are done in order (not in parallele) to avoid mixing queries (path)
|
const existingFiles = await strapi.db
|
||||||
for (let folderId of folderIds) {
|
.queryBuilder(FILE_MODEL_UID)
|
||||||
const updatedFolder = await folderService.update(
|
.select(['id'])
|
||||||
folderId,
|
.where({ id: { $in: fileIds } })
|
||||||
{ parent: destinationFolderId },
|
.transacting(trx)
|
||||||
{ user }
|
.forUpdate()
|
||||||
);
|
.execute();
|
||||||
updatedFolders.push(updatedFolder);
|
|
||||||
|
// fetch destinationFolder path
|
||||||
|
let destinationFolderPath = '/';
|
||||||
|
if (destinationFolderId !== null) {
|
||||||
|
const destinationFolder = await strapi.db
|
||||||
|
.queryBuilder(FOLDER_MODEL_UID)
|
||||||
|
.select('path')
|
||||||
|
.where({ id: destinationFolderId })
|
||||||
|
.transacting(trx)
|
||||||
|
.first()
|
||||||
|
.execute();
|
||||||
|
destinationFolderPath = destinationFolder.path;
|
||||||
}
|
}
|
||||||
|
|
||||||
const updatedFiles = await Promise.all(
|
const fileTable = strapi.getModel(FILE_MODEL_UID).collectionName;
|
||||||
fileIds.map(fileId =>
|
const folderTable = strapi.getModel(FOLDER_MODEL_UID).collectionName;
|
||||||
uploadService.updateFileInfo(fileId, { folder: destinationFolderId }, { user })
|
const folderPathColName = strapi.db.metadata.get(FILE_MODEL_UID).attributes.folderPath
|
||||||
|
.columnName;
|
||||||
|
const pathColName = strapi.db.metadata.get(FOLDER_MODEL_UID).attributes.path.columnName;
|
||||||
|
|
||||||
|
if (existingFolders.length > 0) {
|
||||||
|
// update folders' parent relation (delete + insert; upsert not possible)
|
||||||
|
const joinTable = strapi.db.metadata.get(FOLDER_MODEL_UID).attributes.parent.joinTable;
|
||||||
|
await strapi.db
|
||||||
|
.queryBuilder(joinTable.name)
|
||||||
|
.transacting(trx)
|
||||||
|
.delete()
|
||||||
|
.where({ [joinTable.joinColumn.name]: { $in: folderIds } })
|
||||||
|
.execute();
|
||||||
|
await strapi.db
|
||||||
|
.queryBuilder(joinTable.name)
|
||||||
|
.transacting(trx)
|
||||||
|
.insert(
|
||||||
|
existingFolders.map(folder => ({
|
||||||
|
[joinTable.inverseJoinColumn.name]: destinationFolderId,
|
||||||
|
[joinTable.joinColumn.name]: folder.id,
|
||||||
|
}))
|
||||||
)
|
)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
for (const existingFolder of existingFolders) {
|
||||||
|
// update path for folders themselves & folders below
|
||||||
|
await strapi.db
|
||||||
|
.connection(folderTable)
|
||||||
|
.transacting(trx)
|
||||||
|
.where(pathColName, 'like', `${existingFolder.path}%`)
|
||||||
|
.update(
|
||||||
|
pathColName,
|
||||||
|
strapi.db.connection.raw('REPLACE(??, ?, ?)', [
|
||||||
|
pathColName,
|
||||||
|
existingFolder.path,
|
||||||
|
joinBy('/', destinationFolderPath, existingFolder.uid),
|
||||||
|
])
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// update path of files below
|
||||||
|
await strapi.db
|
||||||
|
.connection(fileTable)
|
||||||
|
.transacting(trx)
|
||||||
|
.where(folderPathColName, 'like', `${existingFolder.path}%`)
|
||||||
|
.update(
|
||||||
|
folderPathColName,
|
||||||
|
strapi.db.connection.raw('REPLACE(??, ?, ?)', [
|
||||||
|
folderPathColName,
|
||||||
|
existingFolder.path,
|
||||||
|
joinBy('/', destinationFolderPath, existingFolder.uid),
|
||||||
|
])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (existingFiles.length > 0) {
|
||||||
|
// update files' folder relation (delete + insert; upsert not possible)
|
||||||
|
const fileJoinTable = strapi.db.metadata.get(FILE_MODEL_UID).attributes.folder.joinTable;
|
||||||
|
await strapi.db
|
||||||
|
.queryBuilder(fileJoinTable.name)
|
||||||
|
.transacting(trx)
|
||||||
|
.delete()
|
||||||
|
.where({ [fileJoinTable.joinColumn.name]: { $in: fileIds } })
|
||||||
|
.execute();
|
||||||
|
await strapi.db
|
||||||
|
.queryBuilder(fileJoinTable.name)
|
||||||
|
.transacting(trx)
|
||||||
|
.insert(
|
||||||
|
existingFiles.map(file => ({
|
||||||
|
[fileJoinTable.inverseJoinColumn.name]: destinationFolderId,
|
||||||
|
[fileJoinTable.joinColumn.name]: file.id,
|
||||||
|
}))
|
||||||
|
)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
// update files main fields (path + updatedBy)
|
||||||
|
await strapi.db
|
||||||
|
.connection(fileTable)
|
||||||
|
.transacting(trx)
|
||||||
|
.whereIn('id', fileIds)
|
||||||
|
.update(folderPathColName, destinationFolderPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
await trx.commit();
|
||||||
|
} catch (e) {
|
||||||
|
await trx.rollback();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
|
const updatedFolders = await strapi.entityService.findMany(FOLDER_MODEL_UID, {
|
||||||
|
filters: { id: { $in: folderIds } },
|
||||||
|
});
|
||||||
|
const updatedFiles = await strapi.entityService.findMany(FILE_MODEL_UID, {
|
||||||
|
filters: { id: { $in: fileIds } },
|
||||||
|
});
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
data: {
|
data: {
|
||||||
files: await pmFile.sanitizeOutput(updatedFiles),
|
files: await pmFile.sanitizeOutput(updatedFiles),
|
||||||
|
@ -74,53 +74,107 @@ const deleteByIds = async (ids = []) => {
|
|||||||
* @returns {Promise<boolean>}
|
* @returns {Promise<boolean>}
|
||||||
*/
|
*/
|
||||||
const update = async (id, { name, parent }, { user }) => {
|
const update = async (id, { name, parent }, { user }) => {
|
||||||
|
// only name is updated
|
||||||
|
if (isUndefined(parent)) {
|
||||||
const existingFolder = await strapi.entityService.findOne(FOLDER_MODEL_UID, id);
|
const existingFolder = await strapi.entityService.findOne(FOLDER_MODEL_UID, id);
|
||||||
|
|
||||||
if (!existingFolder) {
|
if (!existingFolder) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isUndefined(parent)) {
|
const newFolder = setCreatorFields({ user, isEdition: true })({ name, parent });
|
||||||
|
|
||||||
|
if (isUndefined(parent)) {
|
||||||
|
const folder = await strapi.entityService.update(FOLDER_MODEL_UID, id, { data: newFolder });
|
||||||
|
return folder;
|
||||||
|
}
|
||||||
|
// location is updated => using transaction
|
||||||
|
} else {
|
||||||
|
const trx = await strapi.db.transaction();
|
||||||
|
try {
|
||||||
|
// fetch existing folder
|
||||||
|
const existingFolder = await strapi.db
|
||||||
|
.queryBuilder(FOLDER_MODEL_UID)
|
||||||
|
.select(['uid', 'path'])
|
||||||
|
.where({ id })
|
||||||
|
.transacting(trx)
|
||||||
|
.forUpdate()
|
||||||
|
.first()
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
// update parent folder (delete + insert; upsert not possible)
|
||||||
|
const joinTable = strapi.db.metadata.get(FOLDER_MODEL_UID).attributes.parent.joinTable;
|
||||||
|
await strapi.db
|
||||||
|
.queryBuilder(joinTable.name)
|
||||||
|
.transacting(trx)
|
||||||
|
.delete()
|
||||||
|
.where({ [joinTable.joinColumn.name]: id })
|
||||||
|
.execute();
|
||||||
|
await strapi.db
|
||||||
|
.queryBuilder(joinTable.name)
|
||||||
|
.transacting(trx)
|
||||||
|
.insert({ [joinTable.inverseJoinColumn.name]: parent, [joinTable.joinColumn.name]: id })
|
||||||
|
.where({ [joinTable.joinColumn.name]: id })
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
// fetch destinationFolder path
|
||||||
|
let destinationFolderPath = '/';
|
||||||
|
if (parent !== null) {
|
||||||
|
const destinationFolder = await strapi.db
|
||||||
|
.queryBuilder(FOLDER_MODEL_UID)
|
||||||
|
.select('path')
|
||||||
|
.where({ id: parent })
|
||||||
|
.transacting(trx)
|
||||||
|
.first()
|
||||||
|
.execute();
|
||||||
|
destinationFolderPath = destinationFolder.path;
|
||||||
|
}
|
||||||
|
|
||||||
|
const folderTable = strapi.getModel(FOLDER_MODEL_UID).collectionName;
|
||||||
|
const fileTable = strapi.getModel(FILE_MODEL_UID).collectionName;
|
||||||
const folderPathColumnName = strapi.db.metadata.get(FILE_MODEL_UID).attributes.folderPath
|
const folderPathColumnName = strapi.db.metadata.get(FILE_MODEL_UID).attributes.folderPath
|
||||||
.columnName;
|
.columnName;
|
||||||
const pathColumnName = strapi.db.metadata.get(FOLDER_MODEL_UID).attributes.path.columnName;
|
const pathColumnName = strapi.db.metadata.get(FOLDER_MODEL_UID).attributes.path.columnName;
|
||||||
|
|
||||||
// Todo wrap into a transaction
|
// update folders below
|
||||||
const destinationFolder =
|
|
||||||
parent === null ? '/' : (await strapi.entityService.findOne(FOLDER_MODEL_UID, parent)).path;
|
|
||||||
|
|
||||||
const folderTable = strapi.getModel(FOLDER_MODEL_UID).collectionName;
|
|
||||||
const fileTable = strapi.getModel(FILE_MODEL_UID).collectionName;
|
|
||||||
|
|
||||||
await strapi.db
|
await strapi.db
|
||||||
.connection(folderTable)
|
.connection(folderTable)
|
||||||
|
.transacting(trx)
|
||||||
.where(pathColumnName, 'like', `${existingFolder.path}%`)
|
.where(pathColumnName, 'like', `${existingFolder.path}%`)
|
||||||
.update(
|
.update(
|
||||||
pathColumnName,
|
pathColumnName,
|
||||||
strapi.db.connection.raw('REPLACE(??, ?, ?)', [
|
strapi.db.connection.raw('REPLACE(??, ?, ?)', [
|
||||||
pathColumnName,
|
pathColumnName,
|
||||||
existingFolder.path,
|
existingFolder.path,
|
||||||
joinBy('/', destinationFolder, existingFolder.uid),
|
joinBy('/', destinationFolderPath, existingFolder.uid),
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// update files below
|
||||||
await strapi.db
|
await strapi.db
|
||||||
.connection(fileTable)
|
.connection(fileTable)
|
||||||
|
.transacting(trx)
|
||||||
.where(folderPathColumnName, 'like', `${existingFolder.path}%`)
|
.where(folderPathColumnName, 'like', `${existingFolder.path}%`)
|
||||||
.update(
|
.update(
|
||||||
folderPathColumnName,
|
folderPathColumnName,
|
||||||
strapi.db.connection.raw('REPLACE(??, ?, ?)', [
|
strapi.db.connection.raw('REPLACE(??, ?, ?)', [
|
||||||
folderPathColumnName,
|
folderPathColumnName,
|
||||||
existingFolder.path,
|
existingFolder.path,
|
||||||
joinBy('/', destinationFolder, existingFolder.uid),
|
joinBy('/', destinationFolderPath, existingFolder.uid),
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
|
|
||||||
|
await trx.commit();
|
||||||
|
} catch (e) {
|
||||||
|
await trx.rollback();
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
const newFolder = setCreatorFields({ user, isEdition: true })({ name, parent });
|
// update less critical information (name + updatedBy)
|
||||||
|
const newFolder = setCreatorFields({ user, isEdition: true })({ name });
|
||||||
const folder = await strapi.entityService.update(FOLDER_MODEL_UID, id, { data: newFolder });
|
const folder = await strapi.entityService.update(FOLDER_MODEL_UID, id, { data: newFolder });
|
||||||
|
|
||||||
return folder;
|
return folder;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -29,7 +29,7 @@ describe('Folder structure', () => {
|
|||||||
// delete all possibly existing folders
|
// delete all possibly existing folders
|
||||||
const res = await rq({
|
const res = await rq({
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
url: '/upload/folders?pagination[pageSize]=-1',
|
url: '/upload/folders',
|
||||||
});
|
});
|
||||||
|
|
||||||
await rq({
|
await rq({
|
||||||
|
Loading…
x
Reference in New Issue
Block a user