Repository: dbt-labs/dbt-core (language: python)

Issue:
[Feature]  Support `--empty` flag for snapshots
### Is this your first time submitting a feature request?

- [X] I have read the [expectations for open source contributors](https://docs.getdbt.com/docs/contributing/oss-expectations)
- [X] I have searched the existing issues, and I could not find an existing issue for this feature
- [X] I am requesting a straightforward extension of existing dbt functionality, rather than a Big Idea better suited to a discussion

### Describe the feature

Support the empty flag for snapshots. Currently, `dbt snapshot --empty` results in the error: "Error: No such option: --empty Did you mean --help?". This means unit tests for models downstream of snapshots cannot run with the usual workflow.

### Describe alternatives you've considered

Running upstreams of snapshots with --empty then running the snapshots. This makes configuring unit tests in CI much more complex as you need the additional steps:
 - Identify snapshots upstream of modified models
 - Run upstreams of snapshots as empty
 - Run snapshots

### Who will this benefit?

Users of unit tests and snapshots

### Are you interested in contributing this feature?

_No response_

### Anything else?

_No response_


Discussion / hints:
Thanks for opening this @d-cole !

We've got something similar open for seeds as well: https://github.com/dbt-labs/dbt-core/issues/8981

We've added your issue for snapshots to https://github.com/dbt-labs/dbt-core/issues/10151.
Great, thank you @dbeatty10!

Fix patch:
diff --git a/.changes/unreleased/Features-20241031-163149.yaml b/.changes/unreleased/Features-20241031-163149.yaml
new file mode 100644
index 00000000000..209f2180daa
--- /dev/null
+++ b/.changes/unreleased/Features-20241031-163149.yaml
@@ -0,0 +1,6 @@
+kind: Features
+body: Support --empty for snapshots
+time: 2024-10-31T16:31:49.926164-04:00
+custom:
+  Author: gshank
+  Issue: "10372"
diff --git a/core/dbt/cli/main.py b/core/dbt/cli/main.py
index 610163042c4..a9de9441365 100644
--- a/core/dbt/cli/main.py
+++ b/core/dbt/cli/main.py
@@ -716,6 +716,7 @@ def seed(ctx, **kwargs):
 @cli.command("snapshot")
 @click.pass_context
 @global_flags
+@p.empty
 @p.exclude
 @p.profiles_dir
 @p.project_dir


Test patch:
diff --git a/tests/functional/snapshots/test_snapshot_empty.py b/tests/functional/snapshots/test_snapshot_empty.py
new file mode 100644
index 00000000000..a3f648a968f
--- /dev/null
+++ b/tests/functional/snapshots/test_snapshot_empty.py
@@ -0,0 +1,40 @@
+import pytest
+
+from dbt.tests.util import run_dbt
+
+my_model_sql = """
+select 1 as id, {{ dbt.current_timestamp() }} as updated_at
+"""
+
+snapshots_yml = """
+snapshots:
+  - name: my_snapshot
+    relation: "ref('my_model')"
+    config:
+      unique_key: id
+      strategy: check
+      check_cols: all
+      dbt_valid_to_current: "date('9999-12-31')"
+"""
+
+
+class TestSnapshotEmpty:
+    @pytest.fixture(scope="class")
+    def models(self):
+        return {
+            "my_model.sql": my_model_sql,
+            "snapshots.yml": snapshots_yml,
+        }
+
+    def test_check(self, project):
+        run_dbt(["run"])
+        run_dbt(["snapshot", "--empty"])
+
+        query = "select id, updated_at, dbt_valid_from, dbt_valid_to from {database}.{schema}.my_snapshot order by updated_at asc"
+        snapshot_out1 = project.run_sql(query, fetch="all")
+        assert snapshot_out1 == []
+
+        run_dbt(["run"])
+        run_dbt(["snapshot", "--empty"])
+        snapshot_out2 = project.run_sql(query, fetch="all")
+        assert snapshot_out2 == []