Source

fabulator / src / fabulator / core / db / mysql.py

Full commit
Benoit C 3135dbd 














Benoit C 0e75ab7 




Benoit C 3135dbd 










Benoit C 0e75ab7 
Benoit C 3135dbd 
Benoit C 0e75ab7 


Benoit C 3135dbd 
Benoit C 0e75ab7 











Benoit C 3135dbd 
Benoit C 0e75ab7 



Benoit C 3135dbd 


Benoit C 0e75ab7 


Benoit C 3135dbd 
Benoit C 0e75ab7 




Benoit C 3135dbd 






"""
    mysql management functions
"""
import datetime
import time

from fabric.api import run

from base import BaseDB

class MySQL(BaseDB):
    """Base class for database"""

    def create_database(self, database_name=None):
        """create database"""
        conf = self.conf.copy()
        if database_name:
            conf["db"] = database_name

        run("echo 'CREATE DATABASE IF NOT EXISTS %(db)s' | mysql --user=%(user)s --password=%(pw)s --host=%(host)s" % conf)

    def backup(self, filename="plop"):
        """Backups your mysql database"""
        date = datetime.datetime.now()

        conf['backup_fname'] = "%s/backup-%s-%d-%d-%d-%s.sql" % \
            (self.conf.get('backup'), self.conf.get('db'), date.day, date.month,
             date.year, time.time())
        run("mkdir -p %(backup)s" % self.conf)
        run("""mysqldump --host=%(host)s --user=%(user)s --password=%(pw)s %(db)s > %(backup_fname)s""" % self.conf)

    def migrate(self, revision, sqlfile):
        """migrate the database"""
        if not isinstance(sqlfile, list):
            sqlfile = [sqlfile]
        conf = self.conf.copy()

        for sql in sqlfile:
            conf["sqlfile"] = sql
            run("mysql --host=%(host)s --user=%(user)s --password=%(pw)s < %(sqlfile)s" % conf)
            
        
    def populate(self, sqlfile):
        """inserts data into your db"""
        conf = self.conf.copy()
        conf["sqlfile"] = filename
        run("mysql --host=%(host)s --user=%(user)s --password=%(pw)s < %(sqlfile)s" % conf)

    def fallback(self, revision, sqlfile):
        """fallback to an old revision"""
        #well, we have to drop db, create db, populate from
        self.drop_database()
        self.create_database()
        self.populate(sqlfile)

    def drop_table(self, table_name):
        """Drop a table"""
        conf = self.conf.copy()
        conf["table"] = table_name
        run("""DROP TABLE %(table)s | mysql --host=%(host)s --user=%(user)s --password=%(pw)s""" % conf)

    def drop_database(self, database_name=None):
        conf = self.conf.copy()
        if database_name:
            conf["db"] = database_name
        run("echo 'DROP DATABASE %(db)s' | mysql --user=%(user)s --password=%(pw)s" % conf)

    def list_revision(self):
        """Return a list of available database revision"""
        run("ls -l %(backup)s" % self.conf)