vapor / fluent

Vapor ORM (queries, models, and relations) for NoSQL and SQL databases
https://docs.vapor.codes/4.0/fluent/overview/
MIT License
1.32k stars 172 forks source link

Fluent does not raise error in database connection, when passed with wrong credentials. #706

Open mauliknshah opened 3 years ago

mauliknshah commented 3 years ago

I'm using the Fluent as a library independently of vapor, and trying to connect to database using the following simple code:

// Assume that the 'databases', 'logger', and 'eventloop' objects are initialized correctly.
// The connectionString has a wrong set of credentials, but forms a valid postgres connection string. 
do  {
            try databases.use(.postgres(url: connectionString), as: .psql)
            database = databases.database(.psql, logger: logger, on: databases.eventLoopGroup.next())
        } catch {
            throw error
        }

Now, in case of correct credentials, the database connects without any issues. On the other hand, if the database credentials are wrong, the databases.use does not throw any error.

For now, I have found a temporary workaround as follows:


do {
            try databases.use(.postgres(url: connectionString), as: .psql)
            database = databases.database(.psql, logger: logger, on: databases.eventLoopGroup.next())
            // Note: Fluent does not throw error with wrong credentials.
            // Temporary workaround.
            guard let postgres = database as? PostgresDatabase else {
                //Throw some error. 
            }
            _ = try postgres.simpleQuery("SELECT 1").wait()
        }
        catch {
            throw error
        }

Could you please correct the fluent API to test connection in the use call, and throw error if the database is not being able to connect?