Browse Source

Fix - Migration bugs (#1771)

* fix: handle formula parse error in formula migration

re #1760

Signed-off-by: Pranav C <pranavxc@gmail.com>

* fix: avoid project user insertion if corresponding project or user is missing

re #1760

Signed-off-by: Pranav C <pranavxc@gmail.com>

* fix: skip shared views if view_name/model_name is empty or missing

re #1760

Signed-off-by: Pranav C <pranavxc@gmail.com>

* fix: migration down statement correction

Signed-off-by: Pranav C <pranavxc@gmail.com>

* fix(migration): split alter table statement into multiple statements to support MSSQL

re #1764

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/1774/head
Pranav C 3 years ago committed by GitHub
parent
commit
03bf9180ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 49
      packages/nocodb/src/lib/noco/migrationsv2/nc_011.ts
  2. 20
      packages/nocodb/src/lib/noco/migrationsv2/nc_012_alter_column_data_types.ts
  3. 53
      packages/nocodb/src/lib/noco/upgrader/jobs/ncProjectUpgraderV2_0090000.ts

49
packages/nocodb/src/lib/noco/migrationsv2/nc_011.ts

@ -878,27 +878,38 @@ const up = async knex => {
};
const down = async knex => {
// todo: update order based on relation and add missing drop statements
// TODO : delete relations
await knex.schema.dropTable(MetaTable.PROJECT);
await knex.schema.dropTable(MetaTable.BASES);
await knex.schema.dropTable(MetaTable.MODELS);
await knex.schema.dropTable(MetaTable.COLUMNS);
await knex.schema.dropTable(MetaTable.COL_RELATIONS);
await knex.schema.dropTable(MetaTable.COL_RELATIONS);
await knex.schema.dropTable(MetaTable.FILTER_EXP);
await knex.schema.dropTable(MetaTable.SORT);
await knex.schema.dropTable(MetaTable.SHARED_VIEWS);
await knex.schema.dropTable(MetaTable.FORM_VIEW);
await knex.schema.dropTable(MetaTable.FORM_VIEW_COLUMNS);
await knex.schema.dropTable(MetaTable.GALLERY_VIEW);
await knex.schema.dropTable(MetaTable.GALLERY_VIEW_COLUMNS);
await knex.schema.dropTable(MetaTable.KANBAN_VIEW);
await knex.schema.dropTable(MetaTable.MODEL_ROLE_VISIBILITY);
await knex.schema.dropTable(MetaTable.PLUGIN);
await knex.schema.dropTable(MetaTable.AUDIT);
await knex.schema.dropTable(MetaTable.TEAM_USERS);
await knex.schema.dropTable(MetaTable.TEAMS);
await knex.schema.dropTable(MetaTable.ORGS);
await knex.schema.dropTable(MetaTable.PROJECT_USERS);
await knex.schema.dropTable(MetaTable.USERS);
await knex.schema.dropTable(MetaTable.KANBAN_VIEW_COLUMNS);
await knex.schema.dropTable(MetaTable.COL_LOOKUP);
await knex.schema.dropTable(MetaTable.COL_FORMULA);
await knex.schema.dropTable(MetaTable.KANBAN_VIEW);
await knex.schema.dropTable(MetaTable.GRID_VIEW_COLUMNS);
await knex.schema.dropTable(MetaTable.GRID_VIEW);
await knex.schema.dropTable(MetaTable.GALLERY_VIEW_COLUMNS);
await knex.schema.dropTable(MetaTable.GALLERY_VIEW);
await knex.schema.dropTable(MetaTable.FORM_VIEW_COLUMNS);
await knex.schema.dropTable(MetaTable.FORM_VIEW);
await knex.schema.dropTable(MetaTable.SHARED_VIEWS);
await knex.schema.dropTable(MetaTable.SORT);
await knex.schema.dropTable(MetaTable.FILTER_EXP);
await knex.schema.dropTable(MetaTable.HOOK_LOGS);
await knex.schema.dropTable(MetaTable.HOOKS);
await knex.schema.dropTable(MetaTable.VIEWS);
await knex.schema.dropTable(MetaTable.COL_FORMULA);
await knex.schema.dropTable(MetaTable.COL_ROLLUP);
await knex.schema.dropTable(MetaTable.COL_LOOKUP);
await knex.schema.dropTable(MetaTable.COL_SELECT_OPTIONS);
await knex.schema.dropTable(MetaTable.COL_RELATIONS);
await knex.schema.dropTable(MetaTable.COLUMN_VALIDATIONS);
await knex.schema.dropTable(MetaTable.COLUMNS);
await knex.schema.dropTable(MetaTable.MODELS);
await knex.schema.dropTable(MetaTable.BASES);
await knex.schema.dropTable(MetaTable.PROJECT);
};
export { up, down };

20
packages/nocodb/src/lib/noco/migrationsv2/nc_012_alter_column_data_types.ts

@ -5,22 +5,34 @@ const up = async (knex: Knex) => {
if (knex.client.config.client !== 'sqlite3') {
await knex.schema.alterTable(MetaTable.COLUMNS, table => {
table.text('cdf').alter();
});
await knex.schema.alterTable(MetaTable.COLUMNS, table => {
table.text('dtxp').alter();
});
await knex.schema.alterTable(MetaTable.COLUMNS, table => {
table.text('cc').alter();
});
await knex.schema.alterTable(MetaTable.COLUMNS, table => {
table.text('ct').alter();
});
}
};
const down = async knex => {
await knex.schema.alterTable(MetaTable.COLUMNS, table => {
if (knex.client.config.client !== 'sqlite3') {
if (knex.client.config.client !== 'sqlite3') {
await knex.schema.alterTable(MetaTable.COLUMNS, table => {
table.string('cdf').alter();
});
await knex.schema.alterTable(MetaTable.COLUMNS, table => {
table.string('dtxp').alter();
});
await knex.schema.alterTable(MetaTable.COLUMNS, table => {
table.string('cc').alter();
});
await knex.schema.alterTable(MetaTable.COLUMNS, table => {
table.string('ct').alter();
}
});
});
}
};
export { up, down };

53
packages/nocodb/src/lib/noco/upgrader/jobs/ncProjectUpgraderV2_0090000.ts

@ -43,9 +43,9 @@ export default async function(ctx: NcUpgraderCtx) {
await projectBuilder.init();
}
await migrateUsers(ncMeta);
const usersObj = await migrateUsers(ncMeta);
const projectsObj = await migrateProjects(ncMeta);
await migrateProjectUsers(ncMeta);
await migrateProjectUsers(projectsObj, usersObj, ncMeta);
const migrationCtx = await migrateProjectModels(ncMeta);
await migrateUIAcl(migrationCtx, ncMeta);
@ -58,12 +58,13 @@ export default async function(ctx: NcUpgraderCtx) {
async function migrateUsers(ncMeta = Noco.ncMeta) {
const users = await ncMeta.metaList(null, null, 'xc_users');
const userList: User[] = [];
const userObj: { [id: string]: User } = {};
for (const user of users) {
userList.push(await User.insert(user, ncMeta));
const user1 = await User.insert(user, ncMeta);
userObj[user1.id] = user1;
}
return userList;
return userObj;
}
async function migrateProjects(
@ -101,10 +102,20 @@ async function migrateProjects(
return projectsObj;
}
async function migrateProjectUsers(ncMeta = Noco.ncMeta) {
async function migrateProjectUsers(
projectsObj: { [p: string]: Project },
usersObj: { [p: string]: User },
ncMeta = Noco.ncMeta
) {
const projectUsers = await ncMeta.metaList(null, null, 'nc_projects_users');
for (const projectUser of projectUsers) {
// skip if project is missing
if (!(projectUser.project_id in projectsObj)) continue;
// skip if user is missing
if (!(projectUser.user_id in usersObj)) continue;
await ProjectUser.insert(
{
project_id: projectUser.project_id,
@ -199,6 +210,7 @@ interface Formulav1 {
formula: {
value: string;
tree: any;
error: string[] | string;
};
}
@ -649,10 +661,20 @@ async function migrateProjectModels(
const colBody: any = {
_cn: columnMeta._cn
};
colBody.formula = await substituteColumnAliasWithIdInFormula(
columnMeta.formula.value,
await model.getColumns(ncMeta)
);
if (columnMeta?.formula?.error?.length) {
colBody.error = Array.isArray(columnMeta.formula.error)
? columnMeta.formula.error.join(',')
: columnMeta.formula.error;
} else {
try {
colBody.formula = await substituteColumnAliasWithIdInFormula(
columnMeta.formula.value,
await model.getColumns(ncMeta)
);
} catch {
colBody.error = 'Invalid formula';
}
}
colBody.formula_raw = columnMeta.formula.value;
const column = await Column.insert(
{
@ -1084,11 +1106,15 @@ async function migrateSharedViews(ctx: MigrateCtxV1, ncMeta: any) {
for (const sharedView of sharedViews) {
let fk_view_id;
// if missing view name or model name skip the shared view migration
if (!sharedView.view_name || !sharedView.model_name) continue;
if (sharedView.view_type !== 'table' && sharedView.view_type !== 'view') {
fk_view_id =
ctx.objViewRef[sharedView.project_id][sharedView.model_name][
sharedView.view_name
].id;
]?.id;
} else {
fk_view_id =
ctx.objViewRef[sharedView.project_id][sharedView.model_name][
@ -1096,9 +1122,12 @@ async function migrateSharedViews(ctx: MigrateCtxV1, ncMeta: any) {
].id ||
ctx.objViewRef[sharedView.project_id][sharedView.model_name][
sharedView.model_name
].id;
]?.id;
}
// if view id missing skip shared view migration
if (!fk_view_id) continue;
await View.update(
fk_view_id,
{

Loading…
Cancel
Save