diff --git a/tealiumlibrary/src/androidTest/java/com/tealium/core/persistence/DatabaseMigrationTests.kt b/tealiumlibrary/src/androidTest/java/com/tealium/core/persistence/DatabaseMigrationTests.kt new file mode 100644 index 00000000..c1341fca --- /dev/null +++ b/tealiumlibrary/src/androidTest/java/com/tealium/core/persistence/DatabaseMigrationTests.kt @@ -0,0 +1,51 @@ +package com.tealium.core.persistence + +import android.app.Application +import android.database.sqlite.SQLiteDatabase +import androidx.test.core.app.ApplicationProvider +import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.tealium.core.Environment +import com.tealium.core.TealiumConfig +import org.junit.Assert.* +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class DatabaseMigrationTests { + + @Test + fun testUpgrade_1_2() { + val context = ApplicationProvider.getApplicationContext() + val config = TealiumConfig(context, "test", "profile", Environment.QA) + + val databaseHelper = DatabaseHelper(config, databaseVersion = 1) + val db = SQLiteDatabase.create(null) + + databaseHelper.onCreate(db) + + databaseHelper.onUpgrade(db, 1, 2) + + // Verify all 3 tables created + db.rawQuery("SELECT * FROM datalayer", emptyArray()) + db.rawQuery("SELECT * FROM dispatches", emptyArray()) + db.rawQuery("SELECT * FROM visitors", emptyArray()) + } + + @Test + fun testDowngrade_3_2() { + val context = ApplicationProvider.getApplicationContext() + val config = TealiumConfig(context, "test", "profile", Environment.QA) + + val databaseHelper = DatabaseHelper(config, databaseVersion = 3) + val db = SQLiteDatabase.create(null) + + databaseHelper.onCreate(db) + + databaseHelper.onDowngrade(db, 3, 2) + + // Verify all 3 tables created + db.rawQuery("SELECT * FROM datalayer", emptyArray()) + db.rawQuery("SELECT * FROM dispatches", emptyArray()) + db.rawQuery("SELECT * FROM visitors", emptyArray()) + } +} \ No newline at end of file diff --git a/tealiumlibrary/src/main/java/com/tealium/core/persistence/DatabaseHelper.kt b/tealiumlibrary/src/main/java/com/tealium/core/persistence/DatabaseHelper.kt index fc29bbcf..313748a5 100644 --- a/tealiumlibrary/src/main/java/com/tealium/core/persistence/DatabaseHelper.kt +++ b/tealiumlibrary/src/main/java/com/tealium/core/persistence/DatabaseHelper.kt @@ -10,14 +10,17 @@ import java.io.File * the [config] parameter. * @param config - TealiumConfig item with */ -internal class DatabaseHelper(config: TealiumConfig, databaseName: String? = databaseName(config)) : - SQLiteOpenHelper(config.application.applicationContext, databaseName, null, DATABASE_VERSION) { +internal class DatabaseHelper( + config: TealiumConfig, + databaseName: String? = databaseName(config), + private val databaseVersion: Int = DATABASE_VERSION +) : SQLiteOpenHelper(config.application.applicationContext, databaseName, null, databaseVersion) { override fun onCreate(db: SQLiteDatabase?) { db?.execSQL(SqlDataLayer.Sql.getCreateTableSql("datalayer")) db?.execSQL(SqlDataLayer.Sql.getCreateTableSql("dispatches")) // apply all necessary upgrades - onUpgrade(db, 1, DATABASE_VERSION) + onUpgrade(db, 1, databaseVersion) } override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) { @@ -28,6 +31,14 @@ internal class DatabaseHelper(config: TealiumConfig, databaseName: String? = dat } } + override fun onDowngrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) { + db?.execSQL(SqlDataLayer.Sql.getDeleteTableSql("datalayer")) + db?.execSQL(SqlDataLayer.Sql.getDeleteTableSql("dispatches")) + db?.execSQL(SqlDataLayer.Sql.getDeleteTableSql("visitors")) + + onCreate(db) + } + companion object { const val DATABASE_VERSION = 2 diff --git a/tealiumlibrary/src/main/java/com/tealium/core/persistence/SqlDataLayer.kt b/tealiumlibrary/src/main/java/com/tealium/core/persistence/SqlDataLayer.kt index d1d1cc44..12cb47ea 100644 --- a/tealiumlibrary/src/main/java/com/tealium/core/persistence/SqlDataLayer.kt +++ b/tealiumlibrary/src/main/java/com/tealium/core/persistence/SqlDataLayer.kt @@ -21,5 +21,9 @@ object SqlDataLayer { "${Columns.COLUMN_TIMESTAMP} LONG, " + "${Columns.COLUMN_TYPE} SMALLINT)" } + + fun getDeleteTableSql(tableName: String): String { + return "DROP TABLE $tableName" + } } } \ No newline at end of file