Skip to content

Commit 40cdd10

Browse files
committed
Set temp_store_directory on Android
1 parent 568c3b1 commit 40cdd10

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

core-tests-android/src/androidTest/java/com/powersync/AndroidDatabaseTest.kt

+6
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,10 @@ class AndroidDatabaseTest {
222222
// The exception messages differ slightly between drivers
223223
assertEquals(exception.message!!.contains("write a readonly database"), true)
224224
}
225+
226+
@Test
227+
fun canCreateTemporaryTable() = runTest {
228+
database.execute("PRAGMA temp_store = 1;") // Store temporary data as files
229+
database.execute("CREATE TEMP TABLE my_tbl (content ANY) STRICT;")
230+
}
225231
}

core/src/androidMain/kotlin/com/powersync/DatabaseDriverFactory.android.kt

+18-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.powersync.db.internal.InternalSchema
77
import com.powersync.db.migrateDriver
88
import kotlinx.coroutines.CoroutineScope
99
import org.sqlite.SQLiteCommitListener
10+
import java.util.concurrent.atomic.AtomicBoolean
1011

1112
@Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING")
1213
public actual class DatabaseDriverFactory(
@@ -27,10 +28,22 @@ public actual class DatabaseDriverFactory(
2728
context.getDatabasePath(dbFilename)
2829
}
2930

31+
val properties = buildDefaultWalProperties(readOnly = readOnly)
32+
val isFirst = IS_FIRST_CONNECTION.getAndSet(false)
33+
if (isFirst) {
34+
// Make sure the temp_store_directory points towards a temporary directory we actually
35+
// have access to. Due to sandboxing, the default /tmp/ is inaccessible.
36+
// The temp_store_directory pragma is deprecated and not thread-safe, so we only set it
37+
// on the first connection (it sets a global field and will affect every connection
38+
// opened).
39+
val escapedPath = context.cacheDir.absolutePath.replace("\"", "\"\"")
40+
properties.setProperty("temp_store_directory", "\"$escapedPath\"")
41+
}
42+
3043
val driver =
3144
JdbcSqliteDriver(
3245
url = "jdbc:sqlite:$dbPath",
33-
properties = buildDefaultWalProperties(readOnly = readOnly),
46+
properties = properties,
3447
)
3548

3649
migrateDriver(driver, schema)
@@ -59,4 +72,8 @@ public actual class DatabaseDriverFactory(
5972

6073
return mappedDriver
6174
}
75+
76+
private companion object {
77+
val IS_FIRST_CONNECTION = AtomicBoolean(true)
78+
}
6279
}

0 commit comments

Comments
 (0)