From c5292f42b7d9cdc8dd56b425f5ea75f7fff0278c Mon Sep 17 00:00:00 2001 From: Tatsuhiko Miyagawa Date: Wed, 13 May 2009 21:17:02 +0000 Subject: [PATCH] PostgreSQL patch from clkao --- doc/schema-postgres.sql | 5 ++-- t/api.t | 2 +- t/cleanup.t | 4 +-- t/coalesce.t | 2 +- t/dead-dbs.t | 2 +- t/empty-db.t | 6 ++--- t/evenly-distribute.t | 6 ++--- t/fail-working-multiple.t | 2 +- t/funcid.t | 2 +- t/grab_and_work_on.t | 2 +- t/high-funcid-starvation.t | 4 +-- t/insert-and-do.t | 2 +- t/lib/db-common.pl | 51 ++++++++++++++++++++++++++++++++--- t/priority.t | 3 ++- t/replace-with.t | 2 +- t/retry-delay.t | 2 +- t/scoreboard.t | 6 ++--- t/server-time.t | 2 +- t/unique.t | 2 +- t/work-before-funcids-exist.t | 2 +- 20 files changed, 77 insertions(+), 32 deletions(-) diff --git a/doc/schema-postgres.sql b/doc/schema-postgres.sql index c6ad03f..04dc755 100644 --- a/doc/schema-postgres.sql +++ b/doc/schema-postgres.sql @@ -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); diff --git a/t/api.t b/t/api.t index 2032588..96dc273 100644 --- a/t/api.t +++ b/t/api.t @@ -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_") { diff --git a/t/cleanup.t b/t/cleanup.t index e85ed72..e02d762 100644 --- a/t/cleanup.t +++ b/t/cleanup.t @@ -6,7 +6,7 @@ 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 @@ -14,7 +14,7 @@ $TheSchwartz::T_ERRORS_MAX_AGE = 2; # keep errors for 3 seconds, not 1 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"); diff --git a/t/coalesce.t b/t/coalesce.t index a40ac0d..1038f33 100644 --- a/t/coalesce.t +++ b/t/coalesce.t @@ -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']); diff --git a/t/dead-dbs.t b/t/dead-dbs.t index 251e6ba..db64ee9 100644 --- a/t/dead-dbs.t +++ b/t/dead-dbs.t @@ -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'); diff --git a/t/empty-db.t b/t/empty-db.t index 6ef8c6f..e908f9a 100644 --- a/t/empty-db.t +++ b/t/empty-db.t @@ -7,7 +7,7 @@ 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"); @@ -15,8 +15,8 @@ run_tests(3, sub { my $client = TheSchwartz->new(databases => [ { dsn => dsn_for('tempty1'), - user => "root", - pass => "", + user => $ENV{TS_DB_USER}, + pass => $ENV{TS_DB_PASS}, }, ]); diff --git a/t/evenly-distribute.t b/t/evenly-distribute.t index 9155c27..f3fedcc 100644 --- a/t/evenly-distribute.t +++ b/t/evenly-distribute.t @@ -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']); @@ -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"); diff --git a/t/fail-working-multiple.t b/t/fail-working-multiple.t index eca5725..be4dbd4 100644 --- a/t/fail-working-multiple.t +++ b/t/fail-working-multiple.t @@ -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']); diff --git a/t/funcid.t b/t/funcid.t index a9ba62f..6e41092 100644 --- a/t/funcid.t +++ b/t/funcid.t @@ -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 { diff --git a/t/grab_and_work_on.t b/t/grab_and_work_on.t index af5265e..8317358 100644 --- a/t/grab_and_work_on.t +++ b/t/grab_and_work_on.t @@ -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']); diff --git a/t/high-funcid-starvation.t b/t/high-funcid-starvation.t index 2b764e3..d34e50b 100644 --- a/t/high-funcid-starvation.t +++ b/t/high-funcid-starvation.t @@ -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']); @@ -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"); diff --git a/t/insert-and-do.t b/t/insert-and-do.t index bfee381..dc944a1 100644 --- a/t/insert-and-do.t +++ b/t/insert-and-do.t @@ -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']); diff --git a/t/lib/db-common.pl b/t/lib/db-common.pl index 7c8daef..ea24ab9 100755 --- a/t/lib/db-common.pl +++ b/t/lib/db-common.pl @@ -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); } @@ -20,6 +21,7 @@ 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); @@ -27,6 +29,17 @@ sub run_tests_mysql { } } +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) = @_; @@ -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 ]); @@ -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"; } @@ -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); } @@ -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); } @@ -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"); @@ -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; diff --git a/t/priority.t b/t/priority.t index 5713c9d..c54b12f 100644 --- a/t/priority.t +++ b/t/priority.t @@ -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; @@ -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; diff --git a/t/replace-with.t b/t/replace-with.t index cc5051c..4187081 100644 --- a/t/replace-with.t +++ b/t/replace-with.t @@ -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']); diff --git a/t/retry-delay.t b/t/retry-delay.t index 23dfbf4..dd6f5dd 100644 --- a/t/retry-delay.t +++ b/t/retry-delay.t @@ -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']); diff --git a/t/scoreboard.t b/t/scoreboard.t index 59aec26..2e52320 100644 --- a/t/scoreboard.t +++ b/t/scoreboard.t @@ -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(); @@ -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 ]); diff --git a/t/server-time.t b/t/server-time.t index 6853444..df8e6ee 100644 --- a/t/server-time.t +++ b/t/server-time.t @@ -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']); diff --git a/t/unique.t b/t/unique.t index b605d38..8625e38 100644 --- a/t/unique.t +++ b/t/unique.t @@ -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; diff --git a/t/work-before-funcids-exist.t b/t/work-before-funcids-exist.t index 6bea2ad..e8cfd38 100644 --- a/t/work-before-funcids-exist.t +++ b/t/work-before-funcids-exist.t @@ -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']);