Skip to content

Commit

Permalink
Option to rename "db" command group
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Nov 13, 2022
1 parent 2852214 commit b9c9d35
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
4 changes: 4 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ To see all the commands that are available run this command::

Note that the application script must be set in the ``FLASK_APP`` environment variable for all the above commands to work, as required by the ``flask`` command.

If the ``db`` command group name is inconvenient, it can be changed to a different with the ``command`` argument passed to the ``Migrate`` class::

migrate = Migrate(app, db, command='migrate')

Alembic Configuration Options
-----------------------------

Expand Down
4 changes: 0 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,3 @@ install_requires =

[options.packages.find]
where = src

[options.entry_points]
flask.commands =
db = flask_migrate.cli:db
11 changes: 8 additions & 3 deletions src/flask_migrate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,22 @@ def get_template_directory(self):


class Migrate(object):
def __init__(self, app=None, db=None, directory='migrations',
def __init__(self, app=None, db=None, directory='migrations', command='db',
compare_type=True, render_as_batch=True, **kwargs):
self.configure_callbacks = []
self.db = db
self.command = command
self.directory = str(directory)
self.alembic_ctx_kwargs = kwargs
self.alembic_ctx_kwargs['compare_type'] = compare_type
self.alembic_ctx_kwargs['render_as_batch'] = render_as_batch
if app is not None and db is not None:
self.init_app(app, db, directory)

def init_app(self, app, db=None, directory=None, compare_type=None,
render_as_batch=None, **kwargs):
def init_app(self, app, db=None, directory=None, command=None,
compare_type=None, render_as_batch=None, **kwargs):
self.db = db or self.db
self.command = command or self.command
self.directory = str(directory or self.directory)
self.alembic_ctx_kwargs.update(kwargs)
if compare_type is not None:
Expand All @@ -67,6 +69,9 @@ def init_app(self, app, db=None, directory=None, compare_type=None,
app.extensions['migrate'] = _MigrateConfig(
self, self.db, **self.alembic_ctx_kwargs)

from flask_migrate.cli import db as db_cli_group
app.cli.add_command(db_cli_group, name=self.command)

def configure(self, f):
self.configure_callbacks.append(f)
return f
Expand Down
2 changes: 1 addition & 1 deletion tests/app_compare_type1.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)
migrate = Migrate(app, db)
migrate = Migrate(app, db, command='database')


class User(db.Model):
Expand Down
2 changes: 1 addition & 1 deletion tests/app_compare_type2.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)
migrate = Migrate(app, db)
migrate = Migrate(app, db, command='database')


class User(db.Model):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ def test_custom_directory_path(self):
db.session.commit()

def test_compare_type(self):
(o, e, s) = run_cmd('app_compare_type1.py', 'flask db init')
(o, e, s) = run_cmd('app_compare_type1.py', 'flask database init')
self.assertTrue(s == 0)
(o, e, s) = run_cmd('app_compare_type1.py', 'flask db migrate')
(o, e, s) = run_cmd('app_compare_type1.py', 'flask database migrate')
self.assertTrue(s == 0)
(o, e, s) = run_cmd('app_compare_type1.py', 'flask db upgrade')
(o, e, s) = run_cmd('app_compare_type1.py', 'flask database upgrade')
self.assertTrue(s == 0)
(o, e, s) = run_cmd('app_compare_type2.py', 'flask db migrate')
(o, e, s) = run_cmd('app_compare_type2.py', 'flask database migrate')
self.assertTrue(s == 0)
self.assertTrue(b'Detected type change from VARCHAR(length=128) '
b'to String(length=10)' in e)

0 comments on commit b9c9d35

Please sign in to comment.