@@ -7,6 +7,7 @@ import com.powersync.db.internal.InternalSchema
77import com.powersync.db.migrateDriver
88import kotlinx.coroutines.CoroutineScope
99import org.sqlite.SQLiteCommitListener
10+ import java.util.concurrent.atomic.AtomicBoolean
1011
1112@Suppress(" EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING" )
1213public 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