Browse Source

feat: create column with different name if already column exist

pull/7304/head
Pranav C 12 months ago
parent
commit
3e4f59d11f
  1. 31
      packages/nocodb/src/db/BaseModelSqlv2.ts
  2. 55
      packages/nocodb/src/services/columns.service.ts

31
packages/nocodb/src/db/BaseModelSqlv2.ts

@ -101,12 +101,23 @@ function checkColumnRequired(
return !fields || fields.includes(column.title);
}
export function getColumnName(column: Column<any>) {
export function getColumnName(column: Column<any>, columns: Column[] = []) {
switch (column.uidt) {
case UITypes.CreateTime:
return 'created_at';
case UITypes.LastModifiedTime:
return 'updated_at';
case UITypes.CreateTime: {
const createdTimeSystemCol = columns.find(
(col) => col.system && col.uidt === UITypes.CreateTime,
);
if (createdTimeSystemCol) return createdTimeSystemCol.column_name;
return column.column_name || 'created_at';
}
case UITypes.LastModifiedTime: {
const lastModifiedTimeSystemCol = columns.find(
(col) => col.system && col.uidt === UITypes.LastModifiedTime,
);
if (lastModifiedTimeSystemCol)
return lastModifiedTimeSystemCol.column_name;
return column.column_name || 'created_at';
}
default:
return column.column_name;
}
@ -2128,7 +2139,10 @@ class BaseModelSqlv2 {
case UITypes.LastModifiedTime:
case UITypes.DateTime:
{
const columnName = getColumnName(column);
const columnName = getColumnName(
column,
await this.model.getColumns(),
);
if (this.isMySQL) {
// MySQL stores timestamp in UTC but display in timezone
// To verify the timezone, run `SELECT @@global.time_zone, @@session.time_zone;`
@ -5601,10 +5615,11 @@ class BaseModelSqlv2 {
knex?: XKnex;
}) {
const columnName = await model.getColumns().then((columns) => {
return columns.find((c) => c.uidt === UITypes.LastModifiedTime)?.column_name;
return columns.find((c) => c.uidt === UITypes.LastModifiedTime)
?.column_name;
});
if(!columnName) return;
if (!columnName) return;
const qb = knex(model.table_name).update({
[columnName]: Noco.ncMeta.now(),

55
packages/nocodb/src/services/columns.service.ts

@ -1688,34 +1688,36 @@ export class ColumnsService {
case UITypes.CreateTime:
case UITypes.LastModifiedTime:
{
const columns = await table.getColumns();
// check if column already exists, then just create a new column in meta
// else create a new column in meta and db
const existingColumn = await table.getColumns().then((col) => {
return col.find(
(c) => c.uidt === colBody.uidt && c.system,
// ||
// c.column_name ===
// (c.uidt === UITypes.CreateTime ? 'created_at' : 'updated_at'),
);
});
const existingColumn = columns.find(
(c) => c.uidt === colBody.uidt && c.system,
// ||
// c.column_name ===
// (c.uidt === UITypes.CreateTime ? 'created_at' : 'updated_at'),
);
if (!existingColumn) {
const sqlClient = await reuseOrSave('sqlClient', reuse, async () =>
NcConnectionMgrv2.getSqlClient(source),
);
const dbColumns = (
await sqlClient.columnList({
tn: table.table_name,
schema: source.getConfig()?.schema,
})
)?.data?.list;
let columnName =
colBody.uidt === UITypes.CreateTime ? 'created_at' : 'updated_at';
// const sqlClient = await reuseOrSave('sqlClient', reuse, async () =>
// NcConnectionMgrv2.getSqlClient(source),
// );
// const dbColumns = (
// await sqlClient.columnList({
// tn: table.table_name,
// schema: source.getConfig()?.schema,
// })
// )?.data?.list;
// todo: check type as well
const dbColumn = dbColumns.find(
(c) =>
c.column_name ===
(c.uidt === UITypes.CreateTime ? 'created_at' : 'updated_at'),
);
const dbColumn = columns.find((c) => c.column_name === columnName);
if (dbColumn) {
columnName = getUniqueColumnName(columns, columnName);
}
if (!dbColumn) {
// create column in db
@ -1752,11 +1754,11 @@ export class ColumnsService {
system: 1,
fk_model_id: table.id,
});
await Column.insert({
...colBody,
fk_model_id: table.id,
});
}
await Column.insert({
...colBody,
fk_model_id: table.id,
});
}
break;
default:
@ -2775,6 +2777,7 @@ export class ColumnsService {
await Column.update(column.id, colBody);
}
}
async columnsHash(tableId: string) {
const table = await Model.getWithInfo({
id: tableId,

Loading…
Cancel
Save