Skip to content

Commit 8755182

Browse files
authored
Use the cache directory by default (#242)
1 parent 2338624 commit 8755182

File tree

3 files changed

+42
-26
lines changed

3 files changed

+42
-26
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
PUT_CHANGELOG_HERE
44

5+
- Database files are now stored in the `cache` directory by default on Android (#164)
6+
57
# v1.0.0-alpha.7
68
_2025-10-14_
79

normalized-cache-sqlite/src/androidMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactory.android.kt

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ import android.content.Context
44
import androidx.sqlite.db.SupportSQLiteDatabase
55
import androidx.sqlite.db.SupportSQLiteOpenHelper
66
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
7-
import com.apollographql.cache.normalized.api.NormalizedCacheFactory
8-
import com.apollographql.cache.normalized.sql.internal.record.SqlRecordDatabase
97
import app.cash.sqldelight.async.coroutines.synchronous
10-
import app.cash.sqldelight.driver.android.AndroidSqliteDriver
118
import app.cash.sqldelight.db.SqlDriver
12-
import com.apollographql.cache.normalized.api.NormalizedCache
9+
import app.cash.sqldelight.driver.android.AndroidSqliteDriver
10+
import com.apollographql.cache.normalized.api.NormalizedCacheFactory
11+
import com.apollographql.cache.normalized.sql.internal.record.SqlRecordDatabase
1312

1413
actual fun SqlNormalizedCacheFactory(name: String?): NormalizedCacheFactory =
15-
SqlNormalizedCacheFactory(createDriver(name, null))
14+
SqlNormalizedCacheFactory(createDriver(name))
1615

1716
/**
18-
* @param [name] Name of the database file, or null for an in-memory database (as per Android framework implementation).
19-
* @param [factory] Factory class to create instances of [SupportSQLiteOpenHelper]
20-
* @param [configure] Optional callback, called when the database connection is being configured, to enable features such as
21-
* write-ahead logging or foreign key support. It should not modify the database except to configure it.
22-
* @param [useNoBackupDirectory] Sets whether to use a no backup directory or not.
23-
* @param [windowSizeBytes] Size of cursor window in bytes, per [android.database.CursorWindow] (Android 28+ only), or null to use the default.
17+
* @param name Name of the database file in the cache directory, or an absolute path to a file, or null for an in-memory database
18+
* (as per Android framework implementation).
19+
* @param factory Factory class to create instances of [SupportSQLiteOpenHelper]
20+
* @param configure Optional callback, called when the database connection is being configured, to enable features such as
21+
* write-ahead logging or foreign key support. It should not modify the database except to configure it.
22+
* @param useNoBackupDirectory Sets whether to use a no backup directory or not.
23+
* @param windowSizeBytes Size of cursor window in bytes, per [android.database.CursorWindow] (Android 28+ only), or null to use the default.
2424
*/
2525
@JvmOverloads
2626
fun SqlNormalizedCacheFactory(
@@ -32,13 +32,29 @@ fun SqlNormalizedCacheFactory(
3232
windowSizeBytes: Long? = null,
3333
): NormalizedCacheFactory {
3434
val synchronousSchema = SqlRecordDatabase.Schema.synchronous()
35+
val filePath = when {
36+
name == null -> {
37+
null
38+
}
39+
40+
name.startsWith("/") -> {
41+
// Absolute path: keep as-is
42+
name
43+
}
44+
45+
else -> {
46+
// Old versions of the library used to store the database in the database directory.
47+
// If such file exists, use it, otherwise, use the cache directory.
48+
(context.getDatabasePath(name).takeIf { it.exists() } ?: context.cacheDir.resolve(name)).absolutePath
49+
}
50+
}
3551
return SqlNormalizedCacheFactory(
3652
AndroidSqliteDriver(
37-
synchronousSchema,
38-
context.applicationContext,
39-
name,
40-
factory,
41-
object : AndroidSqliteDriver.Callback(synchronousSchema) {
53+
schema = synchronousSchema,
54+
context = context.applicationContext,
55+
name = filePath,
56+
factory = factory,
57+
callback = object : AndroidSqliteDriver.Callback(synchronousSchema) {
4258
override fun onConfigure(db: SupportSQLiteDatabase) {
4359
super.onConfigure(db)
4460
configure?.invoke(db)
@@ -50,15 +66,12 @@ fun SqlNormalizedCacheFactory(
5066
)
5167
}
5268

53-
private fun createDriver(name: String?, baseDir: String?): SqlDriver {
54-
check(baseDir == null) {
55-
"Apollo: Android SqlNormalizedCacheFactory doesn't support 'baseDir'"
56-
}
69+
private fun createDriver(name: String?): SqlDriver {
5770
return AndroidSqliteDriver(
58-
SqlRecordDatabase.Schema.synchronous(),
59-
ApolloInitializer.context,
60-
name,
61-
FrameworkSQLiteOpenHelperFactory(),
71+
schema = SqlRecordDatabase.Schema.synchronous(),
72+
context = ApolloInitializer.context,
73+
name = name,
74+
factory = FrameworkSQLiteOpenHelperFactory(),
6275
)
6376
}
6477

normalized-cache-sqlite/src/commonMain/kotlin/com/apollographql/cache/normalized/sql/SqlNormalizedCacheFactory.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import com.apollographql.cache.normalized.sql.internal.RecordDatabase
88
/**
99
* Creates a new [NormalizedCacheFactory] that uses a persistent cache based on Sqlite
1010
*
11-
* @param name: the name of the database or null for an in-memory database
11+
* @param name The name of the database or null for an in-memory database
1212
* When not in memory, the database will be stored in a platform specific folder
13-
* - on Android it will use [Context.getDatabaseName](https://developer.android.com/reference/android/content/Context#getDatabasePath(java.lang.String))
13+
* - on Android it will use [Context.getCacheDir](https://developer.android.com/reference/android/content/Context#getCacheDir()). It can
14+
* also be an absolute path to a file.
1415
* - on MacOS, it will use "Application Support/databases/name"
1516
* - on the JVM, it will use "System.getProperty("user.home")/.apollo"
1617
* - on JS/Wasm, this argument is unused

0 commit comments

Comments
 (0)