From 3146c983e2e3d0b49cd525e89f063959eb39acff Mon Sep 17 00:00:00 2001 From: Sergey Motornyuk Date: Tue, 5 Dec 2023 14:52:26 +0200 Subject: [PATCH 1/2] feat: db-upgrade runs migrations from plugins --- ckan/cli/db.py | 45 ++++++++++++++++++------- ckan/tests/cli/test_db.py | 19 +++++++++++ doc/maintaining/database-management.rst | 4 +++ 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/ckan/cli/db.py b/ckan/cli/db.py index 2b14fde25e7..a3315985de1 100644 --- a/ckan/cli/db.py +++ b/ckan/cli/db.py @@ -58,13 +58,34 @@ def clean(): @db.command() -@click.option(u'-v', u'--version', help=u'Migration version', default=u'head') +@click.option('-v', '--version', help='Migration version', default='head') +@click.option('--skip-plugins', is_flag=True, help='Skip plugin migrations') +@click.option('--skip-core', is_flag=True, help='Skip core migrations') +@click.pass_context @applies_to_plugin -def upgrade(version: str, plugin: str): +def upgrade( + ctx: click.Context, version: str, plugin: str, + skip_core: bool, skip_plugins: bool +): """Upgrade the database. """ - _run_migrations(plugin, version) - click.secho(u'Upgrading DB: SUCCESS', fg=u'green', bold=True) + if not skip_core: + _run_migrations(plugin, version) + + if not skip_plugins and not plugin: + _migrate_plugins(apply=True) + + click.secho('Upgrading DB: SUCCESS', fg='green', bold=True) + + +def _migrate_plugins(apply: bool): + pending = _get_pending_plugins() + for plugin, n in sorted(pending.items()): + click.secho("{n} unapplied migrations for {p}".format( + p=click.style(plugin, bold=True), + n=click.style(str(n), bold=True))) + if apply: + _run_migrations(plugin) @db.command() @@ -82,15 +103,13 @@ def downgrade(version: str, plugin: str): def pending_migrations(apply: bool): """List all sources with unapplied migrations. """ - pending = _get_pending_plugins() - if not pending: - click.secho("All plugins are up-to-date", fg="green") - for plugin, n in sorted(pending.items()): - click.secho("{n} unapplied migrations for {p}".format( - p=click.style(plugin, bold=True), - n=click.style(str(n), bold=True))) - if apply: - _run_migrations(plugin) + if apply: + click.secho( + "Use `db upgrade` to run migrations for enabled plugins", + fg="yellow" + ) + _migrate_plugins(apply) + click.secho('Upgrading DB: SUCCESS', fg='green', bold=True) def _get_pending_plugins() -> dict[str, int]: diff --git a/ckan/tests/cli/test_db.py b/ckan/tests/cli/test_db.py index 2f184567b4f..346ab800045 100644 --- a/ckan/tests/cli/test_db.py +++ b/ckan/tests/cli/test_db.py @@ -10,6 +10,7 @@ import ckan.model as model import ckan.cli.db as db +from ckan.cli.cli import ckan @pytest.fixture @@ -98,3 +99,21 @@ def test_pending_list(self): assert db._get_pending_plugins() == {"example_database_migrations": 1} db._run_migrations(u'example_database_migrations') assert db._get_pending_plugins() == {} + + @pytest.mark.usefixtures("with_extended_cli") + def test_upgrade_applies_plugin_migrations(self, cli): + """`db upgrade` command automatically applies all pending migrations + from plugins. + + """ + cli.invoke(ckan, ["db", "upgrade"]) + assert db._get_pending_plugins() == {} + + @pytest.mark.usefixtures("with_extended_cli") + def test_upgrade_skips_plugin_migrations(self, cli): + """`db upgrade` command can ignore pending migrations from plugins if + `--skip-plugins` flag is enabled. + + """ + cli.invoke(ckan, ["db", "upgrade", "--skip-plugins"]) + assert db._get_pending_plugins() == {"example_database_migrations": 2} diff --git a/doc/maintaining/database-management.rst b/doc/maintaining/database-management.rst index eff866d1543..5f3e77e2a26 100644 --- a/doc/maintaining/database-management.rst +++ b/doc/maintaining/database-management.rst @@ -161,3 +161,7 @@ CKAN database's schema using the ``ckan db upgrade`` command: .. parsed-literal:: ckan -c |ckan.ini| db upgrade + +This command applies all CKAN core migrations and all unapplied migrations from +enabled plugins. ``--skip-core`` and ``--skip-plugins`` flags can be used to +run either only core migration, or only migrations from enabled plugins. From a502a6ba3a003f095719d1ea492e491c53ea9f72 Mon Sep 17 00:00:00 2001 From: Sergey Motornyuk Date: Tue, 5 Dec 2023 14:57:58 +0200 Subject: [PATCH 2/2] chore: add changelog fragment --- changes/7961.feature | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changes/7961.feature diff --git a/changes/7961.feature b/changes/7961.feature new file mode 100644 index 00000000000..430bfaa4668 --- /dev/null +++ b/changes/7961.feature @@ -0,0 +1,3 @@ +``ckan db upgrade`` CLI command automatically applies migrations from +plugins. Use ``ckan db upgrade --skip-plugins`` if this behavior does not fit +into your deployment process.