1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-08-05 13:35:27 +02:00

fix: Improve upgrade script, log statuses

Closes #1280
This commit is contained in:
Maksim Eltyshev 2025-08-04 20:51:40 +02:00
parent 0049ba2145
commit 48e261ffc4

View file

@ -56,6 +56,18 @@ const loadSails = () =>
); );
}); });
const runStep = async (label, func) => {
process.stdout.write(`${label}... `);
try {
await func();
console.log('[OK]');
} catch (error) {
console.log('[FAIL]');
throw error;
}
};
const knex = initKnex(knexfile); const knex = initKnex(knexfile);
const upgradeDatabase = async () => { const upgradeDatabase = async () => {
@ -136,13 +148,15 @@ const upgradeDatabase = async () => {
const identityProviderUsers = await trx('identity_provider_user') const identityProviderUsers = await trx('identity_provider_user')
.withSchema('v1') .withSchema('v1')
.whereIn('user_id', whereInUserIds); .whereRaw('user_id = ANY (?)', [whereInUserIds]);
if (identityProviderUsers.length > 0) { if (identityProviderUsers.length > 0) {
await knex.batchInsert('identity_provider_user', identityProviderUsers).transacting(trx); await knex.batchInsert('identity_provider_user', identityProviderUsers).transacting(trx);
} }
const sessions = await trx('session').withSchema('v1').whereIn('user_id', whereInUserIds); const sessions = await trx('session')
.withSchema('v1')
.whereRaw('user_id = ANY (?)', [whereInUserIds]);
if (sessions.length > 0) { if (sessions.length > 0) {
await knex.batchInsert('session', sessions).transacting(trx); await knex.batchInsert('session', sessions).transacting(trx);
@ -210,13 +224,15 @@ const upgradeDatabase = async () => {
const projectManagers = await trx('project_manager') const projectManagers = await trx('project_manager')
.withSchema('v1') .withSchema('v1')
.whereIn('project_id', whereInProjectIds); .whereRaw('project_id = ANY (?)', [whereInProjectIds]);
if (projectManagers.length > 0) { if (projectManagers.length > 0) {
await knex.batchInsert('project_manager', projectManagers).transacting(trx); await knex.batchInsert('project_manager', projectManagers).transacting(trx);
} }
const boards = await trx('board').withSchema('v1').whereIn('project_id', whereInProjectIds); const boards = await trx('board')
.withSchema('v1')
.whereRaw('project_id = ANY (?)', [whereInProjectIds]);
const projectIdByBoardId = boards.reduce( const projectIdByBoardId = boards.reduce(
(result, board) => ({ (result, board) => ({
@ -260,7 +276,7 @@ const upgradeDatabase = async () => {
const boardMemberships = await trx('board_membership') const boardMemberships = await trx('board_membership')
.withSchema('v1') .withSchema('v1')
.whereIn('board_id', whereInBoardIds); .whereRaw('board_id = ANY (?)', [whereInBoardIds]);
if (boardMemberships.length > 0) { if (boardMemberships.length > 0) {
await knex await knex
@ -282,13 +298,18 @@ const upgradeDatabase = async () => {
.transacting(trx); .transacting(trx);
} }
const labels = await trx('label').withSchema('v1').whereIn('board_id', whereInBoardIds); const labels = await trx('label')
.withSchema('v1')
.whereRaw('board_id = ANY (?)', [whereInBoardIds]);
if (labels.length > 0) { if (labels.length > 0) {
await knex.batchInsert('label', labels).transacting(trx); await knex.batchInsert('label', labels).transacting(trx);
} }
const lists = await trx('list').withSchema('v1').whereIn('board_id', whereInBoardIds); const lists = await trx('list')
.withSchema('v1')
.whereRaw('board_id = ANY (?)', [whereInBoardIds]);
const whereInListIds = ['0', ...lists.map(({ id }) => id)]; const whereInListIds = ['0', ...lists.map(({ id }) => id)];
if (lists.length > 0) { if (lists.length > 0) {
@ -313,8 +334,8 @@ const upgradeDatabase = async () => {
const cards = await trx('card') const cards = await trx('card')
.withSchema('v1') .withSchema('v1')
.whereIn('board_id', whereInBoardIds) .whereRaw('board_id = ANY (?)', [whereInBoardIds])
.whereIn('list_id', whereInListIds); .whereRaw('list_id = ANY (?)', [whereInListIds]);
const cardById = _.keyBy(cards, 'id'); const cardById = _.keyBy(cards, 'id');
const whereInCardIds = ['0', ...Object.keys(cardById)]; const whereInCardIds = ['0', ...Object.keys(cardById)];
@ -347,7 +368,7 @@ const upgradeDatabase = async () => {
const cardSubscriptions = await trx('card_subscription') const cardSubscriptions = await trx('card_subscription')
.withSchema('v1') .withSchema('v1')
.whereIn('card_id', whereInCardIds); .whereRaw('card_id = ANY (?)', [whereInCardIds]);
if (cardSubscriptions.length > 0) { if (cardSubscriptions.length > 0) {
await knex.batchInsert('card_subscription', cardSubscriptions).transacting(trx); await knex.batchInsert('card_subscription', cardSubscriptions).transacting(trx);
@ -355,19 +376,23 @@ const upgradeDatabase = async () => {
const cardMemberships = await trx('card_membership') const cardMemberships = await trx('card_membership')
.withSchema('v1') .withSchema('v1')
.whereIn('card_id', whereInCardIds); .whereRaw('card_id = ANY (?)', [whereInCardIds]);
if (cardMemberships.length > 0) { if (cardMemberships.length > 0) {
await knex.batchInsert('card_membership', cardMemberships).transacting(trx); await knex.batchInsert('card_membership', cardMemberships).transacting(trx);
} }
const cardLabels = await trx('card_label').withSchema('v1').whereIn('card_id', whereInCardIds); const cardLabels = await trx('card_label')
.withSchema('v1')
.whereRaw('card_id = ANY (?)', [whereInCardIds]);
if (cardLabels.length > 0) { if (cardLabels.length > 0) {
await knex.batchInsert('card_label', cardLabels).transacting(trx); await knex.batchInsert('card_label', cardLabels).transacting(trx);
} }
const tasks = await trx('task').withSchema('v1').whereIn('card_id', whereInCardIds); const tasks = await trx('task')
.withSchema('v1')
.whereRaw('card_id = ANY (?)', [whereInCardIds]);
const tasksByCardId = _.groupBy(tasks, 'card_id'); const tasksByCardId = _.groupBy(tasks, 'card_id');
const taskCardIds = Object.keys(tasksByCardId); const taskCardIds = Object.keys(tasksByCardId);
@ -409,7 +434,9 @@ const upgradeDatabase = async () => {
.transacting(trx); .transacting(trx);
} }
const attachments = await trx('attachment').withSchema('v1').whereIn('card_id', whereInCardIds); const attachments = await trx('attachment')
.withSchema('v1')
.whereRaw('card_id = ANY (?)', [whereInCardIds]);
if (attachments.length > 0) { if (attachments.length > 0) {
await knex await knex
@ -434,7 +461,9 @@ const upgradeDatabase = async () => {
.transacting(trx); .transacting(trx);
} }
const actions = await trx('action').withSchema('v1').whereIn('card_id', whereInCardIds); const actions = await trx('action')
.withSchema('v1')
.whereRaw('card_id = ANY (?)', [whereInCardIds]);
const actionById = _.keyBy(actions, 'id'); const actionById = _.keyBy(actions, 'id');
const whereInActionIds = ['0', ...Object.keys(actionById)]; const whereInActionIds = ['0', ...Object.keys(actionById)];
@ -507,9 +536,9 @@ const upgradeDatabase = async () => {
const notifications = await trx('notification') const notifications = await trx('notification')
.withSchema('v1') .withSchema('v1')
.whereIn('user_id', whereInUserIds) .whereRaw('user_id = ANY (?)', [whereInUserIds])
.whereIn('action_id', whereInActionIds) .whereRaw('action_id = ANY (?)', [whereInActionIds])
.whereIn('card_id', whereInCardIds); .whereRaw('card_id = ANY (?)', [whereInCardIds]);
if (notifications.length > 0) { if (notifications.length > 0) {
await knex await knex
@ -528,9 +557,7 @@ const upgradeDatabase = async () => {
'created_at', 'created_at',
'updated_at', 'updated_at',
]), ]),
creator_user_id: userIdsSet.has(notification.creator_user_id) creator_user_id: userIdsSet.has(action.user_id) ? action.user_id : null,
? notification.creator_user_id
: null,
board_id: card.board_id, board_id: card.board_id,
type: action.type, type: action.type,
}; };
@ -891,15 +918,12 @@ const upgradeFileAttachments = async () => {
await loadSails(); await loadSails();
if (isV1) { if (isV1) {
console.log('Upgrading database...'); await runStep('Upgrading database', upgradeDatabase);
await upgradeDatabase();
} }
console.log('Upgrading files...'); await runStep('Upgrading user avatars', upgradeUserAvatars);
await runStep('Upgrading background images', upgradeBackgroundImages);
await upgradeUserAvatars(); await runStep('Upgrading file attachments', upgradeFileAttachments);
await upgradeBackgroundImages();
await upgradeFileAttachments();
} catch (error) { } catch (error) {
console.error(error); console.error(error);
process.exitCode = 1; process.exitCode = 1;