[Bps-public-commit] smokingit-worker branch, cleanup-users, created. ff8f5b033838d4524c854641b26585894c337d4c

Thomas Sibley trs at bestpractical.com
Wed Mar 27 14:34:36 EDT 2013


The branch, cleanup-users has been created
        at  ff8f5b033838d4524c854641b26585894c337d4c (commit)

- 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