tekartik / sqflite

SQLite flutter plugin
BSD 2-Clause "Simplified" License
2.86k stars 522 forks source link

[iOS] attempt to write a readonly database #1124

Open DjordjeMancic97 opened 1 month ago

DjordjeMancic97 commented 1 month ago

Hello,

I am experiencing weird issue on iOS when trying to create new database after deleting the previous.

Here is the flow:

Database creation

  db = await openDatabase(
      path,
      version: databaseVersion,
      onConfigure: (db) async => await db.execute('PRAGMA foreign_keys = ON'),
      onCreate: (Database db, int version) async {
        await loadDatabaseSchema(db);
      },
    );

After this we have a working database then we close and delete it:

      await db.close();
      final directory = await getApplicationDocumentsDirectory();
      final path = join(directory.path, databaseName);
      await deleteDatabase(path);

All good, db file is gone

However, after this we again run the openDatabase like in first step, database file is created and all tables are there. However at every attempt of writing some data the error message will appear:

DatabaseException(Error Domain=FMDatabase Code=8 "attempt to write a readonly database" UserInfo={NSLocalizedDescription=attempt to write a readonly database})

This happens only on iOS. Using latest version of sqflite and Flutter 3.22.2

alextekartik commented 1 month ago

Thanks for the report. Indeed that's bad. Can you confirm that the database is not used anywhere else in the mean time?

Can you make a quick test by adding a small delay (let's one second) before opening again to see if the same issue happens?

I won't be able to investigate much more in the next 2 weeks, sorry.

DjordjeMancic97 commented 1 month ago

Hello,

Yeah database is not used anywhere else, also delay is not solution especially because the actions of deleting the database and creating it again are pretty separated and happen at different places so there is just no way these 2 actions are performed within 1 second.

alextekartik commented 1 month ago

Ok, also can you make sure the database folder exists before creating the database.