Repository: dbt-labs/dbt-core

Issue:
[CT-2435] [Feature] Display model versions with `dbt show`
### 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

When using [model versioning](https://docs.getdbt.com/docs/collaborate/govern/model-versions#how-to-create-a-new-version-of-a-model) + [`dbt show`](https://docs.getdbt.com/reference/commands/show), it is not clear which output goes with which version.

For versioned models, maybe use naming similar to `dbt list`? So something like this:
```
Previewing node 'dim_customers.v1':
```

If possible, it would also be nice to sort them in their version order (either from latest to earliest or vice versa).
 
### Example
```shell
$ dbt list
my_dbt_project.dim_customers.v1
my_dbt_project.dim_customers.v2
```

```shell
$ dbt show -s dim_customers
22:51:55  Running with dbt=1.5.0-rc1

22:51:56  Previewing node 'dim_customers':
| customer_id | country_name | model_version |
| ----------- | ------------ | ------------- |
|           1 | Nauru        |             1 |

22:51:56  Previewing node 'dim_customers':
| customer_id | country_name | model_version |
| ----------- | ------------ | ------------- |
|           1 | Nauru        |             3 |

22:51:56  Previewing node 'dim_customers':
| customer_id | country_name | model_version |
| ----------- | ------------ | ------------- |
|           1 | Nauru        |             2 |
```

### Describe alternatives you've considered

Rather than all the versions of `dim_customers`, it might be more expected to specify the desired version in the selection syntax like this:

```shell
$ dbt show -s dim_customers.v2
23:04:45  Running with dbt=1.5.0-rc1

23:04:46  Previewing node 'dim_customers':
| customer_id | country_name | model_version |
| ----------- | ------------ | ------------- |
|           1 | Nauru        |             2 |
```

### Who will this benefit?

_No response_

### Are you interested in contributing this feature?

_No response_

### Anything else?

_No response_
[CT-2435] [Feature] Display model versions with `dbt show`
### 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

When using [model versioning](https://docs.getdbt.com/docs/collaborate/govern/model-versions#how-to-create-a-new-version-of-a-model) + [`dbt show`](https://docs.getdbt.com/reference/commands/show), it is not clear which output goes with which version.

For versioned models, maybe use naming similar to `dbt list`? So something like this:
```
Previewing node 'dim_customers.v1':
```

If possible, it would also be nice to sort them in their version order (either from latest to earliest or vice versa).
 
### Example
```shell
$ dbt list
my_dbt_project.dim_customers.v1
my_dbt_project.dim_customers.v2
```

```shell
$ dbt show -s dim_customers
22:51:55  Running with dbt=1.5.0-rc1

22:51:56  Previewing node 'dim_customers':
| customer_id | country_name | model_version |
| ----------- | ------------ | ------------- |
|           1 | Nauru        |             1 |

22:51:56  Previewing node 'dim_customers':
| customer_id | country_name | model_version |
| ----------- | ------------ | ------------- |
|           1 | Nauru        |             3 |

22:51:56  Previewing node 'dim_customers':
| customer_id | country_name | model_version |
| ----------- | ------------ | ------------- |
|           1 | Nauru        |             2 |
```

### Describe alternatives you've considered

Rather than all the versions of `dim_customers`, it might be more expected to specify the desired version in the selection syntax like this:

```shell
$ dbt show -s dim_customers.v2
23:04:45  Running with dbt=1.5.0-rc1

23:04:46  Previewing node 'dim_customers':
| customer_id | country_name | model_version |
| ----------- | ------------ | ------------- |
|           1 | Nauru        |             2 |
```

### Who will this benefit?

_No response_

### Are you interested in contributing this feature?

_No response_

### Anything else?

_No response_


Discussion:
Thanks @dbeatty10! I think it's a matter of updating this line, to include the `node.version` if available:

https://github.com/dbt-labs/dbt-core/blob/ada8860e48b32ac712d92e8b0977b2c3c9749981/core/dbt/task/show.py#L80

As an implementation detail, it would make sense to me to define a new node method, something like `pretty_name`, which would return `node.name` by default and `{node.name}.v{node.version}` if a version is defined.
Thanks @dbeatty10! I think it's a matter of updating this line, to include the `node.version` if available:

https://github.com/dbt-labs/dbt-core/blob/ada8860e48b32ac712d92e8b0977b2c3c9749981/core/dbt/task/show.py#L80

As an implementation detail, it would make sense to me to define a new node method, something like `pretty_name`, which would return `node.name` by default and `{node.name}.v{node.version}` if a version is defined.

Resolving patch:
diff --git a/core/dbt/task/show.py b/core/dbt/task/show.py
--- a/core/dbt/task/show.py
+++ b/core/dbt/task/show.py
@@ -75,9 +75,14 @@ def task_end_messages(self, results):
             else:
                 table.print_table(output=output, max_rows=None)
 
+            node_name = result.node.name
+
+            if hasattr(result.node, "version") and result.node.version:
+                node_name += f".v{result.node.version}"
+
             fire_event(
                 ShowNode(
-                    node_name=result.node.name,
+                    node_name=node_name,
                     preview=output.getvalue(),
                     is_inline=is_inline,
                     output_format=self.args.output,


Test patch:
diff --git a/tests/functional/show/fixtures.py b/tests/functional/show/fixtures.py
--- a/tests/functional/show/fixtures.py
+++ b/tests/functional/show/fixtures.py
@@ -10,6 +10,35 @@
 from {{ ref('sample_model') }}
 """
 
+schema_yml = """
+models:
+  - name: sample_model
+    latest_version: 1
+
+    # declare the versions, and fully specify them
+    versions:
+      - v: 2
+        config:
+          materialized: table
+        columns:
+          - name: sample_num
+            data_type: int
+          - name: sample_bool
+            data_type: bool
+          - name: answer
+            data_type: int
+
+      - v: 1
+        config:
+          materialized: table
+          contract: {enforced: true}
+        columns:
+          - name: sample_num
+            data_type: int
+          - name: sample_bool
+            data_type: bool
+"""
+
 models__ephemeral_model = """
 {{ config(materialized = 'ephemeral') }}
 select
diff --git a/tests/functional/show/test_show.py b/tests/functional/show/test_show.py
--- a/tests/functional/show/test_show.py
+++ b/tests/functional/show/test_show.py
@@ -8,6 +8,7 @@
     models__sample_model,
     models__second_model,
     models__ephemeral_model,
+    schema_yml,
 )
 
 
@@ -85,3 +86,29 @@ def test_second_ephemeral_model(self, project):
             ["show", "--inline", models__second_ephemeral_model]
         )
         assert "col_hundo" in log_output
+
+
+class TestShowModelVersions:
+    @pytest.fixture(scope="class")
+    def models(self):
+        return {
+            "schema.yml": schema_yml,
+            "sample_model.sql": models__sample_model,
+            "sample_model_v2.sql": models__second_model,
+        }
+
+    @pytest.fixture(scope="class")
+    def seeds(self):
+        return {"sample_seed.csv": seeds__sample_seed}
+
+    def test_version_unspecified(self, project):
+        run_dbt(["build"])
+        (results, log_output) = run_dbt_and_capture(["show", "--select", "sample_model"])
+        assert "Previewing node 'sample_model.v1'" in log_output
+        assert "Previewing node 'sample_model.v2'" in log_output
+
+    def test_none(self, project):
+        run_dbt(["build"])
+        (results, log_output) = run_dbt_and_capture(["show", "--select", "sample_model.v2"])
+        assert "Previewing node 'sample_model.v1'" not in log_output
+        assert "Previewing node 'sample_model.v2'" in log_output