Skip to content

Comments

Fail hanami db drop when cannot check database existence#281

Merged
kyleplump merged 3 commits intohanami:mainfrom
katafrakt:raise-on-failed-db-check
Jul 8, 2025
Merged

Fail hanami db drop when cannot check database existence#281
kyleplump merged 3 commits intohanami:mainfrom
katafrakt:raise-on-failed-db-check

Conversation

@katafrakt
Copy link
Contributor

Currently if the existence check for a database fails (for example due to whole database server being down or user not existing), Hanami CLI will anyway return information that the database was dropped.

This changes it to failing with an explicit error message in such case. Examples:

$ hanami db drop
Could not check if the database exists. Error message:
psql: error: connection to server at "localhost" (::1), port 5432 failed: Connection refused
	Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
	Is the server running on that host and accepting TCP/IP connections?
$ hanami db drop
Could not check if the database exists. Error message:
psql: error: connection to server at "localhost" (::1), port 5432 failed: FATAL:  database "katafrakt" does not exist

Addresses #275

@katafrakt katafrakt changed the title Fail db drop when cannot check database existence Fail hanami db drop when cannot check database existence Dec 11, 2024
@KamilMilewski
Copy link

What's the status of this PR? Lately I encountered same issue and independently came up with pretty much same fix as in this PR 😄 thus asking. Would be nice to have it merged because current behavior can be very confusing.

@kyleplump
Copy link
Member

thanks for the nudge @KamilMilewski!

@katafrakt - this looks nice to me. should we implement the same behavior with the other two connection types? (mysql, sqlite)

@katafrakt
Copy link
Contributor Author

@kyleplump for SQLite the check is just File.exist?, so we cannot distinguish between database not existing and not being able to check it. But it seems it would make sense for MySQL. I can implement it, but first I would have to figure out installing MySQL to check it. Haven't really done it in a decade or so 😅

@katafrakt
Copy link
Contributor Author

Added for MySQL now.

Before:

$ be hanami db drop
=> database hanysql dropped
=> database hanysql_test dropped

After:

$ be hanami db drop
Could not check if the database exists. Error message:
mysql: Deprecated program name. It will be removed in a future release, use '/usr/bin/mariadb' instead
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

(also the warning hints some potential problems in the future, but that's out of scope)

Copy link
Member

@kyleplump kyleplump left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @katafrakt!

Copy link
Member

@kyleplump kyleplump left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks again @katafrakt - sorry missed it on the first pass, but have one update. can we also add spec coverage for the new error? past that lgtm!

katafrakt added 2 commits July 8, 2025 15:42
Currently if the existence check for a database fails (for example due
to whole database server being down or user not existing), Hanami CLI
will anyway return information that the database was dropped.

This changes it to failing with an explicit error message in such case.
@katafrakt katafrakt force-pushed the raise-on-failed-db-check branch from b5332e7 to 498ec6a Compare July 8, 2025 13:42
@katafrakt
Copy link
Contributor Author

@kyleplump specs added

Copy link
Member

@kyleplump kyleplump left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

appreciate it @katafrakt!

@kyleplump kyleplump merged commit 40d091c into hanami:main Jul 8, 2025
4 checks passed
@katafrakt katafrakt deleted the raise-on-failed-db-check branch July 8, 2025 21:11
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.

3 participants