import pipes
import subprocess

class Db(object):
    def __init__(self, conf):
        self.conf = conf
    
    def drop_database(self, name):
        raise NotImplementedError
    
    def create_database(self, name):
        raise NotImplementedError

class MysqlDb(Db):
    def drop_database(self, name):
        quoted_name = pipes.quote(name)
        subprocess.check_call('echo drop database if exists %s |mysql -u root' % quoted_name, shell=True)
    
    def create_database(self, name):
        quoted_name = pipes.quote(name)
        subprocess.check_call('echo create database %s |mysql -u root' % quoted_name, shell=True)

MysqliDb = MysqlDb

class PostgresDb(Db):
    def drop_database(self, name):
        import contextlib
        
        with contextlib.closing(self._connect('postgres')) as conn:
            # http://stackoverflow.com/questions/1017463/postgresql-how-to-run-vacuum-from-code-outside-transaction-block
            conn.set_isolation_level(0)
            with contextlib.closing(conn.cursor()) as c:
                c.execute('drop database if exists %s' % name)
    
    def create_database(self, name):
        import contextlib
        
        with contextlib.closing(self._connect('postgres')) as conn:
            # http://stackoverflow.com/questions/1017463/postgresql-how-to-run-vacuum-from-code-outside-transaction-block
            conn.set_isolation_level(0)
            with contextlib.closing(conn.cursor()) as c:
                c.execute('create database %s' % name)
    
    def _connect(self, dbname):
        import psycopg2
        
        conn = psycopg2.connect(database=dbname, host=self.conf.get('host'), user=self.conf.get('user'), password=self.conf.get('password'))
        return conn

@@ -23,16 +23,34 @@
 bprocess
+%0Aimport contextlib
 %0A%0Aclass 
@@ -737,80 +737,32 @@
     
-import contextlib%0A        %0A        with contextlib.closing(
+with 
 self._
-connect
+non_tx_cursor
 ('po
@@ -765,33 +765,29 @@
 ('postgres')
-)
  as c
-onn
 :%0A          
@@ -792,215 +792,148 @@
     
-# http://stackoverflow.com/questions/1017463/postgresql-how-to-run-vacuum-from-code-outside-transaction-block%0A   
+c.execute('drop database if exists %25s' %25 name)%0A
     
+%0A
     
- conn.set_isolation_level(0)%0A            with contextlib.closing(conn.cursor()
+def create_database(self, name):%0A        with self._non_tx_cursor('postgres'
 ) as c:%0A
     
@@ -920,36 +920,32 @@
 ostgres') as c:%0A
-    
             c.ex
@@ -943,36 +943,38 @@
      c.execute('
-drop
+create
  database if exi
@@ -963,34 +963,24 @@
 te database 
-if exists 
 %25s' %25 name)%0A
@@ -996,71 +996,299 @@
 def 
-create_database(self, name):%0A        import contextlib%0A        
+_connect(self, dbname):%0A        import psycopg2%0A        %0A        conn = psycopg2.connect(database=dbname, host=self.conf.get('host'), user=self.conf.get('user'), password=self.conf.get('password'))%0A        return conn%0A    %0A    @contextlib.contextmanager%0A    def _non_tx_cursor(self, dbname):
 %0A   
@@ -1591,274 +1591,12 @@
     
-c.execute('create database %25s' %25 name)%0A    %0A    def _connect(self, dbname):%0A        import psycopg2%0A        %0A        conn = psycopg2.connect(database=dbname, host=self.conf.get('host'), user=self.conf.get('user'), password=self.conf.get('password'))%0A        return conn
+yield c
 %0A

Refactor pg more