|
|
@ -46,7 +46,7 @@ async function upgradeModels({ |
|
|
|
source, |
|
|
|
source, |
|
|
|
}: { |
|
|
|
}: { |
|
|
|
ncMeta: MetaService; |
|
|
|
ncMeta: MetaService; |
|
|
|
source: any; |
|
|
|
source: Source; |
|
|
|
}) { |
|
|
|
}) { |
|
|
|
const models = await Model.list( |
|
|
|
const models = await Model.list( |
|
|
|
{ |
|
|
|
{ |
|
|
@ -68,6 +68,8 @@ async function upgradeModels({ |
|
|
|
let isLastModifiedTimeExists = false; |
|
|
|
let isLastModifiedTimeExists = false; |
|
|
|
for (const column of columns) { |
|
|
|
for (const column of columns) { |
|
|
|
if (column.uidt !== UITypes.DateTime) continue; |
|
|
|
if (column.uidt !== UITypes.DateTime) continue; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if column is created_at or updated_at, update the uidt in meta
|
|
|
|
if (column.column_name === 'created_at') { |
|
|
|
if (column.column_name === 'created_at') { |
|
|
|
isCreatedTimeExists = true; |
|
|
|
isCreatedTimeExists = true; |
|
|
|
await Column.update( |
|
|
|
await Column.update( |
|
|
@ -78,6 +80,7 @@ async function upgradeModels({ |
|
|
|
system: true, |
|
|
|
system: true, |
|
|
|
}, |
|
|
|
}, |
|
|
|
ncMeta, |
|
|
|
ncMeta, |
|
|
|
|
|
|
|
true, |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
/* Enable if planning to remove trigger |
|
|
|
/* Enable if planning to remove trigger |
|
|
@ -98,6 +101,7 @@ async function upgradeModels({ |
|
|
|
au: false, |
|
|
|
au: false, |
|
|
|
}, |
|
|
|
}, |
|
|
|
ncMeta, |
|
|
|
ncMeta, |
|
|
|
|
|
|
|
true, |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -110,25 +114,24 @@ async function upgradeModels({ |
|
|
|
ncMeta.knex, |
|
|
|
ncMeta.knex, |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const dbColumns = ( |
|
|
|
const dbColumns = |
|
|
|
await sqlClient.columnList({ |
|
|
|
( |
|
|
|
tn: model.table_name, |
|
|
|
await sqlClient.columnList({ |
|
|
|
schema: source.getConfig()?.schema, |
|
|
|
tn: model.table_name, |
|
|
|
}) |
|
|
|
schema: source.getConfig()?.schema, |
|
|
|
)?.data?.list; |
|
|
|
}) |
|
|
|
//
|
|
|
|
)?.data?.list?.map((c) => ({ ...c, column_name: c.cn })) || []; |
|
|
|
//
|
|
|
|
|
|
|
|
// // create created_at and updated_at columns
|
|
|
|
// create created_at and updated_at columns
|
|
|
|
const newColumns = []; |
|
|
|
const newColumns = []; |
|
|
|
const existingDbColumns = []; |
|
|
|
const existingDbColumns = []; |
|
|
|
//,
|
|
|
|
|
|
|
|
if (!isCreatedTimeExists) { |
|
|
|
if (!isCreatedTimeExists) { |
|
|
|
//
|
|
|
|
// check column exist and add to meta if found
|
|
|
|
// // check column exist and add to meta if found
|
|
|
|
|
|
|
|
const columnName = getUniqueColumnName(columns, 'created_at'); |
|
|
|
const columnName = getUniqueColumnName(columns, 'created_at'); |
|
|
|
//
|
|
|
|
|
|
|
|
const dbColumn = dbColumns.find((c) => c.cn === columnName); |
|
|
|
const dbColumn = dbColumns.find((c) => c.cn === columnName); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if column already exist, just update the meta
|
|
|
|
if ( |
|
|
|
if ( |
|
|
|
dbColumn && |
|
|
|
dbColumn && |
|
|
|
getColumnUiType(source, dbColumn) === UITypes.DateTime |
|
|
|
getColumnUiType(source, dbColumn) === UITypes.DateTime |
|
|
@ -136,7 +139,7 @@ async function upgradeModels({ |
|
|
|
existingDbColumns.push({ |
|
|
|
existingDbColumns.push({ |
|
|
|
...dbColumn, |
|
|
|
...dbColumn, |
|
|
|
uidt: UITypes.CreatedTime, |
|
|
|
uidt: UITypes.CreatedTime, |
|
|
|
column_name: getUniqueColumnName(columns, 'created_at'), |
|
|
|
column_name: columnName, |
|
|
|
title: getUniqueColumnAliasName(columns, 'CreatedAt'), |
|
|
|
title: getUniqueColumnAliasName(columns, 'CreatedAt'), |
|
|
|
system: true, |
|
|
|
system: true, |
|
|
|
}); |
|
|
|
}); |
|
|
@ -145,7 +148,10 @@ async function upgradeModels({ |
|
|
|
...(await getColumnPropsFromUIDT( |
|
|
|
...(await getColumnPropsFromUIDT( |
|
|
|
{ |
|
|
|
{ |
|
|
|
uidt: UITypes.CreatedTime, |
|
|
|
uidt: UITypes.CreatedTime, |
|
|
|
column_name: getUniqueColumnName(columns, 'created_at'), |
|
|
|
column_name: getUniqueColumnName( |
|
|
|
|
|
|
|
[...columns, ...dbColumns], |
|
|
|
|
|
|
|
'created_at', |
|
|
|
|
|
|
|
), |
|
|
|
title: getUniqueColumnAliasName(columns, 'CreatedAt'), |
|
|
|
title: getUniqueColumnAliasName(columns, 'CreatedAt'), |
|
|
|
}, |
|
|
|
}, |
|
|
|
source, |
|
|
|
source, |
|
|
@ -159,9 +165,9 @@ async function upgradeModels({ |
|
|
|
|
|
|
|
|
|
|
|
if (!isLastModifiedTimeExists) { |
|
|
|
if (!isLastModifiedTimeExists) { |
|
|
|
const columnName = getUniqueColumnName(columns, 'created_at'); |
|
|
|
const columnName = getUniqueColumnName(columns, 'created_at'); |
|
|
|
//
|
|
|
|
|
|
|
|
const dbColumn = dbColumns.find((c) => c.cn === columnName); |
|
|
|
const dbColumn = dbColumns.find((c) => c.cn === columnName); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if column already exist, just update the meta
|
|
|
|
if ( |
|
|
|
if ( |
|
|
|
dbColumn && |
|
|
|
dbColumn && |
|
|
|
getColumnUiType(source, dbColumn) === UITypes.DateTime |
|
|
|
getColumnUiType(source, dbColumn) === UITypes.DateTime |
|
|
@ -169,7 +175,7 @@ async function upgradeModels({ |
|
|
|
existingDbColumns.push({ |
|
|
|
existingDbColumns.push({ |
|
|
|
uidt: UITypes.LastModifiedTime, |
|
|
|
uidt: UITypes.LastModifiedTime, |
|
|
|
...dbColumn, |
|
|
|
...dbColumn, |
|
|
|
column_name: getUniqueColumnName(columns, 'updated_at'), |
|
|
|
column_name: columnName, |
|
|
|
title: getUniqueColumnAliasName(columns, 'UpdatedAt'), |
|
|
|
title: getUniqueColumnAliasName(columns, 'UpdatedAt'), |
|
|
|
system: true, |
|
|
|
system: true, |
|
|
|
}); |
|
|
|
}); |
|
|
@ -178,7 +184,10 @@ async function upgradeModels({ |
|
|
|
...(await getColumnPropsFromUIDT( |
|
|
|
...(await getColumnPropsFromUIDT( |
|
|
|
{ |
|
|
|
{ |
|
|
|
uidt: UITypes.LastModifiedTime, |
|
|
|
uidt: UITypes.LastModifiedTime, |
|
|
|
column_name: getUniqueColumnName(columns, 'updated_at'), |
|
|
|
column_name: getUniqueColumnName( |
|
|
|
|
|
|
|
[...columns, ...dbColumns], |
|
|
|
|
|
|
|
'updated_at', |
|
|
|
|
|
|
|
), |
|
|
|
title: getUniqueColumnAliasName(columns, 'UpdatedAt'), |
|
|
|
title: getUniqueColumnAliasName(columns, 'UpdatedAt'), |
|
|
|
}, |
|
|
|
}, |
|
|
|
source, |
|
|
|
source, |
|
|
@ -190,7 +199,11 @@ async function upgradeModels({ |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// alter table and add new columns if any
|
|
|
|
if (newColumns.length) { |
|
|
|
if (newColumns.length) { |
|
|
|
|
|
|
|
logger.log( |
|
|
|
|
|
|
|
`Altering table ${model.title} from source ${source.alias} for new columns`, |
|
|
|
|
|
|
|
); |
|
|
|
// update column in db
|
|
|
|
// update column in db
|
|
|
|
const tableUpdateBody = { |
|
|
|
const tableUpdateBody = { |
|
|
|
...model, |
|
|
|
...model, |
|
|
@ -246,10 +259,15 @@ export default async function ({ ncMeta }: NcUpgraderCtx) { |
|
|
|
const source = new Source(_source); |
|
|
|
const source = new Source(_source); |
|
|
|
|
|
|
|
|
|
|
|
// skip deleted project bases
|
|
|
|
// skip deleted project bases
|
|
|
|
if (await source.getProject(ncMeta).then((p) => !p || p.deleted)) continue; |
|
|
|
if (await source.getProject(ncMeta).then((p) => !p || p.deleted)) { |
|
|
|
|
|
|
|
logger.log(`Skipped deleted base source ${source.alias}`); |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
logger.log(`Upgrading source ${source.alias}`); |
|
|
|
logger.log(`Upgrading source ${source.alias}`); |
|
|
|
// update the meta props
|
|
|
|
// update the meta props
|
|
|
|
await upgradeModels({ ncMeta, source }); |
|
|
|
await upgradeModels({ ncMeta, source }); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger.log(`Upgraded source ${source.alias}`); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|