Skip to content

Commit 9c1140e

Browse files
author
Luc Merceron
committed
feat(clone_methods): collection, dashboard and card
1 parent 8ff485c commit 9c1140e

File tree

4 files changed

+498
-1
lines changed

4 files changed

+498
-1
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,28 @@ This comes in handy when you want to create similar cards with the same filters
130130
mb.clone_card(card_id=123, source_table_id=456, target_table_id=789, new_card_name='test clone', new_card_collection_id=1)
131131
```
132132

133+
- #### `clone_collection_new_database`
134+
Similar to `copy_collection` but a different database is used as the source for all cards used in the collection.
135+
This comes in handy when you want to duplicate your whole collection to another one while changing datasource for all its cards.
136+
```python
137+
mb.clone_collection_new_database(source_collection_id=14, destination_parent_collection_name="Root", target_database_id=7, destination_collection_name="Staging Collection")
138+
```
139+
140+
- #### `clone_dashboard_new_database`
141+
Similar to `copy_dashboard` but a different database is used as the source for all cards used in the dashboard.
142+
This comes in handy when you want to duplicate your dashboard (and its cards) to another collection while changing datasource (e.g. multi-tenant application or different environments)
143+
```python
144+
mb.clone_dashboard_new_database(source_dashboard_id=10, target_collection_id=23, target_database_id=7)
145+
```
146+
147+
- #### `clone_card_new_database`
148+
Similar to `clone_card` but a different database is used as the source for all cards used in the dashboard.
149+
Used by `clone_dashboard_new_database`, it clone recursively clone linked cards (i.e. when your question reference another one) to a destination collection while changing their data source
150+
```python
151+
cloned_card_mapping = {}
152+
mb.clone_card_new_database(card_id=63, target_database_id=7, destination_collection_id=23, cloned_card_mapping=cloned_card_mapping)
153+
```
154+
133155
- #### `update_column`
134156
Update the column in Data Model by providing the relevant parameter (list of all parameters can be found [here](https://www.metabase.com/docs/latest/api-documentation.html#put-apifieldid)).
135157
For example to change the column type to 'Category', we can use:

metabase_api/_helper_methods.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,29 @@ def get_table_metadata(self, table_name=None, table_id=None, db_name=None, db_id
243243

244244

245245

246+
def get_database_name_id(self, db_id=None, db_name=None, column_id_name=False):
247+
if not db_id:
248+
if not db_name:
249+
raise ValueError(
250+
"Either the name or id of the target database needs to be provided."
251+
)
252+
else:
253+
db_id = self.get_item_id("database", db_name)
254+
255+
db_info = self.get_item_info("database", db_id, params={"include": "tables"})
256+
if column_id_name:
257+
db_table_name_id = {
258+
table["id"]: {"name": table["name"], "columns": {}}
259+
for table in db_info["tables"]
260+
}
261+
else:
262+
db_table_name_id = {
263+
table["name"]: {"id": table["id"], "columns": {}}
264+
for table in db_info["tables"]
265+
}
266+
267+
return db_table_name_id
268+
246269
def get_columns_name_id(self, table_name=None, db_name=None, table_id=None, db_id=None, verbose=False, column_id_name=False):
247270
'''
248271
Return a dictionary with col_name key and col_id value, for the given table_id/table_name in the given db_id/db_name.

0 commit comments

Comments
 (0)