Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PRAGMA version in windows #1096

Open
luciano-cervantes opened this issue Apr 12, 2024 · 0 comments
Open

Update PRAGMA version in windows #1096

luciano-cervantes opened this issue Apr 12, 2024 · 0 comments

Comments

@luciano-cervantes
Copy link

luciano-cervantes commented Apr 12, 2024

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!;
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant