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

Thomas Sibley trs at bestpractical.com
Tue Mar 26 20:15:59 EDT 2013


The branch, cleanup-users has been created
        at  be17c331c8bf02e9bce8b8dc2ea4f9a234f99eae (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 be17c331c8bf02e9bce8b8dc2ea4f9a234f99eae
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 end up with thousands of test users which are
    rarely used again.  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..6782406 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 where User LIKE 'urt_test%'");
+    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..4eb5039 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 where usename like 'urt_test%'");
+    return @$users;
+}
+
+sub clean_user_sql {
+    "DROP ROLE $_[1]\n"
+}
+
 1;
 

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



More information about the Bps-public-commit mailing list