## Parse it out and return our information
for $db (@{$info->{db}}) {
- if ($db->{slurp} !~ /PostgreSQL (\S+)/o) { ## no critic (ProhibitUnusedCapture)
+ if ($db->{slurp} !~ /PostgreSQL (\d+\.\d+\S+)/o) { ## no critic (ProhibitUnusedCapture)
add_unknown msg('invalid-query', $db->{slurp});
next;
}
like ($cp->run('-c -8'), qr{^POSTGRES_BACKENDS OK}, $t);
$t=qq{$S works when no items caught by pg_stat_activity};
-## This is tricky to test properly.
+
+$cp->drop_schema_if_exists($fakeschema);
$cp->create_fake_pg_table('pg_stat_activity');
like ($cp->run(), qr{^POSTGRES_BACKENDS OK: .+No connections}, $t);
$t=qq{$S returned correct performance data with include};
like ($cp->run('--include=postgres'), qr{ \| time=(\d\.\d\d) ardala=0 beedeebeedee=0 postgres=3}, $t);
+$cp->drop_schema_if_exists($fakeschema);
+
exit;
$t=qq{$S returns okay for fresh database with no bloat};
like ($cp->run("-w=1m"), qr{^$label OK: DB "postgres"}, $t);
-local $dbh->{Warn} = 0;
-$dbh->do("DROP TABLE IF EXISTS $tname");
+$cp->drop_table_if_exists($tname);
$dbh->do("CREATE TABLE $tname AS SELECT 123::int AS foo FROM generate_series(1,10000)");
$dbh->do("UPDATE $tname SET foo = foo") for 1..1;
$dbh->do('ANALYZE');
$t=qq{$S returns correct MRTG output when rows found};
is ($cp->run('--output=MRTG'), qq{1\n0\n\n\n}, $t);
-$cp->bad_fake_version();
+$cp->fake_version('ABC');
$t=qq{$S fails if there's a fake version function};
like ($cp->run(), qr{^POSTGRES_CONNECTION UNKNOWN:}, $t);
$cp->reset_path();
sub cleanup {
$dbh->rollback;
- local $dbh->{Warn} = 0;
- $dbh->do(qq{DROP TABLE IF EXISTS "$testtbl"});
- $dbh->do(qq{DROP FUNCTION IF EXISTS "${testtrig_prefix}func"()});
- $dbh->commit;
+ $cp->drop_table_if_exists($testtbl);
+ $cp->drop_function_if_exists($testtrig_prefix.'func','');
}
END { cleanup(); }
+
# Set up a test table with two triggers.
cleanup();
$dbh->do(qq{CREATE TABLE "$testtbl" (a integer)});
## Create a fake fsm 'view' for testing
$cp->set_fake_schema();
my $schema = $cp->get_fake_schema();
-{
- local $dbh->{Warn};
- $dbh->do("DROP TABLE IF EXISTS $schema.pg_freespacemap_pages");
- $dbh->do("DROP TABLE IF EXISTS $schema.pg_freespacemap_relations");
- $dbh->do('DROP FUNCTION IF EXISTS public.version()');
-}
+$cp->drop_table_if_exists($schema, 'pg_freespacemap_pages');
+$cp->drop_table_if_exists($schema, 'pg_freespacemap_relations');
+
$dbh->do(qq{
CREATE TABLE $schema.pg_freespacemap_pages (
reltablespace oid,
## Create a fake fsm 'view' for testing
$cp->set_fake_schema();
my $schema = $cp->get_fake_schema();
-{
- local $dbh->{Warn};
- $dbh->do("DROP TABLE IF EXISTS $schema.pg_freespacemap_pages");
- $dbh->do("DROP TABLE IF EXISTS $schema.pg_freespacemap_relations");
- $dbh->do('DROP FUNCTION IF EXISTS public.version()');
-}
+$cp->drop_table_if_exists($schema, 'pg_freespacemap_pages');
+$cp->drop_table_if_exists($schema, 'pg_freespacemap_relations');
+
$dbh->do(qq{
CREATE TABLE $schema.pg_freespacemap_pages (
reltablespace oid,
local $dbh->{Warn};
$dbh->do('ANALYZE');
-$dbh->do(qq{DROP TABLE IF EXISTS $testtbl});
+$cp->drop_table_if_exists($testtbl);
$dbh->do(qq{CREATE TABLE $testtbl AS SELECT 123::INTEGER AS a FROM generate_series(1,200000)});
$dbh->commit();
local $dbh->{Warn};
local $dbh->{AutoCommit} = 1;
$dbh->do('VACUUM');
-$dbh->do(qq{DROP TABLE IF EXISTS $testtbl});
+$cp->drop_table_if_exists($testtbl);
$dbh->do(qq{CREATE TABLE $testtbl AS SELECT 123::INTEGER AS a FROM generate_series(1,200000)});
$dbh->commit();
$t=qq{$S fails when no matching databases found};
like ($cp->run('--include=foo'), qr{No matching databases found}, $t);
+$cp->drop_schema_if_exists($fakeschema);
$cp->create_fake_pg_table('pg_locks');
$SQL = q{SELECT oid FROM pg_database WHERE datname = 'postgres'};
my $dboid = $dbh->selectall_arrayref($SQL)->[0][0];
like ($cp->run('--warning="waiting=1;exclusive=2"'),
qr{POSTGRES_LOCKS WARNING.*total "waiting" locks: 1 \* total "exclusive" locks: 2 }, $t);
-$cp->remove_fake_pg_table('pg_locks');
+$cp->drop_schema_if_exists($fakeschema);
exit;
$host = $cp->get_host();
my $label = 'POSTGRES_QUERY_RUNTIME';
-local $dbh->{Warn} = 0;
-$dbh->do(qq{DROP TABLE IF EXISTS $testtbl CASCADE});
-$dbh->do(qq{DROP VIEW IF EXISTS $testview});
+$cp->drop_table_if_exists($testtbl);
+$cp->drop_view_if_exists($testview);
+
$dbh->do(qq{CREATE TABLE "$testtbl" ("a" integer)}) or die $dbh->errstr;
$dbh->commit;
like ($cp->run('--critical=80'), qr{ERROR:.+must be a percentage}, $t);
my $seqname = 'cp_test_sequence';
-{
- local $dbh->{Warn};
- $dbh->do("DROP SEQUENCE IF EXISTS $seqname");
- $dbh->commit();
-}
+$cp->drop_sequence_if_exists($seqname);
$t=qq{$S works when no sequences exist};
like ($cp->run(''), qr{OK:.+No sequences found}, $t);
like ($cp->run(''), qr{ERROR:\s*(?:Could not determine|cannot access temporary)}, $t);
$dbh->do("CREATE SEQUENCE $seqname");
-$dbh->commit();
-
-{
- local $dbh->{Warn};
- $dbh->do("DROP SEQUENCE IF EXISTS ${seqname}2");
- $dbh->commit();
-}
+$cp->drop_sequence_if_exists($seqname.'2');
$t=qq{$S returns correct information for a new sequence};
like ($cp->run(''), qr{OK:.+public.cp_test_sequence=0% \(calls left=9223372036854775806\)}, $t);
local $dbh->{Warn} = 0;
$dbh->do('DROP SCHEMA cptest CASCADE');
-$dbh->do('DROP FUNCTION IF EXISTS public.version()');
$cp->reset_path();
exit;
like ($cp->run('--critical=30'), qr{POSTGRES_WAL_FILES OK}, $t);
like ($cp->run('--critical=0'), qr{POSTGRES_WAL_FILES CRITICAL}, $t);
+$cp->drop_schema_if_exists($fakeschema);
$cp->create_fake_pg_table('pg_ls_dir', 'text');
like ($cp->run('--critical=1'), qr{POSTGRES_WAL_FILES OK}, $t);
$t=qq{$S returns correct MRTG information};
is ($cp->run('--critical=101 --output=mrtg'), "99\n0\n\n\n", $t);
-$cp->remove_fake_pg_table('pg_ls_dir()');
+$cp->drop_schema_if_exists($fakeschema);
exit;
my $count = 1;
for my $filename (sort @testfiles) {
-e $filename or die qq{Could not find "$filename"!};
- pass('Skipping test files for now');
- next;
my @vio = $critic->critique($filename);
my $vios = 0;
$dbh->{RaiseError} = 1;
if (! exists $self->{keep_old_schema}) {
- local $dbh->{Warn};
- $dbh->do("DROP SCHEMA IF EXISTS $fakeschema CASCADE");
+ my $SQL = "SELECT count(*) FROM pg_namespace WHERE nspname = " . $dbh->quote($fakeschema);
+ my $count = $dbh->selectall_arrayref($SQL)->[0][0];
+ if ($count) {
+ local $dbh->{Warn} = 0;
+ $dbh->do("DROP SCHEMA $fakeschema CASCADE");
+ }
}
if ($arg->{dbname} ne $self->{dbname}) {
my $dbh = $self->{dbh} || die;
my $dbuser = $self->{testuser} || die;
if ($self->schema_exists($dbh,$fakeschema)) {
- local $dbh->{Warn};
- $dbh->do("DROP TABLE IF EXISTS $fakeschema.$name");
+ $self->drop_table_if_exists($fakeschema,$name);
}
else {
$dbh->do("CREATE SCHEMA $fakeschema");
if ($args) {
local $dbh->{Warn};
- $dbh->do("DROP FUNCTION IF EXISTS $fakeschema.$name($args)");
+ $self->drop_function_if_exists($fakeschema,$name,$args);
$dbh->do("CREATE FUNCTION $fakeschema.$name($args) RETURNS SETOF TEXT LANGUAGE SQL AS 'SELECT * FROM $fakeschema.$name; '");
}
} ## end of set_fake_schema
-sub remove_fake_pg_table {
-
- my $self = shift;
- my $name = shift || die;
- (my $name2 = $name) =~ s/\(.+//;
- my $dbh = $self->{dbh} || die;
- my $dbuser = $self->{testuser} || die;
- {
- local $dbh->{Warn};
- $dbh->do("DROP TABLE IF EXISTS public.$name2");
- }
- $dbh->do("ALTER USER $dbuser SET search_path = public");
- $dbh->commit();
-
-} ## end of remove_fake_pg_table
-
-
sub table_exists {
my ($self,$dbh,$table) = @_;
} ## end of schema_exists
+sub drop_schema_if_exists {
+
+ my ($self,$name) = @_;
+ my $dbh = $self->{dbh} || die;
+
+ if (! exists $self->{keep_old_schema}) {
+ my $SQL = "SELECT count(*) FROM pg_namespace WHERE nspname = " . $dbh->quote($name);
+ my $count = $dbh->selectall_arrayref($SQL)->[0][0];
+ if ($count) {
+ local $dbh->{Warn};
+ $dbh->do("DROP SCHEMA $name CASCADE");
+ $dbh->commit();
+ }
+ }
+ return;
+
+} ## end of drop_schema_if_exists
+
+
+sub drop_table_if_exists {
+
+ my ($self,$name,$name2) = @_;
+ my $dbh = $self->{dbh} || die;
+
+ my $schema = '';
+ if ($name2) {
+ $schema = $name;
+ $name = $name2;
+ }
+
+ my $safetable = $dbh->quote($name);
+ my $safeschema = $dbh->quote($schema);
+ my $SQL = $schema
+ ? q{SELECT count(*) FROM pg_class c JOIN pg_namespace n ON (n.oid = c.relnamespace) }.
+ qq{WHERE relkind = 'r' AND nspname = $safeschema AND relname = $safetable}
+ : qq{SELECT count(*) FROM pg_class WHERE relkind='r' AND relname = $safetable};
+ my $count = $dbh->selectall_arrayref($SQL)->[0][0];
+ if ($count) {
+ local $dbh->{Warn};
+ $dbh->do("DROP TABLE $name CASCADE");
+ $dbh->commit();
+ }
+ return;
+
+} ## end of drop_table_if_exists
+
+
+sub drop_view_if_exists {
+
+ my ($self,$name) = @_;
+ my $dbh = $self->{dbh} || die;
+
+ my $SQL = q{SELECT count(*) FROM pg_class WHERE relkind='v' AND relname = } . $dbh->quote($name);
+ my $count = $dbh->selectall_arrayref($SQL)->[0][0];
+ if ($count) {
+ local $dbh->{Warn};
+ $dbh->do("DROP VIEW $name");
+ $dbh->commit();
+ }
+ return;
+
+} ## end of drop_view_if_exists
+
+
+sub drop_sequence_if_exists {
+
+ my ($self,$name) = @_;
+ my $dbh = $self->{dbh} || die;
+
+ my $SQL = q{SELECT count(*) FROM pg_class WHERE relkind = 'S' AND relname = } . $dbh->quote($name);
+ my $count = $dbh->selectall_arrayref($SQL)->[0][0];
+ if ($count) {
+ $dbh->do("DROP SEQUENCE $name");
+ $dbh->commit();
+ }
+
+} ## end of drop_sequence_if_exists
+
+
+sub drop_function_if_exists {
+
+ my ($self,$name,$args) = @_;
+ my $dbh = $self->{dbh} || die;
+
+ my $SQL = q{SELECT count(*) FROM pg_proc WHERE proname = }. $dbh->quote($name);
+ my $count = $dbh->selectall_arrayref($SQL)->[0][0];
+ if ($count) {
+ $dbh->do("DROP FUNCTION $name($args)");
+ $dbh->commit();
+ }
+ return;
+
+} ## end of drop_function_if_exists
+
+
sub fake_version {
my $self = shift;
});
$dbh->do("ALTER USER $dbuser SET search_path = $fakeschema, public, pg_catalog");
$dbh->commit();
+ return;
} ## end of fake version
-sub bad_fake_version {
-
- my $self = shift;
- my $version = shift || '9.9';
- my $dbh = $self->{dbh} || die;
- my $dbuser = $self->{testuser} || die;
-
- $dbh->do(qq{
-CREATE OR REPLACE FUNCTION public.version()
-RETURNS TEXT
-LANGUAGE SQL
-AS \$\$
-SELECT 'Postgres $version on fakefunction for check_postgres.pl testing'::text;
-\$\$
-});
- $dbh->do("ALTER USER $dbuser SET search_path = public, pg_catalog");
- $dbh->commit();
-
-} ## end of bad fake version
-
sub fake_self_version {
## Look out...
} ## end of fake_self_version
+
sub restore_self_version {
my $self = shift;