flutterings / sqflite_migration

Library to manage sqlite db migrations.
https://efthymis.com/migrating-a-mobile-database-in-flutter-sqlite/
Other
47 stars 18 forks source link

New version (2) requires 1 migrations more than what you have. #1

Closed robinbonnes closed 4 years ago

robinbonnes commented 5 years ago

Thanks for this great plugin.

However, when I migrate it says:

Failed assertion: line 27 pos 12: 'config.migrationScripts.length >= newVersion': New version (2) requires 1 migrations more than what you have.

Also it gives a non-fatal error when you start with an empty migration array.

esarbanis commented 5 years ago

Hi @robinbonnes and thank you for trying out sqflite_migration!

It would be great if you could provide the output of flutter doctor and maybe a test case. It would greatly improve the time needed to debug and fix this.

If you have already spotted a possible fix feel free to create a PR 😃

jamesncl commented 5 years ago

I've also encountered this problem. I think the steps to reproduce are as follows - this is just what I have done. Sorry I don't have time to investigate furhter, I'm massively under pressure to code my ass off at the moment.

Thanks for the great plugin, definitely fulfulls an unmet need in flutter!

jamesncl commented 5 years ago

I've just implemented the logic manually as described in your article here. I got the same problem using this code:

onUpgrade: (Database db, int oldVersion, int newVersion) async {
    for (var i = oldVersion - 1; i <= newVersion - 1; i++) {
      await db.execute(migrationScripts[i]);
    }  
  }

Is there a mistake - shouldn't it be while i < newVersion - 1 in the for loop? When I use that, it works.

esarbanis commented 5 years ago

@jamesncl can you try with the newest version of the library?

marcelovbcfilho commented 4 years ago

I have the same issue, I have an List migatrion with 3 of lenght, when opening the database with openDatabaseWithMigration, inside your code there is a "version: config.migrationScripts.length + 1,", and inside the Migrator you have an assert "assert(config.migrationScripts.length >= newVersion,", so how my migration list that has only 3 of lenght will be run if the new version is 4 ???

esarbanis commented 4 years ago

Hi @marcelovbcfilho there is an implicit assertion that in version 1 there are nomigrationScripts but rather only initializationScript. This means that the version of the database is always the number of migrationScripts + the initialization script. So config.migrationScripts.length + 1 holds this logic.

zoref commented 4 years ago

I have the same problem. It fails on this assert assert(config.migrationScripts.length >= newVersion, 'New version ($newVersion) requires ${newVersion - config.migrationScripts.length} migrations more than what you have.'); in executeMigration.

Shouldn't that be config.migrationScripts.length >= newVersion - 1 since the version is config.migrationScripts.length + 1 ?

I believe @jamesncl is right about the for loop condition as well.

Tibasa commented 4 years ago

Same problem! Any solution?

Dimitry82 commented 4 years ago

Correct will be like this in migrator.dart:

` Future executeMigration(Database db, int oldVersion, int newVersion) async { assert(oldVersion < newVersion, 'The newVersion($newVersion) should always be greater than the oldVersion($oldVersion).'); assert(config.migrationScripts.length <= newVersion, 'New version ($newVersion) requires ${newVersion - config.migrationScripts.length} migrations more than what you have.');

for (var i = oldVersion - 1; i < newVersion - 1; i++) {
  await db.execute(config.migrationScripts[i]);
}

}`

rodyfernandez commented 4 years ago

con mi amigo Matias Araus lo resolvimos de la siguiente forma:

final migrationScripts = [
  'CREATE TABLE Cuotas ('
      ' idcuota INTEGER PRIMARY KEY, '
      ' creditoid INTEGER, '
      ' descripcion TEXT, '
      ' importecuota REAL, '
      ' codigo INTEGER, '
      ' razonsocial TEXT, '
      ' direccion TEXT, '
      ' importepagado REAL, '
      ' localidad TEXT, '
      ' tipocuota TEXT '
      ')',
   'update cuotas set importecuota = importecuota + 2 ',
   'update cuotas set importecuota = importecuota + 3',
   'update cuotas set importecuota = importecuota + 4',
   'update cuotas set importecuota = importecuota + 5 ',
   'update cuotas set importecuota = importecuota + 6 ',

];
  initDB() async {
    Directory documentDirectory = await getApplicationDocumentsDirectory();

    final path = join(documentDirectory.path, 'database12.db');
    return await openDatabase(path, version: migrationScripts.length  ,
        onCreate: (Database db, int version) async {
      migrationScripts.forEach((script) async => await db.execute(script));
    }, onUpgrade: (Database db, int oldVersion, int newVersion) async {

      for (var i = oldVersion  ; i < newVersion ; i++) {

        await db.execute(migrationScripts[i]);

      }

    });
  }

Con una sola lista, ya que el create solo ingresa por unica vez al crear el archivo, y esa primera vez el onUpgrade no se ejecuta porque oldversion no existe aun.

icanall10 commented 4 years ago

Working example https://github.com/icanall10/test/blob/master/flutter_sqlite_migrations.dart

Tomucha commented 4 years ago

Same issue. For now I'm using dependency as:

   git: https://github.com/DrLabx/sqflite_migration/

Thanks for that.

But as @marcelovbcfilho pointed out 9 months ago - this simply cannot work correctly ever. Please merge the pull request and release a new version.

rinatsolmano commented 4 years ago

Same issue. For now I'm using dependency as:

   git: https://github.com/DrLabx/sqflite_migration/

Thanks for that.

But as @marcelovbcfilho pointed out 9 months ago - this simply cannot work correctly ever. Please merge the pull request and release a new version.

You're welcome! I've sent an email to the maintainer, but haven't had any response yet. Hopefully he'll come to us soon.

esarbanis commented 4 years ago

Sorry for leaving this hanging for so long ... I had some personal and professional issues to sort out. I will dig deeper into this issue and will get back ASAP.