Vapor registered 'ToDo' database yet PostgreSQL doesn't show it from the prompt

58 views Asked by At

Environment: Vapor v4.77
Database: Postgresql@15 via Homebrew

Here's the environment:
/Users/Ric> echo $DATABASE_NAME
ToDo
/Users/Ric> echo $DATABASE_HOST
localhost
/Users/Ric> echo $DATABASE_PORT
5432

Here's the Configuraton:

public func configure(_ app: Application) async throws {
    // uncomment to serve files from /Public folder
    // app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))

    app.databases.use(DatabaseConfigurationFactory.postgres(configuration: .init(
        hostname: Environment.get("DATABASE_HOST") ?? "localhost",
        port: Environment.get("DATABASE_PORT").flatMap(Int.init(_:)) ?? SQLPostgresConfiguration.ianaPortNumber,
        username: Environment.get("DATABASE_USERNAME") ?? "vapor_username",
        password: Environment.get("DATABASE_PASSWORD") ?? "vapor_password",
        database: Environment.get("DATABASE_NAME") ?? "vapor_database",
        tls: .prefer(try .init(configuration: .clientDefault)))
    ), as: .psql)

    // Migrations:
    app.migrations.add(CreateTodo())
    try await app.autoMigrate().get()

    // register routes
    try routes(app)

Here's the migration:
{Temporally changed from 'name' to 'first_name', etc.}

import Fluent

struct CreateTodo: AsyncMigration {
    func prepare(on database: Database) async throws {
        try await database.schema("todos")
            .deleteField("title")
            .field("first_name", .string)
            .field("last_name", .string)
            .update()
    }

    func revert(on database: Database) async throws {
        try await database.schema("todos").delete()
    }
}

I've checked PostgreSQL via command problem and am unable to find the Todo database:

enter image description here

I'm able to do a post and get to prove there's persistent data. But I can't find the database via the prompt.

I tried a using an existing database name, 'Turkey' (see above):
app.databases.use(DatabaseConfigurationFactory.postgres(configuration: .init(
        hostname: Environment.get("DATABASE_HOST") ?? "localhost",
        port: Environment.get("DATABASE_PORT").flatMap(Int.init(_:)) ?? SQLPostgresConfiguration.ianaPortNumber,
        username: Environment.get("DATABASE_USERNAME") ?? "vapor_username",
        password: Environment.get("DATABASE_PASSWORD") ?? "vapor_password",
//        database: Environment.get("DATABASE_NAME") ?? "vapor_database",
        database: "Turkey",
        tls: .prefer(try .init(configuration: .clientDefault)))
    ), as: .psql)
... and changed other db references to 'Turkey'.

Here's what I got after doing a >swift run:

enter image description here

Explanation?

1

There are 1 answers

0
Frederick C. Lee On

I finally got this to work.
My problem was a combination of corrupt vapor code and case spelling.

Solution:
1) Rollback code from the repository;
2) Use a global constant to declare the database name lowercase per Postgres wish. This reduces case mismatching hassles.