1. Benoit C
  2. fabulator


fabulator / src / fabulator / core / db / pgsql.py

    psql management functions
import datetime
import time

from fabric.api import run

from base import BaseDB

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

    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)

    def backup(self, filename="plop"):
        """Backups your psql 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)

    def migrate(self, revision):
        """migrate the database"""
        raise NotImplementedError

    def fallback(self, revision):
        """fallback to an old revision"""
        raise NotImplementedError

    def drop_table(self, table_name):
        """Drop a table"""
        raise NotImplementedError

    def drop_database(self, database_name):
        run("echo 'DROP DATABASE %(db)s' | psql --user=%(user)s --password=%(pw)s" % self.conf)

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