fix(cli): bypass migration prompt entirely when --yes flag is passed #1139
@@ -192,12 +192,17 @@ def init_command(
|
||||
|
||||
container = get_container()
|
||||
|
||||
# When --yes is passed, auto-approve migrations so the command never
|
||||
# blocks waiting for interactive input. We inject a prompt callback
|
||||
# that always returns True into the UnitOfWork factory, bypassing
|
||||
# the default prompt (which may call typer.confirm on a TTY).
|
||||
# When --yes is passed, disable the migration confirmation prompt
|
||||
# entirely so the command never blocks waiting for interactive
|
||||
# input (bug #783). ``require_confirmation=False`` tells the
|
||||
# migration runner to skip the prompt; the ``prompt_for_migration``
|
||||
# callback is set as a belt-and-suspenders fallback that
|
||||
# auto-approves in case any code path still reaches the prompt.
|
||||
if yes:
|
||||
container.unit_of_work.add_kwargs(prompt_for_migration=lambda _: True)
|
||||
container.unit_of_work.add_kwargs(
|
||||
require_confirmation=False,
|
||||
prompt_for_migration=lambda _: True,
|
||||
)
|
||||
|
||||
project_service: ProjectService = container.project_service()
|
||||
|
||||
|
||||
@@ -41,6 +41,7 @@ class UnitOfWork:
|
||||
self,
|
||||
database_url: str,
|
||||
prompt_for_migration: Callable[[str], bool] | None = None,
|
||||
require_confirmation: bool = True,
|
||||
) -> None:
|
||||
"""Initialize Unit of Work with database connection.
|
||||
|
||||
@@ -48,12 +49,16 @@ class UnitOfWork:
|
||||
database_url: SQLAlchemy database URL
|
||||
prompt_for_migration: Optional callback to confirm migrations
|
||||
before applying them automatically.
|
||||
require_confirmation: When False, migrations are applied without
|
||||
prompting the user. Set to False when the caller has
|
||||
already obtained blanket approval (e.g. ``--yes`` flag).
|
||||
"""
|
||||
self.database_url = database_url
|
||||
self._engine: Engine | None = None
|
||||
self._session_factory: sessionmaker[Session] | None = None
|
||||
self._database_initialized = False
|
||||
self._prompt_for_migration = prompt_for_migration
|
||||
self._require_confirmation = require_confirmation
|
||||
|
||||
@property
|
||||
def engine(self) -> Engine:
|
||||
@@ -141,7 +146,7 @@ class UnitOfWork:
|
||||
|
||||
runner = MigrationRunner(self.database_url)
|
||||
runner.init_or_upgrade(
|
||||
require_confirmation=True,
|
||||
require_confirmation=self._require_confirmation,
|
||||
prompt_for_migration=self._prompt_for_migration,
|
||||
)
|
||||
self._database_initialized = True
|
||||
|
||||
Reference in New Issue
Block a user