|
|
@ -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 |
|
|
|
|
|
|
|
? '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
|
|
|
|
if (!isCreatedTimeExists) { |
|
|
|
const dbColumn = columns.find((c) => c.column_name === columnName); |
|
|
|
newColumns.push({ |
|
|
|
|
|
|
|
...(await getColumnPropsFromUIDT( |
|
|
|
if (dbColumn) { |
|
|
|
{ |
|
|
|
columnName = getUniqueColumnName(columns, columnName); |
|
|
|
uidt: UITypes.CreatedTime, |
|
|
|
|
|
|
|
column_name: getUniqueColumnName(columns, 'created_at'), |
|
|
|
|
|
|
|
title: getUniqueColumnAliasName(columns, 'Created At'), |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
source, |
|
|
|
|
|
|
|
)), |
|
|
|
|
|
|
|
system: true, |
|
|
|
|
|
|
|
altered: Altered.NEW_COLUMN, |
|
|
|
|
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!isLastModifiedTimeExists) { |
|
|
|
|
|
|
|
newColumns.push({ |
|
|
|
|
|
|
|
...(await getColumnPropsFromUIDT( |
|
|
|
{ |
|
|
|
{ |
|
|
|
colBody = await getColumnPropsFromUIDT(colBody, source); |
|
|
|
uidt: UITypes.LastModifiedTime, |
|
|
|
|
|
|
|
column_name: getUniqueColumnName(columns, 'updated_at'), |
|
|
|
// remove default value for SQLite since it doesn't support default value as function when adding column
|
|
|
|
title: getUniqueColumnAliasName(columns, 'Updated At'), |
|
|
|
// only support default value as constant value
|
|
|
|
cdf: null, |
|
|
|
if (source.type === 'sqlite3') { |
|
|
|
}, |
|
|
|
colBody.cdf = null; |
|
|
|
source, |
|
|
|
|
|
|
|
)), |
|
|
|
|
|
|
|
system: true, |
|
|
|
|
|
|
|
altered: Altered.NEW_COLUMN, |
|
|
|
|
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// create column in db
|
|
|
|
// update column in db
|
|
|
|
const tableUpdateBody = { |
|
|
|
const tableUpdateBody = { |
|
|
|
...table, |
|
|
|
...model, |
|
|
|
tn: table.table_name, |
|
|
|
tn: model.table_name, |
|
|
|
originalColumns: table.columns.map((c) => ({ |
|
|
|
originalColumns: model.columns.map((c) => ({ |
|
|
|
...c, |
|
|
|
...c, |
|
|
|
cn: c.column_name, |
|
|
|
cn: c.column_name, |
|
|
|
})), |
|
|
|
})), |
|
|
|
columns: [ |
|
|
|
columns: [ |
|
|
|
...table.columns.map((c) => ({ ...c, cn: c.column_name })), |
|
|
|
...columns.map((c) => ({ ...c, cn: c.column_name })), |
|
|
|
{ |
|
|
|
...newColumns, |
|
|
|
...colBody, |
|
|
|
|
|
|
|
cn: columnName, |
|
|
|
|
|
|
|
altered: Altered.NEW_COLUMN, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
], |
|
|
|
], |
|
|
|
}; |
|
|
|
}; |
|
|
|
const sqlMgr = await reuseOrSave('sqlMgr', reuse, async () => |
|
|
|
const sqlMgr = ProjectMgrv2.getSqlMgr({ id: source.base_id }, ncMeta); |
|
|
|
ProjectMgrv2.getSqlMgr({ id: source.base_id }), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
await sqlMgr.sqlOpPlus(source, 'tableUpdate', tableUpdateBody); |
|
|
|
await sqlMgr.sqlOpPlus(source, 'tableUpdate', tableUpdateBody); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const title = getUniqueColumnAliasName( |
|
|
|
|
|
|
|
table.columns, |
|
|
|
|
|
|
|
UITypes.CreatedTime ? 'CreatedAt' : 'UpdatedAt', |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|