Skip to content

Commit

Permalink
PostgreSQL patch from clkao
Browse files Browse the repository at this point in the history
  • Loading branch information
miyagawa committed May 13, 2009
1 parent 2c7df63 commit c5292f4
Show file tree
Hide file tree
Showing 20 changed files with 77 additions and 32 deletions.
5 changes: 3 additions & 2 deletions doc/schema-postgres.sql
Expand Up @@ -31,10 +31,11 @@ CREATE TABLE job (
run_after INTEGER NOT NULL,
grabbed_until INTEGER NOT NULL,
priority SMALLINT,
coalesce VARCHAR(255),
UNIQUE(funcid, uniqkey)
coalesce VARCHAR(255)
);

CREATE UNIQUE INDEX job_funcid_uniqkey ON job (funcid, uniqkey);

CREATE INDEX job_funcid_runafter ON job (funcid, run_after);

CREATE INDEX job_funcid_coalesce ON job (funcid, coalesce);
Expand Down
2 changes: 1 addition & 1 deletion t/api.t
Expand Up @@ -7,7 +7,7 @@ use warnings;
require 't/lib/db-common.pl';

use TheSchwartz;
use Test::More tests => 108;
use Test::More tests => 54*3;

run_tests(54, sub {
foreach my $pfx ("", "testprefix_") {
Expand Down
4 changes: 2 additions & 2 deletions t/cleanup.t
Expand Up @@ -6,15 +6,15 @@ use warnings;
require 't/lib/db-common.pl';

use TheSchwartz;
use Test::More tests => 20;
use Test::More tests => 30;

# for testing:
$TheSchwartz::T_EXITSTATUS_CLEAN_THRES = 1; # delete 100% of the time, not 10% of the time
$TheSchwartz::T_ERRORS_MAX_AGE = 2; # keep errors for 3 seconds, not 1 week

run_tests(10, sub {
my $client = test_client(dbs => ['ts1']);
my $dbh = DBI->connect(dsn_for("ts1"), 'root', '');
my $dbh = DBI->connect(dsn_for("ts1"), $ENV{TS_DB_USER}, $ENV{TS_DB_PASS});
$client->can_do("Worker::Fail");
$client->can_do("Worker::Complete");

Expand Down
2 changes: 1 addition & 1 deletion t/coalesce.t
Expand Up @@ -6,7 +6,7 @@ use warnings;
require 't/lib/db-common.pl';

use TheSchwartz;
use Test::More tests => 28;
use Test::More tests => 14 * 3;

run_tests(14, sub {
my $client = test_client(dbs => ['ts1']);
Expand Down
2 changes: 1 addition & 1 deletion t/dead-dbs.t
Expand Up @@ -7,7 +7,7 @@ use warnings;
require 't/lib/db-common.pl';

use TheSchwartz;
use Test::More tests => 4;
use Test::More tests => 6;

run_tests(2, sub {
setup_dbs('ts1');
Expand Down
6 changes: 3 additions & 3 deletions t/empty-db.t
Expand Up @@ -7,16 +7,16 @@ use warnings;
require 't/lib/db-common.pl';

use TheSchwartz;
use Test::More tests => 6;
use Test::More tests => 9;

run_tests(3, sub {
teardown_dbs("tempty1");

my $client = TheSchwartz->new(databases => [
{
dsn => dsn_for('tempty1'),
user => "root",
pass => "",
user => $ENV{TS_DB_USER},
pass => $ENV{TS_DB_PASS},
},
]);

Expand Down
6 changes: 3 additions & 3 deletions t/evenly-distribute.t
Expand Up @@ -7,7 +7,7 @@ use warnings;
require 't/lib/db-common.pl';

use TheSchwartz;
use Test::More tests => 8;
use Test::More tests => 12;

run_tests(4, sub {
my $client = test_client(dbs => ['ts1', 'ts2']);
Expand All @@ -18,8 +18,8 @@ run_tests(4, sub {
die unless $handle;
}

my $db1 = DBI->connect(dsn_for("ts1"), 'root', '');
my $db2 = DBI->connect(dsn_for("ts2"), 'root', '');
my $db1 = DBI->connect(dsn_for("ts1"), $ENV{TS_DB_USER}, $ENV{TS_DB_PASS});
my $db2 = DBI->connect(dsn_for("ts2"), $ENV{TS_DB_USER}, $ENV{TS_DB_PASS});
die unless $db1 && $db2;

my $jobs1 = $db1->selectrow_array("SELECT COUNT(*) FROM job");
Expand Down
2 changes: 1 addition & 1 deletion t/fail-working-multiple.t
Expand Up @@ -6,7 +6,7 @@ use warnings;
require 't/lib/db-common.pl';

use TheSchwartz;
use Test::More tests => 8;
use Test::More tests => 12;

run_tests(4, sub {
my $client = test_client(dbs => ['ts1']);
Expand Down
2 changes: 1 addition & 1 deletion t/funcid.t
Expand Up @@ -7,7 +7,7 @@ use warnings;
require 't/lib/db-common.pl';

use TheSchwartz;
use Test::More tests => 16;
use Test::More tests => 24;

run_tests(8, sub {

Expand Down
2 changes: 1 addition & 1 deletion t/grab_and_work_on.t
Expand Up @@ -6,7 +6,7 @@ use warnings;
require 't/lib/db-common.pl';

use TheSchwartz;
use Test::More tests => 18;
use Test::More tests => 27;

run_tests(9, sub {
my $client = test_client(dbs => ['ts1']);
Expand Down
4 changes: 2 additions & 2 deletions t/high-funcid-starvation.t
Expand Up @@ -7,7 +7,7 @@ use warnings;
require 't/lib/db-common.pl';

use TheSchwartz;
use Test::More tests => 8;
use Test::More tests => 12;

run_tests(4, sub {
my $client = test_client(dbs => ['ts1']);
Expand All @@ -18,7 +18,7 @@ run_tests(4, sub {
$client->insert("Worker::Job2") or die;
}

my $db1 = DBI->connect(dsn_for("ts1"), 'root', '');
my $db1 = DBI->connect(dsn_for("ts1"), $ENV{TS_DB_USER}, $ENV{TS_DB_PASS});
die unless $db1;

my $jobs1 = $db1->selectrow_array("SELECT COUNT(*) FROM job WHERE funcid=1");
Expand Down
2 changes: 1 addition & 1 deletion t/insert-and-do.t
Expand Up @@ -6,7 +6,7 @@ use warnings;
require 't/lib/db-common.pl';

use TheSchwartz;
use Test::More tests => 52;
use Test::More tests => 26*3;

run_tests(26, sub {
my $client = test_client(dbs => ['ts1']);
Expand Down
51 changes: 47 additions & 4 deletions t/lib/db-common.pl
Expand Up @@ -8,6 +8,7 @@ sub run_tests {
my ($n, $code) = @_;

run_tests_mysql($n, $code);
run_tests_pgsql($n, $code);
run_tests_sqlite($n, $code);
}

Expand All @@ -20,13 +21,25 @@ sub run_tests_mysql {
my ($n, $code, $innodb) = @_;
SKIP: {
local $ENV{USE_MYSQL} = 1;
local $ENV{TS_DB_USER} ||= 'root';
my $dbh = eval { mysql_dbh() };
skip "MySQL not accessible as root on localhost", $n if $@;
skip "InnoDB not available on localhost's MySQL", $n if $innodb && ! has_innodb($dbh);
$code->();
}
}

sub run_tests_pgsql {
my ($n, $code) = @_;
SKIP: {
local $ENV{USE_PGSQL} = 1;
local $ENV{TS_DB_USER} ||= 'postgres';
my $dbh = eval { pgsql_dbh() };
skip "PgSQL not accessible as root on localhost", $n if $@;
$code->();
}
}

sub run_tests_sqlite {
my ($n, $code) = @_;

Expand Down Expand Up @@ -69,8 +82,8 @@ sub test_client {
return TheSchwartz->new(databases => [
map { {
dsn => dsn_for($_),
user => "root",
pass => "",
user => $ENV{TS_DB_USER},
pass => $ENV{TS_DB_PASS},
prefix => $pfx,
} } @$dbs
]);
Expand All @@ -91,6 +104,7 @@ sub has_innodb {

sub schema_file {
return "doc/schema.sql" if $ENV{USE_MYSQL};
return "doc/schema-postgres.sql" if $ENV{USE_PGSQL};
return "t/schema-sqlite.sql";
}

Expand All @@ -108,6 +122,9 @@ sub dsn_for {
my $dbname = shift;
if ($ENV{USE_MYSQL}) {
return 'dbi:mysql:' . mysql_dbname($dbname);
}
elsif ($ENV{USE_PGSQL}) {
return 'dbi:Pg:dbname=' . mysql_dbname($dbname);
} else {
return 'dbi:SQLite:dbname=' . db_filename($dbname);
}
Expand All @@ -129,12 +146,16 @@ sub setup_dbs {
if ($ENV{USE_MYSQL}) {
create_mysql_db(mysql_dbname($dbname));
}
elsif ($ENV{USE_PGSQL}) {
create_pgsql_db(mysql_dbname($dbname));
}
my $dbh = DBI->connect(dsn_for($dbname),
'root', '', { RaiseError => 1, PrintError => 0 })
$ENV{TS_DB_USER}, $ENV{TS_DB_PASS}, { RaiseError => 1, PrintError => 0 })
or die "Couldn't connect: $!\n";
my @sql = load_sql($schema);
for my $sql (@sql) {
$sql =~ s!^\s*create\s+table\s+(\w+)!CREATE TABLE ${pfx}$1!i;
$sql =~ s!^\s*create\s+table\s+(\w+)!CREATE TABLE ${pfx}$1!mi;
$sql =~ s!^\s*(create.*?index)\s+(\w+)\s+on\s+(\w+)!$1 $2 ON ${pfx}$3!i;
$sql .= " ENGINE=INNODB\n" if $ENV{USE_MYSQL};
$dbh->do($sql);
}
Expand All @@ -147,6 +168,15 @@ sub mysql_dbh {
or die "Couldn't connect to database";
}

my $pg_dbh;

sub pgsql_dbh {
return $pg_dbh if $pg_dbh;
$pg_dbh ||=
DBI->connect("DBI:Pg:dbname=postgres", "postgres", "", { RaiseError => 1 })
or die "Couldn't connect to database";
}

sub create_mysql_db {
my $dbname = shift;
mysql_dbh()->do("CREATE DATABASE $dbname");
Expand All @@ -157,11 +187,24 @@ sub drop_mysql_db {
mysql_dbh()->do("DROP DATABASE IF EXISTS $dbname");
}

sub create_pgsql_db {
my $dbname = shift;
pgsql_dbh()->do("CREATE DATABASE $dbname");
}

sub drop_pgsql_db {
my $dbname = shift;
undef $pg_dbh;
eval { pgsql_dbh()->do("DROP DATABASE IF EXISTS $dbname") };
}

sub teardown_dbs {
my(@dbs) = @_;
for my $db (@dbs) {
if ($ENV{USE_MYSQL}) {
drop_mysql_db(mysql_dbname($db));
} elsif ($ENV{USE_PGSQL}) {
drop_pgsql_db(mysql_dbname($db));
} else {
my $file = db_filename($db);
next unless -e $file;
Expand Down
3 changes: 2 additions & 1 deletion t/priority.t
Expand Up @@ -6,7 +6,7 @@ use warnings;
require 't/lib/db-common.pl';

use TheSchwartz;
use Test::More tests => 62;
use Test::More tests => 31*3;

our $record_expected;

Expand Down Expand Up @@ -57,6 +57,7 @@ sub set_client { $client = $_[1]; }
sub work {
my ($class, $job) = @_;
my $priority = $job->priority;

ok((!defined($main::record_expected) && (!defined($priority)))
|| ($priority == $main::record_expected), "priority matches expected priority");
$job->completed;
Expand Down
2 changes: 1 addition & 1 deletion t/replace-with.t
Expand Up @@ -6,7 +6,7 @@ use warnings;
require 't/lib/db-common.pl';

use TheSchwartz;
use Test::More tests => 20;
use Test::More tests => 30;

run_tests(10, sub {
my $client = test_client(dbs => ['ts1']);
Expand Down
2 changes: 1 addition & 1 deletion t/retry-delay.t
Expand Up @@ -7,7 +7,7 @@ use warnings;
require 't/lib/db-common.pl';

use TheSchwartz;
use Test::More tests => 16;
use Test::More tests => 24;

run_tests(8, sub {
my $client = test_client(dbs => ['ts1']);
Expand Down
6 changes: 3 additions & 3 deletions t/scoreboard.t
Expand Up @@ -5,7 +5,7 @@ use warnings;

require 't/lib/db-common.pl';

use Test::More tests => 20;
use Test::More tests => 30;

use TheSchwartz;
use File::Spec qw();
Expand All @@ -25,8 +25,8 @@ run_tests(10, sub {
databases => [
map { {
dsn => dsn_for($_),
user => "root",
pass => "",
user => $ENV{TS_DB_USER},
pass => $ENV{TS_DB_PASS},
prefix => $pfx,
} } @$dbs
]);
Expand Down
2 changes: 1 addition & 1 deletion t/server-time.t
Expand Up @@ -6,7 +6,7 @@ use warnings;
require 't/lib/db-common.pl';

use TheSchwartz;
use Test::More tests => 4;
use Test::More tests => 6;

run_tests(2, sub {
my $client = test_client(dbs => ['ts1']);
Expand Down
2 changes: 1 addition & 1 deletion t/unique.t
Expand Up @@ -7,7 +7,7 @@ use warnings;
require 't/lib/db-common.pl';

use TheSchwartz;
use Test::More tests => 12;
use Test::More tests => 18;

#use Data::ObjectDriver;
#$Data::ObjectDriver::DEBUG = 1;
Expand Down
2 changes: 1 addition & 1 deletion t/work-before-funcids-exist.t
Expand Up @@ -7,7 +7,7 @@ use warnings;
require 't/lib/db-common.pl';

use TheSchwartz;
use Test::More tests => 4;
use Test::More tests => 6;

run_tests(2, sub {
my $client = test_client(dbs => ['ts1']);
Expand Down

0 comments on commit c5292f4

Please sign in to comment.