This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a way to check the DB's status
This has been extracted both from https://github.com/openSUSE/docker-containers and #1353, since it will be useful for #1357 as well. Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
- Loading branch information
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# This is a rails runner that will print the status of Portus' database. | ||
# Possible outcomes: | ||
# | ||
# * `DB_READY`: the database has been created and initialized. | ||
# * `DB_EMPTY`: the database has been created but has not been initialized. | ||
# * `DB_MISSING`: the database has not been created. | ||
# * `DB_DOWN`: cannot connect to the database. | ||
# * `DB_UNKNOWN`: unknown error. | ||
# | ||
# Originally included in the https://github.com/openSUSE/docker-containers | ||
# repository under the same license. | ||
|
||
require "portus/db" | ||
|
||
puts case Portus::DB.ping | ||
when :ready | ||
"DB_READY" | ||
when :empty | ||
"DB_EMPTY" | ||
when :missing | ||
"DB_MISSING" | ||
when :down | ||
"DB_DOWN" | ||
else | ||
"DB_UNKNOWN" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Portus | ||
# The DB module has useful methods for DB purposes. | ||
module DB | ||
# Pings the DB and returns a proper symbol depending on the situation: | ||
# * ready: the database has been created and initialized. | ||
# * empty: the database has been created but has not been initialized. | ||
# * missing: the database has not been created. | ||
# * down: cannot connect to the database. | ||
def self.ping | ||
::Portus::DB.migrations? ? :ready : :empty | ||
rescue ActiveRecord::NoDatabaseError | ||
:missing | ||
rescue Mysql2::Error | ||
:down | ||
end | ||
|
||
# Returns true if the migrations have been run. The implementation is pretty | ||
# trivial, but this gives us a nice way to test this module. | ||
def self.migrations? | ||
ActiveRecord::Base.connection | ||
ActiveRecord::Base.connection.table_exists? "schema_migrations" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require "rails_helper" | ||
|
||
describe Portus::DB do | ||
it "returns :ready on the usual case" do | ||
expect(Portus::DB.ping).to eq :ready | ||
end | ||
|
||
it "returns :empty if the DB is still initializing" do | ||
allow(::Portus::DB).to receive(:migrations?).and_return(false) | ||
expect(Portus::DB.ping).to eq :empty | ||
end | ||
|
||
it "returns :missing if the DB is missing" do | ||
allow(::Portus::DB).to receive(:migrations?).and_raise(ActiveRecord::NoDatabaseError, "a") | ||
expect(Portus::DB.ping).to eq :missing | ||
end | ||
|
||
it "returns :down if the DB is down" do | ||
allow(::Portus::DB).to receive(:migrations?).and_raise(Mysql2::Error, "a") | ||
expect(Portus::DB.ping).to eq :down | ||
end | ||
end |