Skip to content

Commit 286784f

Browse files
authored
Merge pull request #69 from touchlab/kpg/no_version_check
Open db without version check
2 parents 617b629 + 75d5c75 commit 286784f

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
kotlin.code.style=official
22

33
GROUP=co.touchlab
4-
VERSION_NAME=1.1.0
4+
VERSION_NAME=1.1.1
55
KOTLIN_VERSION=1.6.20-RC2
66

77
kotlin.native.ignoreDisabledTargets=true

sqliter-driver/src/nativeCommonMain/kotlin/co/touchlab/sqliter/DatabaseConfiguration.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ package co.touchlab.sqliter
1818

1919
import co.touchlab.sqliter.interop.Logger
2020

21+
/**
22+
* The database manager will skip version checks, create, and update when this is set. This is useful if you
23+
* need to do some kind of operation on a db without initializing it. For example, converting a clear text db
24+
* to an encrypted db.
25+
*
26+
* Using this value is a bit of a hack. The next major version will likely include a refactor of config.
27+
*
28+
* User version is usually positive, but there are no enforced restrictions from the sqlite side. Using an "uncommon"
29+
* negative number in case somebody uses negatives for some reason.
30+
*/
31+
const val NO_VERSION_CHECK = -50_001
32+
2133
data class DatabaseConfiguration(
2234
val name: String?,
2335
val version: Int,

sqliter-driver/src/nativeCommonMain/kotlin/co/touchlab/sqliter/native/NativeDatabaseManager.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ class NativeDatabaseManager(private val path:String,
8787
conn.updateJournalMode(configuration.journalMode)
8888

8989
try {
90-
conn.migrateIfNeeded(configuration.create, configuration.upgrade, configuration.version)
90+
val version = configuration.version
91+
if(version != NO_VERSION_CHECK)
92+
conn.migrateIfNeeded(configuration.create, configuration.upgrade, version)
9193
} catch (e: Exception) {
9294

9395
// If this failed, we have to close the connection or we will end up leaking it.

sqliter-driver/src/nativeCommonTest/kotlin/co/touchlab/sqliter/DatabaseConfigurationTest.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,18 @@ class DatabaseConfigurationTest : BaseDatabaseTest(){
165165
runFkTest("fkon", true)
166166
}
167167

168+
@Test
169+
fun noVersionTest(){
170+
val conf = DatabaseConfiguration(
171+
name = TEST_DB_NAME,
172+
inMemory = true,
173+
version = NO_VERSION_CHECK,
174+
create = { throw IllegalStateException("Shouldn't be here") })
175+
val manager = createDatabaseManager(conf)
176+
val conn = manager.createMultiThreadedConnection()
177+
assertEquals(conn.getVersion(), 0)
178+
}
179+
168180
private fun runFkTest(dbname: String, enableFK: Boolean){
169181
var bookId = 1
170182
fun makeBookWithoutTransaction(conn: DatabaseConnection) =

0 commit comments

Comments
 (0)