freeletics / migrator

General purpose library for android to do in app migrations.
Apache License 2.0
18 stars 2 forks source link

New installs don't need migrations - mentally wrong thought. #1

Open soenkegissel opened 5 years ago

soenkegissel commented 5 years ago

I like this project. Was looking for exactly a construct like this. BUT this is mentally wrong thought. A newly installed app or an all very close to the current development state does not need as much migrations as a very old version of the app. I am trying my best to explain it with this example:

image

The far left evolutionary state needs the most migrations.

The tool using evolutionary state just needs two more migrations.

But a child of the modern human (newly installed app in this case) does not need a single migration. All newly features and data states are already built in. It can every introduce problems.

I came up with a solution where I leverage the BuildConfig.VERSION_CODE.

public Migrator createMigration() {

    for (int i = PrefManager.with(mContext).getInt(CURRENT_VERSIONCODE, 0); i <= BuildConfig.VERSION_CODE; i++) {

        switch (i) {
            case 14:
                    return new Migrator(mContext, new V1Migration(mContext));
                break;
            case 34:
                return new Migrator(mContext, new V2Migration(mContext), new V3Migration(mContext));
            case 77:
                return new Migrator(mContext, new V4Migration(mContext));
                break;
        }
    }
    //throw new... ?!
    return null;
}
    if (MigrationFactory.getInstance(this).getVersionCode() < BuildConfig.VERSION_CODE) {
            Migrator migrator = MigrationFactory.getInstance(this).createMigration();
            migrator.runMigrationsIfNeeded();
        }

Hope this helps.

soenkegissel commented 5 years ago

I answered my own question on StackOverflow with a solution I came up.