-
-
Notifications
You must be signed in to change notification settings - Fork 898
CLI support #520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
CLI support #520
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e408c78
cli support
singingwolfboy 28f4037
Added cli binds
singingwolfboy 5c8f5bd
Remove hard depednency on click
singingwolfboy efb4d20
fix format() for 2.6
singingwolfboy c2becb9
Remove cli 'binds'
singingwolfboy 92783d3
remove `db drop` cli command
singingwolfboy 02a7dfb
cleanups
singingwolfboy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """ | ||
| flask_sqlalchemy.cli | ||
| ~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| Command Line Interface for managing the database. | ||
|
|
||
| :copyright: (c) 2017 by David Baumgold. | ||
| :license: MIT, see LICENSE for more details. | ||
| """ | ||
|
|
||
| from __future__ import absolute_import | ||
|
|
||
| from functools import wraps | ||
|
|
||
| import click | ||
| from flask import current_app | ||
| from werkzeug.local import LocalProxy | ||
|
|
||
| try: | ||
| from flask.cli import with_appcontext | ||
| except ImportError: | ||
| from flask_cli import with_appcontext | ||
|
|
||
| state = LocalProxy(lambda: current_app.extensions['sqlalchemy']) | ||
|
|
||
|
|
||
| def commit(fn): | ||
| """Decorator to commit changes after the command is run.""" | ||
| @wraps(fn) | ||
| def wrapper(*args, **kwargs): | ||
| fn(*args, **kwargs) | ||
| state.db.session.commit() | ||
| return wrapper | ||
|
|
||
|
|
||
| @click.group() | ||
| def db(): | ||
| """Manages the database.""" | ||
|
|
||
|
|
||
| @db.command('create') | ||
| @click.option('--bind', default='__all__') | ||
| @with_appcontext | ||
| @commit | ||
| def db_create(bind): | ||
| """Creates database tables.""" | ||
| state.db.create_all(bind=bind) | ||
| click.secho('Database created successfully.', fg='green') | ||
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import pytest | ||
| pytest.importorskip("click") | ||
|
|
||
| from flask_sqlalchemy.cli import db_create | ||
|
|
||
|
|
||
| def test_cli_create(clirunner, db, script_info, mocker): | ||
| mock_execute = mocker.patch.object( | ||
| db, | ||
| "_execute_for_all_tables", | ||
| ) | ||
|
|
||
| result = clirunner.invoke(db_create, obj=script_info) | ||
| assert result.exit_code == 0 | ||
| mock_execute.assert_called_once_with(None, '__all__', 'create_all') | ||
| assert "Database created successfully." in result.output |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ envlist = | |
| [testenv] | ||
| deps = | ||
| pytest>=3 | ||
| pytest-mock | ||
| coverage | ||
| blinker | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO it'd be better to just drop support for anclient flask versions
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flask-CLI supports Flask 0.10, and CLI support was added to Flask in 0.11. Flask 0.10 was released in June 2013, while Flask 0.11 was released in June 2016. Three years is hardly ancient history, even in software development.
Besides, the cost of supporting Flask-CLI is so low. Is it really a problem to keep this in?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, just my opinion :) And I'd say using a 3y old version from a 0.x line is kind of ancient.