Skip to content

Set temp_store_directory on Android #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,14 @@ class AndroidDatabaseTest {
// The exception messages differ slightly between drivers
assertEquals(exception.message!!.contains("write a readonly database"), true)
}

@Test
fun canUseTempStore() = runTest {
database.execute("PRAGMA temp_store = 1;") // Store temporary data as files
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))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -59,4 +72,8 @@ public actual class DatabaseDriverFactory(

return mappedDriver
}

private companion object {
val IS_FIRST_CONNECTION = AtomicBoolean(true)
}
}