Skip to content
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

Add airflow db autogenerate command for generating migration files #39576

Closed
wants to merge 1 commit into from
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
13 changes: 13 additions & 0 deletions airflow/cli/cli_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,13 @@ def string_lower_type(val):
type=positive_int(allow_zero=False),
help="Wait time between retries in seconds",
)
ARG_DB_MESSAGE = Arg(
(
"-m",
"--message",
),
help="Message to apply to the migration",
)

# pool
ARG_POOL_NAME = Arg(("pool",), metavar="NAME", help="Pool name")
Expand Down Expand Up @@ -1644,6 +1651,12 @@ class GroupCommand(NamedTuple):
func=lazy_load_command("airflow.cli.commands.db_command.drop_archived"),
args=(ARG_DB_TABLES, ARG_YES),
),
ActionCommand(
name="autogenerate",
help="Reset the DB and generate a migration script for the database",
func=lazy_load_command("airflow.cli.commands.db_command.autogenerate"),
args=(ARG_DB_MESSAGE, ARG_YES),
),
)
CONNECTIONS_COMMANDS = (
ActionCommand(
Expand Down
10 changes: 10 additions & 0 deletions airflow/cli/commands/db_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,13 @@ def drop_archived(args):
table_names=args.tables,
needs_confirm=not args.yes,
)


@cli_utils.action_cli(check_db=False)
@providers_configuration_loaded
def autogenerate(args):
"""Generate migration script."""
print(f"DB: {settings.engine.url!r}")
if not (args.yes or input("This will drop existing tables if they exist. Proceed? (y/n)").upper() == "Y"):
raise SystemExit("Cancelled")
db.autogenerate(message=args.message)
14 changes: 14 additions & 0 deletions airflow/utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,20 @@ def drop_airflow_moved_tables(connection):
Base.metadata.remove(tbl)


def autogenerate(message: str | None = None) -> None:
"""Automatically generate the migration scripts.

The database is first reset and built using the migration files so that autogenerate
would accurately reflect the current state of the database and ORM
"""
from alembic import command

resetdb(use_migration_files=True)

config = _get_alembic_config()
command.revision(config, message=message, autogenerate=True)


@provide_session
def check(session: Session = NEW_SESSION):
"""
Expand Down
11 changes: 11 additions & 0 deletions tests/utils/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from airflow.settings import engine
from airflow.utils.db import (
_get_alembic_config,
autogenerate,
check_bad_references,
check_migrations,
compare_server_default,
Expand Down Expand Up @@ -235,6 +236,16 @@ def test_resetdb(
else:
mock_init.assert_called_once_with(session=session_mock, use_migration_files=False)

@mock.patch("airflow.utils.db._get_alembic_config")
@mock.patch("airflow.utils.db.resetdb")
@mock.patch("alembic.command")
def test_db_autogenerate(self, mock_alembic_command, mock_resetdb, mock_config):
autogenerate(message="test migration")
mock_resetdb.assert_called_once_with(use_migration_files=True)
mock_alembic_command.revision.assert_called_once_with(
mock_config.return_value, message="test migration", autogenerate=True
)

def test_alembic_configuration(self):
with mock.patch.dict(
os.environ, {"AIRFLOW__DATABASE__ALEMBIC_INI_FILE_PATH": "/tmp/alembic.ini"}, clear=True
Expand Down
Loading