From 179b012a8e804dd0a9a4fe1fee0d15e7dcb47fa7 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 13 Mar 2012 12:10:19 +0100 Subject: [PATCH] Open the DB using Hydra::Model::DB->new This gets rid of the openHydraDB function and ensures that we open the database in a consistent way. Also drop the PostgreSQL sequence hacks. They don't seem to be necessary anymore. --- src/lib/Hydra/Helper/AddBuilds.pm | 3 ++- src/lib/Hydra/Helper/Nix.pm | 33 +++-------------------------- src/lib/Hydra/Model/DB.pm | 15 ++++++++++++- src/lib/Hydra/Schema/BuildInputs.pm | 12 ----------- src/lib/Hydra/Schema/Builds.pm | 11 ---------- src/lib/Hydra/Schema/JobsetEvals.pm | 8 ------- src/lib/Hydra/Schema/NewsItems.pm | 8 ------- src/script/hydra-build | 3 ++- src/script/hydra-evaluator | 4 ++-- src/script/hydra-init | 3 ++- src/script/hydra-queue-runner | 5 +++-- src/script/hydra-update-gc-roots | 5 +++-- 12 files changed, 31 insertions(+), 79 deletions(-) diff --git a/src/lib/Hydra/Helper/AddBuilds.pm b/src/lib/Hydra/Helper/AddBuilds.pm index 3eff4f75..27e68737 100644 --- a/src/lib/Hydra/Helper/AddBuilds.pm +++ b/src/lib/Hydra/Helper/AddBuilds.pm @@ -6,6 +6,7 @@ use XML::Simple; use POSIX qw(strftime); use IPC::Run; use Nix::Store; +use Hydra::Model::DB; use Hydra::Helper::Nix; use Digest::SHA qw(sha256_hex); use File::Basename; @@ -22,7 +23,7 @@ our @EXPORT = qw( sub scmPath { - return getHydraPath . "/scm" ; + return Hydra::Model::DB::getHydraPath . "/scm" ; } diff --git a/src/lib/Hydra/Helper/Nix.pm b/src/lib/Hydra/Helper/Nix.pm index 07ea5858..ab87989e 100644 --- a/src/lib/Hydra/Helper/Nix.pm +++ b/src/lib/Hydra/Helper/Nix.pm @@ -5,23 +5,17 @@ use Exporter; use File::Path; use File::Basename; use Hydra::Helper::CatalystUtils; +use Hydra::Model::DB; our @ISA = qw(Exporter); our @EXPORT = qw( - getHydraPath getHydraHome getHydraDBPath openHydraDB getHydraConf txn_do + getHydraHome getHydraConf txn_do registerRoot getGCRootsDir gcRootFor getPrimaryBuildsForView getPrimaryBuildTotal getViewResult getLatestSuccessfulViewResult jobsetOverview removeAsciiEscapes); -sub getHydraPath { - my $dir = $ENV{"HYDRA_DATA"} || "/var/lib/hydra"; - die "The HYDRA_DATA directory ($dir) does not exist!\n" unless -d $dir; - return $dir; -} - - sub getHydraHome { my $dir = $ENV{"HYDRA_HOME"} or die "The HYDRA_HOME directory does not exist!\n"; return $dir; @@ -29,33 +23,12 @@ sub getHydraHome { sub getHydraConf { - my $conf = $ENV{"HYDRA_CONFIG"} || (getHydraPath . "/hydra.conf"); + my $conf = $ENV{"HYDRA_CONFIG"} || (Hydra::Model::DB::getHydraPath . "/hydra.conf"); die "The HYDRA_CONFIG file ($conf) does not exist!\n" unless -f $conf; return $conf; } -sub getHydraDBPath { - my $db = $ENV{"HYDRA_DBI"}; - if ( defined $db ) { - return $db ; - } - else { - my $path = getHydraPath . '/hydra.sqlite'; - die "The Hydra database ($path) not exist!\n" unless -f $path; - return "dbi:SQLite:$path"; - } -} - - -sub openHydraDB { - my $db = Hydra::Schema->connect(getHydraDBPath, "", "", {}); - $db->storage->dbh->do("PRAGMA synchronous = OFF;") - if defined $ENV{'HYDRA_NO_FSYNC'}; - return $db; -} - - # Awful hack to handle timeouts in SQLite: just retry the transaction. # DBD::SQLite *has* a 30 second retry window, but apparently it # doesn't work. diff --git a/src/lib/Hydra/Model/DB.pm b/src/lib/Hydra/Model/DB.pm index eb5f5168..c76ce37c 100644 --- a/src/lib/Hydra/Model/DB.pm +++ b/src/lib/Hydra/Model/DB.pm @@ -2,7 +2,20 @@ package Hydra::Model::DB; use strict; use base 'Catalyst::Model::DBIC::Schema'; -use Hydra::Helper::Nix; + +sub getHydraPath { + my $dir = $ENV{"HYDRA_DATA"} || "/var/lib/hydra"; + die "The HYDRA_DATA directory ($dir) does not exist!\n" unless -d $dir; + return $dir; +} + +sub getHydraDBPath { + my $db = $ENV{"HYDRA_DBI"}; + return $db if defined $db; + my $path = getHydraPath . '/hydra.sqlite'; + die "The Hydra database ($path) not exist!\n" unless -f $path; + return "dbi:SQLite:$path"; +} __PACKAGE__->config( schema_class => 'Hydra::Schema', diff --git a/src/lib/Hydra/Schema/BuildInputs.pm b/src/lib/Hydra/Schema/BuildInputs.pm index 3cf6c687..3ba61a46 100644 --- a/src/lib/Hydra/Schema/BuildInputs.pm +++ b/src/lib/Hydra/Schema/BuildInputs.pm @@ -156,16 +156,4 @@ __PACKAGE__->belongs_to( # Created by DBIx::Class::Schema::Loader v0.07014 @ 2011-12-05 14:15:43 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:yaqa9gcGx9Z+Nfr3xgX28g -use Hydra::Helper::Nix; - -sub addSequence { - my $hydradbi = getHydraDBPath ; - if ($hydradbi =~ m/^dbi:Pg/) { - __PACKAGE__->sequence('builds_id_seq'); - } -} - -addSequence ; - -# You can replace this text with custom content, and it will be preserved on regeneration 1; diff --git a/src/lib/Hydra/Schema/Builds.pm b/src/lib/Hydra/Schema/Builds.pm index 12040b92..def719e8 100644 --- a/src/lib/Hydra/Schema/Builds.pm +++ b/src/lib/Hydra/Schema/Builds.pm @@ -432,8 +432,6 @@ __PACKAGE__->has_many( # Created by DBIx::Class::Schema::Loader v0.07014 @ 2012-02-29 18:56:22 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:w16c86FRReLPdA8H0yTIRg -use Hydra::Helper::Nix; - __PACKAGE__->has_many( "dependents", "Hydra::Schema::BuildInputs", @@ -456,13 +454,6 @@ __PACKAGE__->has_one( }, ); -sub addSequence { - my $hydradbi = getHydraDBPath; - if ($hydradbi =~ m/^dbi:Pg/) { - __PACKAGE__->sequence('builds_id_seq'); - } -} - sub makeSource { my ($name, $query) = @_; my $source = __PACKAGE__->result_source_instance(); @@ -526,8 +517,6 @@ QUERY ); } -addSequence; - makeQueries('', ""); makeQueries('ForProject', "and project = ?"); makeQueries('ForJobset', "and project = ? and jobset = ?"); diff --git a/src/lib/Hydra/Schema/JobsetEvals.pm b/src/lib/Hydra/Schema/JobsetEvals.pm index f3feaa16..27145d15 100644 --- a/src/lib/Hydra/Schema/JobsetEvals.pm +++ b/src/lib/Hydra/Schema/JobsetEvals.pm @@ -145,14 +145,6 @@ __PACKAGE__->belongs_to("project", "Hydra::Schema::Projects", { name => "project # Created by DBIx::Class::Schema::Loader v0.07014 @ 2011-12-05 14:15:43 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:eQtF5bcR/qZ625LxWBc7ug -use Hydra::Helper::Nix; - -# !!! Ugly, should be generated. -my $hydradbi = getHydraDBPath; -if ($hydradbi =~ m/^dbi:Pg/) { - __PACKAGE__->sequence('jobsetevals_id_seq'); -} - __PACKAGE__->has_many( "buildIds", "Hydra::Schema::JobsetEvalMembers", diff --git a/src/lib/Hydra/Schema/NewsItems.pm b/src/lib/Hydra/Schema/NewsItems.pm index 2c731440..6b4aa9d1 100644 --- a/src/lib/Hydra/Schema/NewsItems.pm +++ b/src/lib/Hydra/Schema/NewsItems.pm @@ -86,12 +86,4 @@ __PACKAGE__->belongs_to("author", "Hydra::Schema::Users", { username => "author" # Created by DBIx::Class::Schema::Loader v0.07014 @ 2011-12-05 14:15:43 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:YRMh0QI4JezFLj7nywGu6Q -use Hydra::Helper::Nix; - -# !!! Ugly, should be generated. -my $hydradbi = getHydraDBPath; -if ($hydradbi =~ m/^dbi:Pg/) { - __PACKAGE__->sequence('newsitems_id_seq'); -} - 1; diff --git a/src/script/hydra-build b/src/script/hydra-build index 60e27e64..aeb70574 100755 --- a/src/script/hydra-build +++ b/src/script/hydra-build @@ -6,6 +6,7 @@ use File::stat; use Nix::Store; use Hydra::Schema; use Hydra::Helper::Nix; +use Hydra::Model::DB; use Hydra::Helper::AddBuilds; use Email::Sender::Simple qw(sendmail); use Email::Sender::Transport::SMTP; @@ -21,7 +22,7 @@ use feature qw/switch/; STDOUT->autoflush(); -my $db = openHydraDB; +my $db = Hydra::Model::DB->new(); my %config = new Config::General(getHydraConf)->getall; diff --git a/src/script/hydra-evaluator b/src/script/hydra-evaluator index 2765458f..9b0165a0 100755 --- a/src/script/hydra-evaluator +++ b/src/script/hydra-evaluator @@ -5,8 +5,8 @@ use feature 'switch'; use Hydra::Schema; use Hydra::Helper::Nix; use Hydra::Helper::AddBuilds; +use Hydra::Model::DB; use Digest::SHA qw(sha256_hex); - use Email::Sender::Simple qw(sendmail); use Email::Sender::Transport::SMTP; use Email::Simple; @@ -17,7 +17,7 @@ use Data::Dump qw(dump); STDOUT->autoflush(); -my $db = openHydraDB; +my $db = Hydra::Model::DB->new(); my %config = new Config::General(getHydraConf)->getall; diff --git a/src/script/hydra-init b/src/script/hydra-init index a0cbe146..0163d30a 100755 --- a/src/script/hydra-init +++ b/src/script/hydra-init @@ -3,11 +3,12 @@ use strict; use Hydra::Schema; use Hydra::Helper::Nix; +use Hydra::Model::DB; use File::Slurp; use SQL::SplitStatement; use List::Util qw(max); -my $db = openHydraDB; +my $db = Hydra::Model::DB->new(); my $dbh = $db->storage->dbh; $dbh->{RaiseError} = 1; diff --git a/src/script/hydra-queue-runner b/src/script/hydra-queue-runner index 745a990c..5d774d8e 100755 --- a/src/script/hydra-queue-runner +++ b/src/script/hydra-queue-runner @@ -6,11 +6,12 @@ use File::Basename; use POSIX qw(dup2 :sys_wait_h); use Hydra::Schema; use Hydra::Helper::Nix; +use Hydra::Model::DB; use IO::Handle; use Nix::Store; -chdir getHydraPath or die; -my $db = openHydraDB; +chdir Hydra::Model::DB::getHydraPath or die; +my $db = Hydra::Model::DB->new(); STDOUT->autoflush(); diff --git a/src/script/hydra-update-gc-roots b/src/script/hydra-update-gc-roots index b86c2507..d0341897 100755 --- a/src/script/hydra-update-gc-roots +++ b/src/script/hydra-update-gc-roots @@ -6,9 +6,10 @@ use File::Basename; use Nix::Store; use Hydra::Schema; use Hydra::Helper::Nix; +use Hydra::Model::DB; use POSIX qw(strftime); -my $db = openHydraDB; +my $db = Hydra::Model::DB->new(); my %roots; @@ -75,7 +76,7 @@ foreach my $project ($db->resultset('Projects')->search({}, { order_by => ["name # If the jobset has been disabled for more than one week, than # don't keep its builds anymore. - if ($jobset->enabled == 0 && (time() - $jobset->lastcheckedtime > (7 * 24 * 3600))) { + if ($jobset->enabled == 0 && (time() - ($jobset->lastcheckedtime || 0) > (7 * 24 * 3600))) { print STDERR "*** skipping disabled jobset ", $project->name, ":", $jobset->name, "\n"; next; }