Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion alembic/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,11 +681,16 @@ def branches(config: Config, verbose: bool = False) -> None:
)


def current(config: Config, verbose: bool = False) -> None:
def current(
config: Config, check_heads: bool = False, verbose: bool = False
Comment thread
sscherfke marked this conversation as resolved.
) -> None:
"""Display the current revision for a database.

:param config: a :class:`.Config` instance.

:param check_heads: Check if all head revisions are applied to the
database. Exit with an error if this is not the case.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Michael Bayer (zzzeek) wrote:

.. versionadded:: 1.17.1

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/6265


:param verbose: output in verbose mode.

"""
Expand All @@ -698,6 +703,10 @@ def display_version(rev, context):
"Current revision(s) for %s:",
util.obfuscate_url_pw(context.connection.engine.url),
)
if check_heads and (
set(context.get_current_heads()) != set(script.get_heads())
):
raise util.CommandError("Database is not on all head revisions")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Michael Bayer (zzzeek) wrote:

like alembic check, will add a subclass here

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/6265

for rev in script.get_all_current(rev):
config.print_stdout(rev.cmd_format(verbose))

Expand Down
11 changes: 11 additions & 0 deletions alembic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,17 @@ def __init__(self, prog: Optional[str] = None) -> None:
"environment and version locations",
),
),
"check_heads": (
Comment thread
sscherfke marked this conversation as resolved.
"-c",
"--check-heads",
dict(
action="store_true",
help=(
"Check if all head revisions are applied to the database. "
"Exit with an error code if this is not the case."
),
),
),
}
_POSITIONAL_OPTS = {
"directory": dict(help="location of scripts directory"),
Expand Down
8 changes: 8 additions & 0 deletions docs/build/unreleased/1705.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.. change::
:tags: usecase, commands
:tickets: 1705

Added ``--check-heads`` option to ``current`` command which
checks if all head revisions are applied to the database.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Michael Bayer (zzzeek) wrote:

will mention the cookbook recipe here

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/6265

The command exists with a non-zero exit code if this is not the
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Michael Bayer (zzzeek) wrote:

typo exists

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/6265

case.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Michael Bayer (zzzeek) wrote:

pull request courtesy Stefan Scherfke

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/6265

34 changes: 34 additions & 0 deletions tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,40 @@ def test_heads_upg(self):
with self._assert_lines(["a2", "b3"]):
command.current(self.cfg)

def test_check_heads_success(self):
"""
"--check-heads" succeeds if all head revisions are applied.
"""
command.stamp(self.cfg, ())
command.stamp(self.cfg, (self.a3.revision, self.b3.revision))
with self._assert_lines(["a3", "b3"]):
command.current(self.cfg, check_heads=True)

@testing.combinations(
["a2"],
["a3"],
["b3"],
["a2", "b3"],
["a3", "b2"],
argnames="revs",
)
def test_check_heads_fail(self, revs):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Michael Bayer (zzzeek) wrote:

nice job

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/6265

"""
"--check-heads" succeeds if all head revisions are applied.
"""
command.stamp(self.cfg, ())
command.stamp(
self.cfg, tuple(getattr(self, rev).revision for rev in revs)
)
assert_raises_message(
util.CommandError,
"Database is not on all head revisions",
command.current,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Michael Bayer (zzzeek) wrote:

there might be some test cleanup error occurring, investigating

it looks like test_doesnt_create_alembic_version was relying on test ordering to work, fixed that test separately.

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/6265

self.cfg,
check_heads=True,
)
command.stamp(self.cfg, ())


class RevisionTest(TestBase):
def setUp(self):
Expand Down
Loading