4

It looks like in Vapor 2 you could do something like:

let query = <some fluent query object>
logger?.debug(query)

and it would print out the full SQL statement, but I'm not seeing any documentation of how to do that now in Vapor 3.

How can I see what SQL is being generated by my QueryBuilder?

tanner0101
  • 4,005
  • 19
  • 33
Elliot Schrock
  • 1,406
  • 2
  • 9
  • 20
  • 1
    Isn't this: https://docs.vapor.codes/3.0/database-kit/overview/ a starting point? Check the final paragraphs of the Config section. – Nick Oct 27 '18 at 18:46
  • Thanks @Nick! Was able to find it in that doc – kept looking in all the wrong places, so thank you for pointing it out :) Added an answer, but would be happy to give you credit if you post one! – Elliot Schrock Oct 27 '18 at 22:31
  • Thanks for the offer, but you've done all the work! – Nick Oct 27 '18 at 22:40

1 Answers1

7

Thanks to Nick in the comments, who pointed me to the right set of docs. This can be accomplished by using the enableLogging method. So now my configure.swift includes this code:

let dbConfig: PostgreSQLDatabaseConfig
if let url = Environment.get("DATABASE_URL"), let psqlConfig = PostgreSQLDatabaseConfig(url: url, transport: .unverifiedTLS) {
    dbConfig = psqlConfig
} else {
    dbConfig = ...something for the local db...
}

let postgresql = PostgreSQLDatabase(config: dbConfig)

/// Register the configured SQLite database to the database config.
var databases = DatabasesConfig()
databases.enableLogging(on: .psql)
databases.add(database: postgresql, as: .psql)
services.register(databases)

The important line being the third from the bottom. For a while I was trying to enable debugging on PostgreSQLDatabaseConfig, so to anyone in the future, take note that you're enabling it on the DatabasesConfig object instead.

Elliot Schrock
  • 1,406
  • 2
  • 9
  • 20