Skip to content
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

Allow custom SupportSQLiteOpenHelper.Callback in the SqlNormalizedCacheFactory #5488

Merged

Conversation

rohandhruva
Copy link
Contributor

A quick PR to allow custom callbacks in the SqlNormalizedCacheFactory. This should allow us to get lifecycle events and apply custom database settings if needed. The eventual plan is to allow us to try something like this: https://stackoverflow.com/questions/65425352/sqldelight-slow-performance-compared-to-room

Copy link

netlify bot commented Dec 15, 2023

👷 Deploy request for apollo-android-docs pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 7427688

@rohandhruva
Copy link
Contributor Author

I just realized that this approach will mean that callers have to import sqldelight dependency to be able to delegate to the upstream AndroidSqliteDriver.Callback as well as get a reference to the schema.

I wonder if it's ok for apollo-kotlin to do the delegation to the upstream callback? Or does it feel like too much "business logic" at this point?

@rohandhruva rohandhruva marked this pull request as ready for review December 15, 2023 02:53
@BoD
Copy link
Contributor

BoD commented Dec 15, 2023

I wonder if it's ok for apollo-kotlin to do the delegation to the upstream callback? Or does it feel like too much "business logic" at this point?

I think doing so would avoid potential user mistake, and simplify the usage, right? I'm not 100% sure of the consequences of not calling the default sqldelight callback, but it looks like it would break (e.g. the schema wouldn't be created)? So if it must be called always, we might as well do it in the library?

@martinbonnin
Copy link
Contributor

martinbonnin commented Dec 15, 2023

What about a configure function?

  @JvmOverloads
  constructor(
      context: Context,
      name: String? = "apollo.db",
      factory: SupportSQLiteOpenHelper.Factory = FrameworkSQLiteOpenHelperFactory(),
      configure: (SupportSQLiteDatabase) -> Unit,
      useNoBackupDirectory: Boolean = false,
  ) : this(
      AndroidSqliteDriver(
          getSchema(),
          context.applicationContext,
          name,
          factory,
          object : AndroidSqliteDriver.Callback(getSchema()) {
            override fun onConfigure(db: SupportSQLiteDatabase) {
              super.onConfigure(db)
              configure(db)
            }
          },
          useNoBackupDirectory = useNoBackupDirectory
      ),
  ) 

Doesn't leak sqldelight in the public API and mandates that calls to AndroidSqliteDriver.Callback are made?

@rohandhruva
Copy link
Contributor Author

I think doing so would avoid potential user mistake, and simplify the usage, right? I'm not 100% sure of the consequences of not calling the default sqldelight callback, but it looks like it would break (e.g. the schema wouldn't be created)? So if it must be called always, we might as well do it in the library?

Yes, I think skipping the call to the upstream callback (AndroidSqliteDriver) is not an option. It is bound to cause a lot of other issues.

What about a configure function?
...
Doesn't leak sqldelight in the public API and mandates that calls to AndroidSqliteDriver.Callback are made?

I think that works for now, and I also like the fact that it does not introduce a sqldelight API as part of the apollo-kotlin API. However, in the future, if we end up needing to introducing more more of the callback's APIs, we'll need to add them all to the constructor (e.g. if someone wants a custom onCreate in the future).

Maybe it's ok to just have configure function for now..?

@rohandhruva rohandhruva force-pushed the rdhruva/expose_sqldelight_callback branch from a4d750c to 7427688 Compare December 17, 2023 16:58
@martinbonnin
Copy link
Contributor

we'll need to add them all to the constructor (e.g. if someone wants a custom onCreate in the future).

Let's revisit when that happens? My guiding principle here is that in the benefit of the doubt, the most restrictive API is safer. It's always easier to add a constructor than remove one

Copy link
Contributor

@BoD BoD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@martinbonnin martinbonnin merged commit e0e32d7 into apollographql:main Dec 19, 2023
5 checks passed
@rohandhruva rohandhruva deleted the rdhruva/expose_sqldelight_callback branch April 13, 2024 22:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants