Browse Source

fix: merge upgraders

Signed-off-by: mertmit <mertmit99@gmail.com>
pull/7373/head
mertmit 9 months ago
parent
commit
65827231ce
  1. 12
      packages/nocodb-sdk/src/lib/Api.ts
  2. 113
      packages/nocodb/src/version-upgrader/ncXcdbCreatedAndUpdatedSystemFieldsUpgrader.ts

12
packages/nocodb-sdk/src/lib/Api.ts

@ -433,7 +433,7 @@ export interface ColumnType {
| 'Checkbox' | 'Checkbox'
| 'Collaborator' | 'Collaborator'
| 'Count' | 'Count'
| 'CreateTime' | 'CreatedTime'
| 'Currency' | 'Currency'
| 'Date' | 'Date'
| 'DateTime' | 'DateTime'
@ -464,7 +464,9 @@ export interface ColumnType {
| 'Year' | 'Year'
| 'QrCode' | 'QrCode'
| 'Links' | 'Links'
| 'User'; | 'User'
| 'CreatedBy'
| 'LastModifiedBy';
/** Is Unsigned? */ /** Is Unsigned? */
un?: BoolType; un?: BoolType;
/** Is unique? */ /** Is unique? */
@ -1751,7 +1753,7 @@ export interface NormalColumnRequestType {
| 'Checkbox' | 'Checkbox'
| 'Collaborator' | 'Collaborator'
| 'Count' | 'Count'
| 'CreateTime' | 'CreatedTime'
| 'Currency' | 'Currency'
| 'Date' | 'Date'
| 'DateTime' | 'DateTime'
@ -1782,7 +1784,9 @@ export interface NormalColumnRequestType {
| 'Year' | 'Year'
| 'QrCode' | 'QrCode'
| 'Links' | 'Links'
| 'User'; | 'User'
| 'CreatedBy'
| 'LastModifiedBy';
/** Is this column unique? */ /** Is this column unique? */
un?: BoolType; un?: BoolType;
/** Is this column unique? */ /** Is this column unique? */

113
packages/nocodb/src/version-upgrader/ncXcdbCreatedAndUpdatedSystemFieldsUpgrader.ts

@ -57,28 +57,22 @@ async function upgradeModels({
ncMeta, ncMeta,
source, source,
base, base,
models,
}: { }: {
ncMeta: MetaService; ncMeta: MetaService;
source: Source; source: Source;
base: Base; base: Base;
models: Model[];
}) { }) {
const models = await Model.list( // get existing columns from database
{ const sqlClient = await NcConnectionMgrv2.getSqlClient(source, ncMeta.knex);
base_id: source.base_id, const sqlMgr = ProjectMgrv2.getSqlMgr({ id: source.base_id }, ncMeta);
source_id: source.id,
},
ncMeta,
);
await Promise.all( await Promise.all(
models.map(async (model: any) => { models.map(async (model) => {
if (model.mm) return; if (model.mm) return;
try { try {
logger.log( const columns = model.columns;
`Upgrading model '${model.title}'(${model.id}) from base '${base.title}'(${base.id}})`,
);
const columns = await model.getColumns(ncMeta);
const oldColumns = columns.map((c) => ({ ...c, cn: c.column_name })); const oldColumns = columns.map((c) => ({ ...c, cn: c.column_name }));
let isCreatedTimeExists = false; let isCreatedTimeExists = false;
let isLastModifiedTimeExists = false; let isLastModifiedTimeExists = false;
@ -125,46 +119,51 @@ async function upgradeModels({
// if column is created_at or updated_at, update the uidt in meta // 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( if (column.uidt !== UITypes.CreatedTime || !column.system) {
column.id, await Column.update(
{ column.id,
...column, {
uidt: UITypes.CreatedTime, ...column,
system: true, uidt: UITypes.CreatedTime,
}, system: true,
ncMeta, },
true, ncMeta,
); true,
);
}
/* Enable if planning to remove trigger /* Enable if planning to remove trigger
if (source.type === 'pg') { if (source.type === 'pg') {
// delete pg trigger if exists // delete pg trigger if exists
await deletePgTrigger({ column, ncMeta, model }); await deletePgTrigger({ column, ncMeta, model });
} }
*/ */
} }
if (column.column_name === 'updated_at') { if (column.column_name === 'updated_at') {
isLastModifiedTimeExists = true; isLastModifiedTimeExists = true;
await Column.update( if (column.uidt !== UITypes.LastModifiedTime || !column.system) {
column.id, await Column.update(
{ column.id,
...column, {
uidt: UITypes.LastModifiedTime, ...column,
system: true, uidt: UITypes.LastModifiedTime,
cdf: '', system: true,
au: false, },
}, ncMeta,
ncMeta, true,
true, );
); }
} }
} }
// get existing columns from database if (
const sqlClient = await NcConnectionMgrv2.getSqlClient( isCreatedTimeExists &&
source, isLastModifiedTimeExists &&
ncMeta.knex, isCreatedByExists &&
); isLastModifiedByExists
) {
return;
}
const dbColumns = const dbColumns =
( (
@ -327,9 +326,6 @@ async function upgradeModels({
// alter table and add new columns if any // alter table and add new columns if any
if (newColumns.length) { if (newColumns.length) {
logger.log(
`Altering table '${model.title}'(${model.id}) from base '${base.title}'(${base.id}}) for new columns`,
);
// update column in db // update column in db
const tableUpdateBody = { const tableUpdateBody = {
...model, ...model,
@ -340,23 +336,18 @@ async function upgradeModels({
cn: c.column_name, cn: c.column_name,
})), })),
}; };
const sqlMgr = ProjectMgrv2.getSqlMgr({ id: source.base_id }, ncMeta);
await sqlMgr.sqlOpPlus(source, 'tableUpdate', tableUpdateBody); await sqlMgr.sqlOpPlus(source, 'tableUpdate', tableUpdateBody);
} }
for (const newColumn of [...existingDbColumns, ...newColumns]) { for (const newColumn of [...existingDbColumns, ...newColumns]) {
await Column.insert( await Column.insert(
{ {
...newColumn, ...newColumn,
system: 1, system: true,
fk_model_id: model.id, fk_model_id: model.id,
}, },
ncMeta, ncMeta,
); );
} }
logger.log(
`Upgraded model '${model.title}'(${model.id}) from base '${base.title}'(${base.id}})`,
);
} catch (e) { } catch (e) {
logger.error( logger.error(
`Upgrading model '${model.title}'(${model.id}) from base '${base.title}'(${base.id}}) failed`, `Upgrading model '${model.title}'(${model.id}) from base '${base.title}'(${base.id}}) failed`,
@ -407,13 +398,25 @@ export default async function ({ ncMeta }: NcUpgraderCtx) {
// update the meta props // update the meta props
return requestQueue.enqueue(async () => { return requestQueue.enqueue(async () => {
const models = await Model.list(
{
base_id: source.base_id,
source_id: source.id,
},
ncMeta,
);
for (const model of models) {
await model.getColumns(ncMeta);
}
logger.log( logger.log(
`Upgrading base ${base.title}(${base.id},${source.id}) (${i + 1}/${ `Upgrading base ${base.title}(${base.id},${source.id}) (${i + 1}/${
sources.length sources.length
})`, })`,
); );
await upgradeModels({ ncMeta, source, base }).then(() => { await upgradeModels({ ncMeta, source, models, base }).then(() => {
logger.log( logger.log(
`Upgraded base '${base.title}'(${base.id},${source.id}) (${i + 1}/${ `Upgraded base '${base.title}'(${base.id},${source.id}) (${i + 1}/${
sources.length sources.length

Loading…
Cancel
Save