Skip to content

Update PRAGMA version in windows #1096

Open
@luciano-cervantes

Description

@luciano-cervantes

Hello, I'm facing a problem on Windows, when a new version of my application is launched with migrations to be executed, some clients run the migrations, that is, they update the tables, but the PRAGMA version does not update, and when opening the app again the system tries to run the migrations again, this causes some duplicate column errors or something like that. I would like to know if anyone else has experienced something similar and what the possible solution is.

My code:

FutureOr<void> _onUpgrade(Database db, int oldVersion, int newVersion) async {
    await db.transaction((txn) async {
      await SQFliteUtils.removeAllTriggers(txn);

      Batch batch = txn.batch();

      if (newVersion != oldVersion) {
        batch.delete('ncm_config');
      }

      if (oldVersion < 1) {
        batch.execute('''
          CREATE TABLE test1 (
            id_teste1 TEXT PRIMARY KEY NOT NULL,
            nome TEXT NOT NULL
          );
        ''');
     
         batch.execute('''
          CREATE TABLE test2 (
            id_teste2 TEXT PRIMARY KEY NOT NULL,
            nome TEXT NOT NULL
          );
        ''');
      }
      
     if (oldVersion < 2) {
        //Migrations version 2
      }

      enableTriggers(batch);

      await batch.commit(noResult: true);
    });
  }
Future<Database> getDb() async {
    if (_db == null) {
      await _lock.synchronized(() async {
        // Check again once entering the synchronized block
        if (_db == null) {
            _db = await openDatabase(
              await getPath(),
              version: _versionDb,
              onCreate: _onCreate,
              onConfigure: _onConfigureBeforeOpenDatabase,
              onOpen: _onOpen,
              onUpgrade: _onUpgrade,
            );
          
          await _onConfigureAfterOpenDatabase(_db);
        }
      });
    }

    return _db!;
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions