Browse Source

fix: skip meta migration if associated project doesn't exist

re #2449

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/2560/head
Pranav C 2 years ago
parent
commit
e1aa5f598c
  1. 184
      packages/nocodb/src/lib/version-upgrader/ncProjectUpgraderV2_0090000.ts

184
packages/nocodb/src/lib/version-upgrader/ncProjectUpgraderV2_0090000.ts

@ -8,7 +8,7 @@ import {
ModelTypes, ModelTypes,
substituteColumnAliasWithIdInFormula, substituteColumnAliasWithIdInFormula,
UITypes, UITypes,
ViewTypes ViewTypes,
} from 'nocodb-sdk'; } from 'nocodb-sdk';
import Column from '../models/Column'; import Column from '../models/Column';
import LinkToAnotherRecordColumn from '../models/LinkToAnotherRecordColumn'; import LinkToAnotherRecordColumn from '../models/LinkToAnotherRecordColumn';
@ -30,7 +30,7 @@ import { getUniqueColumnAliasName } from '../meta/helpers/getUniqueName';
import NcProjectBuilderEE from '../v1-legacy/NcProjectBuilderEE'; import NcProjectBuilderEE from '../v1-legacy/NcProjectBuilderEE';
import Audit from '../models/Audit'; import Audit from '../models/Audit';
export default async function(ctx: NcUpgraderCtx) { export default async function (ctx: NcUpgraderCtx) {
const ncMeta = ctx.ncMeta; const ncMeta = ctx.ncMeta;
const projects = await ctx.ncMeta.projectList(); const projects = await ctx.ncMeta.projectList();
@ -85,7 +85,7 @@ async function migrateProjects(
prefix: projectConfig.prefix, prefix: projectConfig.prefix,
is_meta: !!projectConfig.prefix, is_meta: !!projectConfig.prefix,
title: projectConfig?.title, title: projectConfig?.title,
bases: projectConfig?.envs?._noco?.db?.map(d => { bases: projectConfig?.envs?._noco?.db?.map((d) => {
const inflection = (d && d.meta && d.meta.inflection) || {}; const inflection = (d && d.meta && d.meta.inflection) || {};
return { return {
is_meta: !!projectConfig.prefix, is_meta: !!projectConfig.prefix,
@ -94,11 +94,11 @@ async function migrateProjects(
created_at: project.created_at, created_at: project.created_at,
updated_at: project.updated_at, updated_at: project.updated_at,
inflection_column: inflection.cn, inflection_column: inflection.cn,
inflection_table: inflection.tn inflection_table: inflection.tn,
}; };
}), }),
created_at: project.created_at, created_at: project.created_at,
updated_at: project.updated_at updated_at: project.updated_at,
}; };
const p = await Project.createProject(projectBody, ncMeta); const p = await Project.createProject(projectBody, ncMeta);
projectsObj[p.id] = p; projectsObj[p.id] = p;
@ -126,7 +126,7 @@ async function migrateProjectUsers(
fk_user_id: projectUser.user_id, fk_user_id: projectUser.user_id,
roles: projectUser.roles, roles: projectUser.roles,
created_at: projectUser.created_at, created_at: projectUser.created_at,
updated_at: projectUser.updated_at updated_at: projectUser.updated_at,
}, },
ncMeta ncMeta
); );
@ -328,7 +328,7 @@ const filterV1toV2CompOpMap = {
'is not null': 'notnull', 'is not null': 'notnull',
'is null': 'null', 'is null': 'null',
'is not equal': 'neq', 'is not equal': 'neq',
'is not like': 'nlike' 'is not like': 'nlike',
}; };
interface Relationv1 { interface Relationv1 {
@ -387,6 +387,12 @@ async function migrateProjectModels(
// parse meta // parse meta
const project = await Project.getWithInfo(modelData.project_id, ncMeta); const project = await Project.getWithInfo(modelData.project_id, ncMeta);
// skip if associated project is not found
if (!project) {
continue;
}
const baseId = project.bases[0].id; const baseId = project.bases[0].id;
const meta = JSON.parse(modelData.meta); const meta = JSON.parse(modelData.meta);
@ -401,7 +407,7 @@ async function migrateProjectModels(
type: modelData.type === 'table' ? ModelTypes.TABLE : ModelTypes.VIEW, type: modelData.type === 'table' ? ModelTypes.TABLE : ModelTypes.VIEW,
created_at: modelData.created_at, created_at: modelData.created_at,
updated_at: modelData.updated_at, updated_at: modelData.updated_at,
mm: !!modelData.mm mm: !!modelData.mm,
}, },
ncMeta ncMeta
); );
@ -435,7 +441,7 @@ async function migrateProjectModels(
for (const columnMeta of meta.columns) { for (const columnMeta of meta.columns) {
let system = false; let system = false;
if (meta.belongsTo?.find(bt => bt.cn === columnMeta.cn)) { if (meta.belongsTo?.find((bt) => bt.cn === columnMeta.cn)) {
system = true; system = true;
columnMeta.uidt = UITypes.ForeignKey; columnMeta.uidt = UITypes.ForeignKey;
} }
@ -450,16 +456,13 @@ async function migrateProjectModels(
title: columnMeta._cn, title: columnMeta._cn,
column_name: columnMeta.cn, column_name: columnMeta.cn,
system, system,
fk_model_id: model.id fk_model_id: model.id,
}, },
ncMeta ncMeta
); );
projectModelColumnRefs[model.table_name][ projectModelColumnRefs[model.table_name][column.column_name] =
column.column_name projectModelColumnAliasRefs[model.table_name][column.title] = column;
] = projectModelColumnAliasRefs[model.table_name][
column.title
] = column;
} }
// migrate table virtual columns // migrate table virtual columns
@ -494,7 +497,7 @@ async function migrateProjectModels(
let virtual = false; let virtual = false;
if (columnMeta.mm) { if (columnMeta.mm) {
const relation = relations.find( const relation = relations.find(
r => (r) =>
r.rtn === columnMeta.mm.tn && r.rtn === columnMeta.mm.tn &&
r.rcn === columnMeta.mm.cn && r.rcn === columnMeta.mm.cn &&
r.tn === columnMeta.mm.vtn && r.tn === columnMeta.mm.vtn &&
@ -504,7 +507,7 @@ async function migrateProjectModels(
} else if (columnMeta.hm) { } else if (columnMeta.hm) {
virtual = virtual =
relations.find( relations.find(
r => (r) =>
r.rtn === columnMeta.hm.rtn && r.rtn === columnMeta.hm.rtn &&
r.tn === columnMeta.hm.tn && r.tn === columnMeta.hm.tn &&
r.rcn === columnMeta.hm.rcn && r.rcn === columnMeta.hm.rcn &&
@ -513,7 +516,7 @@ async function migrateProjectModels(
} else if (columnMeta.bt) { } else if (columnMeta.bt) {
virtual = virtual =
relations.find( relations.find(
r => (r) =>
r.rtn === columnMeta.bt.rtn && r.rtn === columnMeta.bt.rtn &&
r.tn === columnMeta.bt.tn && r.tn === columnMeta.bt.tn &&
r.rcn === columnMeta.bt.rcn && r.rcn === columnMeta.bt.rcn &&
@ -539,14 +542,13 @@ async function migrateProjectModels(
fk_mm_child_column_id, fk_mm_child_column_id,
fk_mm_parent_column_id, fk_mm_parent_column_id,
fk_related_model_id: columnMeta.hm ? tnId : rtnId, fk_related_model_id: columnMeta.hm ? tnId : rtnId,
virtual virtual,
}, },
ncMeta ncMeta
); );
projectModelColumnAliasRefs[model.table_name][ projectModelColumnAliasRefs[model.table_name][column.title] =
column.title column;
] = column;
}); });
} else { } else {
// other virtual columns insert // other virtual columns insert
@ -556,7 +558,7 @@ async function migrateProjectModels(
const columnMeta: Lookupv1 = _columnMeta; const columnMeta: Lookupv1 = _columnMeta;
const colBody: any = { const colBody: any = {
_cn: columnMeta._cn _cn: columnMeta._cn,
}; };
colBody.fk_lookup_column_id = colBody.fk_lookup_column_id =
@ -569,9 +571,8 @@ async function migrateProjectModels(
// extract related(virtual relation) column id // extract related(virtual relation) column id
for (const col of columns) { for (const col of columns) {
if (col.uidt === UITypes.LinkToAnotherRecord) { if (col.uidt === UITypes.LinkToAnotherRecord) {
const colOpt = await col.getColOptions< const colOpt =
LinkToAnotherRecordColumn await col.getColOptions<LinkToAnotherRecordColumn>(ncMeta);
>(ncMeta);
if ( if (
colOpt.type === columnMeta.lk.type && colOpt.type === columnMeta.lk.type &&
colOpt.fk_child_column_id === colOpt.fk_child_column_id ===
@ -599,20 +600,19 @@ async function migrateProjectModels(
{ {
uidt: UITypes.Lookup, uidt: UITypes.Lookup,
...colBody, ...colBody,
fk_model_id: model.id fk_model_id: model.id,
}, },
ncMeta ncMeta
); );
projectModelColumnAliasRefs[model.table_name][ projectModelColumnAliasRefs[model.table_name][column.title] =
column.title column;
] = column;
} else if (_columnMeta.rl) { } else if (_columnMeta.rl) {
// migrate rollup column // migrate rollup column
const columnMeta: Rollupv1 = _columnMeta; const columnMeta: Rollupv1 = _columnMeta;
const colBody: Partial<RollupColumn & Column> = { const colBody: Partial<RollupColumn & Column> = {
title: columnMeta._cn, title: columnMeta._cn,
rollup_function: columnMeta.rl.fn rollup_function: columnMeta.rl.fn,
}; };
colBody.fk_rollup_column_id = colBody.fk_rollup_column_id =
@ -627,9 +627,8 @@ async function migrateProjectModels(
// extract related(virtual relation) column id // extract related(virtual relation) column id
for (const col of columns) { for (const col of columns) {
if (col.uidt === UITypes.LinkToAnotherRecord) { if (col.uidt === UITypes.LinkToAnotherRecord) {
const colOpt = await col.getColOptions< const colOpt =
LinkToAnotherRecordColumn await col.getColOptions<LinkToAnotherRecordColumn>(ncMeta);
>(ncMeta);
if ( if (
colOpt.type === columnMeta.rl.type && colOpt.type === columnMeta.rl.type &&
colOpt.fk_child_column_id === colOpt.fk_child_column_id ===
@ -656,18 +655,17 @@ async function migrateProjectModels(
{ {
uidt: UITypes.Rollup, uidt: UITypes.Rollup,
...colBody, ...colBody,
fk_model_id: model.id fk_model_id: model.id,
}, },
ncMeta ncMeta
); );
projectModelColumnAliasRefs[model.table_name][ projectModelColumnAliasRefs[model.table_name][column.title] =
column.title column;
] = column;
} else if (_columnMeta.formula) { } else if (_columnMeta.formula) {
const columnMeta: Formulav1 = _columnMeta; const columnMeta: Formulav1 = _columnMeta;
// migrate formula column // migrate formula column
const colBody: any = { const colBody: any = {
_cn: columnMeta._cn _cn: columnMeta._cn,
}; };
if (columnMeta?.formula?.error?.length) { if (columnMeta?.formula?.error?.length) {
colBody.error = Array.isArray(columnMeta.formula.error) colBody.error = Array.isArray(columnMeta.formula.error)
@ -688,14 +686,13 @@ async function migrateProjectModels(
{ {
uidt: UITypes.Formula, uidt: UITypes.Formula,
...colBody, ...colBody,
fk_model_id: model.id fk_model_id: model.id,
}, },
ncMeta ncMeta
); );
projectModelColumnAliasRefs[model.table_name][ projectModelColumnAliasRefs[model.table_name][column.title] =
column.title column;
] = column;
} }
}); });
} }
@ -703,9 +700,9 @@ async function migrateProjectModels(
// extract system hasmany relation // extract system hasmany relation
const hmColumns = meta.hasMany?.filter( const hmColumns = meta.hasMany?.filter(
hm => (hm) =>
!meta.v.find( !meta.v.find(
v => (v) =>
v.hm && v.hm &&
v.hm.rtn === hm.rtn && v.hm.rtn === hm.rtn &&
v.hm.rcn === hm.rcn && v.hm.rcn === hm.rcn &&
@ -727,7 +724,7 @@ async function migrateProjectModels(
const virtual = const virtual =
relations.find( relations.find(
r => (r) =>
r.rtn === rel.rtn && r.rtn === rel.rtn &&
r.tn === rel.tn && r.tn === rel.tn &&
r.rcn === rel.rcn && r.rcn === rel.rcn &&
@ -750,7 +747,7 @@ async function migrateProjectModels(
dr: rel.dr, dr: rel.dr,
fk_related_model_id: tnId, fk_related_model_id: tnId,
system: true, system: true,
virtual virtual,
}, },
ncMeta ncMeta
); );
@ -763,15 +760,13 @@ async function migrateProjectModels(
// insert default view data here // insert default view data here
// @ts-ignore // @ts-ignore
const defaultView = await View.list(model.id, ncMeta).then( const defaultView = await View.list(model.id, ncMeta).then(
views => views[0] (views) => views[0]
); );
objViewRef[project.id][modelData.title][ objViewRef[project.id][modelData.title][defaultView.title] =
defaultView.title defaultView;
] = defaultView; objViewQPRef[project.id][modelData.title][defaultView.title] =
objViewQPRef[project.id][modelData.title][ queryParams;
defaultView.title
] = queryParams;
const viewColumns = await View.getColumns(defaultView.id, ncMeta); const viewColumns = await View.getColumns(defaultView.id, ncMeta);
@ -786,7 +781,7 @@ async function migrateProjectModels(
let orderCount = 1; let orderCount = 1;
for (const [_cn, column] of aliasColArr) { for (const [_cn, column] of aliasColArr) {
const viewColumn = viewColumns.find( const viewColumn = viewColumns.find(
c => column.id === c.fk_column_id (c) => column.id === c.fk_column_id
); );
if (!viewColumn) continue; if (!viewColumn) continue;
await GridViewColumn.update( await GridViewColumn.update(
@ -796,7 +791,7 @@ async function migrateProjectModels(
show: queryParams?.showFields show: queryParams?.showFields
? queryParams?.showFields?.[_cn] || false ? queryParams?.showFields?.[_cn] || false
: true, : true,
width: queryParams?.columnsWidth?.[_cn] width: queryParams?.columnsWidth?.[_cn],
}, },
ncMeta ncMeta
); );
@ -805,7 +800,7 @@ async function migrateProjectModels(
defaultView.id, defaultView.id,
{ {
show_system_fields: queryParams.showSystemFields, show_system_fields: queryParams.showSystemFields,
order: modelData.view_order order: modelData.view_order,
}, },
ncMeta ncMeta
); );
@ -830,7 +825,7 @@ async function migrateProjectModels(
objModelColumnRef, objModelColumnRef,
objViewRef, objViewRef,
objViewQPRef, objViewQPRef,
objModelAliasRef objModelAliasRef,
}, },
ncMeta ncMeta
); );
@ -844,7 +839,7 @@ async function migrateProjectModels(
objModelColumnRef, objModelColumnRef,
objViewRef, objViewRef,
objViewQPRef, objViewQPRef,
objModelAliasRef objModelAliasRef,
}, },
ncMeta ncMeta
); );
@ -857,7 +852,7 @@ async function migrateProjectModels(
objModelColumnRef, objModelColumnRef,
objViewRef, objViewRef,
objViewQPRef, objViewQPRef,
objModelAliasRef objModelAliasRef,
}; };
} }
@ -868,7 +863,7 @@ async function migrateProjectModelViews(
// objModelColumnRef, // objModelColumnRef,
objModelColumnAliasRef, objModelColumnAliasRef,
objViewRef, objViewRef,
objViewQPRef objViewQPRef,
}: MigrateCtxV1, }: MigrateCtxV1,
ncMeta ncMeta
) { ) {
@ -883,18 +878,19 @@ async function migrateProjectModelViews(
queryParams = JSON.parse(viewData.query_params); queryParams = JSON.parse(viewData.query_params);
} }
objViewQPRef[project.id][viewData.parent_model_title][ objViewQPRef[project.id][viewData.parent_model_title][viewData.title] =
viewData.title queryParams;
] = queryParams;
const insertObj: Partial<
const insertObj: Partial<View & View &
GridView & GridView &
KanbanView & KanbanView &
FormView & FormView &
GalleryView & { GalleryView & {
created_at; created_at;
updated_at; updated_at;
}> = { }
> = {
title: viewData.title, title: viewData.title,
show: true, show: true,
order: viewData.view_order, order: viewData.view_order,
@ -902,7 +898,7 @@ async function migrateProjectModelViews(
project_id: project.id, project_id: project.id,
base_id: baseId, base_id: baseId,
created_at: viewData.created_at, created_at: viewData.created_at,
updated_at: viewData.updated_at updated_at: viewData.updated_at,
}; };
if (viewData.show_as === 'grid') { if (viewData.show_as === 'grid') {
@ -948,7 +944,7 @@ async function migrateProjectModelViews(
let orderCount = 1; let orderCount = 1;
for (const [_cn, column] of aliasColArr) { for (const [_cn, column] of aliasColArr) {
const viewColumn = viewColumns.find(c => column.id === c.fk_column_id); const viewColumn = viewColumns.find((c) => column.id === c.fk_column_id);
const order = orderCount++; const order = orderCount++;
const show = queryParams?.showFields const show = queryParams?.showFields
? queryParams?.showFields?.[_cn] || false ? queryParams?.showFields?.[_cn] || false
@ -964,19 +960,21 @@ async function migrateProjectModelViews(
required: columnParams?.required, required: columnParams?.required,
description: columnParams?.description, description: columnParams?.description,
order, order,
show show,
}, },
ncMeta ncMeta
); );
} else if (viewData.show_as === 'grid') { } else if (viewData.show_as === 'grid') {
const viewColumn = viewColumns.find(c => column.id === c.fk_column_id); const viewColumn = viewColumns.find(
(c) => column.id === c.fk_column_id
);
if (!viewColumn) continue; if (!viewColumn) continue;
await GridViewColumn.update( await GridViewColumn.update(
viewColumn.id, viewColumn.id,
{ {
order, order,
show, show,
width: queryParams?.columnsWidth?.[_cn] width: queryParams?.columnsWidth?.[_cn],
}, },
ncMeta ncMeta
); );
@ -986,7 +984,7 @@ async function migrateProjectModelViews(
viewColumn.id, viewColumn.id,
{ {
order, order,
show show,
}, },
ncMeta ncMeta
); );
@ -996,7 +994,7 @@ async function migrateProjectModelViews(
view.id, view.id,
{ {
show_system_fields: queryParams.showSystemFields, show_system_fields: queryParams.showSystemFields,
order: viewData.view_order order: viewData.view_order,
}, },
ncMeta ncMeta
); );
@ -1009,7 +1007,7 @@ async function migrateViewsParams(
objModelColumnAliasRef, objModelColumnAliasRef,
objViewRef, objViewRef,
objViewQPRef, objViewQPRef,
objModelColumnRef objModelColumnRef,
}: MigrateCtxV1, }: MigrateCtxV1,
ncMeta ncMeta
) { ) {
@ -1027,7 +1025,7 @@ async function migrateViewsParams(
await View.update( await View.update(
view.id, view.id,
{ {
lock_type: queryParams?.viewStatus?.type lock_type: queryParams?.viewStatus?.type,
}, },
ncMeta ncMeta
); );
@ -1044,7 +1042,7 @@ async function migrateViewsParams(
)?.id || null )?.id || null
: null, : null,
fk_view_id: view.id, fk_view_id: view.id,
direction: sort.order === '-' ? 'desc' : 'asc' direction: sort.order === '-' ? 'desc' : 'asc',
}, },
ncMeta ncMeta
); );
@ -1063,7 +1061,7 @@ async function migrateViewsParams(
fk_view_id: view.id, fk_view_id: view.id,
comparison_op: filterV1toV2CompOpMap[filter.op], comparison_op: filterV1toV2CompOpMap[filter.op],
logical_op: filter.logicOp, logical_op: filter.logicOp,
value: filter.value value: filter.value,
}, },
ncMeta ncMeta
); );
@ -1117,7 +1115,7 @@ async function migrateUIAcl(ctx: MigrateCtxV1, ncMeta: any) {
fk_view_id, fk_view_id,
disabled: acl.disabled, disabled: acl.disabled,
created_at: acl.created_at, created_at: acl.created_at,
updated_at: acl.updated_at updated_at: acl.updated_at,
}, },
ncMeta ncMeta
); );
@ -1167,7 +1165,7 @@ async function migrateSharedViews(ctx: MigrateCtxV1, ncMeta: any) {
fk_view_id, fk_view_id,
{ {
uuid: sharedView.view_id, uuid: sharedView.view_id,
password: sharedView.password password: sharedView.password,
}, },
ncMeta ncMeta
); );
@ -1189,7 +1187,7 @@ async function migrateSharedBase(ncMeta: any) {
{ {
uuid: sharedBase.shared_base_id, uuid: sharedBase.shared_base_id,
password: sharedBase.password, password: sharedBase.password,
roles: sharedBase.roles roles: sharedBase.roles,
}, },
ncMeta ncMeta
); );
@ -1217,7 +1215,7 @@ async function migratePlugins(ncMeta: any) {
creator_website: plugin.creator_website, creator_website: plugin.creator_website,
price: plugin.price, price: plugin.price,
created_at: plugin.created_at, created_at: plugin.created_at,
updated_at: plugin.updated_at updated_at: plugin.updated_at,
}); });
} }
} }
@ -1275,7 +1273,7 @@ async function migrateWebhooks(ctx: MigrateCtxV1, ncMeta: any) {
timeout: hookMeta.timeout, timeout: hookMeta.timeout,
active: hookMeta.active, active: hookMeta.active,
created_at: hookMeta.created_at, created_at: hookMeta.created_at,
updated_at: hookMeta.updated_at updated_at: hookMeta.updated_at,
}, },
ncMeta ncMeta
); );
@ -1300,7 +1298,7 @@ async function migrateWebhooks(ctx: MigrateCtxV1, ncMeta: any) {
fk_hook_id: hook.id, fk_hook_id: hook.id,
logical_op: filter.logicOp, logical_op: filter.logicOp,
comparison_op: filterV1toV2CompOpMap[filter.op], comparison_op: filterV1toV2CompOpMap[filter.op],
value: filter.value value: filter.value,
}, },
ncMeta ncMeta
); );
@ -1344,7 +1342,7 @@ async function migrateAutitLog(
description: audit.description, description: audit.description,
details: audit.details, details: audit.details,
created_at: audit.created_at, created_at: audit.created_at,
updated_at: audit.updated_at updated_at: audit.updated_at,
}; };
if (audit.model_name) { if (audit.model_name) {
@ -1353,10 +1351,10 @@ async function migrateAutitLog(
ctx.objModelRef?.[audit.project_id]?.[audit.model_name] || ctx.objModelRef?.[audit.project_id]?.[audit.model_name] ||
// extract model by using model_id property from audit // extract model by using model_id property from audit
ctx.objModelRef?.[audit.project_id]?.[ ctx.objModelRef?.[audit.project_id]?.[
ctx.metas?.find(m => m.id == audit.model_id)?.title ctx.metas?.find((m) => m.id == audit.model_id)?.title
] || ] ||
ctx.objModelAliasRef?.[audit.project_id]?.[ ctx.objModelAliasRef?.[audit.project_id]?.[
ctx.metas?.find(m => m.id == audit.model_id)?.alias ctx.metas?.find((m) => m.id == audit.model_id)?.alias
]; ];
// if model is not found skip audit insertion // if model is not found skip audit insertion

Loading…
Cancel
Save