0

How can I make sure that Database.Schema.create() is only called once when using the application? You could add ...IF NOT EXISTS... to the table definition, then the SQL instructions are executed during every startup but no tables are created after the first run, however, I'm not sure if this is the correct way to do this.

Alternatively, I could query the tables and check if they are already present.

What I'm looking for is something like:

if (<first run>) {
    Database.Schema.create()
}

I thought I could use migrations for this, but migrations seem to work differently for sqldelight then I expected (I tried to create the tables inside a migration but this does not work).

matthjes
  • 671
  • 7
  • 20

1 Answers1

3

There has been a very helpful reply posted here: https://github.com/cashapp/sqldelight/issues/1605

It basically boils down to storing the current version inside PRAGMA user_version (which is initially 0) and performing a migration when this version differs from the latest migration version.

This has the advantage that the version is stored inside the database. I've adopted this solution for my Kotlin project.

matthjes
  • 671
  • 7
  • 20