Browse Source

fix: use batchInsert with transaction

pull/8590/head
DarkPhoenix2704 4 months ago
parent
commit
6890f7431b
  1. 113
      packages/nocodb/src/meta/migrations/v2/nc_046_comment_mentions.ts

113
packages/nocodb/src/meta/migrations/v2/nc_046_comment_mentions.ts

@ -4,6 +4,8 @@ import { MetaTable } from '~/utils/globals';
const logger = new Logger('nc_046_comment_mentions'); const logger = new Logger('nc_046_comment_mentions');
const BATCH_SIZE = 5000;
const up = async (knex: Knex) => { const up = async (knex: Knex) => {
await knex.schema.createTable(MetaTable.COMMENTS, (table) => { await knex.schema.createTable(MetaTable.COMMENTS, (table) => {
table.string('id', 20).primary(); table.string('id', 20).primary();
@ -42,7 +44,7 @@ const up = async (knex: Knex) => {
table.string('row_id', 255); table.string('row_id', 255);
table.string('user_id', 255); table.string('user_id', 20);
table.string('fk_model_id', 20); table.string('fk_model_id', 20);
@ -70,7 +72,7 @@ const up = async (knex: Knex) => {
table.string('source_id', 20); table.string('source_id', 20);
table.string('fk_modal_id', 20); table.string('fk_model_id', 20);
table.string('base_id', 128); table.string('base_id', 128);
@ -83,52 +85,69 @@ const up = async (knex: Knex) => {
logger.log('nc_046_comment_mentions: Tables Created'); logger.log('nc_046_comment_mentions: Tables Created');
knex try {
.select( logger.log('nc_046_comment_mentions: Fetching Data from Audit Table');
`${MetaTable.AUDIT}.id`,
`${MetaTable.AUDIT}.row_id`, const rows = await knex
`${MetaTable.AUDIT}.description`, .select(
`${MetaTable.AUDIT}.user as user_email`, `${MetaTable.AUDIT}.id`,
`${MetaTable.AUDIT}.source_id`, `${MetaTable.AUDIT}.row_id`,
`${MetaTable.AUDIT}.base_id`, `${MetaTable.AUDIT}.description`,
`${MetaTable.AUDIT}.fk_model_id`, `${MetaTable.AUDIT}.user as user_email`,
`${MetaTable.AUDIT}.created_at`, `${MetaTable.AUDIT}.source_id`,
`${MetaTable.AUDIT}.updated_at`, `${MetaTable.AUDIT}.base_id`,
`${MetaTable.USERS}.id as user_id`, `${MetaTable.AUDIT}.fk_model_id`,
) `${MetaTable.AUDIT}.created_at`,
.from(MetaTable.AUDIT) `${MetaTable.AUDIT}.updated_at`,
.where(`${MetaTable.AUDIT}.op_type`, 'COMMENT') `${MetaTable.USERS}.id as user_id`,
.leftJoin( )
MetaTable.USERS, .from(MetaTable.AUDIT)
`${MetaTable.AUDIT}.user`, .where(`${MetaTable.AUDIT}.op_type`, 'COMMENT')
`${MetaTable.USERS}.email`, .leftJoin(
) MetaTable.USERS,
.then(async (rows) => { `${MetaTable.AUDIT}.user`,
if (!rows.length) return; `${MetaTable.USERS}.email`,
);
logger.log('nc_046_comment_mentions: Data from Audit Table Selected');
logger.log('nc_046_comment_mentions: Data from Audit Table fetched');
const formattedRows = rows.map((row) => ({
id: row.id, if (!rows.length) {
row_id: row.row_id, logger.log(
comment: row.description 'nc_046_comment_mentions: No Data Found to Migrate from Audit Table',
.substring(row.description.indexOf(':') + 1) );
.trim(), return;
created_by: row.user_id, }
created_by_email: row.user_email, const formattedRows = rows.map((row) => ({
source_id: row.source_id, id: row.id,
base_id: row.base_id, row_id: row.row_id,
fk_model_id: row.fk_model_id, comment: (row.description ?? '')
created_at: row.created_at, .substring((row.description ?? '').indexOf(':') + 1)
updated_at: row.updated_at, .trim(),
})); created_by: row.user_id,
created_by_email: row.user_email,
logger.log('nc_046_comment_mentions: Data from Audit Table Formatted'); source_id: row.source_id,
base_id: row.base_id,
await knex(MetaTable.COMMENTS).insert(formattedRows); fk_model_id: row.fk_model_id,
created_at: row.created_at,
logger.log('nc_046_comment_mentions: Data from Audit Table Migrated'); updated_at: row.updated_at,
}));
logger.log('nc_046_comment_mentions: Data from Audit Table formatted');
await knex.transaction(function (trx) {
return knex
.batchInsert(MetaTable.COMMENTS, formattedRows, BATCH_SIZE)
.transacting(trx);
}); });
logger.log(
'nc_046_comment_mentions: Data migrated from Audit Table to Comments Table',
);
} catch (error) {
logger.error(
'nc_046_comment_mentions: Error while migrating data from Audit Table',
);
}
}; };
const down = async (knex: Knex) => { const down = async (knex: Knex) => {

Loading…
Cancel
Save