jeudi 25 mars 2021

Testing Flutter local sqflite database onUpgrade and no data shown after app update

I want to update an app that has been already released on Google Play, but beforehand I wanted to test and see if the local sqlite database would preserve all it's data after the update as it stores user added contents.

First I added some contents to the database on the older version and then updated it.When i updated the app through Internal testing it displayed no data as if it would have created an empty fresh database.

  • Is it possible that the db path has changed? How to check it?
  • Could it be that the older Database has been overwritten?

Is there something else that I should be on the look out for? As I have only added few more columns to the database and not changed any code regarding database initialization and databasepath.

used packages:sqflite: 2.0.0+3 and path: 1.8.0

Code snippet from the dart file for the database :

    import 'package:path/path.dart';
    import 'package:sqflite/sqflite.dart';
    import 'package:sqflite/sqlite_api.dart';
  
    class DatabaseHelper{

        /// ------- name variables ----------------
        static final _dbName = 'userTasks.db';
        static final _dbVersion = 2;                
        static final _tableName = 'myTasks';

        /// ------- table column Names -------------
        static final columnID = 'id';
        static final columnName = 'name';
        static final columnContent = 'content';
        static final columnType = 'type';
        static final columnDescription = 'description';
        static final columnFavorite = 'favorite';

        /// -------- DataBase initialization ---------
        DatabaseHelper._privateConstructor();
        static final DatabaseHelper instance = DatabaseHelper._privateConstructor();

        static Database _database;
        Future<Database> get database async{
          if(_database !=null)  return _database;

          _database = await _initiateDatabase();
          return _database;
        }

        _initiateDatabase () async{

          Directory directory = await getApplicationDocumentsDirectory();
          String path = join(directory.path,_dbName);
          return await openDatabase(path, version: _dbVersion, onCreate: _onCreate, onUpgrade: _onUpgrade);

        }

        /// ---------------    _onCreate() getting called on fresh install(no previous DB)  -----------------

        Future _onCreate(Database db, int version){
          db.execute(
              '''
              CREATE TABLE $_tableName( 
              $columnID INTEGER PRIMARY KEY,
              $columnName TEXT,
              $columnContent TEXT NOT NULL,
              $columnType TEXT NOT NULL,
              $columnDescription TEXT NOT NULL,
              $columnFavorite INTEGER)
              '''
          );
        }

        /// ---------------    _onUpgrade() getting called on _dbVersion versionChange  -----------------

        Future _onUpgrade(Database db, int oldVersion, int newVersion) {
          db.execute(''' ALTER TABLE $_tableName ADD COLUMN $columnDescription TEXT''');
          db.execute(''' ALTER TABLE $_tableName ADD COLUMN $columnFavorite INTEGER DEFAULT 0 ''');
        }


Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire