Commits

Benoit C  committed 0e75ab7

finishing mysql class

  • Participants
  • Parent commits 9f6381b
  • Branches abstraction

Comments (0)

Files changed (3)

File src/fabfile.py

 
 def build(conf):
     """instanciate the backend"""
-    env.vc = vc_factory(conf["vc"].get("type"), conf.get("vc"), env)
-    env.db = database_factory(conf["database"].get("type"), conf.get("database"), env)
-    env.httpd = http_factory(conf["httpd"].get("type"), conf.get("httpd"), env)
-    env.virtualenv = VirtualEnv(conf.get("virtualenv"), env)
+    # we can be more magic here
+    if "vc" in conf:
+        env.vc = vc_factory(conf["vc"].get("type"), conf.get("vc"), env)
+    if "database" in conf:
+        env.db = database_factory(conf["database"].get("type"), conf.get("database"), env)
+    if "httpd" in conf:
+        env.httpd = http_factory(conf["httpd"].get("type"), conf.get("httpd"), env)
+    if "virtualenv" in conf:
+        env.virtualenv = VirtualEnv(conf.get("virtualenv"), env)
 
 def preprod(conf="conf.yaml"):
     """Preprod tasks"""
     env.vc.clone_repository()
     env.virtualenv.create_virtualenv()
     env.virtualenv.install_requirements(False)
+    
+    env.db.create_database()
 
 
 

File src/fabulator/core/db/base.py

         """migrate the database"""
         raise NotImplementedError
 
-    def fallback(self, revision):
+    def fallback(self, revision, sqlfile):
         """fallback to an old revision"""
         raise NotImplementedError
 
+    def populate(self, sqlfile):
+        """insert data into your database"""
+        raise NotImplementedError
+
     def drop_table(self, table_name):
         """Drop a table"""
         raise NotImplementedError

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

 
     def create_database(self, database_name=None):
         """create database"""
-        run("echo 'CREATE DATABASE IF NOT EXISTS %(db)s' | mysql --user=%(user)s --password=%(pw)s" % self.conf)
+        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"""
         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):
+    def migrate(self, revision, sqlfile):
         """migrate the database"""
-        raise NotImplementedError
+        if not isinstance(sqlfile, list):
+            sqlfile = [sqlfile]
+        conf = self.conf.copy()
 
-    def fallback(self, revision):
+        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"""
-        raise NotImplementedError
+        #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"""
-        raise NotImplementedError
+        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):
-        run("echo 'DROP DATABASE %(db)s' | mysql --user=%(user)s --password=%(pw)s" % self.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)
-        raise NotImplementedError