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

feat(clone_methods): collection, dashboard and card #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,28 @@ This comes in handy when you want to create similar cards with the same filters
mb.clone_card(card_id=123, source_table_id=456, target_table_id=789, new_card_name='test clone', new_card_collection_id=1)
```

- #### `clone_collection_new_database`
Similar to `copy_collection` but a different database is used as the source for all cards used in the collection.
This comes in handy when you want to duplicate your whole collection to another one while changing datasource for all its cards.
```python
mb.clone_collection_new_database(source_collection_id=14, destination_parent_collection_name="Root", target_database_id=7, destination_collection_name="Staging Collection")
```

- #### `clone_dashboard_new_database`
Similar to `copy_dashboard` but a different database is used as the source for all cards used in the dashboard.
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)
```python
mb.clone_dashboard_new_database(source_dashboard_id=10, target_collection_id=23, target_database_id=7)
```

- #### `clone_card_new_database`
Similar to `clone_card` but a different database is used as the source for all cards used in the dashboard.
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
```python
cloned_card_mapping = {}
mb.clone_card_new_database(card_id=63, target_database_id=7, destination_collection_id=23, cloned_card_mapping=cloned_card_mapping)
```

- #### `update_column`
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)).
For example to change the column type to 'Category', we can use:
Expand Down
23 changes: 23 additions & 0 deletions metabase_api/_helper_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,29 @@ def get_table_metadata(self, table_name=None, table_id=None, db_name=None, db_id



def get_database_name_id(self, db_id=None, db_name=None, column_id_name=False):
if not db_id:
if not db_name:
raise ValueError(
"Either the name or id of the target database needs to be provided."
)
else:
db_id = self.get_item_id("database", db_name)

db_info = self.get_item_info("database", db_id, params={"include": "tables"})
if column_id_name:
db_table_name_id = {
table["id"]: {"name": table["name"], "columns": {}}
for table in db_info["tables"]
}
else:
db_table_name_id = {
table["name"]: {"id": table["id"], "columns": {}}
for table in db_info["tables"]
}

return db_table_name_id

def get_columns_name_id(self, table_name=None, db_name=None, table_id=None, db_id=None, verbose=False, column_id_name=False):
'''
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.
Expand Down
Loading
Loading