mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-22 22:49:45 +01:00
core: make item migration fall through all db versions
this was not exactly a bug but it can cause a lot of unintended behaviour. Previously, you'd have to manually specify which version the item migration should jump to. This was buggy and poorly designed. This change makes the item iterate over all the db migrations one by one automatically. For example: An item at version 5.2 will go through: - 5.3 - 5.4 - and so on
This commit is contained in:
committed by
Abdullah Atta
parent
9701a3d660
commit
c09715d053
@@ -17,10 +17,9 @@ You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { migrations } from "../../migrations";
|
||||
import { migrateItem } from "../../migrations";
|
||||
import SparkMD5 from "spark-md5";
|
||||
import setManipulator from "../../utils/set";
|
||||
import { CURRENT_DATABASE_VERSION } from "../../common";
|
||||
import { logger } from "../../logger";
|
||||
|
||||
class Merger {
|
||||
@@ -136,19 +135,7 @@ class Merger {
|
||||
deserialized.type = type;
|
||||
}
|
||||
|
||||
if (!migrations[version]) {
|
||||
throw new Error(
|
||||
version > CURRENT_DATABASE_VERSION
|
||||
? `Cannot migrate item to v${version}. Please update your client to the latest version.`
|
||||
: `Could not migrate item to v${version}. Please report this bug to the developers.`
|
||||
);
|
||||
}
|
||||
|
||||
const migrate = migrations[version][type];
|
||||
if (migrate) {
|
||||
return await migrate(deserialized, this._db);
|
||||
}
|
||||
return deserialized;
|
||||
return migrateItem(deserialized, version, type, this._db);
|
||||
}
|
||||
|
||||
async _deserialize(item, migrate = true) {
|
||||
|
||||
@@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { migrations } from "../migrations";
|
||||
import { migrateItem } from "../migrations";
|
||||
|
||||
class Migrator {
|
||||
async migrate(db, collections, get, version) {
|
||||
@@ -40,8 +40,12 @@ class Migrator {
|
||||
item.type = "tiptap";
|
||||
}
|
||||
|
||||
const migrate = migrations[version][item.type || collection.type];
|
||||
if (migrate) item = await migrate(item, db);
|
||||
item = await migrateItem(
|
||||
item,
|
||||
version,
|
||||
item.type || collection.type,
|
||||
db
|
||||
);
|
||||
|
||||
if (collection.dbCollection.merge) {
|
||||
await collection.dbCollection.merge(item);
|
||||
|
||||
@@ -19,49 +19,58 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import { parseHTML } from "./utils/html-parser";
|
||||
import { decodeHTML5 } from "entities";
|
||||
import { CURRENT_DATABASE_VERSION } from "./common";
|
||||
|
||||
export const migrations = {
|
||||
5.0: {},
|
||||
5.1: {},
|
||||
5.2: {
|
||||
note: replaceDateEditedWithDateModified(),
|
||||
notebook: (item) => {
|
||||
item = replaceDateEditedWithDateModified()(item);
|
||||
return migrations["5.6"].notebook(item);
|
||||
},
|
||||
const migrations = [
|
||||
{ version: 5.0, types: {} },
|
||||
{ version: 5.1, types: {} },
|
||||
{
|
||||
version: 5.2,
|
||||
types: {
|
||||
note: replaceDateEditedWithDateModified(false),
|
||||
notebook: replaceDateEditedWithDateModified(false),
|
||||
tag: replaceDateEditedWithDateModified(true),
|
||||
attachment: replaceDateEditedWithDateModified(true),
|
||||
trash: replaceDateEditedWithDateModified(),
|
||||
tiny: (item) => {
|
||||
item = replaceDateEditedWithDateModified()(item);
|
||||
item = replaceDateEditedWithDateModified(false)(item);
|
||||
|
||||
if (!item.data || item.data.iv) return migrations["5.3"].tiny(item);
|
||||
if (!item.data || item.data.iv) return item;
|
||||
|
||||
item.data = removeToxClassFromChecklist(wrapTablesWithDiv(item.data));
|
||||
return migrations["5.3"].tiny(item);
|
||||
return item;
|
||||
},
|
||||
settings: (item) => {
|
||||
item = replaceDateEditedWithDateModified(true)(item);
|
||||
return migrations["5.6"].settings(item);
|
||||
settings: replaceDateEditedWithDateModified(true)
|
||||
}
|
||||
},
|
||||
5.3: {
|
||||
{
|
||||
version: 5.3,
|
||||
types: {
|
||||
tiny: (item) => {
|
||||
if (!item.data || item.data.iv) return migrations["5.4"].tiny(item);
|
||||
if (!item.data || item.data.iv) return item;
|
||||
item.data = decodeWrappedTableHtml(item.data);
|
||||
return migrations["5.4"].tiny(item);
|
||||
return item;
|
||||
}
|
||||
}
|
||||
},
|
||||
5.4: {
|
||||
{
|
||||
version: 5.4,
|
||||
types: {
|
||||
tiny: (item) => {
|
||||
if (!item.data || item.data.iv) return item;
|
||||
item.type = "tiptap";
|
||||
item.data = tinyToTiptap(item.data);
|
||||
return item;
|
||||
}
|
||||
}
|
||||
},
|
||||
5.5: {},
|
||||
5.6: {
|
||||
{
|
||||
version: 5.5,
|
||||
types: {}
|
||||
},
|
||||
{
|
||||
version: 5.6,
|
||||
types: {
|
||||
notebook: (item) => {
|
||||
if (!item.topics) return item;
|
||||
|
||||
@@ -76,7 +85,6 @@ export const migrations = {
|
||||
|
||||
for (const pin of item.pins) {
|
||||
if (!pin.data) continue;
|
||||
|
||||
await db.shortcuts.add({
|
||||
item: {
|
||||
type: pin.type,
|
||||
@@ -88,19 +96,31 @@ export const migrations = {
|
||||
delete item.pins;
|
||||
return item;
|
||||
}
|
||||
},
|
||||
5.7: {
|
||||
note: false,
|
||||
shortcut: false,
|
||||
notebook: false,
|
||||
tag: false,
|
||||
attachment: false,
|
||||
trash: false,
|
||||
tiny: false,
|
||||
tiptap: false,
|
||||
settings: false
|
||||
}
|
||||
};
|
||||
},
|
||||
{ version: 5.7, types: {} }
|
||||
];
|
||||
|
||||
export async function migrateItem(item, version, type, database) {
|
||||
let migrationStartIndex = migrations.findIndex((m) => m.version === version);
|
||||
if (migrationStartIndex <= -1) {
|
||||
throw new Error(
|
||||
version > CURRENT_DATABASE_VERSION
|
||||
? `Please update the app to the latest version.`
|
||||
: `You seem to be on a very outdated version. Please update the app to the latest version.`
|
||||
);
|
||||
}
|
||||
|
||||
for (; migrationStartIndex < migrations.length; ++migrationStartIndex) {
|
||||
const migration = migrations[migrationStartIndex];
|
||||
if (migration.version === CURRENT_DATABASE_VERSION) break;
|
||||
|
||||
const itemMigrator = migration.types[type];
|
||||
if (!itemMigrator) continue;
|
||||
item = await itemMigrator(item, database);
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
function replaceDateEditedWithDateModified(removeDateEditedProperty = false) {
|
||||
return function (item) {
|
||||
|
||||
Reference in New Issue
Block a user