Skip to content

Commit

Permalink
fix: raise error on migration fail (meltano#6601)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Payne committed Aug 15, 2022
1 parent d480573 commit 1eaec5d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
29 changes: 26 additions & 3 deletions src/meltano/cli/params.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Click parameter helper decorators."""

from __future__ import annotations

import functools
Expand All @@ -6,12 +8,19 @@
from click.globals import get_current_context as get_current_click_context

from meltano.core.db import project_engine
from meltano.core.migration_service import MigrationError
from meltano.core.project_settings_service import ProjectSettingsService

from .utils import CliError


def database_uri_option(func):
"""Database URI Click option decorator.
args:
func: The function to decorate.
"""

@click.option("--database-uri", help="System database URI.")
def decorate(*args, database_uri=None, **kwargs):
if database_uri:
Expand All @@ -28,9 +37,20 @@ class pass_project: # noqa: N801
__name__ = "project"

def __init__(self, migrate=False):
"""Instantiate decorator.
args:
migrate: Flag to perform database migration before passing the project.
"""
self.migrate = migrate

def __call__(self, func):
"""Return decorated function.
args:
func: The function to decorate.
"""

@database_uri_option
def decorate(*args, **kwargs):
ctx = get_current_click_context()
Expand All @@ -48,9 +68,12 @@ def decorate(*args, **kwargs):
if self.migrate:
from meltano.core.migration_service import MigrationService

migration_service = MigrationService(engine)
migration_service.upgrade(silent=True)
migration_service.seed(project)
try:
migration_service = MigrationService(engine)
migration_service.upgrade(silent=True)
migration_service.seed(project)
except MigrationError as err:
raise CliError(str(err))

func(project, *args, **kwargs)

Expand Down
6 changes: 2 additions & 4 deletions src/meltano/core/migration_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,8 @@ def upgrade( # noqa: WPS213, WPS231 too many expression and too complex
click.secho("System database up-to-date.")
except Exception as err:
logging.exception(str(err))
click.secho(
"Cannot upgrade the system database. It might be corrupted or was created before database migrations where introduced (v0.34.0)",
fg="yellow",
err=True,
raise MigrationError(
"Cannot upgrade the system database. It might be corrupted or was created before database migrations where introduced (v0.34.0)"
)
finally:
conn.close()
Expand Down

0 comments on commit 1eaec5d

Please sign in to comment.