Skip to content

Commit 4d671eb

Browse files
committed
[IMP] data: match data versions to release versions
This commit fixes two issues at once. Issue 1 ------- Currently, the migration process will ONLY run if the version of the original data (in json) is smaller than the last version set in the source code (`CURRENT_VERSION`). This means that any additional step set outside of the library is not run without incrementing CURRENT_VERSION in o-spreadsheet. Example: - we bump the latest version to 25 in o-spreadsheet; - we push that value in Odoo - users start creating spreadsheets in that version so their spreadsheets are set to version 25 - later on, we need to add a migration step for whatever reason in Odoo, we assign it to 25.1 => the migration code will check against the latest version in the source (25) vs the one in the spreadsheet data (25 as well) and will simply skip the migration. So 25.1 will never run... Issue 2 ------- The current scheme of the incremented integer is impractical when it comes to figure out which odoo version it matches(*). Knowing the release version is usefull for bug fixes to know which version of the code generated the json data. With this commit, data format version are now aligned with odoo versions Current master is 18.3. Next, it'll be 18.4, 19.0, 19.1, etc. (*) o-spreadsheet releases follow the same version numbers as odoo releases. closes #5973 Task: 4659206 Signed-off-by: Pierre Rousseau (pro) <[email protected]>
1 parent 27b7506 commit 4d671eb

File tree

8 files changed

+115
-90
lines changed

8 files changed

+115
-90
lines changed

src/helpers/ui/paste_interactive.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CURRENT_VERSION } from "../../migrations/data";
1+
import { getCurrentVersion } from "../../migrations/data";
22
import { _t } from "../../translation";
33
import {
44
ClipboardPasteOptions,
@@ -75,7 +75,7 @@ export async function interactivePasteFromOS(
7575
} catch (error) {
7676
const parsedSpreadsheetContent = parsedClipboardContent.data;
7777

78-
if (parsedSpreadsheetContent?.version !== CURRENT_VERSION) {
78+
if (parsedSpreadsheetContent?.version !== getCurrentVersion()) {
7979
env.raiseError(
8080
_t(
8181
"An unexpected error occurred while pasting content.\

src/migrations/data.ts

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@ import { XlsxReader } from "../xlsx/xlsx_reader";
1515
import { migrationStepRegistry } from "./migration_steps";
1616

1717
/**
18-
* This is the current state version number. It should be incremented each time
19-
* a breaking change is made in the way the state is handled, and an upgrade
20-
* function should be defined
18+
* Represents the current version of the exported JSON data.
19+
* A new version must be created whenever a breaking change is introduced in the export format.
20+
* To define a new version, add an upgrade function to `migrationStepRegistry`.
2121
*/
22-
export const CURRENT_VERSION = 25;
22+
export function getCurrentVersion() {
23+
return getSortedVersions().at(-1)!;
24+
}
25+
26+
function getSortedVersions() {
27+
return migrationStepRegistry.getKeys().sort(compareVersions);
28+
}
29+
2330
const INITIAL_SHEET_ID = "Sheet1";
2431

2532
/**
@@ -47,7 +54,10 @@ export function load(data?: any, verboseImport?: boolean): WorkbookData {
4754

4855
// apply migrations, if needed
4956
if ("version" in data) {
50-
if (data.version < CURRENT_VERSION) {
57+
if (isLegacyVersioning(data)) {
58+
data.version = LEGACY_VERSION_MAPPING[data.version];
59+
}
60+
if (data.version !== getCurrentVersion()) {
5161
console.debug("Migrating data from version", data.version);
5262
data = migrate(data);
5363
}
@@ -58,6 +68,44 @@ export function load(data?: any, verboseImport?: boolean): WorkbookData {
5868
return data;
5969
}
6070

71+
const LEGACY_VERSION_MAPPING = {
72+
25: "18.2",
73+
24: "18.1.1",
74+
23: "18.1",
75+
22: "18.0.4",
76+
21: "18.0.3",
77+
20: "18.0.2",
78+
19: "18.0.1",
79+
18: "18.0",
80+
17: "17.4",
81+
16: "17.3",
82+
15: "17.2",
83+
14: "16.4",
84+
13: "16.3",
85+
12: "15.4",
86+
87+
// not accurate starting at this point
88+
11: "0.10",
89+
10: "0.9",
90+
9: "0.8",
91+
8: "0.7",
92+
7: "0.6",
93+
6: "0.5",
94+
5: "0.4",
95+
4: "0.3",
96+
3: "0.2",
97+
2: "0.1",
98+
1: "0",
99+
};
100+
101+
/**
102+
* Versions used to be an incremented integer.
103+
* This was later changed to match release versions (matching Odoo release names).
104+
*/
105+
function isLegacyVersioning(data: { version: number | string }): boolean {
106+
return typeof data.version === "number";
107+
}
108+
61109
// -----------------------------------------------------------------------------
62110
// Migrations
63111
// -----------------------------------------------------------------------------
@@ -83,12 +131,11 @@ function compareVersions(v1: string, v2: string): number {
83131

84132
function migrate(data: any): WorkbookData {
85133
const start = performance.now();
86-
const steps = migrationStepRegistry
87-
.getAll()
88-
.sort((a, b) => compareVersions(a.versionFrom, b.versionFrom));
89-
const index = steps.findIndex((step) => step.versionFrom === data.version.toString());
90-
for (let i = index; i < steps.length; i++) {
91-
data = steps[i].migrate(data);
134+
const versions = getSortedVersions();
135+
const index = versions.findIndex((v) => v === data.version);
136+
for (let i = index + 1; i < versions.length; i++) {
137+
const nextVersion = versions[i];
138+
data = migrationStepRegistry.get(nextVersion).migrate(data);
92139
}
93140
console.debug("Data migrated in", performance.now() - start, "ms");
94141
return data;
@@ -131,7 +178,7 @@ function forceUnicityOfFigure(data: Partial<WorkbookData>): Partial<WorkbookData
131178
*/
132179
function setDefaults(partialData: Partial<WorkbookData>): Partial<WorkbookData> {
133180
const data: WorkbookData = Object.assign(createEmptyWorkbookData(), partialData, {
134-
version: CURRENT_VERSION,
181+
version: getCurrentVersion(),
135182
});
136183
data.sheets = data.sheets
137184
? data.sheets.map((s, i) =>
@@ -291,7 +338,7 @@ export function createEmptySheet(sheetId: UID, name: string): SheetData {
291338

292339
export function createEmptyWorkbookData(sheetName = "Sheet1"): WorkbookData {
293340
return {
294-
version: CURRENT_VERSION,
341+
version: getCurrentVersion(),
295342
sheets: [createEmptySheet(INITIAL_SHEET_ID, sheetName)],
296343
styles: {},
297344
formats: {},

0 commit comments

Comments
 (0)