Lighter icon indicating copy to clipboard operation
Lighter copied to clipboard

Add ability to configure pragmas

Open insha opened this issue 2 months ago • 2 comments

These pragmas are applied after opening a connection; e.g. enforcing foreign keys.

Examples

Using the .bootstrap method

struct MyApp: App {
    let db = {
        let pragmas: [SQLConnectionHandler.Pragma] = [
            .init("PRAGMA foreign_keys = ON"),
        ]

        return try! MyDatabase.bootstrap(pragmas: pragmas, overwrite: true)
        ...
    }
}

Initializing the database with the SQLConnectionHandler

struct MyApp: App {
    let db = {
        let pragmas: [SQLConnectionHandler.Pragma] = [
            .init("PRAGMA foreign_keys = ON"),
        ]

        return MyDatabase(connectionHandler: .simplePool(url: url, readOnly: false, pragmas: pragmas))
        ...
    }
}

Resolves #46

NOTE: This is my first attempt at adding this functionality and it is mainly driven by my use case. I am still finding my way around the codebase; so this may or may not be optimal. Looking forward to seeing feedback to make it better and more intuitive.

insha avatar Oct 28 '25 21:10 insha

I think we can avoid the Pragma type, if it is just a String anyways (we could the enum them out, but I think that's not really necessary). Let's not make it pragmatic specific, but just call it maybe bootstrapSQL : String? We don't even need an array here, I guess? To keep everything as simple as possible.

You would use it like:

MyDatabase(connectionHandler: .simplePool(url: url, readOnly: false, bootstrapSQL:
  """
  PRAGMA foreign_keys = ON;
  PRAGMA blub = ON;
  INSERT INTO connection_log ( last_connect ) VALUES ( now() ) -- Just a demo :-)
  """
)

helje5 avatar Nov 16 '25 14:11 helje5

I think we can avoid the Pragma type, if it is just a String anyways (we could the enum them out, but I think that's not really necessary). Let's not make it pragmatic specific, but just call it maybe bootstrapSQL : String? We don't even need an array here, I guess? To keep everything as simple as possible.

@helje5 Yes, I agree. I was too narrowly focused on my use case and overlooked the fact that it's "just a String". I will update the pull request as per your suggestion.

insha avatar Nov 27 '25 16:11 insha