初始提交
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
from .command import app
|
||||
|
||||
__all__ = ['app']
|
||||
@@ -0,0 +1,191 @@
|
||||
from typing import Annotated
|
||||
|
||||
import typer
|
||||
|
||||
from cli.completion.providers import COMPLETION_PROVIDER_GATEWAY
|
||||
from cli.context import AllowProdOption, DryRunOption, EnvOption, OutputOption, YesOption
|
||||
|
||||
from .controller import DbCommandController
|
||||
|
||||
app = typer.Typer(
|
||||
help='数据库相关命令',
|
||||
no_args_is_help=True,
|
||||
context_settings={'help_option_names': ['-h', '--help']},
|
||||
)
|
||||
_DB_COMMAND_CONTROLLER = DbCommandController()
|
||||
|
||||
|
||||
@app.command('check', help='检查数据库连接状态')
|
||||
def check(
|
||||
env: EnvOption = 'dev',
|
||||
output: OutputOption = 'text',
|
||||
) -> None:
|
||||
"""
|
||||
检查数据库连接状态。
|
||||
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:return: None
|
||||
"""
|
||||
_DB_COMMAND_CONTROLLER.check(env, output)
|
||||
|
||||
|
||||
@app.command('current', help='查看当前数据库迁移版本')
|
||||
def current(
|
||||
env: EnvOption = 'dev',
|
||||
output: OutputOption = 'text',
|
||||
) -> None:
|
||||
"""
|
||||
查看数据库当前迁移版本。
|
||||
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:return: None
|
||||
"""
|
||||
_DB_COMMAND_CONTROLLER.current(env, output)
|
||||
|
||||
|
||||
@app.command('upgrade', help='执行数据库升级')
|
||||
def upgrade(
|
||||
env: EnvOption = 'dev',
|
||||
output: OutputOption = 'text',
|
||||
allow_prod: AllowProdOption = False,
|
||||
yes: YesOption = False,
|
||||
dry_run: DryRunOption = False,
|
||||
revision: Annotated[
|
||||
str,
|
||||
typer.Option(
|
||||
'--revision',
|
||||
help='目标迁移版本',
|
||||
autocompletion=COMPLETION_PROVIDER_GATEWAY.complete_alembic_revisions,
|
||||
),
|
||||
] = 'head',
|
||||
) -> None:
|
||||
"""
|
||||
执行数据库升级。
|
||||
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:param allow_prod: 是否允许生产环境危险命令
|
||||
:param yes: 是否跳过确认
|
||||
:param dry_run: 是否演练执行
|
||||
:param revision: 目标迁移版本
|
||||
:return: None
|
||||
"""
|
||||
_DB_COMMAND_CONTROLLER.upgrade(env, output, allow_prod, yes, dry_run, revision=revision)
|
||||
|
||||
|
||||
@app.command('init', help='初始化数据库到最新迁移版本')
|
||||
def init(
|
||||
env: EnvOption = 'dev',
|
||||
output: OutputOption = 'text',
|
||||
allow_prod: AllowProdOption = False,
|
||||
yes: YesOption = False,
|
||||
dry_run: DryRunOption = False,
|
||||
) -> None:
|
||||
"""
|
||||
初始化数据库到最新迁移版本。
|
||||
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:param allow_prod: 是否允许生产环境危险命令
|
||||
:param yes: 是否跳过确认
|
||||
:param dry_run: 是否演练执行
|
||||
:return: None
|
||||
"""
|
||||
_DB_COMMAND_CONTROLLER.init(env, output, allow_prod, yes, dry_run)
|
||||
|
||||
|
||||
@app.command('downgrade', help='执行数据库回退')
|
||||
def downgrade(
|
||||
env: EnvOption = 'dev',
|
||||
output: OutputOption = 'text',
|
||||
allow_prod: AllowProdOption = False,
|
||||
yes: YesOption = False,
|
||||
dry_run: DryRunOption = False,
|
||||
revision: Annotated[
|
||||
str,
|
||||
typer.Option(
|
||||
'--revision',
|
||||
help='目标回退版本',
|
||||
autocompletion=COMPLETION_PROVIDER_GATEWAY.complete_alembic_revisions,
|
||||
),
|
||||
] = '-1',
|
||||
) -> None:
|
||||
"""
|
||||
执行数据库回退。
|
||||
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:param allow_prod: 是否允许生产环境危险命令
|
||||
:param yes: 是否跳过确认
|
||||
:param dry_run: 是否演练执行
|
||||
:param revision: 目标回退版本
|
||||
:return: None
|
||||
"""
|
||||
_DB_COMMAND_CONTROLLER.downgrade(env, output, allow_prod, yes, dry_run, revision=revision)
|
||||
|
||||
|
||||
@app.command('revision', help='创建新的数据库迁移版本文件')
|
||||
def revision(
|
||||
message: Annotated[str, typer.Option('--message', '-m', help='迁移说明')],
|
||||
env: EnvOption = 'dev',
|
||||
output: OutputOption = 'text',
|
||||
allow_prod: AllowProdOption = False,
|
||||
yes: YesOption = False,
|
||||
dry_run: DryRunOption = False,
|
||||
autogenerate: bool = typer.Option(False, '--autogenerate/--no-autogenerate', help='是否自动生成迁移内容'),
|
||||
) -> None:
|
||||
"""
|
||||
创建新的数据库迁移版本文件。
|
||||
|
||||
:param message: 迁移说明
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:param allow_prod: 是否允许生产环境危险命令
|
||||
:param yes: 是否跳过确认
|
||||
:param dry_run: 是否演练执行
|
||||
:param autogenerate: 是否自动生成迁移内容
|
||||
:return: None
|
||||
"""
|
||||
_DB_COMMAND_CONTROLLER.revision(
|
||||
message,
|
||||
env,
|
||||
output,
|
||||
allow_prod,
|
||||
yes,
|
||||
dry_run,
|
||||
autogenerate=autogenerate,
|
||||
)
|
||||
|
||||
|
||||
@app.command('heads', help='查看当前代码仓库中的 Alembic heads')
|
||||
def heads(
|
||||
env: EnvOption = 'dev',
|
||||
output: OutputOption = 'text',
|
||||
) -> None:
|
||||
"""
|
||||
查看当前代码仓库中的 Alembic heads。
|
||||
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:return: None
|
||||
"""
|
||||
_DB_COMMAND_CONTROLLER.heads(env, output)
|
||||
|
||||
|
||||
@app.command('history', help='查看当前代码仓库中的 Alembic 历史版本')
|
||||
def history(
|
||||
env: EnvOption = 'dev',
|
||||
output: OutputOption = 'text',
|
||||
limit: Annotated[int, typer.Option('--limit', min=1, help='输出的最大历史记录数量')] = 20,
|
||||
) -> None:
|
||||
"""
|
||||
查看当前代码仓库中的 Alembic 历史版本。
|
||||
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:param limit: 输出的最大历史记录数量
|
||||
:return: None
|
||||
"""
|
||||
_DB_COMMAND_CONTROLLER.history(env, output, limit=limit)
|
||||
@@ -0,0 +1,237 @@
|
||||
from cli.core import (
|
||||
DEFAULT_CORE_SERVICES,
|
||||
CliContextFactory,
|
||||
CliExecutionService,
|
||||
)
|
||||
from cli.runtime.db import DATABASE_RUNTIME, DatabaseRuntimeService
|
||||
|
||||
from .presenter import DbCommandPresenter
|
||||
|
||||
|
||||
class DbCommandController:
|
||||
"""
|
||||
数据库命令控制器。
|
||||
|
||||
该控制器负责组织 `db` 命令组的上下文准备、runtime 调用、
|
||||
payload 注入,以及基于输出格式选择 presenter 或直接返回 JSON。
|
||||
|
||||
:param context_factory: CLI 上下文工厂
|
||||
:param execution_service: CLI 执行服务
|
||||
:param presenter: 数据库命令文本渲染器
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
context_factory: CliContextFactory | None = None,
|
||||
execution_service: CliExecutionService | None = None,
|
||||
presenter: DbCommandPresenter | None = None,
|
||||
runtime_service: DatabaseRuntimeService | None = None,
|
||||
) -> None:
|
||||
"""
|
||||
初始化数据库命令控制器。
|
||||
|
||||
:param context_factory: CLI 上下文工厂
|
||||
:param execution_service: CLI 执行服务
|
||||
:param presenter: 数据库命令文本渲染器
|
||||
:param runtime_service: 数据库运行时服务
|
||||
:return: None
|
||||
"""
|
||||
self.context_factory = context_factory or DEFAULT_CORE_SERVICES.context_factory
|
||||
self.execution_service = execution_service or DEFAULT_CORE_SERVICES.execution_service
|
||||
self.presenter = presenter or DbCommandPresenter()
|
||||
self.runtime_service = runtime_service or DATABASE_RUNTIME
|
||||
|
||||
def check(self, env: str, output: str) -> None:
|
||||
"""
|
||||
检查数据库连接状态。
|
||||
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:return: None
|
||||
"""
|
||||
ctx = self.context_factory.build_readonly(env, output)
|
||||
self.execution_service.complete_payload(
|
||||
ctx, self.execution_service.run_async(self.runtime_service.ping_database())
|
||||
)
|
||||
|
||||
def current(self, env: str, output: str) -> None:
|
||||
"""
|
||||
查看数据库当前迁移版本。
|
||||
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:return: None
|
||||
"""
|
||||
ctx = self.context_factory.build_readonly(env, output)
|
||||
payload = self.runtime_service.get_current_revision()
|
||||
payload['env'] = ctx.env
|
||||
self.execution_service.complete_payload_with_text(
|
||||
ctx,
|
||||
payload,
|
||||
text_builder=self.presenter.build_current_revision_text,
|
||||
text_condition=lambda result_data: bool(result_data.get('ok', False)),
|
||||
)
|
||||
|
||||
def upgrade(
|
||||
self,
|
||||
env: str,
|
||||
output: str,
|
||||
allow_prod: bool,
|
||||
yes: bool,
|
||||
dry_run: bool,
|
||||
*,
|
||||
revision: str,
|
||||
) -> None:
|
||||
"""
|
||||
执行数据库升级。
|
||||
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:param allow_prod: 是否允许生产环境危险命令
|
||||
:param yes: 是否跳过确认
|
||||
:param dry_run: 是否演练执行
|
||||
:param revision: 目标迁移版本
|
||||
:return: None
|
||||
"""
|
||||
ctx = self.context_factory.build_dangerous(
|
||||
env,
|
||||
output,
|
||||
allow_prod,
|
||||
yes,
|
||||
dry_run,
|
||||
command_name='db upgrade',
|
||||
)
|
||||
self.execution_service.complete_payload(ctx, self.runtime_service.upgrade_database(revision, dry_run=dry_run))
|
||||
|
||||
def init(
|
||||
self,
|
||||
env: str,
|
||||
output: str,
|
||||
allow_prod: bool,
|
||||
yes: bool,
|
||||
dry_run: bool,
|
||||
) -> None:
|
||||
"""
|
||||
初始化数据库到最新迁移版本。
|
||||
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:param allow_prod: 是否允许生产环境危险命令
|
||||
:param yes: 是否跳过确认
|
||||
:param dry_run: 是否演练执行
|
||||
:return: None
|
||||
"""
|
||||
ctx = self.context_factory.build_dangerous(
|
||||
env,
|
||||
output,
|
||||
allow_prod,
|
||||
yes,
|
||||
dry_run,
|
||||
command_name='db init',
|
||||
)
|
||||
self.execution_service.complete_payload(ctx, self.runtime_service.init_database(dry_run=dry_run))
|
||||
|
||||
def downgrade(
|
||||
self,
|
||||
env: str,
|
||||
output: str,
|
||||
allow_prod: bool,
|
||||
yes: bool,
|
||||
dry_run: bool,
|
||||
*,
|
||||
revision: str,
|
||||
) -> None:
|
||||
"""
|
||||
执行数据库回退。
|
||||
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:param allow_prod: 是否允许生产环境危险命令
|
||||
:param yes: 是否跳过确认
|
||||
:param dry_run: 是否演练执行
|
||||
:param revision: 目标回退版本
|
||||
:return: None
|
||||
"""
|
||||
ctx = self.context_factory.build_dangerous(
|
||||
env,
|
||||
output,
|
||||
allow_prod,
|
||||
yes,
|
||||
dry_run,
|
||||
command_name='db downgrade',
|
||||
)
|
||||
self.execution_service.complete_payload(ctx, self.runtime_service.downgrade_database(revision, dry_run=dry_run))
|
||||
|
||||
def revision(
|
||||
self,
|
||||
message: str,
|
||||
env: str,
|
||||
output: str,
|
||||
allow_prod: bool,
|
||||
yes: bool,
|
||||
dry_run: bool,
|
||||
*,
|
||||
autogenerate: bool,
|
||||
) -> None:
|
||||
"""
|
||||
创建新的数据库迁移版本文件。
|
||||
|
||||
:param message: 迁移说明
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:param allow_prod: 是否允许生产环境危险命令
|
||||
:param yes: 是否跳过确认
|
||||
:param dry_run: 是否演练执行
|
||||
:param autogenerate: 是否自动生成迁移内容
|
||||
:return: None
|
||||
"""
|
||||
ctx = self.context_factory.build_dangerous(
|
||||
env,
|
||||
output,
|
||||
allow_prod,
|
||||
yes,
|
||||
dry_run,
|
||||
command_name='db revision',
|
||||
)
|
||||
self.execution_service.complete_payload(
|
||||
ctx,
|
||||
self.runtime_service.create_revision(message, autogenerate=autogenerate, dry_run=dry_run),
|
||||
)
|
||||
|
||||
def heads(self, env: str, output: str) -> None:
|
||||
"""
|
||||
查看当前代码仓库中的 Alembic heads。
|
||||
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:return: None
|
||||
"""
|
||||
ctx = self.context_factory.build_readonly(env, output)
|
||||
payload = self.runtime_service.get_alembic_heads()
|
||||
payload['env'] = ctx.env
|
||||
self.execution_service.complete_payload_with_text(
|
||||
ctx,
|
||||
payload,
|
||||
text_builder=self.presenter.build_alembic_revisions_text,
|
||||
text_condition=lambda result_data: 'error' not in result_data,
|
||||
)
|
||||
|
||||
def history(self, env: str, output: str, *, limit: int) -> None:
|
||||
"""
|
||||
查看当前代码仓库中的 Alembic 历史版本。
|
||||
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:param limit: 输出的最大历史记录数量
|
||||
:return: None
|
||||
"""
|
||||
ctx = self.context_factory.build_readonly(env, output)
|
||||
payload = self.runtime_service.get_alembic_history(limit=limit)
|
||||
payload['env'] = ctx.env
|
||||
self.execution_service.complete_payload_with_text(
|
||||
ctx,
|
||||
payload,
|
||||
text_builder=self.presenter.build_alembic_revisions_text,
|
||||
text_condition=lambda result_data: 'error' not in result_data,
|
||||
)
|
||||
@@ -0,0 +1,71 @@
|
||||
class DbCommandPresenter:
|
||||
"""
|
||||
数据库命令文本渲染器。
|
||||
|
||||
该渲染器负责将 `db` 命令组产生的结构化 payload 转换为稳定的文本摘要,
|
||||
同时保持 JSON 输出仍由控制器直接返回,不在此处做契约变形。
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def build_current_revision_text(payload: dict[str, object]) -> str:
|
||||
"""
|
||||
将数据库当前迁移版本结果渲染为文本摘要。
|
||||
|
||||
:param payload: 当前迁移版本结果字典
|
||||
:return: 文本摘要
|
||||
"""
|
||||
return '\n'.join(
|
||||
[
|
||||
f'ok: {str(payload.get("ok", False)).lower()}',
|
||||
f'env: {payload.get("env", "")}',
|
||||
f'current_revision: {payload.get("currentRevision", "-")}',
|
||||
]
|
||||
)
|
||||
|
||||
def build_alembic_revisions_text(self, payload: dict[str, object]) -> str:
|
||||
"""
|
||||
将 Alembic 修订版本结果渲染为文本摘要。
|
||||
|
||||
:param payload: 修订版本结果字典
|
||||
:return: 文本摘要
|
||||
"""
|
||||
items = payload.get('items')
|
||||
lines = [
|
||||
f'ok: {str(payload.get("ok", False)).lower()}',
|
||||
f'env: {payload.get("env", "")}',
|
||||
f'message: {payload.get("message", "-")}',
|
||||
f'count: {payload.get("count", 0)}',
|
||||
]
|
||||
if 'totalCount' in payload:
|
||||
lines.append(f'total_count: {payload.get("totalCount", 0)}')
|
||||
if 'limit' in payload:
|
||||
lines.append(f'limit: {payload.get("limit", 0)}')
|
||||
if not isinstance(items, list) or not items:
|
||||
lines.append('items: none')
|
||||
return '\n'.join(lines)
|
||||
|
||||
lines.append('items:')
|
||||
for item in items:
|
||||
if isinstance(item, dict):
|
||||
lines.extend([f' {line}' for line in self._build_alembic_revision_item_lines(item)])
|
||||
return '\n'.join(lines)
|
||||
|
||||
@staticmethod
|
||||
def _build_alembic_revision_item_lines(revision_item: dict[str, object]) -> list[str]:
|
||||
"""
|
||||
构建单个 Alembic 修订版本的文本行。
|
||||
|
||||
:param revision_item: 修订版本结果字典
|
||||
:return: 文本行列表
|
||||
"""
|
||||
down_revisions = revision_item.get('downRevisions')
|
||||
branch_labels = revision_item.get('branchLabels')
|
||||
depends_on = revision_item.get('dependsOn')
|
||||
return [
|
||||
f'- revision: {revision_item.get("revision", "-")}',
|
||||
f' down_revisions: {",".join(down_revisions) if isinstance(down_revisions, list) and down_revisions else "-"}',
|
||||
f' branch_labels: {",".join(branch_labels) if isinstance(branch_labels, list) and branch_labels else "-"}',
|
||||
f' depends_on: {",".join(depends_on) if isinstance(depends_on, list) and depends_on else "-"}',
|
||||
f' doc: {revision_item.get("doc", "") or "-"}',
|
||||
f' path: {revision_item.get("path", "-")}',
|
||||
]
|
||||
Reference in New Issue
Block a user