Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dbt): Adding flag to raise an error when a model sync fails #266

Merged
merged 11 commits into from
Mar 11, 2024

Conversation

Vitor-Avila
Copy link
Contributor

@Vitor-Avila Vitor-Avila commented Mar 5, 2024

Currently, during the dbt sync models might fail to be synced. The operation is not atomic (a sync failure won't prevent other models from being synced) and the CLI logs the error messages.

While the error messages are helpful to debug the issue, when using the CLI in a CI/CD pipeline, the fact that the execution ends up successfully (doesn't return a failed status) results in a successful job, which could be misleading to admins since there were failures.

This PR adds a new flag --raise-failures that when used would end the command execution with an error status code in case any model failed to sync, and also list failures. This can be further used in the future when failing to sync incompatible metrics.

Also added a deprecation logic, so using the --raise-failures flag would also return an error code in case the command was triggered using deprecated features. This is going to be used in the future when deprecating --preserve-columns (replaced by --preserve-metadata).

Copy link
Member

@betodealmeida betodealmeida left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome! My only comment is that we should replace raise_error with a proper raise Exception, see my comments.

),
)
sys.exit(1)
raise_error(1, "Couldn't read credentials")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better approach here would be to define a new exception:

class CLIError(Exception):
    def __init__(self, code: int, message: str):
        ...

Then here you can do:

except Exception as ex:
    raise CLIError(1, "Couldn't read credentials") from ex

And then we can either add a decorator to the command that catches CLIError and displays the message and exits, or we can catch it explicitly where we call this function.

With raise CLIError() it's clear that the code execution stops there (since it raises an exception). The problem with raise_error() is that there's no indication that the flow will stop, unless I check the function and see that it calls sys.exit() at the end.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh that's a good idea! let me look into that!

Comment on lines +604 to +606
preset_cli.add_command(auth, name="auth")
preset_cli.add_command(invite_users, name="invite-users")
preset_cli.add_command(import_users, name="import-users")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, for the commands that involved the raise_cli_errors decorator, the tests were failing with command not found until I explicitly added the command names. 🤷

Comment on lines +131 to +132
if raise_failures:
warnings.simplefilter("always", DeprecationWarning)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this otherwise the DeprecationWarning doesn't get logged in the terminal.

@@ -40,6 +42,7 @@ def setup_logging(loglevel: str) -> None:
handlers=[RichHandler()],
force=True,
)
logging.captureWarnings(True)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add to add this to include DeprecationWarnings in the terminal

@@ -54,7 +54,7 @@ repos:
# additional_dependencies: [flake8-bugbear]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v0.910' # Use the sha / tag you want to point at
rev: 'v0.981' # Use the sha / tag you want to point at
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to bump it to fix pre-commit for newer Python versions. python/mypy#13627

Copy link
Member

@betodealmeida betodealmeida left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

@Vitor-Avila Vitor-Avila merged commit 099ba40 into main Mar 11, 2024
4 checks passed
@Vitor-Avila Vitor-Avila deleted the raise-failures branch March 11, 2024 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants