Lighter-swift / Lighter

Swift APIs for SQLite: Type-safe down to the schema. Very, very, fast. Dependency free.
https://lighter-swift.github.io/documentation/lighter/
MIT License
459 stars 12 forks source link

Code generated based on database creation SQL should not create `module` #11

Closed dhoepfl closed 2 years ago

dhoepfl commented 2 years ago

While testing the fix of #8, using develop branch (works) I noticed the following:

I added a database creation script Li_Talents.sql but forgot to exclude it from the project.

This created the following code within the struct LiTalents:

  public static let module : LiTalents! = {
    #if SWIFT_PACKAGE
    let bundle = Bundle.module
    #else
    final class Helper {}
    let bundle = Bundle(for: Helper.self)
    #endif
    if let url = bundle.url(forResource: "Li_Talents", withExtension: "sql") {
      return LiTalents(url: url, readOnly: true)
    }
    else {
      fatalError(#"Missing db resource Li_Talents.sql, not copied?"#)
    }
  }()

Notice how this tries to use the URL to the SQL file when constructing the instance. Using a plain SQL file results in an assertion failure when using the instance:

print(try? await LiTalents.module?.liLiTalents.fetchCount())
// → 2022-08-24 13:33:50.896955+0200 XXX[37374:5177759] [logging] file is not a database in "SELECT COUNT(*) FROM "LiLiTalents""
// → Lighter/SQLDatabaseOperations.swift:56: Fatal error: Failed to prepare SQL SELECT COUNT(*) FROM "LiLiTalents"

The module static is not generated if the SQL file is not included in the project.

helje5 commented 2 years ago

Hm, right. The problem ist that it doesn't distinguish between binary SQLite databases (which this feature is intended for) and plain SQL source scripts as "resources". Need to fix that, should be easy 👍

There is another variant of this, when a binary DB is used alongside SQL files, the copied DB is just the raw DB. E.g.:

So maybe the proper solution is actually to generate the combined DB and copy that 🤔

helje5 commented 2 years ago

OK, that should be fixed (only generate module accessor, when there is an actual database file in the resources).