How to use orm sugars for migration

I have an existing Android app that prepackages a database called app.db. The app.db file is located in the src / main / res / raw file. When the app starts, it is copied to data / data / app / databases.

The current db version is 2 which was installed using SQLiteOpenHelper. I don't want to bind this db to any model object but only add or remove lines from it by running some dmls.

The manifest looks like this:

<application
   android:label="@string/app_name"
   android:icon="@drawable/launcher"
   android:name="MyApplication">
   ...
 <meta-data android:name="DATABASE" android:value="app.db" />
 <meta-data android:name="VERSION" android:value="3" />
 <meta-data android:name="QUERY_LOG" android:value="true" />
</application>

      

The MyAppplication class extends SugarApp. I created a folder called sugar_upgrades in assets and created a file called 3.sql in it. This file contains script like:

DELETE FROM images WHERE id = 53;

      

But after starting the application, this script never runs. I think the problem is that Sugar expects the db to be present in the / data folder. But at the beginning of the application it is present in res / raw and therefore it cannot pick it up. Any way I can achieve this?

+3


source to share


1 answer


I decided to write an answer rather than a comment because I need more help with formatting. I will update this post as needed.

First of all, you should look at the SchemaGenerator

class here . You will notice that there are several journal operators that can help narrow down the problem. Here's one example:

Log.i("Sugar", "not a sugar script. ignored." + file);

      

So, I suggest you search the logcat output for the "Sugar" tag and see if there is any useful information that is shown.

The second place that would be useful for debugging is the class SugarDb

found here . This class extends SQLiteOpenHelper

and implements the method onUpgrade

:



@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
    schemaGenerator.doUpgrade(sqLiteDatabase, oldVersion, newVersion);
}

      

It might be helpful to clone Sugar source code and compile the project manually and add it as a dependency. Then you will be able to test your SQL query directly in the SQLite database like this:

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
    schemaGenerator.doUpgrade(sqLiteDatabase, oldVersion, newVersion);

    sqLiteDatabase.rawQuery("DELETE FROM images WHERE id = ?", new String[]{"53"});
}

      

Finally, if you find that the data is not available, as you suspect in the / data folder at startup, you will have to modify the Sugar source code to change the data after it is loaded.

0


source







All Articles