Search in sources :

Example 6 with SQLiteUpdateTask

use of com.abubusoft.kripton.android.sqlite.SQLiteUpdateTask in project kripton by xcesco.

the class BindDummy02DataSource method onUpgrade.

/**
 * onUpgrade
 */
@Override
public void onUpgrade(SQLiteDatabase database, int previousVersion, int currentVersion) {
    // log section BEGIN
    if (this.logEnabled) {
        Logger.info("Update database '%s' from version %s to version %s", this.name, previousVersion, currentVersion);
    }
    // if we have a list of update task, try to execute them
    if (options.updateTasks != null) {
        List<SQLiteUpdateTask> tasks = buildTaskList(previousVersion, currentVersion);
        for (SQLiteUpdateTask task : tasks) {
            // log section BEGIN
            if (this.logEnabled) {
                Logger.info("Begin update database from version %s to %s", previousVersion, previousVersion + 1);
            }
            // log section END
            task.execute(database, previousVersion, previousVersion + 1);
            // log section BEGIN
            if (this.logEnabled) {
                Logger.info("End update database from version %s to %s", previousVersion, previousVersion + 1);
            }
            // log section END
            previousVersion++;
        }
    } else {
        // drop all tables
        SQLiteUpdateTaskHelper.dropTablesAndIndices(database);
        // log section BEGIN
        if (this.logEnabled) {
            Logger.info("DDL: %s", Bean02Table.CREATE_TABLE_SQL);
        }
        // log section END
        database.execSQL(Bean02Table.CREATE_TABLE_SQL);
    }
    if (options.databaseLifecycleHandler != null) {
        options.databaseLifecycleHandler.onUpdate(database, previousVersion, currentVersion, true);
    }
}
Also used : SQLiteUpdateTask(com.abubusoft.kripton.android.sqlite.SQLiteUpdateTask)

Example 7 with SQLiteUpdateTask

use of com.abubusoft.kripton.android.sqlite.SQLiteUpdateTask in project kripton by xcesco.

the class BindShortDataSource method onUpgrade.

/**
 * onUpgrade
 */
@Override
public void onUpgrade(SQLiteDatabase database, int previousVersion, int currentVersion) {
    // log section BEGIN
    if (this.logEnabled) {
        Logger.info("Update database '%s' from version %s to version %s", this.name, previousVersion, currentVersion);
    }
    // if we have a list of update task, try to execute them
    if (options.updateTasks != null) {
        List<SQLiteUpdateTask> tasks = buildTaskList(previousVersion, currentVersion);
        for (SQLiteUpdateTask task : tasks) {
            // log section BEGIN
            if (this.logEnabled) {
                Logger.info("Begin update database from version %s to %s", previousVersion, previousVersion + 1);
            }
            // log section END
            task.execute(database, previousVersion, previousVersion + 1);
            // log section BEGIN
            if (this.logEnabled) {
                Logger.info("End update database from version %s to %s", previousVersion, previousVersion + 1);
            }
            // log section END
            previousVersion++;
        }
    } else {
        // drop all tables
        SQLiteUpdateTaskHelper.dropTablesAndIndices(database);
        // log section BEGIN
        if (this.logEnabled) {
            Logger.info("DDL: %s", ShortBeanTable.CREATE_TABLE_SQL);
        }
        // log section END
        database.execSQL(ShortBeanTable.CREATE_TABLE_SQL);
    }
    if (options.databaseLifecycleHandler != null) {
        options.databaseLifecycleHandler.onUpdate(database, previousVersion, currentVersion, true);
    }
}
Also used : SQLiteUpdateTask(com.abubusoft.kripton.android.sqlite.SQLiteUpdateTask)

Example 8 with SQLiteUpdateTask

use of com.abubusoft.kripton.android.sqlite.SQLiteUpdateTask in project kripton by xcesco.

the class BindBeanDataSource method onUpgrade.

/**
 * onUpgrade
 */
