Skip to content

Commit 475c5c1

Browse files
committed
upsert datasources
1 parent 49f1ab4 commit 475c5c1

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

minds/datasources/datasources.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ class DatabaseConfig(BaseModel):
1616
class Datasource(DatabaseConfig):
1717
...
1818

19+
1920
class Datasources:
2021
def __init__(self, client):
2122
self.api = client.api
2223

23-
def create(self, ds_config: DatabaseConfig, replace=False):
24+
def create(self, ds_config: DatabaseConfig, replace=False, update=False):
2425
"""
2526
Create new datasource and return it
2627
@@ -30,6 +31,8 @@ def create(self, ds_config: DatabaseConfig, replace=False):
3031
- description: str, description of the database. Used by mind to know what data can be got from it.
3132
- connection_data: dict, optional, credentials to connect to database
3233
- tables: list of str, optional, list of allowed tables
34+
:param replace: if true - to remove existing datasource, default is false
35+
:param update: if true - to update datasourse if exists, default is false
3336
:return: datasource object
3437
"""
3538

@@ -42,7 +45,10 @@ def create(self, ds_config: DatabaseConfig, replace=False):
4245
except exc.ObjectNotFound:
4346
...
4447

45-
self.api.post('/datasources', data=ds_config.model_dump())
48+
if update:
49+
self.api.put('/datasources', data=ds_config.model_dump())
50+
else:
51+
self.api.post('/datasources', data=ds_config.model_dump())
4652
return self.get(name)
4753

4854
def list(self) -> List[Datasource]:

minds/rest_api.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ def post(self, url, data):
5353
_raise_for_status(resp)
5454
return resp
5555

56+
def put(self, url, data):
57+
resp = requests.put(
58+
self.base_url + url,
59+
headers=self._headers(),
60+
json=data,
61+
)
62+
63+
_raise_for_status(resp)
64+
return resp
65+
5666
def patch(self, url, data):
5767
resp = requests.patch(
5868
self.base_url + url,

tests/integration/test_base_flow.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ def test_datasources():
3737

3838
# create
3939
ds = client.datasources.create(example_ds)
40+
assert ds.name == example_ds.name
4041
ds = client.datasources.create(example_ds, replace=True)
4142
assert ds.name == example_ds.name
43+
ds = client.datasources.create(example_ds, update=True)
44+
assert ds.name == example_ds.name
4245

4346
# get
4447
ds = client.datasources.get(example_ds.name)

tests/unit/test_unit.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ def _compare_ds(self, ds1, ds2):
3636
assert ds1.tables == ds2.tables
3737

3838
@patch('requests.get')
39+
@patch('requests.put')
3940
@patch('requests.post')
4041
@patch('requests.delete')
41-
def test_create_datasources(self, mock_del, mock_post, mock_get):
42+
def test_create_datasources(self, mock_del, mock_post, mock_put, mock_get):
4243
client = get_client()
4344
response_mock(mock_get, example_ds.model_dump())
4445

@@ -60,6 +61,10 @@ def check_ds_created(ds, mock_post):
6061

6162
check_ds_created(ds, mock_post)
6263

64+
# with update
65+
ds = client.datasources.create(example_ds, update=True)
66+
check_ds_created(ds, mock_put)
67+
6368
@patch('requests.get')
6469
def test_get_datasource(self, mock_get):
6570
client = get_client()

0 commit comments

Comments
 (0)