Commits

Benoit C committed 1486cce

psql: class to manage psql database

Comments (0)

Files changed (3)

 #    services_reload()
 #    services_restart()
 
+def reset():
+    env.vc.remove_repository()
+    env.db.drop_database()
+
 @Permissions("sudo_user", env)
 def nginx_logs():
     sudo("sudo tail -f /var/log/nginx/*.log")
 
-def reset():
-    env.vc.remove_repository()
-
 
 @Permissions("sudo_user", env)
 @ConfChecker("distro", env)

src/fabulator/core/db/base.py

     def drop_database(self, database_name):
         raise NotImplementedError
 
+
     def list_revision(self):
-        """Return a list of available database revision"""
-        raise NotImplementedError
+         """Return a list of available database revision"""
+         run("ls -l %(backup)s" % self.conf)
 
     def rc(self, service_name, rc_name=None, command="start", require_space=None):
         """manage the daemon using system RC"""

src/fabulator/core/db/pgsql.py

 """
-    psql management functions
+    mysql management functions
 """
 import datetime
 import time
 
     def create_database(self, database_name=None):
         """create database"""
-        run("echo 'CREATE DATABASE IF NOT EXISTS %(db)s' | psql --user=%(user)s --password=%(pw)s" % self.conf)
+        conf = self.conf.copy()
+        if database_name:
+            conf["db"] = database_name
+
+        if "host" in conf:
+            run("createdb %(db)s --username %(user)s --host %(host)s --port %(port)s" % conf)
+        else:
+            run("createdb %(db)s --username %(user)s" % conf)
 
     def backup(self, filename="plop"):
-        """Backups your psql database"""
+        """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("""psqldump --host=%(host)s --user=%(user)s --password=%(pw)s %(db)s > %(backup_fname)s""" % self.conf)
+        if "host" in conf:
+            run("pg_dump --host %(host)s --port %(port)s --username %(user)s %(db)s > %(backup_fname)s" % self.conf)
+        else:
+            run("pg_dump --username %(user)s %(db)s > %(backup_fname)s" % self.conf)
 
-    def migrate(self, revision):
-        """migrate the database"""
-        raise NotImplementedError
+    def migrate(self, revision, sqlfile):
+        """applies sql file to the database"""
+        if not isinstance(sqlfile, list):
+            sqlfile = [sqlfile]
+        conf = self.conf.copy()
 
-    def fallback(self, revision):
+        for sql in sqlfile:
+            conf["sqlfile"] = sql
+            if "host" in conf:
+                run("psql --host %(host)s --port %(port)s --username %(user)s %(db)s < %(sqlfile)s" % conf)
+            else:
+                run("psql --username %(user)s %(db)s < %(sqlfile)s" % conf)
+            
+        
+    def populate(self, sqlfile):
+        """inserts data into your db"""
+        conf = self.conf.copy()
+        conf["sqlfile"] = filename
+        if "host" in conf:
+            run("psql --host %(host)s --port %(port)s --username %(user)s < %(sqlfile)s" % conf)
+        else:
+            run("psql --username %(user)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
+        if "host" in conf:
+            run("DROP TABLE %(table)s | pgsql --host %(host)s --port %(port)s --username %(user)s" % conf)
+        else:
+            run("DROP TABLE %(table)s | pgsql --username %(user)s" % conf)
 
-    def drop_database(self, database_name):
-        run("echo 'DROP DATABASE %(db)s' | psql --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
+        if "host" in conf:
+            run("dropdb --username %(user)s --host %(host)s %(db)s" % conf)
+        else:
+            run("dropdb --username %(user)s %(db)s" % conf)
+            
 
-    def list_revision(self):
-        """Return a list of available database revision"""
-        run("ls -l %(backup)s" % self.conf)
-        raise NotImplementedError
 
 
 
-
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.