Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

devops: add support for postgres databases

  • Loading branch information...
commit 30d76c430485c9d826d30a7a2092d37c04d87d38 1 parent 91ee6b5
@yaniv-aknin yaniv-aknin authored
Showing with 32 additions and 7 deletions.
  1. +5 −7 manage.py
  2. +27 −0 utils/dbutils.py
View
12 manage.py
@@ -18,7 +18,7 @@
app = initialize_app(settings)
# import libraries
-from utils.ext.path import Path
+from utils.dbutils import reset_database, DatabaseControlError
# import our code after initialization of app
from config.log import setup_logging
@@ -40,12 +40,10 @@ def log_request(self, *args, **kwargs): pass
@manager.command
def recreatedb():
"destroy the database (if any) and recreate it"
- uri = settings.SQLALCHEMY_DATABASE_URI
- if uri.scheme == 'sqlite':
- Path(uri.path).unlink_p()
- pass
- else:
- raise NotImplementedError('unknown database scheme %s' % (uri.scheme,))
+ try:
+ reset_database(settings.SQLALCHEMY_DATABASE_URI)
+ except DatabaseControlError, error:
+ print("failed resetting database: %s" % (error,))
with app.app_context():
db.create_all()
View
27 utils/dbutils.py
@@ -0,0 +1,27 @@
+import os
+
+class DatabaseControlError(Exception):
+ pass
+
+class UnsupportedDatabase(NotImplementedError, DatabaseControlError):
+ pass
+
+def reset_postgres_database(uri):
+ if uri.hostname != 'localhost':
+ for key in os.environ:
+ if 'HEROKU' in key:
+ print("Assuming Heroku database is blank (consider `heroku pg:reset`)")
+ else:
+ print("Assuming non-local database is blank")
+ return
+ if 0 != os.system('dropdb %(name)s && createdb %(name)s' % {'name': uri.path[1:]}):
+ raise DatabaseControlError('nonzero result while recreating database (see stderr)')
+
+def reset_database(uri):
+ if uri.scheme == 'sqlite':
+ if os.path.exists(uri.path):
+ os.remove(uri.path)
+ elif uri.scheme == 'postgres':
+ reset_postgres_database(uri)
+ else:
+ raise UnsupportedDatabase('unknown database scheme %s' % (uri.scheme,))
Please sign in to comment.
Something went wrong with that request. Please try again.