1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 05:09:43 +02:00
planka/server/db/migrations/20220523131229_add_image_to_attachment_table.js

74 lines
1.8 KiB
JavaScript
Raw Normal View History

const path = require('path');
const sharp = require('sharp');
const _ = require('lodash');
const getConfig = require('../../get-config');
module.exports.up = async (knex) => {
await knex.schema.table('attachment', (table) => {
/* Columns */
table.jsonb('image');
});
const config = await getConfig();
const attachments = await knex('attachment');
// eslint-disable-next-line no-restricted-syntax
for (attachment of attachments) {
if (attachment.is_image) {
const image = sharp(
path.join(config.custom.attachmentsPath, attachment.dirname, attachment.filename),
);
let metadata;
try {
metadata = await image.metadata(); // eslint-disable-line no-await-in-loop
} catch (error) {
continue; // eslint-disable-line no-continue
}
if (!['svg', 'pdf'].includes(metadata.format)) {
// eslint-disable-next-line no-await-in-loop
await knex('attachment')
.update({
image: _.pick(metadata, ['width', 'height']),
})
.where('id', attachment.id);
}
}
}
return knex.schema.table('attachment', (table) => {
table.dropColumn('is_image');
});
};
module.exports.down = async (knex) => {
await knex.schema.table('attachment', (table) => {
/* Columns */
table.boolean('is_image');
});
const attachments = await knex('attachment');
// eslint-disable-next-line no-restricted-syntax
for (attachment of attachments) {
// eslint-disable-next-line no-await-in-loop
await knex('attachment')
.update({
is_image: !!attachment.image,
})
.where('id', attachment.id);
}
await knex.schema.table('attachment', (table) => {
table.dropColumn('image');
});
return knex.schema.alterTable('attachment', (table) => {
table.boolean('is_image').notNullable().alter();
});
};