Browse Source

fix(nocodb): update_at issue for pg and mssql during upgrade

pull/5642/head
Wing-Kam Wong 2 years ago
parent
commit
dc9d19ee31
  1. 48
      packages/nocodb/src/version-upgrader/ncDateTimeUpgrader.ts

48
packages/nocodb/src/version-upgrader/ncDateTimeUpgrader.ts

@ -9,6 +9,7 @@ import type { BaseType } from 'nocodb-sdk';
import type { NcUpgraderCtx } from './NcUpgrader'; import type { NcUpgraderCtx } from './NcUpgrader';
import type { XKnex } from '../db/CustomKnex'; import type { XKnex } from '../db/CustomKnex';
import type { Knex } from 'knex'; import type { Knex } from 'knex';
import NcConnectionMgrv2 from '../utils/common/NcConnectionMgrv2';
dayjs.extend(utc); dayjs.extend(utc);
@ -28,6 +29,22 @@ function getTnPath(knex: XKnex, tb: Model) {
} }
} }
function getTriggerPath(knex: XKnex, trigger: any) {
const schema = (knex as any).searchPath?.();
const clientType = knex.clientType();
if (clientType === 'mssql' && schema) {
return knex.raw('??.??', [schema, trigger.trigger_name]).toQuery();
} else if (clientType === 'snowflake') {
return [
knex.client.config.connection.database,
knex.client.config.connection.schema,
trigger.trigger_name,
].join('.');
} else {
return trigger.trigger_name;
}
}
// This upgrader is to update all datetime fields in xcdb base due to the datetime changes // This upgrader is to update all datetime fields in xcdb base due to the datetime changes
// ref: https://github.com/nocodb/nocodb/pull/5505 // ref: https://github.com/nocodb/nocodb/pull/5505
// Originally, for XCDB-based projects, we store the local time in DB and display local time in UI // Originally, for XCDB-based projects, we store the local time in DB and display local time in UI
@ -70,6 +87,9 @@ export default async function ({ ncMeta }: NcUpgraderCtx) {
}; };
for (const model of models) { for (const model of models) {
let disableTriggers = [];
let enableTriggers = [];
try { try {
// if the table is missing in database, skip // if the table is missing in database, skip
if (!(await knex.schema.hasTable(getTnPath(knex, model)))) { if (!(await knex.schema.hasTable(getTnPath(knex, model)))) {
@ -115,8 +135,29 @@ export default async function ({ ncMeta }: NcUpgraderCtx) {
// disable triggers for pg / mssql // disable triggers for pg / mssql
if (knex.clientType() === 'pg' || knex.clientType() === 'mssql') { if (knex.clientType() === 'pg' || knex.clientType() === 'mssql') {
// TODO: get triggerList from client // get sql clients
// TODO: disable trigger const sqlClient = await NcConnectionMgrv2.getSqlClient(base);
const triggerList = (
await sqlClient.triggerList({
tn: model.table_name,
})
).data.list;
for (const trigger of triggerList) {
disableTriggers.push(
knex.raw('ALTER TABLE ?? DISABLE TRIGGER ??', [
getTnPath(knex, model),
getTriggerPath(knex, trigger),
]),
);
enableTriggers.push(
knex.raw('ALTER TABLE ?? ENABLE TRIGGER ??', [
getTnPath(knex, model),
getTriggerPath(knex, trigger),
]),
);
}
await Promise.all(disableTriggers);
} }
const records = await knex(getTnPath(knex, model)).select(); const records = await knex(getTnPath(knex, model)).select();
@ -172,8 +213,7 @@ export default async function ({ ncMeta }: NcUpgraderCtx) {
} finally { } finally {
// enable triggers for pg / mssql back // enable triggers for pg / mssql back
if (knex.clientType() === 'pg' || knex.clientType() === 'mssql') { if (knex.clientType() === 'pg' || knex.clientType() === 'mssql') {
// TODO: get triggerList from client await Promise.all(enableTriggers);
// TODO: enable trigger
} }
} }
} }

Loading…
Cancel
Save