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

[CT-1823] [Bug] [1.4.0rc1] --favor-state not working as intended #6617

Closed
jtcohen6 opened this issue Jan 14, 2023 · 1 comment · Fixed by #6616
Closed

[CT-1823] [Bug] [1.4.0rc1] --favor-state not working as intended #6617

jtcohen6 opened this issue Jan 14, 2023 · 1 comment · Fixed by #6616
Assignees
Labels
bug Something isn't working cli
Milestone

Comments

@jtcohen6
Copy link
Contributor

jtcohen6 commented Jan 14, 2023

Not working

In situations of "deferral," the --favor-state flag should tell dbt to always use the upstream resources defined in the --state manifest, even in cases where that resource has already been materialized in the current (dev/CI) namespace.

While #5859 added the flag appropriately, and the favor_state argument to Manifest.merge_from_artifact, the flag is not actually being piped through the task and into that method. So it never takes effect!

I think this wasn't caught because the old-style integration tests for this functionality were really tricky to read & understand. I only caught this while converting those tests to the new framework (#6616).

Repro case

-- models/model_a.sql
select 1 as id

-- models/model_b.sql
select * from {{ ref('model_a') }}
$ dbt run --target prod
$ mkdir state
$ mv target/manifest.json state/
$ dbt run --target dev
$ dbt run --target dev --select model_b --defer --favor-state --state state/

Because the --favor-state flag was passed, model_b should select from the "prod" version of model_a, even though it exists in the dev schema. Instead, it still selects from the dev version:

  create view "jerco"."dbt_jcohen"."model_b__dbt_tmp" as (
    select * from "jerco"."dbt_jcohen"."model_a"
  );

Missing from some relevant tasks

Also in v1.4, we're adding support for --defer to dbt compile + dbt docs generate. (Makes sense!) Let's make sure --favor-state is available on all the same commands as --defer.

[Tech debt] Inconsistent implementation

Also, the way --favor-state was implemented is parallel to --defer, but different from most of our other flags since v1.0:

dbt-core/core/dbt/flags.py

Lines 117 to 118 in 20c95a4

DEFER_MODE = env_set_truthy("DBT_DEFER_TO_STATE")
FAVOR_STATE_MODE = env_set_truthy("DBT_FAVOR_STATE")

(Namely, the argparse default is set based on the flag, rather than the other way around!)

dbt-core/core/dbt/main.py

Lines 504 to 515 in 20c95a4

def _add_favor_state_argument(*subparsers):
for sub in subparsers:
sub.add_optional_argument_inverse(
"--favor-state",
enable_help="""
If set, defer to the state variable for resolving unselected nodes, even if node exist as a database object in the current environment.
""",
disable_help="""
If defer is set, expect standard defer behaviour.
""",
default=flags.FAVOR_STATE_MODE,
)

Let's just solve for this when we move these flags/etc into the new CLI (#6585). We can always add back-compat for the older env vars + flag names — since, in the meantime, some users might add code to their projects like {{ flags.FAVOR_STATE_MODE }}.

@jtcohen6 jtcohen6 added bug Something isn't working cli labels Jan 14, 2023
@jtcohen6 jtcohen6 self-assigned this Jan 14, 2023
@github-actions github-actions bot changed the title [Bug] [1.4.0rc1] --favor-state not working as intended [CT-1823] [Bug] [1.4.0rc1] --favor-state not working as intended Jan 14, 2023
@jtcohen6
Copy link
Contributor Author

jtcohen6 commented Jan 14, 2023

The old integration tests pass with or without the fix for this in ccbdcba — not very helpful!

Whereas, the new converted tests include one (test_defer_state.py::TestRunDeferStateIFFNotExists::test_run_defer_iff_not_exists) that really does fail if I un-commit the changes from ccbdcba.

I'm inclined to keep the fix + the test conversion together, to merge into main + backport to 1.4.latest. That way, we'll actually have a way to know that this new functionality is still working for all v1.4.x releases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cli
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant