Skip to content

Commit

Permalink
Merge pull request ckan#7961 from ckan/include-pending-migrations-int…
Browse files Browse the repository at this point in the history
…o-main-db-upgrade

Apply migrations from plugins during `ckan db upgrade`
  • Loading branch information
pdelboca authored Dec 11, 2023
2 parents 3816367 + a502a6b commit 4974db6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 13 deletions.
3 changes: 3 additions & 0 deletions changes/7961.feature
Original file line number Diff line number Diff line change
@@ -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.
45 changes: 32 additions & 13 deletions ckan/cli/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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]:
Expand Down
19 changes: 19 additions & 0 deletions ckan/tests/cli/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import ckan.model as model
import ckan.cli.db as db
from ckan.cli.cli import ckan


@pytest.fixture
Expand Down Expand Up @@ -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}
4 changes: 4 additions & 0 deletions doc/maintaining/database-management.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit 4974db6

Please sign in to comment.