Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

some modifications to make the migration a bit smoother on new boxes

  • Loading branch information...
commit bfccddc482dac9aa0cdd12875a57da85fa1c9b06 1 parent 29f6e9a
@jmoiron authored
Showing with 45 additions and 20 deletions.
  1. +19 −12 fabfile.py
  2. +14 −1 manage.py
  3. +8 −7 misc/migrate.py
  4. +4 −0 misc/requirements.txt
View
31 fabfile.py
@@ -5,21 +5,28 @@
from datetime import datetime
from fabric.api import *
-env.hosts = ['jmoiron.net']
-def backup_db(db='jmoiron_net'):
- """Dump db and fetch db from the server."""
+LOCAL_DATABASE_NAME = "jmoiron"
+
+env.hosts = ["jmoiron.net"]
+
+@task
+def fetch_db(db="jmoiron_net"):
+ """Fetch db from the server."""
# TODO: this fetches the old db, make it fetch the new one
- timestamp = datetime.now().isoformat().split('T')[0]
- path = '/tmp/%s.%s.sql' % (db, timestamp)
- cmd = 'pg_dump -U jmoiron -dCE utf8 %s -f "%s"' % (db, path)
+ timestamp = datetime.now().isoformat().split("T")[0]
+ path = "/tmp/%s.%s.sql" % (db, timestamp)
+ cmd = "pg_dump -U jmoiron -dCE utf8 %s -f \"%s\"" % (db, path)
run(cmd)
- run('gzip "%s"' % path)
- get(path + '.gz', './')
- run('rm "%s.gz"' % path)
+ run("gzip \"%s\"" % path)
+ get(path + ".gz", "./")
+ run("rm \"%s.gz\"" % path)
# remove it if it already exists
- local('rm -f %s.%s.sql' % (db, timestamp))
- local('gzip -d %s.%s.sql.gz' % (db, timestamp))
+ local("rm -f %s.%s.sql" % (db, timestamp))
+ local("gzip -d %s.%s.sql.gz" % (db, timestamp))
+ local("sed -i 's/^CREATE DATABASE/-- CREATE DATABASE/' %s.%s.sql" % (db, timestamp))
+ local("sed -i 's/^ALTER DATABASE/-- ALTER DATABASE/' %s.%s.sql" % (db, timestamp))
+ local(r"sed -i 's/^\\connect/-- \\connect/' %s.%s.sql" % (db, timestamp))
+
-backup_legacy_db = backup_db
View
15 manage.py
@@ -4,11 +4,25 @@
"""jmoiron.net script/commands"""
from flaskext.script import Manager
+from glob import glob
+
+from misc import migrate
from jmoiron.app import app, db
script = Manager(app)
@script.command
+def loadsql(dumpfile=None):
+ """Reload the SQL database."""
+ if dumpfile is None:
+ try:
+ dumpfile = glob("*.sql")[-1]
+ except:
+ print "Could not find a dumpfile (*.sql) in CWD."
+ return
+ migrate.loaddb(dumpfile)
+
+@script.command
def flushdb():
"""Flush the database."""
db.drop_collection('blog_post')
@@ -32,7 +46,6 @@ def create_indexes():
@script.command
def migratedb(dumpfile=None):
"""Run a migration from an SQL database."""
- from misc import migrate
print 'Flushing current mongo database...'
flushdb()
if dumpfile:
View
15 misc/migrate.py
@@ -10,14 +10,14 @@
from psycopg2 import connect
from psycopg2.extras import RealDictCursor
-db = connect(database='jmoiron_net')
+DATABASE_NAME = "jmoiron"
-def loaddb(filename, db='jmoiron_net'):
- with hide('running', 'stdout', 'stderr'):
- def psql(string):
- local('echo "%s" | psql -d postgres' % string)
- psql("DROP DATABASE %s" % db)
- local('psql -d postgres < %s' % (filename))
+# db = connect(database=DATABASE_NAME)
+
+def loaddb(filename, db=DATABASE_NAME):
+ local("echo \"DROP DATABASE %s\" | psql -d postgres" % (db), capture=True)
+ local("createdb %s" % db)
+ local('psql -d %s < %s' % (db, filename), capture=True)
def typemap(val):
"""Provide type mapping between values that psycopg2 return to us
@@ -32,6 +32,7 @@ def typemap(val):
raise Exception("What is this?: %r" % val)
def read_table(table):
+ db = connect(database=DATABASE_NAME)
cursor = db.cursor(cursor_factory=RealDictCursor)
cursor.execute("SELECT * FROM %s;" % table)
rows = list(cursor)
View
4 misc/requirements.txt
@@ -1,4 +1,8 @@
+# pip-requirements file
+
+# backup and migration requirements
Fabric
+psycopg2
# flask stuff
Flask
Flask-Script
Please sign in to comment.
Something went wrong with that request. Please try again.