diff --git a/examples/flaskr/flaskr.py b/examples/flaskr/flaskr.py index 2cd2fd19..a38a04eb 100644 --- a/examples/flaskr/flaskr.py +++ b/examples/flaskr/flaskr.py @@ -37,13 +37,18 @@ def connect_db(): return rv -@app.cli.command() -def initdb(): - """Creates the database tables.""" +def init_db(): + """Initializes the database.""" db = get_db() with app.open_resource('schema.sql', mode='r') as f: db.cursor().executescript(f.read()) db.commit() + + +@app.cli.command('initdb') +def initdb_command(): + """Creates the database tables.""" + init_db() print('Initialized the database.') diff --git a/examples/flaskr/flaskr_tests.py b/examples/flaskr/flaskr_tests.py index ab08a60a..b90a7be7 100644 --- a/examples/flaskr/flaskr_tests.py +++ b/examples/flaskr/flaskr_tests.py @@ -21,7 +21,8 @@ class FlaskrTestCase(unittest.TestCase): self.db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp() flaskr.app.config['TESTING'] = True self.app = flaskr.app.test_client() - flaskr.init_db() + with flaskr.app.app_context(): + flaskr.init_db() def tearDown(self): """Get rid of the database again after each test.""" diff --git a/examples/minitwit/minitwit.py b/examples/minitwit/minitwit.py index 0a85247b..f772b682 100644 --- a/examples/minitwit/minitwit.py +++ b/examples/minitwit/minitwit.py @@ -49,15 +49,21 @@ def close_database(exception): top.sqlite_db.close() -@app.cli.command() -def initdb(): - """Creates the database tables.""" +def init_db(): + """Initializes the database.""" db = get_db() with app.open_resource('schema.sql', mode='r') as f: db.cursor().executescript(f.read()) db.commit() +@app.cli.command('initdb') +def initdb_command(): + """Creates the database tables.""" + init_db() + print('Initialized the database.') + + def query_db(query, args=(), one=False): """Queries the database and returns a list of dictionaries.""" cur = get_db().execute(query, args) diff --git a/examples/minitwit/minitwit_tests.py b/examples/minitwit/minitwit_tests.py index 9b027629..0a1a3f67 100644 --- a/examples/minitwit/minitwit_tests.py +++ b/examples/minitwit/minitwit_tests.py @@ -20,7 +20,8 @@ class MiniTwitTestCase(unittest.TestCase): """Before each test, set up a blank database""" self.db_fd, minitwit.app.config['DATABASE'] = tempfile.mkstemp() self.app = minitwit.app.test_client() - minitwit.init_db() + with minitwit.app.app_context(): + minitwit.init_db() def tearDown(self): """Get rid of the database again after each test.""" diff --git a/flask/cli.py b/flask/cli.py index 45e68ec5..0b37e3f9 100644 --- a/flask/cli.py +++ b/flask/cli.py @@ -199,9 +199,9 @@ class FlaskGroup(click.Group): value = prepare_exec_for_file(value) elif '.' not in sys.path: sys.path.insert(0, '.') - ctx.obj.app_import_path = value + ctx.ensure_object(ScriptInfo).app_import_path = value def set_debug(ctx, value): - ctx.obj.debug = value + ctx.ensure_object(ScriptInfo).debug = value click.Group.__init__(self, help=help, params=[ click.Option(['-a', '--app'],