Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Added a way to check the DB's status
Browse files Browse the repository at this point in the history
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
mssola committed Aug 1, 2017
1 parent 45c77a1 commit 564c3cb
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
26 changes: 26 additions & 0 deletions bin/check_db.rb
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
24 changes: 24 additions & 0 deletions lib/portus/db.rb
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
22 changes: 22 additions & 0 deletions spec/lib/portus/db_spec.rb
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

0 comments on commit 564c3cb

Please sign in to comment.