Browse Source

refactor: move data migration to a migration file and do it with batchInsert

pull/8590/head
Pranav C 6 months ago
parent
commit
46a6616442
  1. 76
      packages/nocodb/src/meta/migrations/v2/nc_046_comment_mentions.ts
  2. 75
      packages/nocodb/src/meta/migrations/v2/nc_047_comment_migration.ts

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

@ -2,23 +2,19 @@ import { Logger } from '@nestjs/common';
import type { Knex } from 'knex'; import type { Knex } from 'knex';
import { MetaTable } from '~/utils/globals'; import { MetaTable } from '~/utils/globals';
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();
table.string('row_id', 255); table.string('row_id', 255);
table.string('comment', 3000); table.text('comment');
table.string('created_by', 255); table.string('created_by', 20);
table.string('created_by_email', 255); table.string('created_by_email', 255);
table.string('resolved_by', 255); table.string('resolved_by', 20);
table.string('resolved_by_email', 255); table.string('resolved_by_email', 255);
@ -82,72 +78,6 @@ const up = async (knex: Knex) => {
table.timestamps(true, true); table.timestamps(true, true);
}); });
logger.log('nc_046_comment_mentions: Tables Created');
try {
logger.log('nc_046_comment_mentions: Fetching Data from Audit Table');
const rows = await knex
.select(
`${MetaTable.AUDIT}.id`,
`${MetaTable.AUDIT}.row_id`,
`${MetaTable.AUDIT}.description`,
`${MetaTable.AUDIT}.user as user_email`,
`${MetaTable.AUDIT}.source_id`,
`${MetaTable.AUDIT}.base_id`,
`${MetaTable.AUDIT}.fk_model_id`,
`${MetaTable.AUDIT}.created_at`,
`${MetaTable.AUDIT}.updated_at`,
`${MetaTable.USERS}.id as user_id`,
)
.from(MetaTable.AUDIT)
.where(`${MetaTable.AUDIT}.op_type`, 'COMMENT')
.leftJoin(
MetaTable.USERS,
`${MetaTable.AUDIT}.user`,
`${MetaTable.USERS}.email`,
);
logger.log('nc_046_comment_mentions: Data from Audit Table fetched');
if (!rows.length) {
logger.log(
'nc_046_comment_mentions: No Data Found to Migrate from Audit Table',
);
return;
}
const formattedRows = rows.map((row) => ({
id: row.id,
row_id: row.row_id,
comment: (row.description ?? '')
.substring((row.description ?? '').indexOf(':') + 1)
.trim(),
created_by: row.user_id,
created_by_email: row.user_email,
source_id: row.source_id,
base_id: row.base_id,
fk_model_id: row.fk_model_id,
created_at: row.created_at,
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) => {

75
packages/nocodb/src/meta/migrations/v2/nc_047_comment_migration.ts

@ -0,0 +1,75 @@
import { Logger } from '@nestjs/common';
import type { Knex } from 'knex';
import { MetaTable } from '~/utils/globals';
const logger = new Logger('nc_046_comment_mentions');
const BATCH_SIZE = 5000;
const up = async (knex: Knex) => {
try {
logger.log('nc_047_comment_migration: Migration Started');
const rows = await knex
.select(
`${MetaTable.AUDIT}.id`,
`${MetaTable.AUDIT}.row_id`,
`${MetaTable.AUDIT}.description`,
`${MetaTable.AUDIT}.user as user_email`,
`${MetaTable.AUDIT}.source_id`,
`${MetaTable.AUDIT}.base_id`,
`${MetaTable.AUDIT}.fk_model_id`,
`${MetaTable.AUDIT}.created_at`,
`${MetaTable.AUDIT}.updated_at`,
`${MetaTable.USERS}.id as user_id`,
)
.from(MetaTable.AUDIT)
.where(`${MetaTable.AUDIT}.op_type`, 'COMMENT')
.leftJoin(
MetaTable.USERS,
`${MetaTable.AUDIT}.user`,
`${MetaTable.USERS}.email`,
);
logger.log('nc_046_comment_mentions: Data from Audit Table fetched');
if (!rows.length) {
logger.log(
'nc_046_comment_mentions: No Data Found to Migrate from Audit Table',
);
return;
}
const formattedRows = rows.map((row) => ({
id: row.id,
row_id: row.row_id,
comment: (row.description ?? '')
.substring((row.description ?? '').indexOf(':') + 1)
.trim(),
created_by: row.user_id,
created_by_email: row.user_email,
source_id: row.source_id,
base_id: row.base_id,
fk_model_id: row.fk_model_id,
created_at: row.created_at,
updated_at: row.updated_at,
}));
logger.log('nc_046_comment_mentions: Data from Audit Table formatted');
return knex.batchInsert(MetaTable.COMMENTS, formattedRows, BATCH_SIZE);
logger.log(
'nc_047_comment_migration: 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) => {
await knex.schema.dropTable(MetaTable.COMMENTS);
};
export { up, down };
Loading…
Cancel
Save