Browse Source

feat: create column with different name if already column exist

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

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

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

Loading…
Cancel
Save