This script updates Metabase cards to use a new table, replacing occurrences of an old table name with a new one and updating field IDs accordingly. It's useful when migrating or renaming tables within your Metabase instance.
This code is privded
- Python 3.12 or higher (I am usually on one of the latest version)
- Necessary Python modules:
requests
- Metabase API access
-
Clone the Repository
git clone [email protected]:untitled-data-company/metabase-migrate-table.git cd metabase-migrate-table
-
Install Dependencies
It's recommended to use a virtual environment. Once you have it activated just install the required modules.
pip install -r requirements.txt
-
Credential
You will need to add in the file
common.py
:- The url (
BASE_URL
) of your Metabase instance. - Your Metabase API Key (
METABASE_API_KEY
).
- The url (
The script is executed via the command line and requires specific arguments to function correctly.
IMPORTANT: In case a card is modified by this script independently from the dashboard containing it, it is possible that the dashboard filters associated to the dashcard will be disconnected and the dashcard will unable to retrieve data.
This can be fixed opening the dashboard, reconnecting the filters, and saving it. For some Metabase internal reasons, changes will not be visible before saving the dashboard.
-
-o
,--old_table
(required):
The name of the old table to be replaced.
Example:'old_schema.old_table'
-
-n
,--new_table
(required):
The name of the new table that will replace the old one.
Example:'new_schema.new_table'
-
-l
,--List
: List all the cards/questions and dashboards depending on the old table. -
-a
,--all
: Migrate all the cards and dashboards depending on the old table. -
-c
,--card_ids
:
One or more Metabase card IDs to update. Provide multiple IDs separated by spaces.
Example:10717 12345 67890
-
-d
,--dashboard_ids
:
One or more Metabase dashboard IDs to update. Provide multiple IDs separated by spaces.
Example:10717 12345 67890
-
-r
,--renamed_columns
: Pass a json object withold:new
column names, to handle renamed columns. Example:{"insurance_category_label":"insurance_category"}
-a
, -c
, and -d
are mutually exclusive.
python migrate_table.py -o OLD_TABLE_NAME -n NEW_TABLE_NAME [-l] [-a] [-c CARD_ID [CARD_ID ...]] [-d DASHBOARD_ID [DASHBOARD_ID ...]] [-r '{"old_name":"new_name"}']
List all objects associated to a table:
python migrate_table.py \
-o new_schema.new_table \
-n new_schema.new_table \
-l
Update card with ID 10717
, replacing 'old_schema.old_table'
with 'new_schema.new_table'
:
python migrate_table.py \
-o new_schema.new_table \
-n new_schema.new_table \
-c 10717
Update cards with IDs 10717
, 12345
, and 67890
:
python migrate_table.py \
-o new_schema.new_table \
-n new_schema.new_table \
-c 10717 12345 67890
To migrate in one go all the objects associated to a given table you can the following command:
python migrate_table.py \
-o new_schema.new_table \
-n new_schema.new_table \
-a
In case the new table contains columns with names different from the old one, you can pass a JSON string with the couple "old_colum_name":"new_column_name"
:
python migrate_table.py \
-o new_schema.new_table \
-n new_schema.new_table \
-a \
-r '{"successfull_connections":"successful_connections"}'
For development or debugging purposes, default arguments can be set within the script. When running the script without command-line arguments in a debugging environment (e.g., an IDE like VSCode), it will use these default values.
In the script, locate the default_arguments
dictionary:
# Use it for development or debugging purposes
default_arguments = {
"old": "new_schema.new_table",
"new": "new_schema.new_table",
"all": False,
"card_ids": "10717 10733",
"dashboard_ids": None,
"renamed_columns": '{"insurance_category_label":"insurance_category"}',
}
Set the desired default values for old
, new
, and the other parameters.