Browse Source

fix: check column already exist in database

pull/7369/head
Pranav C 10 months ago
parent
commit
f60bf18f29
  1. 155
      packages/nocodb/src/version-upgrader/ncXcdbCreatedAndUpdatedTimeUpgrader.ts

155
packages/nocodb/src/version-upgrader/ncXcdbCreatedAndUpdatedTimeUpgrader.ts

@ -11,6 +11,8 @@ import {
import getColumnPropsFromUIDT from '~/helpers/getColumnPropsFromUIDT'; import getColumnPropsFromUIDT from '~/helpers/getColumnPropsFromUIDT';
import ProjectMgrv2 from '~/db/sql-mgr/v2/ProjectMgrv2'; import ProjectMgrv2 from '~/db/sql-mgr/v2/ProjectMgrv2';
import { Altered } from '~/services/columns.service'; import { Altered } from '~/services/columns.service';
import NcConnectionMgrv2 from '~/utils/common/NcConnectionMgrv2';
import getColumnUiType from '~/helpers/getColumnUiType';
// An upgrader for upgrading created_at and updated_at columns // An upgrader for upgrading created_at and updated_at columns
// to system column and convert to new uidt CreatedTime and LastModifiedTime // to system column and convert to new uidt CreatedTime and LastModifiedTime
@ -58,6 +60,8 @@ async function upgradeModels({
models.map(async (model: any) => { models.map(async (model: any) => {
if (model.mm) return; if (model.mm) return;
logger.log(`Upgrading model ${model.title} from source ${source.alias}`);
const columns = await model.getColumns(ncMeta); const columns = await model.getColumns(ncMeta);
const oldColumns = columns.map((c) => ({ ...c, cn: c.column_name })); const oldColumns = columns.map((c) => ({ ...c, cn: c.column_name }));
let isCreatedTimeExists = false; let isCreatedTimeExists = false;
@ -99,56 +103,108 @@ async function upgradeModels({
} }
if (!isCreatedTimeExists || !isLastModifiedTimeExists) { if (!isCreatedTimeExists || !isLastModifiedTimeExists) {
// create created_at and updated_at columns // get existing columns from database
const sqlClient = await NcConnectionMgrv2.getSqlClient(
source,
ncMeta.knex,
);
const dbColumns = (
await sqlClient.columnList({
tn: model.table_name,
schema: source.getConfig()?.schema,
})
)?.data?.list;
//
//
// // create created_at and updated_at columns
const newColumns = []; const newColumns = [];
const existingDbColumns = [];
//,
if (!isCreatedTimeExists) { if (!isCreatedTimeExists) {
newColumns.push({ //
...(await getColumnPropsFromUIDT( // // check column exist and add to meta if found
{ const columnName = getUniqueColumnName(columns, 'created_at');
uidt: UITypes.CreatedTime, //
column_name: getUniqueColumnName(columns, 'created_at'), const dbColumn = dbColumns.find((c) => c.cn === columnName);
title: getUniqueColumnAliasName(columns, 'CreatedAt'),
}, if (
source, dbColumn &&
)), getColumnUiType(source, dbColumn) === UITypes.DateTime
cdf: null, ) {
system: true, existingDbColumns.push({
altered: Altered.NEW_COLUMN, ...dbColumn,
}); uidt: UITypes.CreatedTime,
column_name: getUniqueColumnName(columns, 'created_at'),
title: getUniqueColumnAliasName(columns, 'CreatedAt'),
system: true,
});
} else {
newColumns.push({
...(await getColumnPropsFromUIDT(
{
uidt: UITypes.CreatedTime,
column_name: getUniqueColumnName(columns, 'created_at'),
title: getUniqueColumnAliasName(columns, 'CreatedAt'),
},
source,
)),
cdf: null,
system: true,
altered: Altered.NEW_COLUMN,
});
}
} }
if (!isLastModifiedTimeExists) { if (!isLastModifiedTimeExists) {
newColumns.push({ const columnName = getUniqueColumnName(columns, 'created_at');
...(await getColumnPropsFromUIDT( //
{ const dbColumn = dbColumns.find((c) => c.cn === columnName);
uidt: UITypes.LastModifiedTime,
column_name: getUniqueColumnName(columns, 'updated_at'), if (
title: getUniqueColumnAliasName(columns, 'UpdatedAt'), dbColumn &&
}, getColumnUiType(source, dbColumn) === UITypes.DateTime
source, ) {
)), existingDbColumns.push({
cdf: null, uidt: UITypes.LastModifiedTime,
system: true, ...dbColumn,
altered: Altered.NEW_COLUMN, column_name: getUniqueColumnName(columns, 'updated_at'),
}); title: getUniqueColumnAliasName(columns, 'UpdatedAt'),
system: true,
});
} else {
newColumns.push({
...(await getColumnPropsFromUIDT(
{
uidt: UITypes.LastModifiedTime,
column_name: getUniqueColumnName(columns, 'updated_at'),
title: getUniqueColumnAliasName(columns, 'UpdatedAt'),
},
source,
)),
cdf: null,
system: true,
altered: Altered.NEW_COLUMN,
});
}
} }
// update column in db if (newColumns.length) {
const tableUpdateBody = { // update column in db
...model, const tableUpdateBody = {
tn: model.table_name, ...model,
originalColumns: oldColumns, tn: model.table_name,
columns: [...columns, ...newColumns].map((c) => ({ originalColumns: oldColumns,
...c, columns: [...columns, ...newColumns].map((c) => ({
cn: c.column_name, ...c,
})), cn: c.column_name,
}; })),
const sqlMgr = ProjectMgrv2.getSqlMgr({ id: source.base_id }, ncMeta); };
await sqlMgr.sqlOpPlus(source, 'tableUpdate', tableUpdateBody); const sqlMgr = ProjectMgrv2.getSqlMgr({ id: source.base_id }, ncMeta);
await sqlMgr.sqlOpPlus(source, 'tableUpdate', tableUpdateBody);
for (const newColumn of newColumns) { }
for (const newColumn of [...existingDbColumns, ...newColumns]) {
await Column.insert( await Column.insert(
{ {
...newColumn, ...newColumn,
@ -160,7 +216,7 @@ async function upgradeModels({
} }
} }
logger.log(`Upgraded model ${model.name} from source ${source.name}`); logger.log(`Upgraded model ${model.title} from source ${source.alias}`);
}), }),
); );
} }
@ -186,9 +242,14 @@ export default async function ({ ncMeta }: NcUpgraderCtx) {
}); });
// iterate and upgrade each base // iterate and upgrade each base
for (const source of sources) { for (const _source of sources) {
logger.log(`Upgrading source ${source.name}`); const source = new Source(_source);
// skip deleted project bases
if (await source.getProject(ncMeta).then((p) => !p || p.deleted)) continue;
logger.log(`Upgrading source ${source.alias}`);
// update the meta props // update the meta props
await upgradeModels({ ncMeta, source: new Source(source) }); await upgradeModels({ ncMeta, source });
} }
} }

Loading…
Cancel
Save