def _reset_databases(self):
     for key, engine in self.engines.items():
         conn_string = self.test_databases[key]
         conn_pieces = parse.urlparse(conn_string)
         engine.dispose()
         if conn_string.startswith('sqlite'):
             # We can just delete the SQLite database, which is
             # the easiest and cleanest solution
             db_path = conn_pieces.path.strip('/')
             if os.path.exists(db_path):
                 os.unlink(db_path)
             # No need to recreate the SQLite DB. SQLite will
             # create it for us if it's not there...
         elif conn_string.startswith('mysql'):
             # We can execute the MySQL client to destroy and re-create
             # the MYSQL database, which is easier and less error-prone
             # than using SQLAlchemy to do this via MetaData...trust me.
             (user, password, database, host) = \
                 utils.get_db_connection_info(conn_pieces)
             sql = ("drop database if exists %(db)s; "
                    "create database %(db)s;") % {
                        'db': database
                    }
             cmd = ("mysql -u \"%(user)s\" -p\"%(password)s\" -h %(host)s "
                    "-e \"%(sql)s\"") % {
                        'user': user,
                        'password': password,
                        'host': host,
                        'sql': sql
                    }
             self.execute_cmd(cmd)
         elif conn_string.startswith('postgresql'):
             self._reset_pg(conn_pieces)
 def _reset_databases(self):
     for key, engine in self.engines.items():
         conn_string = self.test_databases[key]
         conn_pieces = parse.urlparse(conn_string)
         engine.dispose()
         if conn_string.startswith('sqlite'):
             # We can just delete the SQLite database, which is
             # the easiest and cleanest solution
             db_path = conn_pieces.path.strip('/')
             if os.path.exists(db_path):
                 os.unlink(db_path)
             # No need to recreate the SQLite DB. SQLite will
             # create it for us if it's not there...
         elif conn_string.startswith('mysql'):
             # We can execute the MySQL client to destroy and re-create
             # the MYSQL database, which is easier and less error-prone
             # than using SQLAlchemy to do this via MetaData...trust me.
             (user, password, database, host) = \
                 utils.get_db_connection_info(conn_pieces)
             sql = ("drop database if exists %(db)s; "
                    "create database %(db)s;") % {'db': database}
             cmd = ("mysql -u \"%(user)s\" -p\"%(password)s\" -h %(host)s "
                    "-e \"%(sql)s\"") % {'user': user, 'password': password,
                                         'host': host, 'sql': sql}
             self.execute_cmd(cmd)
         elif conn_string.startswith('postgresql'):
             self._reset_pg(conn_pieces)
    def _reset_pg(self, conn_pieces):
        (user, password, database,
         host) = utils.get_db_connection_info(conn_pieces)
        os.environ['PGPASSWORD'] = password
        os.environ['PGUSER'] = user
        # note(boris-42): We must create and drop database, we can't
        # drop database which we have connected to, so for such
        # operations there is a special database template1.
        sqlcmd = ("psql -w -U %(user)s -h %(host)s -c"
                  " '%(sql)s' -d template1")

        sql = ("drop database if exists %s;") % database
        droptable = sqlcmd % {'user': user, 'host': host, 'sql': sql}
        self.execute_cmd(droptable)

        sql = ("create database %s;") % database
        createtable = sqlcmd % {'user': user, 'host': host, 'sql': sql}
        self.execute_cmd(createtable)

        os.unsetenv('PGPASSWORD')
        os.unsetenv('PGUSER')
    def _reset_pg(self, conn_pieces):
        (user,
         password,
         database,
         host) = utils.get_db_connection_info(conn_pieces)
        os.environ['PGPASSWORD'] = password
        os.environ['PGUSER'] = user
        # note(boris-42): We must create and drop database, we can't
        # drop database which we have connected to, so for such
        # operations there is a special database template1.
        sqlcmd = ("psql -w -U %(user)s -h %(host)s -c"
                  " '%(sql)s' -d template1")

        sql = ("drop database if exists %s;") % database
        droptable = sqlcmd % {'user': user, 'host': host, 'sql': sql}
        self.execute_cmd(droptable)

        sql = ("create database %s;") % database
        createtable = sqlcmd % {'user': user, 'host': host, 'sql': sql}
        self.execute_cmd(createtable)

        os.unsetenv('PGPASSWORD')
        os.unsetenv('PGUSER')