-
Notifications
You must be signed in to change notification settings - Fork 5
Use the cache directory by default #242
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,23 +4,23 @@ import android.content.Context | |
| import androidx.sqlite.db.SupportSQLiteDatabase | ||
| import androidx.sqlite.db.SupportSQLiteOpenHelper | ||
| import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory | ||
| import com.apollographql.cache.normalized.api.NormalizedCacheFactory | ||
| import com.apollographql.cache.normalized.sql.internal.record.SqlRecordDatabase | ||
| import app.cash.sqldelight.async.coroutines.synchronous | ||
| import app.cash.sqldelight.driver.android.AndroidSqliteDriver | ||
| import app.cash.sqldelight.db.SqlDriver | ||
| import com.apollographql.cache.normalized.api.NormalizedCache | ||
| import app.cash.sqldelight.driver.android.AndroidSqliteDriver | ||
| import com.apollographql.cache.normalized.api.NormalizedCacheFactory | ||
| import com.apollographql.cache.normalized.sql.internal.record.SqlRecordDatabase | ||
|
|
||
| actual fun SqlNormalizedCacheFactory(name: String?): NormalizedCacheFactory = | ||
| SqlNormalizedCacheFactory(createDriver(name, null)) | ||
| SqlNormalizedCacheFactory(createDriver(name)) | ||
|
|
||
| /** | ||
| * @param [name] Name of the database file, or null for an in-memory database (as per Android framework implementation). | ||
| * @param [factory] Factory class to create instances of [SupportSQLiteOpenHelper] | ||
| * @param [configure] Optional callback, called when the database connection is being configured, to enable features such as | ||
| * write-ahead logging or foreign key support. It should not modify the database except to configure it. | ||
| * @param [useNoBackupDirectory] Sets whether to use a no backup directory or not. | ||
| * @param [windowSizeBytes] Size of cursor window in bytes, per [android.database.CursorWindow] (Android 28+ only), or null to use the default. | ||
| * @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 | ||
| * (as per Android framework implementation). | ||
| * @param factory Factory class to create instances of [SupportSQLiteOpenHelper] | ||
| * @param configure Optional callback, called when the database connection is being configured, to enable features such as | ||
| * write-ahead logging or foreign key support. It should not modify the database except to configure it. | ||
| * @param useNoBackupDirectory Sets whether to use a no backup directory or not. | ||
| * @param windowSizeBytes Size of cursor window in bytes, per [android.database.CursorWindow] (Android 28+ only), or null to use the default. | ||
| */ | ||
| @JvmOverloads | ||
| fun SqlNormalizedCacheFactory( | ||
|
|
@@ -32,13 +32,29 @@ fun SqlNormalizedCacheFactory( | |
| windowSizeBytes: Long? = null, | ||
| ): NormalizedCacheFactory { | ||
| val synchronousSchema = SqlRecordDatabase.Schema.synchronous() | ||
| val filePath = when { | ||
| name == null -> { | ||
| null | ||
| } | ||
|
|
||
| name.startsWith("/") -> { | ||
| // Absolute path: keep as-is | ||
| name | ||
| } | ||
|
|
||
| else -> { | ||
| // Old versions of the library used to store the database in the database directory. | ||
| // If such file exists, use it, otherwise, use the cache directory. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For users who would prefer to switch to using the cache directory, do you think it's possible to provide a migration path? (understanding that you'll lose all the existing data) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think switching to the cache directory should become the default (with a big warning in the release notes). Users that want to keep the old one could use a dedicated overload that takes a path maybe? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can pass an absolute path for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Works for me! |
||
| (context.getDatabasePath(name).takeIf { it.exists() } ?: context.cacheDir.resolve(name)).absolutePath | ||
| } | ||
| } | ||
| return SqlNormalizedCacheFactory( | ||
| AndroidSqliteDriver( | ||
| synchronousSchema, | ||
| context.applicationContext, | ||
| name, | ||
| factory, | ||
| object : AndroidSqliteDriver.Callback(synchronousSchema) { | ||
| schema = synchronousSchema, | ||
| context = context.applicationContext, | ||
| name = filePath, | ||
| factory = factory, | ||
| callback = object : AndroidSqliteDriver.Callback(synchronousSchema) { | ||
| override fun onConfigure(db: SupportSQLiteDatabase) { | ||
| super.onConfigure(db) | ||
| configure?.invoke(db) | ||
|
|
@@ -50,15 +66,12 @@ fun SqlNormalizedCacheFactory( | |
| ) | ||
| } | ||
|
|
||
| private fun createDriver(name: String?, baseDir: String?): SqlDriver { | ||
| check(baseDir == null) { | ||
| "Apollo: Android SqlNormalizedCacheFactory doesn't support 'baseDir'" | ||
| } | ||
| private fun createDriver(name: String?): SqlDriver { | ||
| return AndroidSqliteDriver( | ||
| SqlRecordDatabase.Schema.synchronous(), | ||
| ApolloInitializer.context, | ||
| name, | ||
| FrameworkSQLiteOpenHelperFactory(), | ||
| schema = SqlRecordDatabase.Schema.synchronous(), | ||
| context = ApolloInitializer.context, | ||
| name = name, | ||
| factory = FrameworkSQLiteOpenHelperFactory(), | ||
| ) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably worth and explicit warning that it's going to lose all the data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not!