Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add destructive migration to database downgrade #217

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<Application>()
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<Application>()
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())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@ object SqlDataLayer {
"${Columns.COLUMN_TIMESTAMP} LONG, " +
"${Columns.COLUMN_TYPE} SMALLINT)"
}

fun getDeleteTableSql(tableName: String): String {
return "DROP TABLE $tableName"
}
}
}