Skip to content

Commit

Permalink
Centralized database configuration
Browse files Browse the repository at this point in the history
This creates a single source of truth for configuring the main database and any tests.
  • Loading branch information
JaniruTEC committed Jun 2, 2024
1 parent d65e674 commit 01a8bd6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,9 @@ private fun createVersion0Database(context: Context, databaseName: String) {
val config = SupportSQLiteOpenHelper.Configuration.builder(context) //
.name(databaseName) //
.callback(object : SupportSQLiteOpenHelper.Callback(1) {
override fun onConfigure(db: SupportSQLiteDatabase) {
db.disableWriteAheadLogging()
db.setForeignKeyConstraintsEnabled(true)
}
override fun onConfigure(db: SupportSQLiteDatabase) = db.applyDefaultConfiguration( //
writeAheadLoggingEnabled = false //
)

override fun onCreate(db: SupportSQLiteDatabase) = throw InterruptCreationException()
override fun onUpgrade(db: SupportSQLiteDatabase, oldVersion: Int, newVersion: Int) = throw IllegalStateException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,10 @@ class UpgradeDatabaseTest {
templateDbFile.copyTo(dbFile)
}

//This needs to stay in sync with changes to DatabaseOpenHelperFactory/PatchedCallback
db = SupportSQLiteOpenHelper.Configuration(context, TEST_DB, object : SupportSQLiteOpenHelper.Callback(LATEST_LEGACY_MIGRATION) {
override fun onConfigure(db: SupportSQLiteDatabase) {
db.disableWriteAheadLogging()
db.setForeignKeyConstraintsEnabled(true)
}
override fun onConfigure(db: SupportSQLiteDatabase) = db.applyDefaultConfiguration( //
writeAheadLoggingEnabled = false //
)

override fun onCreate(db: SupportSQLiteDatabase) {
fail("Database should not be created, but copied from template")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,13 @@ fun Cursor?.stringify(): String {

private fun <T> Array<T>.uniqueToSet(): Set<T> = toSet().also {
require(this.size == it.size) { "Array contained ${this.size - it.size} duplicate elements." }
}

fun SupportSQLiteDatabase.applyDefaultConfiguration(writeAheadLoggingEnabled: Boolean?) {
when (writeAheadLoggingEnabled) {
true -> enableWriteAheadLogging()
false -> disableWriteAheadLogging()
null -> {}
}
setForeignKeyConstraintsEnabled(true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ private class PatchedCallback(

override fun onConfigure(db: SupportSQLiteDatabase) {
LOG.d("Called onConfigure for \"${db.path}\"@${db.version}")
db.setForeignKeyConstraintsEnabled(true)
db.applyDefaultConfiguration( //
writeAheadLoggingEnabled = null //WAL is handled by Room
)
//
delegateCallback.onConfigure(db)
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.sqlite.db.SupportSQLiteDatabase
import androidx.sqlite.db.SupportSQLiteOpenHelper
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
import org.cryptomator.data.db.DATABASE_NAME
import org.cryptomator.data.db.applyDefaultConfiguration
import org.cryptomator.data.db.migrations.legacy.Upgrade0To1
import org.cryptomator.util.ThreadUtil
import org.cryptomator.util.named
Expand Down Expand Up @@ -49,10 +50,9 @@ class DbTemplateModule {
return SupportSQLiteOpenHelper.Configuration.builder(templateDatabaseContext) //
.name(DATABASE_NAME) //
.callback(object : SupportSQLiteOpenHelper.Callback(1) {
override fun onConfigure(db: SupportSQLiteDatabase) {
db.disableWriteAheadLogging()
db.setForeignKeyConstraintsEnabled(true)
}
override fun onConfigure(db: SupportSQLiteDatabase) = db.applyDefaultConfiguration( //
writeAheadLoggingEnabled = false //
)

override fun onCreate(db: SupportSQLiteDatabase) {
Upgrade0To1().migrate(db)
Expand Down

0 comments on commit 01a8bd6

Please sign in to comment.