@Override
public void onUpgrade(SQLiteDatabase database, int previousVersion, int currentVersion) {
    // log section BEGIN
    if (this.logEnabled) {
        Logger.info("Update database '%s' from version %s to version %s", this.name, previousVersion, currentVersion);
    }
    // if we have a list of update task, try to execute them
    if (options.updateTasks != null) {
        List<SQLiteUpdateTask> tasks = buildTaskList(previousVersion, currentVersion);
        for (SQLiteUpdateTask task : tasks) {
            // log section BEGIN
            if (this.logEnabled) {
                Logger.info("Begin update database from version %s to %s", previousVersion, previousVersion + 1);
            }
            // log section END
            task.execute(database, previousVersion, previousVersion + 1);
            // log section BEGIN
            if (this.logEnabled) {
                Logger.info("End update database from version %s to %s", previousVersion, previousVersion + 1);
            }
            // log section END
            previousVersion++;
        }
    } else {
        // drop all tables
        SQLiteUpdateTaskHelper.dropTablesAndIndices(database);
        // log section BEGIN
        if (this.logEnabled) {
            Logger.info("DDL: %s", BeanTable.CREATE_TABLE_SQL);
        }
        // log section END
        database.execSQL(BeanTable.CREATE_TABLE_SQL);
    }
    if (options.databaseLifecycleHandler != null) {
        options.databaseLifecycleHandler.onUpdate(database, previousVersion, currentVersion, true);
    }
}
Also used : SQLiteUpdateTask(com.abubusoft.kripton.android.sqlite.SQLiteUpdateTask)

Example 9 with SQLiteUpdateTask

use of com.abubusoft.kripton.android.sqlite.SQLiteUpdateTask in project kripton by xcesco.

the class SQLiteUpdateTestDatabase method findTask.

List<SQLiteUpdateTask> findTask(int previousVersion, int currentVersion) {
    List<SQLiteUpdateTask> result = new ArrayList<>();
    final One<Integer> ref = new One<>(null);
    for (int i = previousVersion; i < currentVersion; i++) {
        ref.value0 = i;
        SQLiteUpdateTask t = null;
        for (Pair<Integer, ? extends SQLiteUpdateTask> item : updateTasks) {
            if (item.value0 - 1 == ref.value0) {
                t = item.value1;
                break;
            }
        }
        if (t != null) {
            result.add(t);
        }
    }
    return result;
}
Also used : One(com.abubusoft.kripton.common.One) ArrayList(java.util.ArrayList) SQLiteUpdateTask(com.abubusoft.kripton.android.sqlite.SQLiteUpdateTask)

Example 10 with SQLiteUpdateTask

use of com.abubusoft.kripton.android.sqlite.SQLiteUpdateTask in project kripton by xcesco.

the class TestSchemaUpdater method testUpdateWithHelper.

@Test
public void testUpdateWithHelper() throws FileNotFoundException {
    final FileInputStream stream = new FileInputStream("schemas/school_schema_2.sql");
    SQLiteUpdateTestDatabase.builder(1, new FileInputStream("schemas/school_schema_1.sql")).addVersionUpdateTask(2, new SQLiteUpdateTask() {

        @Override
        public void execute(SQLiteDatabase database, int previousVersion, int currentVersion) {
            SQLiteSchemaVerifierHelper.renameAllTablesWithPrefix(database, "tmp_");
            SQLiteSchemaVerifierHelper.executeSQL(database, stream);
            SQLiteSchemaVerifierHelper.dropTablesWithPrefix(database, "tmp_");
        }
    }).build().updateAndVerify(2, new FileInputStream("schemas/school_schema_2.sql"));
    log("finish");
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) FileInputStream(java.io.FileInputStream) SQLiteUpdateTask(com.abubusoft.kripton.android.sqlite.SQLiteUpdateTask) Test(org.junit.Test) BaseAndroidTest(base.BaseAndroidTest)

Aggregations

SQLiteUpdateTask (com.abubusoft.kripton.android.sqlite.SQLiteUpdateTask)123 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)4 BaseAndroidTest (base.BaseAndroidTest)3 Test (org.junit.Test)3 FileInputStream (java.io.FileInputStream)2 ArrayList (java.util.ArrayList)2 SQLiteOpenHelper (android.database.sqlite.SQLiteOpenHelper)1 DataSourceOptions (com.abubusoft.kripton.android.sqlite.DataSourceOptions)1 DatabaseLifecycleHandler (com.abubusoft.kripton.android.sqlite.DatabaseLifecycleHandler)1 One (com.abubusoft.kripton.common.One)1 KriptonRuntimeException (com.abubusoft.kripton.exception.KriptonRuntimeException)1 List (java.util.List)1