From: Andreas Scherbaum Date: Thu, 21 Jun 2012 20:51:41 +0000 (+0200) Subject: - verify required tables at startup X-Git-Url: http://git.postgresql.org/gitweb/static/gitweb.js?a=commitdiff_plain;h=10e0467e5b67702016d0c3aa28f6762091b78172;p=docbot.git - verify required tables at startup --- diff --git a/db.pm b/db.pm index 9555f4b..7265f51 100644 --- a/db.pm +++ b/db.pm @@ -221,6 +221,37 @@ sub test_database { } +# verify_tables() +# +# verify table existence +# +# parameter: +# - class name ($self) +# return: +# - 0/1 (1 = OK) +sub verify_tables { + my $self = shift; + + my $query = "SELECT COUNT(1) AS count + FROM information_schema.tables + WHERE table_name IN ('docbot_key', 'docbot_url', 'docbot_user')"; + + my $st = $main::db->query($query); + if (!defined($st)) { + main::print_msg("Can't execute query - $DBI::errstr\n", ERROR); + return 0; + } + + my @row = $st->fetchrow_array; + if ($row[0] != 3) { + main::print_msg("Missing one or more tables", ERROR); + return 0; + } + + return 1; +} + + # ping() # # execute a test query diff --git a/docbot.pl b/docbot.pl index 59396ff..1f35bfb 100755 --- a/docbot.pl +++ b/docbot.pl @@ -369,6 +369,10 @@ sub init_database { if (!$main::db->test_database()) { die("Could not test database connection!\n"); } + # and verify table existence + if (!$main::db->verify_tables()) { + die("Required tables are not available!\n"); + } }