Skip to content

Conversation

@insha
Copy link

@insha insha commented Oct 28, 2025

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.

These pragmas are applied after opening a connection; e.g. enforcing foreign keys.
@helje5
Copy link
Member

helje5 commented Nov 16, 2025

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 :-)
  """
)

@insha
Copy link
Author

insha commented Nov 27, 2025

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.

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.

Add ability to configure pragmas that are applied after opening a connection

2 participants