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:
Abdullah Atta
2022-10-12 10:39:47 +05:00
committed by Abdullah Atta
parent 9701a3d660
commit c09715d053
3 changed files with 102 additions and 91 deletions

View File

@@ -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) {