https://kotlinlang.org logo
#squarelibraries
Title
# squarelibraries
r

ribesg

03/26/2024, 10:23 AM
The SQLDelight documentation is unclear: do you need to run
Database.Schema.migrate(…)
manually or not? https://cashapp.github.io/sqldelight/2.0.1/multiplatform_sqlite/migrations/ The documentation describes how to define migrations, states that
These SQL statements are run by the Database.Schema.migrate() method
but then
If you run your migration from code
. What does
If
mean? It's optional after all?
b

Benoit Quenaudon

03/26/2024, 12:06 PM
SqlDelight will run migration on startup. The
If
means
In the case of
: you have the possibility to run this code yourself.
r

ribesg

03/26/2024, 12:43 PM
Right, that's what I guessed then confirmed by trying, but my point is that it should be obvious in the documentation and there is room for improvement here. The fact that these migration files are executed automatically is simply not stated in the documentation
b

Benoit Quenaudon

03/26/2024, 12:47 PM
Got it. I think the repo is open to PR if you wanna improve the doc.
d

Derek Ellis

03/26/2024, 1:24 PM
It depends on the driver. The Android (and iOS) drivers will automatically handle migrations for you. All others don't.
r

ribesg

03/26/2024, 1:28 PM
I don't even get how you could call
Database.Schema.migrate()
as there don't seem to be a way to know what the current version of the db file is. Are you supposed to store it somewhere else? Or maybe it's platform specific
j

jw

03/26/2024, 1:31 PM
You store it
Common place to put it is in a non-schema table
r

ribesg

03/26/2024, 1:32 PM
How does it work automatically then? Isn't it already stored
j

jw

03/26/2024, 1:33 PM
You didn't ask about the automatic case. You asked about the case where you would need to call
migrate()
yourself.
For sqlite on Android, for example, the version is stored with a PRAGMA that gets persisted
The built-in API supports providing a version and will automatically give a callback to perform migration, if needed
On platforms without built-in support, you can replicate such behavior yourself and explicitly call the create or migrate APIs as needed
r

ribesg

03/26/2024, 1:34 PM
I only know what's in the documentation, so almost nothing. I just learned that some drivers just don't do the automatic migrations.
When does the automatic migration occur? How do I know when to call
Database.Schema.migrate()
if I want to? Won't it be called twice then?
j

jw

03/26/2024, 1:40 PM
If there is automatic migration you will never call it
It occurs when the database is opened
d

Derek Ellis

03/26/2024, 1:40 PM
There's an example in the SQLite JDBC driver that demonstrates how to check for the database's current version and migrate if needed: https://github.com/cashapp/sqldelight/blob/master/drivers/sqlite-driver/src/main/k[…]tlin/app/cash/sqldelight/driver/jdbc/sqlite/JdbcSqliteSchema.kt
☝️ 1
r

ribesg

03/26/2024, 1:50 PM
So: on all platforms that are not Android & iOS, I need to store the version of the db file somewhere and call
Database.Schema.migrate()
. On Android & iOS, I just let it work automatically. Right?
d

Derek Ellis

03/26/2024, 2:00 PM
Correct
👍 1