1

I want to write a migration script for dbflow (v4+).. I want to delete all the previous tables and then build everything from scratch. Currently only way I found we can do this is like:

@Migration(version = 2, database = AppDatabase.class)
public static class Migration2 extends BaseMigration {

    @Override
    public void migrate(DatabaseWrapper database) {

        ArrayList<ModelAdapter> modelAdapters = new ArrayList<>();
        // Old tables
        modelAdapters.add(FlowManager.getModelAdapter(Events.class));
        modelAdapters.add(FlowManager.getModelAdapter(Notes.class));
        // New table
        modelAdapters.add(FlowManager.getModelAdapter(User.class));

        for (ModelAdapter modelAdapter : modelAdapters) {
            database.execSQL("DROP TABLE IF EXISTS " + modelAdapter.getTableName());
            database.execSQL(modelAdapter.getCreationQuery());
        }
    }
}

I don't like this approach though as we have to manually mention all the old and new tables. Is there a better way to do it. My db has changed significantly and therefore dropping all tables seems to be the only option.

Ubaier Bhat
  • 854
  • 13
  • 33

1 Answers1

0

You can use a property file and write your drop statements in that file. Then, Create a class that use the below function to dynamically load the file and execute all the statements in it. You can also use another file to dynamically create all tables in your DB.

public void createTable() {
        try {
            SQLiteDatabase sqLiteDBConn = SQLLiteDBUtility.getHelper(context).getWritableDatabase();
            InputStream inputStream = assetManager
                    .open("deletetable.properties");
            Properties properties = new Properties();
            properties.load(inputStream);
            Enumeration<?> propertyKeys = properties.propertyNames();
            while (propertyKeys.hasMoreElements()) {
                String key = (String) propertyKeys.nextElement();
                String strDeleteStatement = properties
                        .getProperty(key);
                sqLiteDBConn.execSQL(strCreateStatement);
            }

        } catch (Exception e) {
            Log.e("Exception", e.getMessage(), e);
        }
    }

SAMPLE PROPERTY FILE PLACED IN ASSETS CONTAINING CREATE TABLE COMMANDS:

client_data.create_table = create table if not exists client_data("db_name"  TEXT PRIMARY KEY NOT NULL, "db_password" TEXT  DEFAULT 'F')

For Database Upgradation you can create a class and then extend it to SQLiteOpenHelper and then ovveride the OnUpgrade method and execute the statements for your new DB version. Example:

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        SQLDBUpgradeUtils sqlStatementsUtil = new SQLDBUpgradeUtils ();
        sqlStatementsUtil.executeStatement(db, oldVersion);
    }

The in SQLDBUpgradeUtils you can use something like this:

    public void executeStatement(SQLiteDatabase db, int oldVersion) {
           DataLoaderUtility dataLoaderUtility = new DataLoaderUtility();
           if(oldversion < PresentDBVersion){
            String alterTable = "ALTER TABLE client_data ADD location TEXT"
}
        }
Abhinash
  • 368
  • 2
  • 13