[Bps-public-commit] smokingit-worker branch, master, updated. ff8f5b033838d4524c854641b26585894c337d4c

Alex Vandiver alexmv at bestpractical.com
Wed Mar 27 17:49:33 EDT 2013


The branch, master has been updated
       via  ff8f5b033838d4524c854641b26585894c337d4c (commit)
       via  83c8775f15b802bd06f795e00df193f9b89a06c8 (commit)
      from  f3f9989167f23000de4d03cd6df054eacc3dacfa (commit)

Summary of changes:
 lib/Smokingit/Worker/Clean/Database.pm | 26 ++++++++++++++++++++------
 lib/Smokingit/Worker/Clean/Mysql.pm    | 11 +++++++++++
 lib/Smokingit/Worker/Clean/Postgres.pm | 11 +++++++++++
 3 files changed, 42 insertions(+), 6 deletions(-)

- Log -----------------------------------------------------------------
commit 83c8775f15b802bd06f795e00df193f9b89a06c8
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Tue Mar 26 17:02:09 2013 -0700

    DB cleaning: Refactor connecting to the database into a method

diff --git a/lib/Smokingit/Worker/Clean/Database.pm b/lib/Smokingit/Worker/Clean/Database.pm
index e62a45c..737a9be 100644
--- a/lib/Smokingit/Worker/Clean/Database.pm
+++ b/lib/Smokingit/Worker/Clean/Database.pm
@@ -21,19 +21,23 @@ sub new {
     return $self;
 }
 
-sub clean {
+sub dbh {
     my $self = shift;
-    my @dbs = grep !$self->{dbs}{ $_ }, $self->list_dbs;
-    return unless @dbs;
-
-    my $dbh = DBI->connect(
+    return $self->{dbh} ||= DBI->connect(
         $self->dsn,
         $self->{user},
         $self->{password},
         {RaiseError => 1}
     );
+}
+
+sub clean {
+    my $self = shift;
+    my @dbs = grep !$self->{dbs}{ $_ }, $self->list_dbs;
+    return unless @dbs;
+
     warn "DROP DATABASE $_\n" for @dbs;
-    $dbh->do("DROP DATABASE $_") for @dbs;
+    $self->dbh->do("DROP DATABASE $_") for @dbs;
 }
 
 sub list_dbs { die "!!!\n" }

commit ff8f5b033838d4524c854641b26585894c337d4c
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Tue Mar 26 17:03:11 2013 -0700

    DB cleaning: Drop users created during smoke runs
    
    Otherwise the databases may end up with thousands of test users which
    are rarely used again (for example, RT's test suite does this).  This
    can cause decent slowdowns when performing DDL operations in tests.

diff --git a/lib/Smokingit/Worker/Clean/Database.pm b/lib/Smokingit/Worker/Clean/Database.pm
index 737a9be..37264f0 100644
--- a/lib/Smokingit/Worker/Clean/Database.pm
+++ b/lib/Smokingit/Worker/Clean/Database.pm
@@ -18,6 +18,7 @@ sub new {
     my $self = $class->SUPER::new();
     $self->{$_} = $args{$_} for qw/user password/;
     $self->{dbs}{$_}++ for $self->list_dbs;
+    $self->{users}{$_}++ for $self->list_users;
     return $self;
 }
 
@@ -34,13 +35,22 @@ sub dbh {
 sub clean {
     my $self = shift;
     my @dbs = grep !$self->{dbs}{ $_ }, $self->list_dbs;
-    return unless @dbs;
+    my @users = grep !$self->{users}{ $_ }, $self->list_users;
+    return unless @dbs or @users;
 
     warn "DROP DATABASE $_\n" for @dbs;
     $self->dbh->do("DROP DATABASE $_") for @dbs;
+
+    for (@users) {
+        my $sql = $self->clean_user_sql($_);
+        warn $sql;
+        $self->dbh->do($sql);
+    }
 }
 
 sub list_dbs { die "!!!\n" }
+sub list_users { die "!!!\n" }
+sub clean_user_sql { die "!!!\n" }
 
 1;
 
diff --git a/lib/Smokingit/Worker/Clean/Mysql.pm b/lib/Smokingit/Worker/Clean/Mysql.pm
index 2a77f0e..f90662b 100644
--- a/lib/Smokingit/Worker/Clean/Mysql.pm
+++ b/lib/Smokingit/Worker/Clean/Mysql.pm
@@ -20,5 +20,16 @@ sub list_dbs {
     return map {s/^DBI:mysql:(.*)/$1/ ? $_ : () } grep defined, @dbs;
 }
 
+sub list_users {
+    my $self = shift;
+    my $users = $self->dbh->selectcol_arrayref(
+        "SELECT concat(User,'\@',Host) FROM mysql.user");
+    return @$users;
+}
+
+sub clean_user_sql {
+    "DROP USER $_[1]\n"
+}
+
 1;
 
diff --git a/lib/Smokingit/Worker/Clean/Postgres.pm b/lib/Smokingit/Worker/Clean/Postgres.pm
index 4504838..2812af2 100644
--- a/lib/Smokingit/Worker/Clean/Postgres.pm
+++ b/lib/Smokingit/Worker/Clean/Postgres.pm
@@ -18,5 +18,16 @@ sub list_dbs {
     return map {s/.*dbname=([^;]+).*/$1/ ? $_ : () } grep defined, @dbs;
 }
 
+sub list_users {
+    my $self = shift;
+    my $users = $self->dbh->selectcol_arrayref(
+        "select usename from postgres.pg_user");
+    return @$users;
+}
+
+sub clean_user_sql {
+    "DROP ROLE $_[1]\n"
+}
+
 1;
 

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list