Skip to content

Commit

Permalink
Add check for temporary database path (#4662)
Browse files Browse the repository at this point in the history
  • Loading branch information
05nelsonm authored Oct 18, 2023
1 parent 6c57055 commit c7c941c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ abstract class EphemeralTest {
TEMPORARY,
}

protected open val skipNamed: Boolean = false

protected val schema = object : SqlSchema<QueryResult.Value<Unit>> {
override val version: Long = 1

Expand Down Expand Up @@ -86,6 +88,8 @@ abstract class EphemeralTest {

@Test
fun namedCreatesSharedDatabase() {
if (skipNamed) return

val data1 = TestData(1, "val1")
val driver1 = setupDatabase(Type.NAMED)
driver1.insertTestData(data1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class NativeSqliteDriver(
private val lock = PoolLock(reentrant = true)

init {
if (databaseManager.configuration.inMemory) {
if (databaseManager.configuration.isEphemeral) {
// Single connection for transactions
transactionPool = Pool(1) {
ThreadConnection(databaseManager.createMultiThreadedConnection()) { _ ->
Expand Down Expand Up @@ -413,3 +413,7 @@ internal class ThreadConnection(
}
}
}

private inline val DatabaseConfiguration.isEphemeral: Boolean get() {
return inMemory || (name?.isEmpty() == true && extendedConfig.basePath?.isEmpty() == true)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.squareup.sqldelight.drivers.native

import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.native.NativeSqliteDriver
import co.touchlab.sqliter.DatabaseConfiguration
import com.squareup.sqldelight.driver.test.EphemeralTest

class NativeEphemeralTest : EphemeralTest() {

// TODO: Issue #3241
override val skipNamed: Boolean = true

override fun setupDatabase(type: Type): SqlDriver {
return NativeSqliteDriver(
schema = schema,
name = "replaceme",
onConfiguration = { configuration ->
configuration.copy(
name = when (type) {
Type.IN_MEMORY -> null
Type.NAMED -> "memdb1"
Type.TEMPORARY -> ""
},
inMemory = when (type) {
Type.IN_MEMORY -> true
Type.NAMED -> true
Type.TEMPORARY -> false
},
extendedConfig = DatabaseConfiguration.Extended(
basePath = when (type) {
Type.IN_MEMORY -> null
Type.NAMED -> null
Type.TEMPORARY -> ""
},
),
)
},
)
}
}

0 comments on commit c7c941c

Please sign in to comment.