Ejemplo n.º 1
0
 def test_close_does_not_fail_when_connection_is_None(self):
     conn = Connection(hostname="localhost", username="******")
     try:
         conn.close()
     except Exception as e:
         self.fail("Should not raise any exception when closing a None connection, but raised:\n%s" %
                 traceback.format_exc(e))
Ejemplo n.º 2
0
 def test_close_does_not_fail_when_connection_is_None(self):
     conn = Connection(hostname="localhost", username="******")
     try:
         conn.close()
     except Exception as e:
         msg = "Should not raise any exception when closing a None " +\
               "connection, but raised:\n%s"
         self.fail(msg % traceback.format_exc(e))
Ejemplo n.º 3
0
 def __init__(self,
              name,
              host="localhost",
              user="******",
              password="",
              public_host=None):
     self.name = canonicalize_db_name(name)
     self._host = host
     self.port = '3306'
     self.conn = Connection(self._host, user, password, "")
     self._public_host = public_host
Ejemplo n.º 4
0
 def __init__(self,
              name,
              host="localhost",
              user="******",
              password="",
              public_host=None):
     self.name = canonicalize_db_name(name)
     self._host = host
     self.port = '3306'
     self.conn = Connection(self._host, user, password, "")
     self._public_host = public_host
Ejemplo n.º 5
0
class DatabaseManager(object):

    def __init__(self,
                 name,
                 host="localhost",
                 port="3306",
                 user="******",
                 password="",
                 public_host=None):
        self.name = canonicalize_db_name(name)
        self._host = host
        self.port = port
        self.conn = Connection(self._host, self.port, user, password, "")
        self._public_host = public_host

    @property
    def public_host(self):
        if self._public_host:
            return self._public_host
        return self.host

    def create_database(self):
        self.conn.open()
        cursor = self.conn.cursor()
        sql = "CREATE DATABASE %s default character set utf8 " + \
              "default collate utf8_general_ci"
        cursor.execute(sql % self.name)
        self.conn.close()

    def drop_database(self):
        self.conn.open()
        cursor = self.conn.cursor()
        cursor.execute("DROP DATABASE %s" % self.name)
        self.conn.close()

    def create_user(self, username, host):
        self.conn.open()
        cursor = self.conn.cursor()
        username = generate_user(username)
        password = generate_password(username)
        sql = ("grant all privileges on {0}.* to '{1}'@'{2}'"
               " identified by '{3}'")
        cursor.execute(sql.format(self.name, username, host, password))
        self.conn.close()
        return username, password

    def drop_user(self, username, host):
        self.conn.open()
        cursor = self.conn.cursor()
        cursor.execute("drop user '{0}'@'{1}'".format(username, host))
        self.conn.close()

    def export(self):
        cmd = ["mysqldump", "-u", "root", "-d", self.name, "--compact"]
        return subprocess.check_output(cmd, stderr=subprocess.STDOUT)

    def is_up(self):
        try:
            self.conn.open()
            return True
        except:
            return False
        finally:
            self.conn.close()

    @property
    def host(self):
        if self._host == "localhost":
            return os.environ.get("MYSQLAPI_DATABASE_HOST", "localhost")
        return self._host
Ejemplo n.º 6
0
 def setUpClass(cls):
     cls.conn = Connection(hostname="localhost", username="******")
     cls.conn.open()
     cls.cursor = cls.conn.cursor()
Ejemplo n.º 7
0
 def setUpClass(cls):
     cls.conn = Connection(hostname="localhost", username="******")
     cls.conn.open()
     cls.old_poll_interval = settings.EC2_POLL_INTERVAL
     settings.EC2_POLL_INTERVAL = 0
     set_model(Instance)
Ejemplo n.º 8
0
 def test_should_return_cursor(self):
     conn = Connection(hostname="localhost", username="******")
     conn.open()
     self.assertTrue(conn.cursor())
     conn.close()
Ejemplo n.º 9
0
 def __init__(self, name, host="localhost", user="******", password=""):
     self.name = name
     self._host = host
     self.port = '3306'
     self.conn = Connection(self._host, user, password, "")
Ejemplo n.º 10
0
class DatabaseManager(object):

    def __init__(self, name, host="localhost", user="******", password=""):
        self.name = name
        self._host = host
        self.port = '3306'
        self.conn = Connection(self._host, user, password, "")

    def create_database(self):
        self.conn.open()
        cursor = self.conn.cursor()
        cursor.execute("CREATE DATABASE %s default character set utf8 default collate utf8_general_ci" % self.name)
        self.conn.close()

    def drop_database(self):
        self.conn.open()
        cursor = self.conn.cursor()
        cursor.execute("DROP DATABASE %s" % self.name)
        self.conn.close()

    def create_user(self, username, host):
        self.conn.open()
        cursor = self.conn.cursor()
        username = generate_user(username)
        password = generate_password()
        cursor.execute("grant all privileges on %s.* to %s@%s identified by '%s'" % (self.name, username, host, password))
        self.conn.close()
        return username, password

    def drop_user(self, username, host):
        self.conn.open()
        cursor = self.conn.cursor()
        cursor.execute("drop user %s@%s" % (username, host))
        self.conn.close()

    def export(self):
        return subprocess.check_output(["mysqldump", "-u", "root", "-d", self.name, "--compact"], stderr=subprocess.STDOUT)

    def is_up(self):
        try:
            self.conn.open()
            return True
        except:
            return False
        finally:
            self.conn.close()

    @property
    def host(self):
        if self._host == "localhost":
            return os.environ.get("MYSQLAPI_DATABASE_HOST", "localhost")
        return self._host
Ejemplo n.º 11
0
 def test_should_return_cursor(self):
     conn = Connection(hostname="localhost", username="******")
     conn.open()
     self.assertTrue(conn.cursor())
     conn.close()