Skip to content

Commit

Permalink
Beginnings of postgres DB support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Taylor committed Sep 1, 2010
0 parents commit c7ee1fd
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.pyc
*.swp
.project
.pydevproject
MANIFEST
build
dist
Empty file added __init__.py
Empty file.
Empty file added fabtastic/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions fabtastic/db/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.conf import settings
from fabtastic.db import util

db_engine = util.get_db_setting('ENGINE')

if db_engine in ['postgresql_psycopg2', 'postgresql']:
from fabtastic.db.postgres import *
else:
raise NotImplementedError("Fabtastic: DB engine '%s' is not supported" % db_engine)
41 changes: 41 additions & 0 deletions fabtastic/db/postgres.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os
import stat

def set_pgpass(db_name, db_user, db_pass, db_host="*", db_port="*"):
"""
Sets the ~/.pgpass file up so that psql and pg_dump doesn't ask
for a password.
"""
pgpass_file = os.path.expanduser('~')
pgpass_file = os.path.join(pgpass_file, '.pgpass')

fd = open(pgpass_file, 'wb')
fd.write('*:*:%s:%s:%s' % (db_name, db_user, db_pass))
fd.close()

perms = [stat.S_IRUSR, stat.S_IWUSR]
os.chmod(pgpass_file, perms)

def backup_to_tmp():
"""
Backs up the current ligonier DB to a .gz file, stashed in /tmp/.
Returns a tuple in the format of (filename, full_file_path). For example:
(ligonier-<dtime>.sql.gz, /tmp/ligonier-<dtime.sql.gz)
You will probably want to run _remove_tmp_backup(db_filepath) after doing
whatever you need to do with the file.
"""
print("Backing up database '%s'. This may take a minute or so..." % db_name)

today = datetime.today()
db_filename = "%s-%s.sql.gz" % (db_name, today.strftime("%Y%m%d-%H%M"));
db_filepath = "/tmp/%s" % db_filename

# Set up a ~/.pgpass to avoid password prompts when running pg_dump.
_set_pgpass()
run("pg_dump -i -h %s -U %s %s | gzip > %s" % (db_host, db_user, db_name,
db_filepath))

return (db_filename, db_filepath)
14 changes: 14 additions & 0 deletions fabtastic/db/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""
Various database-related utility functions.
"""
from django.conf import settings

def get_db_setting(db_setting, db_alias='default'):
"""
Gets a database setting from settings.py. Extra logic to support
Django 1.1 and Django 1.2.
"""
if hasattr(settings, 'DATABASE'):
return settings.DATABASE[db_alias]['ENGINE']
else:
return settings.DATABASE_ENGINE
Empty file.
Empty file.
31 changes: 31 additions & 0 deletions fabtastic/management/commands/ft_dump_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os
from datetime import datetime
from django.core.management.base import BaseCommand, CommandError
from django.contrib.contenttypes.models import ContentType
from fabtastic import db

class Command(BaseCommand):
args = '[<output_file_path>]'
help = 'Dumps a SQL backup of your entire DB. Defaults to CWD.'

def _set_db_values(self):
self.db_name = db.util.get_db_setting('NAME')
self.db_user = db.util.get_db_setting('USER')
self.db_password = db.util.get_db_setting('PASSWORD')

def _set_dump_path(self):
if len(self.args) > 0:
self.dump_path = self.args[0]
else:
today = datetime.today()
db_filename = "%s-%s.sql" % (self.db_name, today.strftime("%Y%m%d-%H%M"));
self.dump_path = os.path.join(os.getcwd(), db_filename)

def handle(self, *args, **options):
self.args = args
self.options = options


db.set_pgpass(self.db_name, self.db_user, self.db_password,
db_host="*", db_port="*")
self._set_dump_path()

0 comments on commit c7ee1fd

Please sign in to comment.