get_lcov_cached_status has been removed build the cachefile name for lcov in the...
[build-farm.git] / hostdb.pm
index de3ea7d3ff5ffd685a2e2595a0cc63b9fb68b7d3..372483db942dcb62b72829eef1c98436f37f9399 100644 (file)
--- a/hostdb.pm
+++ b/hostdb.pm
@@ -28,7 +28,8 @@ sub new($)
  {
     my ($class, $filename) = @_;
     
-    my $dbh = DBI->connect("dbi:SQLite:$filename") or return undef;
+    my $dbh = DBI->connect("dbi:SQLite:$filename", "", "", {RaiseError => 1, PrintError => 0,
+                        ShowErrorStatement => 1, AutoCommit => 0}) or return undef;
     
     my $self = { filename => $filename, dbh => $dbh };
     
@@ -38,32 +39,60 @@ sub new($)
 sub provision($)
 {
        my ($self) = @_;
-       
-       $self->{dbh}->do("CREATE TABLE host ( name text, owner text, owner_email text, password text, ssh_access int, fqdn text, platform text, permission text );");
-       
-       $self->{dbh}->do("CREATE UNIQUE INDEX unique_hostname ON host (name);");
-
-       $self->{dbh}->do("CREATE TABLE build ( id integer primary key autoincrement, tree text, revision text, host text, compiler text, checksum text, age int );");
-       $self->{dbh}->do("CREATE TABLE test_run ( build int, test text, result text, output text);");
-       $self->{dbh}->do("CREATE TABLE build_stage_run ( build int, action text, result text, output text, num int);");
-
+       eval {
+           $self->{dbh}->do("CREATE TABLE host ( name text, owner text, owner_email text, password text, ssh_access int, fqdn text, platform text, permission text, last_dead_mail int, join_time int );");
+           
+           $self->{dbh}->do("CREATE UNIQUE INDEX unique_hostname ON host (name);");
+           
+           $self->{dbh}->do("CREATE TABLE build ( id integer primary key autoincrement, tree text, revision text, host text, compiler text, checksum text, age int, status text, commit_revision text);");
+           $self->{dbh}->do("CREATE UNIQUE INDEX unique_checksum ON build (checksum);");
+           
+           $self->{dbh}->do("CREATE TABLE test_run ( build int, test text, result text, output text);");
+           $self->{dbh}->commit();
+       };
+       if ($@) {
+           local $self->{dbh}->{RaiseError} = 0;
+           $self->{dbh}->rollback();
+           print "DB Failure: $@";
+           return 0;
+       }
+       return 1;
 }
 
 sub createhost($$$$$$)
 {
        my ($self, $name, $platform, $owner, $owner_email, $password, $permission) = @_;
-       my $sth = $self->{dbh}->prepare("INSERT INTO host (name, platform, owner, owner_email, password, permission) VALUES (?,?,?,?,?,?)");
-       
-       $sth->execute($name, $platform, $owner, $owner_email, $password, $permission);
+       my $sth = $self->{dbh}->prepare("INSERT INTO host (name, platform, owner, owner_email, password, permission, join_time) VALUES (?,?,?,?,?,?,?)");
+       
+       eval {
+           $sth->execute($name, $platform, $owner, $owner_email, $password, $permission, time());
+           $self->{dbh}->commit();
+       };
+       if ($@) {
+           local $self->{dbh}->{RaiseError} = 0;
+           $self->{dbh}->rollback();
+           print "DB Failure: $@";
+           return 0;
+       }
+       return 1;
 }
 
 sub deletehost($$)
 {
        my ($self, $name) = @_;
-       
+       my $ret;
        my $sth = $self->{dbh}->prepare("DELETE FROM host WHERE name = ?");
        
-       my $ret = $sth->execute($name);
+       eval {
+           $ret = $sth->execute($name);
+           $self->{dbh}->commit();
+       };
+       if ($@) {
+           local $self->{dbh}->{RaiseError} = 0;
+           $self->{dbh}->rollback();
+           print "DB Failure: $@";
+           return 0;
+       }
        
        return ($ret == 1);
 }
@@ -75,6 +104,38 @@ sub hosts($)
        return $self->{dbh}->selectall_arrayref("SELECT * FROM host ORDER BY name", { Slice => {} });
 }
 
+sub dead_hosts($$)
+{
+        my ($self, $age) = @_;
+       my $dead_age = time() - $age;
+       return $self->{dbh}->selectall_arrayref("SELECT host.name AS host, host.owner AS owner, host.owner_email AS owner_email, MAX(age) AS last_update FROM host LEFT JOIN build ON ( host.name == build.host) WHERE ifnull(last_dead_mail, 0) < $dead_age AND ifnull(join_time, 0) < $dead_age GROUP BY host.name having ifnull(MAX(age),0) < $dead_age", { Slice => {} });
+}
+
+sub host_ages($)
+{
+        my ($self) = @_;
+       return $self->{dbh}->selectall_arrayref("SELECT host.name AS host, host.owner AS owner, host.owner_email AS owner_email, MAX(age) AS last_update FROM host LEFT JOIN build ON ( host.name == build.host) GROUP BY host.name ORDER BY age", { Slice => {} });
+}
+
+sub sent_dead_mail($$) 
+{
+        my ($self, $host) = @_;
+       my $changed;
+       eval {
+           $changed = $self->{dbh}->do("UPDATE host SET last_dead_mail = ? WHERE name = ?", undef, 
+               (time(), $host));
+           $self->{dbh}->commit();
+       };
+       if ($@) {
+           local $self->{dbh}->{RaiseError} = 0;
+           $self->{dbh}->rollback();
+           print "DB Failure: $@";
+           return 0;
+       }
+       
+       return ($changed == 1);
+}
+
 sub host($$)
 {
        my ($self, $name) = @_;
@@ -91,9 +152,19 @@ sub host($$)
 sub update_platform($$$)
 {
        my ($self, $name, $new_platform) = @_;
-       
-       my $changed = $self->{dbh}->do("UPDATE host SET platform = ? WHERE name = ?", undef, 
+       my $changed;
+
+       eval {
+           $changed = $self->{dbh}->do("UPDATE host SET platform = ? WHERE name = ?", undef, 
                ($new_platform, $name));
+           $self->{dbh}->commit();
+       };
+       if ($@) {
+           local $self->{dbh}->{RaiseError} = 0;
+           $self->{dbh}->rollback();
+           print "DB Failure: $@";
+           return 0;
+       }
        
        return ($changed == 1);
 }
@@ -101,9 +172,18 @@ sub update_platform($$$)
 sub update_owner($$$$)
 {
        my ($self, $name, $new_owner, $new_owner_email) = @_;
-       
-       my $changed = $self->{dbh}->do("UPDATE host SET owner = ?, owner_email = ? WHERE name = ?", 
+       my $changed;
+
+       eval {
+           $changed = $self->{dbh}->do("UPDATE host SET owner = ?, owner_email = ? WHERE name = ?", 
                                       undef, ($new_owner, $new_owner_email, $name));
+           $self->{dbh}->commit();
+       };
+       if ($@) {
+           local $self->{dbh}->{RaiseError} = 0;
+           $self->{dbh}->rollback();
+           return 0;
+       }
        
        return ($changed == 1);
 }