Created
September 26, 2013 16:50
-
-
Save xsleonard/6716936 to your computer and use it in GitHub Desktop.
sqlalchemy schema create script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
from forq import db, create_app | |
def drop(): | |
db.drop_all() | |
print 'Dropping completed successful!' | |
def create(): | |
db.create_all() | |
print 'Creating completed successful!' | |
def reset(): | |
drop() | |
create() | |
actions = dict( | |
drop=(drop, 'drop tables'), | |
create=(create, 'create tables'), | |
reset=(reset, 'drop and create'), | |
) | |
def help(action=None): | |
if action is not None: | |
print 'Unknown command {0}'.format(str(action)) | |
print 'Please, use:' | |
for name, (action, desc) in actions.items(): | |
print '{0} - {1}'.format(name, desc) | |
def _handle_cli(): | |
if len(sys.argv) < 2: | |
help() | |
sys.exit() | |
action = sys.argv[1] | |
if action not in actions: | |
help(action) | |
sys.exit() | |
try: | |
actions[action][0]() | |
except Exception, e: | |
print 'Error: ', e | |
if __name__ == '__main__': | |
app = create_app() | |
with app.test_request_context(): | |
_handle_cli() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment