Skip to content

Commit a484b49

Browse files
refactor: [SNOW-1890085] use object plugin for listing dbt projects
1 parent cf32dd1 commit a484b49

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

src/snowflake/cli/_plugins/dbt/commands.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
import typer
2121
from snowflake.cli._plugins.dbt.constants import DBT_COMMANDS
2222
from snowflake.cli._plugins.dbt.manager import DBTManager
23+
from snowflake.cli._plugins.object.command_aliases import add_object_command_aliases
24+
from snowflake.cli._plugins.object.commands import scope_option
2325
from snowflake.cli.api.commands.decorators import global_options_with_connection
24-
from snowflake.cli.api.commands.flags import identifier_argument
26+
from snowflake.cli.api.commands.flags import identifier_argument, like_option
2527
from snowflake.cli.api.commands.snow_typer import SnowTyperFactory
28+
from snowflake.cli.api.constants import ObjectType
2629
from snowflake.cli.api.feature_flags import FeatureFlag
2730
from snowflake.cli.api.identifiers import FQN
2831
from snowflake.cli.api.output.types import CommandResult, MessageResult, QueryResult
@@ -39,17 +42,16 @@
3942
DBTNameArgument = identifier_argument(sf_object="DBT Project", example="my_pipeline")
4043

4144

42-
@app.command(
43-
"list",
44-
requires_connection=True,
45+
add_object_command_aliases(
46+
app=app,
47+
object_type=ObjectType.DBT_PROJECT,
48+
name_argument=DBTNameArgument,
49+
like_option=like_option(
50+
help_example='`list --like "my%"` lists all dbt projects that begin with “my”'
51+
),
52+
scope_option=scope_option(help_example="`list --in database my_db`"),
53+
ommit_commands=["drop", "create", "describe"],
4554
)
46-
def list_dbts(
47-
**options,
48-
) -> CommandResult:
49-
"""
50-
List all dbt on Snowflake projects.
51-
"""
52-
return QueryResult(DBTManager().list())
5355

5456

5557
@app.command(

src/snowflake/cli/api/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __str__(self):
3535

3636
class ObjectType(Enum):
3737
COMPUTE_POOL = ObjectNames("compute-pool", "compute pool", "compute pools")
38-
DBT_PROJECT = ObjectNames("dbt-project", "DBT project", "DBT projects")
38+
DBT_PROJECT = ObjectNames("dbt-project", "dbt project", "dbt projects")
3939
DATABASE = ObjectNames("database", "database", "databases")
4040
FUNCTION = ObjectNames("function", "function", "functions")
4141
INTEGRATION = ObjectNames("integration", "integration", "integrations")

tests/dbt/test_dbt_commands.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,34 @@ def mock_connect(mock_ctx):
3232

3333

3434
class TestDBTList:
35-
def test_dbt_list(self, mock_connect, runner):
36-
37-
result = runner.invoke(["dbt", "list"])
35+
def test_list_command_alias(self, mock_connect, runner):
36+
result = runner.invoke(
37+
[
38+
"object",
39+
"list",
40+
"dbt-project",
41+
"--like",
42+
"%PROJECT_NAME%",
43+
"--in",
44+
"database",
45+
"my_db",
46+
]
47+
)
3848

3949
assert result.exit_code == 0, result.output
40-
assert mock_connect.mocked_ctx.get_query() == "SHOW DBT PROJECTS"
50+
result = runner.invoke(
51+
["dbt", "list", "--like", "%PROJECT_NAME%", "--in", "database", "my_db"],
52+
catch_exceptions=False,
53+
)
54+
assert result.exit_code == 0, result.output
55+
56+
queries = mock_connect.mocked_ctx.get_queries()
57+
assert len(queries) == 2
58+
assert (
59+
queries[0]
60+
== queries[1]
61+
== "show dbt projects like '%PROJECT_NAME%' in database my_db"
62+
)
4163

4264

4365
class TestDBTDeploy:

0 commit comments

Comments
 (0)