[REF] spreadsheet: move migrate code from import to migration

Before this revision, the code to ensure that a list has a name was done
in the import function instead of the migration process.

Part-of: odoo/odoo#100198
This commit is contained in:
Pierre Rousseau
2022-09-15 16:08:19 +02:00
parent c873acc6dd
commit 6909640d90
4 changed files with 60 additions and 12 deletions
@@ -332,11 +332,7 @@ export default class ListCorePlugin extends spreadsheet.CorePlugin {
context: list.context,
orderBy: list.orderBy,
},
/**
* As we are not able to migrate the json, we have to fallback
* in the case where the name is not yet present
*/
name: list.name || list.model,
name: list.name,
};
this._addList(id, definition, this.uuidGenerator.uuidv4());
}
@@ -4,7 +4,7 @@ import spreadsheet from "./o_spreadsheet_extended";
const { load, CorePlugin, tokenize, parse, convertAstNodes, astToFormula } = spreadsheet;
const { corePluginRegistry } = spreadsheet.registries;
export const ODOO_VERSION = 3;
export const ODOO_VERSION = 4;
const MAP = {
PIVOT: "ODOO.PIVOT",
@@ -29,6 +29,9 @@ export function migrate(data) {
if (version < 3) {
_data = migrate2to3(_data);
}
if (version < 4) {
_data = migrate3to4(_data);
}
return _data;
}
@@ -110,6 +113,23 @@ function migrate2to3(data) {
return data;
}
/**
* Migration of list/pivot names
*/
function migrate3to4(data) {
if (data.lists) {
for (const list of Object.values(data.lists)) {
list.name = list.name || list.model;
}
}
if (data.pivots) {
for (const pivot of Object.values(data.pivots)) {
pivot.name = pivot.name || pivot.model;
}
}
return data;
}
/**
* Convert pivot formulas days parameters from day/month/year
* format to the standard spreadsheet month/day/year format.
@@ -121,7 +141,7 @@ function migratePivotDaysParameters(formulaString) {
const ast = parse(formulaString);
const convertedAst = convertAstNodes(ast, "FUNCALL", (ast) => {
if (["ODOO.PIVOT", "ODOO.PIVOT.HEADER"].includes(ast.value.toUpperCase())) {
for (let subAst of ast.args) {
for (const subAst of ast.args) {
if (subAst.type === "STRING") {
const date = subAst.value.match(dmyRegex);
if (date) {
@@ -443,11 +443,7 @@ export default class PivotCorePlugin extends CorePlugin {
domain: pivot.domain,
context: pivot.context,
},
/**
* As we are not able to migrate the json, we have to fallback
* in the case where the name is not yet present
*/
name: pivot.name || pivot.model,
name: pivot.name,
};
this._addPivot(id, definition, this.uuidGenerator.uuidv4());
}
@@ -164,6 +164,42 @@ QUnit.test("Global filters: list and graph fields are added", (assert) => {
});
});
QUnit.test("List name default is model name", (assert) => {
const data = {
lists: {
1: {
name: "Name",
model: "Model",
},
2: {
model: "Model",
},
},
};
const migratedData = migrate(data);
assert.strictEqual(Object.values(migratedData.lists).length, 2);
assert.strictEqual(migratedData.lists["1"].name, "Name");
assert.strictEqual(migratedData.lists["2"].name, "Model");
});
QUnit.test("Pivot name default is model name", (assert) => {
const data = {
pivots: {
1: {
name: "Name",
model: "Model",
},
2: {
model: "Model",
},
},
};
const migratedData = migrate(data);
assert.strictEqual(Object.values(migratedData.pivots).length, 2);
assert.strictEqual(migratedData.pivots["1"].name, "Name");
assert.strictEqual(migratedData.pivots["2"].name, "Model");
});
QUnit.test("Odoo version is exported", (assert) => {
const model = new Model();
assert.strictEqual(model.exportData().odooVersion, ODOO_VERSION);