Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(superset): get_database #211

Merged
merged 1 commit into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/preset_cli/api/clients/superset.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,17 @@ def get_database(self, database_id: int) -> Any:
"""
Return a single database.
"""
return self.get_resource("database", database_id)
database = self.get_resource("database", database_id)
if "sqlalchemy_uri" in database:
return database

url = self.baseurl / "api/v1/database" / str(database_id) / "connection"
response = self.session.get(url)
validate_response(response)

resource = response.json()["result"]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will be nice to have a schema for the return object

Copy link

@zephyring zephyring May 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not familiar with the above 2 endpoints but another option is to introduce a new method for the connection endpoint.
The API client should be as dumb as possible otherwise changes from the API could easily break this client again.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I have a TODO in that file to add them. It's not super trivial because the schema depends on the version of Superset running.


return resource

def get_databases(self, **kwargs: str) -> List[Any]:
"""
Expand Down
87 changes: 86 additions & 1 deletion tests/api/clients/superset_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,11 @@ def test_get_database(mocker: MockerFixture) -> None:
"""
auth = Auth()
client = SupersetClient("https://superset.example.org/", auth)
get_resource = mocker.patch.object(client, "get_resource")
get_resource = mocker.patch.object(
client,
"get_resource",
return_value={"sqlalchemy_uri": "preset://"},
)

client.get_database(1)
get_resource.assert_called_with("database", 1)
Expand Down Expand Up @@ -2957,3 +2961,84 @@ def test_create_virtual_dataset(requests_mock: Mocker) -> None:
},
],
}


def test_get_database_connection(requests_mock: Mocker) -> None:
"""
Test that the client uses the new database endpoint for DB info.
"""
requests_mock.get(
"https://superset.example.org/api/v1/database/1",
json={
"id": 1,
"result": {
"allow_ctas": False,
"allow_cvas": False,
"allow_dml": True,
"allow_file_upload": True,
"allow_run_async": False,
"backend": "preset",
"cache_timeout": None,
"configuration_method": "sqlalchemy_form",
"database_name": "File Uploads",
"driver": "apsw",
"engine_information": {
"disable_ssh_tunneling": False,
"supports_file_upload": True,
},
"expose_in_sqllab": True,
"force_ctas_schema": None,
"id": 1,
"impersonate_user": False,
"is_managed_externally": False,
"uuid": "d21c03ce-86ec-461f-ac82-0add92a8ee07",
},
},
)
requests_mock.get(
"https://superset.example.org/api/v1/database/1/connection",
json={
"id": 1,
"result": {
"allow_ctas": False,
"allow_cvas": False,
"allow_dml": True,
"allow_file_upload": True,
"allow_run_async": False,
"backend": "preset",
"cache_timeout": None,
"configuration_method": "sqlalchemy_form",
"database_name": "File Uploads",
"driver": "apsw",
"engine_information": {
"disable_ssh_tunneling": False,
"supports_file_upload": True,
},
"expose_in_sqllab": True,
"extra": json.dumps(
{
"allows_virtual_table_explore": True,
"metadata_params": {},
"engine_params": {},
"schemas_allowed_for_file_upload": ["main"],
},
),
"force_ctas_schema": None,
"id": 1,
"impersonate_user": False,
"is_managed_externally": False,
"masked_encrypted_extra": None,
"parameters": {},
"parameters_schema": {},
"server_cert": None,
"sqlalchemy_uri": "preset://",
"uuid": "d21c03ce-86ec-461f-ac82-0add92a8ee07",
},
},
)

auth = Auth()
client = SupersetClient("https://superset.example.org/", auth)

response = client.get_database(1)
assert response["sqlalchemy_uri"] == "preset://"