Browse Source

feat: upgrader

pull/7304/head
Pranav C 11 months ago
parent
commit
0568e359d1
  1. 144
      packages/nocodb/src/version-upgrader/ncXcdbCreatedAndUpdatedTimeUpgrader.ts

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

@ -4,10 +4,13 @@ import type { NcUpgraderCtx } from './NcUpgrader';
import type { MetaService } from '~/meta/meta.service'; import type { MetaService } from '~/meta/meta.service';
import { MetaTable } from '~/utils/globals'; import { MetaTable } from '~/utils/globals';
import { Column, Model } from '~/models'; import { Column, Model } from '~/models';
import { getUniqueColumnAliasName, getUniqueColumnName } from '~/helpers/getUniqueName' import {
import getColumnPropsFromUIDT from '~/helpers/getColumnPropsFromUIDT' getUniqueColumnAliasName,
import ProjectMgrv2 from '~/db/sql-mgr/v2/ProjectMgrv2' getUniqueColumnName,
import { Altered } from '~/services/columns.service' } from '~/helpers/getUniqueName';
import getColumnPropsFromUIDT from '~/helpers/getColumnPropsFromUIDT';
import ProjectMgrv2 from '~/db/sql-mgr/v2/ProjectMgrv2';
import { Altered } from '~/services/columns.service';
// 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
@ -60,78 +63,65 @@ async function upgradeModels({
} }
} }
if(!isCreatedTimeExists || !isLastModifiedTimeExists) { if (!isCreatedTimeExists || !isLastModifiedTimeExists) {
// create created_at and updated_at columns // create created_at and updated_at columns
/* if (!existingColumn) { const newColumns = [];
columnName =
colBody.uidt === UITypes.CreatedTime if (!isCreatedTimeExists) {
? 'created_at' newColumns.push({
: 'updated_at'; ...(await getColumnPropsFromUIDT(
// const sqlClient = await reuseOrSave('sqlClient', reuse, async () => {
// NcConnectionMgrv2.getSqlClient(source), uidt: UITypes.CreatedTime,
// ); column_name: getUniqueColumnName(columns, 'created_at'),
// const dbColumns = ( title: getUniqueColumnAliasName(columns, 'Created At'),
// await sqlClient.columnList({ },
// tn: table.table_name, source,
// schema: source.getConfig()?.schema, )),
// }) system: true,
// )?.data?.list; altered: Altered.NEW_COLUMN,
});
// todo: check type as well }
const dbColumn = columns.find((c) => c.column_name === columnName);
if (dbColumn) {
columnName = getUniqueColumnName(columns, columnName);
}
{
colBody = await getColumnPropsFromUIDT(colBody, source);
// remove default value for SQLite since it doesn't support default value as function when adding column
// only support default value as constant value
if (source.type === 'sqlite3') {
colBody.cdf = null;
}
// create column in db
const tableUpdateBody = {
...table,
tn: table.table_name,
originalColumns: table.columns.map((c) => ({
...c,
cn: c.column_name,
})),
columns: [
...table.columns.map((c) => ({ ...c, cn: c.column_name })),
{
...colBody,
cn: columnName,
altered: Altered.NEW_COLUMN,
},
],
};
const sqlMgr = await reuseOrSave('sqlMgr', reuse, async () =>
ProjectMgrv2.getSqlMgr({ id: source.base_id }),
);
await sqlMgr.sqlOpPlus(source, 'tableUpdate', tableUpdateBody);
}
const title = getUniqueColumnAliasName( if (!isLastModifiedTimeExists) {
table.columns, newColumns.push({
UITypes.CreatedTime ? 'CreatedAt' : 'UpdatedAt', ...(await getColumnPropsFromUIDT(
); {
uidt: UITypes.LastModifiedTime,
column_name: getUniqueColumnName(columns, 'updated_at'),
title: getUniqueColumnAliasName(columns, 'Updated At'),
cdf: null,
},
source,
)),
system: true,
altered: Altered.NEW_COLUMN,
});
}
// update column in db
const tableUpdateBody = {
...model,
tn: model.table_name,
originalColumns: model.columns.map((c) => ({
...c,
cn: c.column_name,
})),
columns: [
...columns.map((c) => ({ ...c, cn: c.column_name })),
...newColumns,
],
};
const sqlMgr = ProjectMgrv2.getSqlMgr({ id: source.base_id }, ncMeta);
await sqlMgr.sqlOpPlus(source, 'tableUpdate', tableUpdateBody);
for (const newColumn of newColumns) {
await Column.insert({ await Column.insert({
...colBody, ...newColumn,
title,
system: 1, system: 1,
fk_model_id: table.id, fk_model_id: model.id,
column_name: columnName,
}); });
}*/ }
} }
logger.log(`Upgraded model ${model.name} from source ${source.name}`); logger.log(`Upgraded model ${model.name} from source ${source.name}`);
@ -143,10 +133,20 @@ async function upgradeModels({
export default async function ({ ncMeta }: NcUpgraderCtx) { export default async function ({ ncMeta }: NcUpgraderCtx) {
// get all xcdb sources // get all xcdb sources
const sources = await ncMeta.metaList2(null, null, MetaTable.BASES, { const sources = await ncMeta.metaList2(null, null, MetaTable.BASES, {
condition: { xcCondition: {
is_meta: 1, _or: [
{
is_meta: {
eq: 1,
},
},
{
is_local: {
eq: 1,
},
},
],
}, },
orderBy: {},
}); });
// iterate and upgrade each base // iterate and upgrade each base

Loading…
Cancel
Save