From dd2e10fcb450f6c5bc1739fc680bd6eaed6d2274 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Tue, 22 Apr 2025 10:45:29 +0200 Subject: [PATCH 1/3] Set temp_store_directory on Android --- .../java/com/powersync/AndroidDatabaseTest.kt | 6 ++++++ .../DatabaseDriverFactory.android.kt | 19 ++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/core-tests-android/src/androidTest/java/com/powersync/AndroidDatabaseTest.kt b/core-tests-android/src/androidTest/java/com/powersync/AndroidDatabaseTest.kt index 39c289f2..3bc80523 100644 --- a/core-tests-android/src/androidTest/java/com/powersync/AndroidDatabaseTest.kt +++ b/core-tests-android/src/androidTest/java/com/powersync/AndroidDatabaseTest.kt @@ -222,4 +222,10 @@ class AndroidDatabaseTest { // The exception messages differ slightly between drivers assertEquals(exception.message!!.contains("write a readonly database"), true) } + + @Test + fun canCreateTemporaryTable() = runTest { + database.execute("PRAGMA temp_store = 1;") // Store temporary data as files + database.execute("CREATE TEMP TABLE my_tbl (content ANY) STRICT;") + } } diff --git a/core/src/androidMain/kotlin/com/powersync/DatabaseDriverFactory.android.kt b/core/src/androidMain/kotlin/com/powersync/DatabaseDriverFactory.android.kt index 3354c9d1..8eba77b2 100644 --- a/core/src/androidMain/kotlin/com/powersync/DatabaseDriverFactory.android.kt +++ b/core/src/androidMain/kotlin/com/powersync/DatabaseDriverFactory.android.kt @@ -7,6 +7,7 @@ import com.powersync.db.internal.InternalSchema import com.powersync.db.migrateDriver import kotlinx.coroutines.CoroutineScope import org.sqlite.SQLiteCommitListener +import java.util.concurrent.atomic.AtomicBoolean @Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING") public actual class DatabaseDriverFactory( @@ -27,10 +28,22 @@ public actual class DatabaseDriverFactory( context.getDatabasePath(dbFilename) } + val properties = buildDefaultWalProperties(readOnly = readOnly) + val isFirst = IS_FIRST_CONNECTION.getAndSet(false) + if (isFirst) { + // Make sure the temp_store_directory points towards a temporary directory we actually + // have access to. Due to sandboxing, the default /tmp/ is inaccessible. + // The temp_store_directory pragma is deprecated and not thread-safe, so we only set it + // on the first connection (it sets a global field and will affect every connection + // opened). + val escapedPath = context.cacheDir.absolutePath.replace("\"", "\"\"") + properties.setProperty("temp_store_directory", "\"$escapedPath\"") + } + val driver = JdbcSqliteDriver( url = "jdbc:sqlite:$dbPath", - properties = buildDefaultWalProperties(readOnly = readOnly), + properties = properties, ) migrateDriver(driver, schema) @@ -59,4 +72,8 @@ public actual class DatabaseDriverFactory( return mappedDriver } + + private companion object { + val IS_FIRST_CONNECTION = AtomicBoolean(true) + } } From 8158d880416b66ecf8cf6bee285cb289e3211044 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Tue, 22 Apr 2025 11:47:43 +0200 Subject: [PATCH 2/3] Try inserting data --- .../src/androidTest/java/com/powersync/AndroidDatabaseTest.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core-tests-android/src/androidTest/java/com/powersync/AndroidDatabaseTest.kt b/core-tests-android/src/androidTest/java/com/powersync/AndroidDatabaseTest.kt index 3bc80523..f3b9c88c 100644 --- a/core-tests-android/src/androidTest/java/com/powersync/AndroidDatabaseTest.kt +++ b/core-tests-android/src/androidTest/java/com/powersync/AndroidDatabaseTest.kt @@ -227,5 +227,8 @@ class AndroidDatabaseTest { fun canCreateTemporaryTable() = runTest { database.execute("PRAGMA temp_store = 1;") // Store temporary data as files database.execute("CREATE TEMP TABLE my_tbl (content ANY) STRICT;") + for (i in 0..128) { + database.execute("INSERT INTO my_tbl VALUES (randomblob(1024 * 1024))") + } } } From cf775daa2f605c864c3b1aed50940cdec5e2b004 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Mon, 2 Jun 2025 10:48:40 +0200 Subject: [PATCH 3/3] Fix test by inserting more data --- .../java/com/powersync/AndroidDatabaseTest.kt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/core-tests-android/src/androidTest/java/com/powersync/AndroidDatabaseTest.kt b/core-tests-android/src/androidTest/java/com/powersync/AndroidDatabaseTest.kt index f3b9c88c..ef3800f7 100644 --- a/core-tests-android/src/androidTest/java/com/powersync/AndroidDatabaseTest.kt +++ b/core-tests-android/src/androidTest/java/com/powersync/AndroidDatabaseTest.kt @@ -224,11 +224,12 @@ class AndroidDatabaseTest { } @Test - fun canCreateTemporaryTable() = runTest { + fun canUseTempStore() = runTest { database.execute("PRAGMA temp_store = 1;") // Store temporary data as files - database.execute("CREATE TEMP TABLE my_tbl (content ANY) STRICT;") - for (i in 0..128) { - database.execute("INSERT INTO my_tbl VALUES (randomblob(1024 * 1024))") + database.execute("CREATE TEMP TABLE foo (bar TEXT);") + val data = "new row".repeat(100); + for (i in 0..10000) { + database.execute("INSERT INTO foo VALUES (?)", parameters = listOf(data)) } } }