Skip to content

Instantly share code, notes, and snippets.

@hillscottc
Created June 1, 2014 06:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hillscottc/8310f5c112c4118bd535 to your computer and use it in GitHub Desktop.
Save hillscottc/8310f5c112c4118bd535 to your computer and use it in GitHub Desktop.
Sample Fabric file to rebuild db.
from __future__ import with_statement
import os
from fabric.api import local, settings, abort, sudo, env
from fabric.contrib.console import confirm
env.hosts = [
# 'me@my-imac',
'me@my-desk',
]
def db_exists(db='testdb'):
if (sudo("psql -l | grep {} | wc -l".format(db), user='postgres')) != '0':
return True
else:
return False
def db_rebuild(db='testdb'):
"""Drop, create, and sync the db."""
## Warning.
if not confirm("Running on {} with database {}. Proceed?".format(env.host, db)):
print("Ok, no changes.")
return
## Drop the database.
if db_exists(db):
sudo("dropdb {}".format(db), user='postgres')
## Create the database.
sudo("createdb -E UTF8 -e {}".format(db), user='postgres')
## Run django's syncdb.
local('python ./manage.py syncdb')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment