Exemple #1
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
Exemple #2
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
 def test_should_return_cursor(self):
     conn = Connection(hostname="localhost", username="******")
     conn.open()
     self.assertTrue(conn.cursor())
     conn.close()
 def test_should_return_cursor(self):
     conn = Connection(hostname="localhost", username="******")
     conn.open()
     self.assertTrue(conn.cursor())
     conn.close()