Skip to content

Commit 27d1f3d

Browse files
committed
Remove usage of no_gaps
1 parent 83cfc2b commit 27d1f3d

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

sqlmesh/core/context.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,8 +1518,11 @@ def plan_builder(
15181518
include_unmodified = self.config.plan.include_unmodified
15191519

15201520
if skip_backfill and not no_gaps and not is_dev:
1521-
raise ConfigError(
1522-
"When targeting the production environment either the backfill should not be skipped or the lack of data gaps should be enforced (--no-gaps flag)."
1521+
# note: we deliberately don't mention the --no-gaps flag in case the plan came from the sqlmesh_dbt command
1522+
# todo: perhaps we could have better error messages if we check sys.argv[0] for which cli is running?
1523+
self.console.log_warning(
1524+
"Skipping the backfill stage for production can lead to unexpected results, such as tables being empty or incremental data with non-contiguous time ranges being made available.\n"
1525+
"If you are doing this deliberately to create an empty version of a table to test a change, please consider using Virtual Data Environments instead."
15231526
)
15241527

15251528
if not skip_linter:

sqlmesh_dbt/operations.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,12 @@ def _plan_options(
127127
)
128128

129129
if empty:
130-
# dbt --empty adds LIMIT 0 to the queries, resulting in empty tables
131-
# this lines up with --skip-backfill in SQLMesh
130+
# `dbt --empty` adds LIMIT 0 to the queries, resulting in empty tables. In addition, it happily clobbers existing tables regardless of if they are populated.
131+
# This *partially* lines up with --skip-backfill in SQLMesh, which indicates to not populate tables if they happened to be created/updated as part of this plan.
132+
# However, if a table already exists and has data in it, there is no change so SQLMesh will not recreate the table and thus it will not be cleared.
133+
# So in order to fully replicate dbt's --empty, we also need --full-refresh semantics in order to replace existing tables
132134
options["skip_backfill"] = True
133-
134-
if is_prod:
135-
# to prevent the following error:
136-
# > ConfigError: When targeting the production environment either the backfill should not be skipped or
137-
# > the lack of data gaps should be enforced (--no-gaps flag).
138-
options["no_gaps"] = True
135+
full_refresh = True
139136

140137
if full_refresh:
141138
# TODO: handling this requires some updates in the engine to enable restatements+changes in the same plan without affecting prod
@@ -186,6 +183,12 @@ def create(
186183
from sqlmesh_dbt.console import DbtCliConsole
187184
from sqlmesh.utils.errors import SQLMeshError
188185

186+
# clear any existing handlers set up by click/rich as defaults so that once SQLMesh logging config is applied,
187+
# we dont get duplicate messages logged from things like console.log_warning()
188+
root_logger = logging.getLogger()
189+
while root_logger.hasHandlers():
190+
root_logger.removeHandler(root_logger.handlers[0])
191+
189192
configure_logging(force_debug=debug)
190193
set_console(DbtCliConsole())
191194

tests/cli/test_cli.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,7 @@ def test_plan_skip_backfill(runner, tmp_path, flag):
261261
result = runner.invoke(cli, ["--log-file-dir", tmp_path, "--paths", tmp_path, "plan", flag])
262262
assert result.exit_code == 1
263263
assert (
264-
"Error: When targeting the production environment either the backfill should not be skipped or the lack of data gaps should be enforced (--no-gaps flag)."
265-
in result.output
264+
"Skipping the backfill stage for production can lead to unexpected results" in result.output
266265
)
267266

268267
# plan executes virtual update without executing model batches

0 commit comments

Comments
 (0)