vapor / fluent-postgres-driver

🐘 PostgreSQL driver for Fluent.
MIT License
149 stars 53 forks source link

Database queries never complete #16

Closed martinlasek closed 6 years ago

martinlasek commented 6 years ago

Expected Behavior

A request to the database completes

Current Behavior

A request to the database never completes

How to reproduce

1. create a new project

vapor new brokenPSQL --template=api --branch=beta

2. add FluentPostgreSQL as a dependencyin Package.swift

let package = Package(
    name: "psqlSaveIsHanging",
    dependencies: [
        .package(url: "https://github.com/vapor/vapor.git", from: "3.0.0-beta"),
        .package(url: "https://github.com/vapor/fluent.git", from: "3.0.0-beta"),
        .package(url: "https://github.com/vapor/fluent-postgresql.git", from: "1.0.0-beta")
    ],
    targets: [
        .target(name: "App", dependencies: ["FluentSQLite", "FluentPostgreSQL", "Vapor"]),
        .target(name: "Run", dependencies: ["App"]),
        .testTarget(name: "AppTests", dependencies: ["App"])
    ]
)

3. fetch dependencies + create xcode project

swift package generate-xcodeproj

4. open project and adjust Todo Model to conform to PostgreSQLModel

import FluentSQLite
import Vapor
import FluentPostgreSQL

/// A single entry of a Todo list.
final class Todo: PostgreSQLModel {
  ...
}

...

5. Set PostgreSQL as your database within configure.swift

import FluentPostgreSQL
import Foundation
import PostgreSQL
import Vapor

public func configure(
    _ config: inout Config,
    _ env: inout Environment,
    _ services: inout Services
) throws {
    // Register providers first
    try services.register(FluentPostgreSQLProvider())

    // Register routes to the router
    let router = EngineRouter.default()
    try routes(router)
    services.register(router, as: Router.self)

    // Configure a PostgrSQL database
    var databases = DatabaseConfig()
    let databaseConfig = PostgreSQLDatabaseConfig(
        hostname: "127.0.0.1",
        port: 5432,
        username: "yourUserName",
        database: "yourDatabaseName",
        password: nil
    )

    databases.add(database: PostgreSQLDatabase(config: databaseConfig), as: .psql)
    services.register(databases)

    // Configure migrations
    var migrations = MigrationConfig()
    migrations.add(model: Todo.self, database: .psql)
    services.register(migrations)
}

5. run the project and visit the site http://127.0.0.1:8080/todos

All you see is the site loads forever.

bensyverson commented 6 years ago

Seeing this as well!

alxstu commented 6 years ago

@tanner0101 wrote: " You can get around it with:

req.withPooledConnection(to: .psql) { conn in ... }

instead of using the request directly as your DatabaseConnectable"

on slack.