Skip to content

Commit

Permalink
[DB] move pragma for foreign keys into the onconfigure method of the …
Browse files Browse the repository at this point in the history
…db helper. #1652
  • Loading branch information
agrosner committed Feb 17, 2021
1 parent b763a77 commit 0f7ba59
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 24 deletions.
4 changes: 4 additions & 0 deletions lib/src/main/kotlin/com/dbflow5/config/DBFlowDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -500,5 +500,9 @@ abstract class DBFlowDatabase : DatabaseWrapper {
override fun onDowngrade(databaseWrapper: DatabaseWrapper, oldVersion: Int, newVersion: Int) {
callback?.onDowngrade(databaseWrapper, oldVersion, newVersion)
}

override fun onConfigure(db: DatabaseWrapper) {
callback?.onConfigure(db)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ open class AndroidSQLiteOpenHelper(
databaseHelperDelegate.setDatabaseHelperListener(callback)
}

override fun onConfigure(db: SQLiteDatabase) {
databaseHelperDelegate.onConfigure(AndroidDatabase.from(db))
}

override fun onCreate(db: SQLiteDatabase) {
databaseHelperDelegate.onCreate(AndroidDatabase.from(db))
}
Expand Down Expand Up @@ -122,6 +126,10 @@ open class AndroidSQLiteOpenHelper(

override fun setDatabaseListener(callback: DatabaseCallback?) {}

override fun onConfigure(db: SQLiteDatabase) {
databaseHelper.onConfigure(AndroidDatabase.from(db))
}

override fun onCreate(db: SQLiteDatabase) {
databaseHelper.onCreate(AndroidDatabase.from(db))
}
Expand Down
6 changes: 6 additions & 0 deletions lib/src/main/kotlin/com/dbflow5/database/DatabaseCallback.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ interface DatabaseCallback {
* @param newVersion The new lower version.
*/
fun onDowngrade(databaseWrapper: DatabaseWrapper, oldVersion: Int, newVersion: Int)

/**
* Called when DB connection is being configured. Useful for checking foreign key support or enabling
* write-ahead-logging.
*/
fun onConfigure(db: DatabaseWrapper)
}
44 changes: 21 additions & 23 deletions lib/src/main/kotlin/com/dbflow5/database/DatabaseHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ open class DatabaseHelper(private val migrationFileHelper: MigrationFileHelper,
private val dbMigrationPath
get() = "$MIGRATION_PATH/${databaseDefinition.databaseName}"

open fun onCreate(db: DatabaseWrapper) {
open fun onConfigure(db: DatabaseWrapper) {
checkForeignKeySupport(db)
}

open fun onCreate(db: DatabaseWrapper) {
// table creations done first to get tables in db.
executeTableCreations(db)

Expand All @@ -30,8 +32,6 @@ open class DatabaseHelper(private val migrationFileHelper: MigrationFileHelper,
}

open fun onUpgrade(db: DatabaseWrapper, oldVersion: Int, newVersion: Int) {
checkForeignKeySupport(db)

// create new tables if not previously created
executeTableCreations(db)

Expand All @@ -43,11 +43,9 @@ open class DatabaseHelper(private val migrationFileHelper: MigrationFileHelper,
}

open fun onOpen(db: DatabaseWrapper) {
checkForeignKeySupport(db)
}

open fun onDowngrade(db: DatabaseWrapper, oldVersion: Int, newVersion: Int) {
checkForeignKeySupport(db)
}

/**
Expand All @@ -63,15 +61,15 @@ open class DatabaseHelper(private val migrationFileHelper: MigrationFileHelper,
protected fun executeTableCreations(database: DatabaseWrapper) {
database.executeTransaction {
databaseDefinition.modelAdapters
.asSequence()
.filter { it.createWithDatabase() }
.forEach {
try {
it.createIfNotExists(this)
} catch (e: SQLiteException) {
FlowLog.logError(e)
}
.asSequence()
.filter { it.createWithDatabase() }
.forEach {
try {
it.createIfNotExists(this)
} catch (e: SQLiteException) {
FlowLog.logError(e)
}
}
}
}

Expand All @@ -81,16 +79,16 @@ open class DatabaseHelper(private val migrationFileHelper: MigrationFileHelper,
protected fun executeViewCreations(database: DatabaseWrapper) {
database.executeTransaction {
databaseDefinition.modelViewAdapters
.asSequence()
.filter { it.createWithDatabase() }
.forEach {
try {
it.createIfNotExists(this)
} catch (e: SQLiteException) {
FlowLog.logError(e)
}

.asSequence()
.filter { it.createWithDatabase() }
.forEach {
try {
it.createIfNotExists(this)
} catch (e: SQLiteException) {
FlowLog.logError(e)
}

}
}
}

Expand All @@ -100,7 +98,7 @@ open class DatabaseHelper(private val migrationFileHelper: MigrationFileHelper,
// will try migrations file or execute migrations from code
try {
val files: List<String> = migrationFileHelper.getListFiles(dbMigrationPath)
.sortedWith(NaturalOrderComparator())
.sortedWith(NaturalOrderComparator())

val migrationFileMap = hashMapOf<Int, MutableList<String>>()
for (file in files) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ class DatabaseHelperDelegate(
this.databaseCallback = databaseCallback
}

override fun onConfigure(db: DatabaseWrapper) {
databaseCallback?.onConfigure(db)
super.onConfigure(db)
}

override fun onCreate(db: DatabaseWrapper) {
databaseCallback?.onCreate(db)
super.onCreate(db)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ abstract class SQLCipherOpenHelper(
delegate.setDatabaseHelperListener(callback)
}

override fun onConfigure(db: SQLiteDatabase) {
delegate.onConfigure(SQLCipherDatabase.from(db))
}

override fun onCreate(db: SQLiteDatabase) {
delegate.onCreate(SQLCipherDatabase.from(db))
}
Expand Down Expand Up @@ -136,6 +140,10 @@ abstract class SQLCipherOpenHelper(

override fun setDatabaseListener(callback: DatabaseCallback?) = Unit

override fun onConfigure(db: SQLiteDatabase) {
databaseHelper.onConfigure(SQLCipherDatabase.from(db))
}

override fun onCreate(db: SQLiteDatabase) {
databaseHelper.onCreate(SQLCipherDatabase.from(db))
}
Expand Down
16 changes: 15 additions & 1 deletion tests/src/androidTest/java/com/dbflow5/TestDatabase.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.dbflow5

import com.dbflow5.annotation.Database
import com.dbflow5.annotation.ForeignKey
import com.dbflow5.annotation.Migration
import com.dbflow5.annotation.PrimaryKey
import com.dbflow5.annotation.Table
import com.dbflow5.config.DBFlowDatabase
import com.dbflow5.database.DatabaseWrapper
import com.dbflow5.migration.BaseMigration
Expand All @@ -23,4 +26,15 @@ abstract class TestDatabase : DBFlowDatabase() {

}
}
}
}

@Database(version = 1, foreignKeyConstraintsEnforced = true)
abstract class TestForeignKeyDatabase : DBFlowDatabase() {

@Table(database = TestForeignKeyDatabase::class)
data class SimpleModel(@PrimaryKey var name: String = "")

@Table(database = TestForeignKeyDatabase::class)
data class SimpleForeignModel(@PrimaryKey var id: Int = 0,
@ForeignKey var model: SimpleModel? = null)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.dbflow5

import com.dbflow5.config.database
import org.junit.Test

class TestForeignKeyDatabaseTest : BaseUnitTest() {

@Test
fun verifyDB() {
database<TestForeignKeyDatabase> { db ->
val enabled = longForQuery(db, "PRAGMA foreign_keys;")
assert(enabled == 1L)
}
}
}

0 comments on commit 0f7ba59

Please sign in to comment.