🐛(sw) keep incremental versioning for IndexedDB
IndexDB need a integer versioning when upgrading the database, it has to be incremental. Before the fix, version 4.0.0 would give 400, when 3.10.0 would give 3100. That would cause an error and the database would be destroyed then recreated. We improve the way we compute the version number to ensure it is always incremental, avoiding such issues.
This commit is contained in:
@@ -33,12 +33,21 @@ interface IDocsDB extends DBSchema {
|
|||||||
type TableName = 'doc-list' | 'doc-item' | 'doc-mutation';
|
type TableName = 'doc-list' | 'doc-item' | 'doc-mutation';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* IndexDB version must be a integer
|
* IndexDB prefers incremental versioning when upgrading the database,
|
||||||
* @returns
|
* otherwise it may throw an error, and we need to destroy the database
|
||||||
|
* and recreate it.
|
||||||
|
* To calculate the version number, we use the following formula,
|
||||||
|
* assuring incremental versioning:
|
||||||
|
* version = major * 1000000 + minor * 1000 + patch
|
||||||
|
* ex:
|
||||||
|
* - version 1.2.3 => 1002003
|
||||||
|
* - version 0.10.15 => 10015
|
||||||
|
* - version 2.0.0 => 2000000
|
||||||
|
* @returns number
|
||||||
*/
|
*/
|
||||||
const getCurrentVersion = () => {
|
export const getCurrentVersion = () => {
|
||||||
const [major, minor, patch] = pkg.version.split('.');
|
const [major, minor, patch] = pkg.version.split('.').map(Number);
|
||||||
return parseFloat(`${major}${minor}${patch}`);
|
return major * 1000000 + minor * 1000 + patch;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||||
|
|
||||||
|
vi.mock('@/../package.json', () => ({
|
||||||
|
default: { version: '0.0.0' },
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe('DocsDB', () => {
|
||||||
|
afterEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
vi.resetModules();
|
||||||
|
});
|
||||||
|
|
||||||
|
let previousExpected = 0;
|
||||||
|
|
||||||
|
[
|
||||||
|
{ version: '0.0.1', expected: 1 },
|
||||||
|
{ version: '0.10.15', expected: 10015 },
|
||||||
|
{ version: '1.0.0', expected: 1000000 },
|
||||||
|
{ version: '2.105.3', expected: 2105003 },
|
||||||
|
{ version: '3.0.0', expected: 3000000 },
|
||||||
|
{ version: '10.20.30', expected: 10020030 },
|
||||||
|
].forEach(({ version, expected }) => {
|
||||||
|
it(`correctly computes version for ${version}`, () => {
|
||||||
|
vi.doMock('@/../package.json', () => ({
|
||||||
|
default: { version },
|
||||||
|
}));
|
||||||
|
|
||||||
|
return vi.importActual('../DocsDB').then((module: any) => {
|
||||||
|
const result = module.getCurrentVersion();
|
||||||
|
expect(result).toBe(expected);
|
||||||
|
expect(result).toBeGreaterThan(previousExpected);
|
||||||
|
previousExpected = result;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user