Skip to content

Commit 3ac92a8

Browse files
fix sql-facade
1 parent 834bf96 commit 3ac92a8

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

RELEASE-NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* Added `--encryption` flag to `snow stage create` command defining the type of encryption for all files on the stage.
2323

2424
## Fixes and improvements
25+
* Fix `use` commands error if current database is not set.
2526

2627

2728
# v3.8.3

src/snowflake/cli/_plugins/nativeapp/sf_sql_facade.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,10 @@ def _use_object_optional(self, object_type: UseObjectType, name: str | None):
159159
)
160160

161161
try:
162-
prev_obj = to_identifier(current_obj_result_row[0])
162+
if current_obj_result_row[0]:
163+
prev_obj = to_identifier(current_obj_result_row[0])
164+
else:
165+
prev_obj = None
163166
except IndexError:
164167
prev_obj = None
165168

tests/nativeapp/test_sf_sql_facade.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,24 @@ def test_use_role_same_id(mock_execute_query, mock_cursor, new_role, current_rol
841841
assert mock_execute_query.mock_calls == expected
842842

843843

844+
def test_use_role_current_role_empty(mock_execute_query, mock_cursor):
845+
side_effects, expected = mock_execute_helper(
846+
[
847+
(
848+
mock_cursor([(None,)], []),
849+
mock.call("select current_role()"),
850+
),
851+
(None, mock.call('use role "test role"')),
852+
]
853+
)
854+
mock_execute_query.side_effect = side_effects
855+
856+
with sql_facade._use_role_optional("test role"): # noqa: SLF001
857+
pass
858+
859+
assert mock_execute_query.mock_calls == expected
860+
861+
844862
@pytest.mark.parametrize(
845863
"old_db, expected_old_db",
846864
[("old_db", "old_db"), ("old db", '"old db"')],
@@ -888,6 +906,24 @@ def test_use_db_same_id(mock_execute_query, mock_cursor, new_db, current_db):
888906
assert mock_execute_query.mock_calls == expected
889907

890908

909+
def test_use_db_current_db_empty(mock_execute_query, mock_cursor):
910+
side_effects, expected = mock_execute_helper(
911+
[
912+
(
913+
mock_cursor([(None,)], []),
914+
mock.call("select current_database()"),
915+
),
916+
(None, mock.call('use database "new db"')),
917+
]
918+
)
919+
mock_execute_query.side_effect = side_effects
920+
921+
with sql_facade._use_database_optional("new db"): # noqa: SLF001
922+
pass
923+
924+
assert mock_execute_query.mock_calls == expected
925+
926+
891927
@pytest.mark.parametrize(
892928
"old_schema, expected_old_schema",
893929
[("old_schema", "old_schema"), ("old schema", '"old schema"')],
@@ -941,6 +977,24 @@ def test_use_schema_same_id(
941977
assert mock_execute_query.mock_calls == expected
942978

943979

980+
def test_use_schema_current_schema_empty(mock_execute_query, mock_cursor):
981+
side_effects, expected = mock_execute_helper(
982+
[
983+
(
984+
mock_cursor([(None,)], []),
985+
mock.call("select current_schema()"),
986+
),
987+
(None, mock.call('use schema "new schema"')),
988+
]
989+
)
990+
mock_execute_query.side_effect = side_effects
991+
992+
with sql_facade._use_schema_optional("new schema"): # noqa: SLF001
993+
pass
994+
995+
assert mock_execute_query.mock_calls == expected
996+
997+
944998
@pytest.mark.parametrize(
945999
"error_raised, error_caught, error_message",
9461000
[

0 commit comments

Comments
 (0)