Simple tool for writing Google BigQuery migrations
Project description
python-bigquery-migrations
Python bigquery-migrations package is for creating and manipulating BigQuery databases easily.
Migrations are like version control for your database, allowing you to define and share the application's datasets and table schema definitions.
Getting Started
Install
pip install bigquery-migrations
Create the project folder structure
Create two subdirectory:
- credentials
- migrations
your-project-root-folder
├── credentials
├── migrations
└── ...
Create the neccessary files in the folders
Put your Google Cloud Service Account JSON file in the credentials subdirectory. See more info in the Authorize BigQuery Client section
Create your own migrations and put them in the migrations directory. See the Migration structure section and Migration naming conventions section for more info.
your-project
├── credentials
│ ├── gcp-sa.json
├── migrations
│ ├── 2024_12_01_120000_create_users_table.py
└── ...
Running migrations
IMPORTANT!
You have to create your own Migrations first! Jump to Creating Migrations section
To run all of your outstanding migrations, execute the run command:
bigquery-migrations run
You can specify the Google Cloud Project id witth the --gcp-project-id argument:
bigquery-migrations run --gcp-project-id
Rolling Back Migrations
To reverse all of your migrations, execute the reset command:
bigquery-migrations reset
Authorize BigQuery Client
Put your service account JSON file in the credentials subdirectory in the root of your project.
your-project
├── credentials
│ ├── gcp-sa.json
...
Creating a Service Account for Google BigQuery
You can connect to BigQuery with a user account or a service account. A service account is a special kind of account designed to be used by applications or compute workloads, rather than a person.
Service accounts don’t have passwords and use a unique email address for identification. You can associate each service account with a service account key, which is a public or private RSA key pair. In this walkthrough, we use a service account key in AWS SCT to access your BigQuery project.
To create a BigQuery service account key
- Sign in to the Google Cloud management console.
- Make sure that you have API enabled on your BigQuery API page. If you don’t see API Enabled, choose Enable.
- On the Service accounts page, choose your BigQuery project, and then choose Create service account.
- On the Service account details page, enter a descriptive value for Service account name. Choose Create and continue. The Grant this service account access to the project page opens.
- For Select a role, choose BigQuery, and then choose BigQuery Admin.
- Choose Continue, and then choose Done.
- On the Service account page, choose the service account that you created.
- Choose Keys, Add key, Create new key.
- Choose JSON, and then choose Create. Choose the folder to save your private key or check the default folder for downloads in your browser.
Creating migrations
Put your migrations files in the migrations subdirectory of the root of your project.
your-project
├── migrations
│ ├── 2024_12_01_120000_create_users_table.py
...
Migration structure
The migration class must contain two methods: up and down.
The up method is used to add new dataset, tables, columns etc. to your BigQuery project, while the down method should reverse the operations performed by the up method.
from google.cloud import bigquery
from bigquery_migrations import Migration
class CreateUsersTable(Migration):
"""
See:
https://github.com/googleapis/python-bigquery/tree/main/samples
"""
def up(self):
# TODO: Set table_id to the ID of the table to create.
table_id = "your_project.your_dataset.example_table"
# TODO: Define table schema
schema = [
bigquery.SchemaField("id", "INTEGER", mode="REQUIRED"),
bigquery.SchemaField("name", "STRING", mode="REQUIRED"),
bigquery.SchemaField("created_at", "TIMESTAMP", mode="NULLABLE"),
]
table = bigquery.Table(table_id, schema=schema)
table = self.client.create_table(table)
print(
"Created table {}.{}.{}".format(table.project, table.dataset_id, table.table_id)
)
def down(self):
# TODO: Set table_id to the ID of the table to fetch.
table_id = "your_project.your_dataset.example_table"
# If the table does not exist, delete_table raises
# google.api_core.exceptions.NotFound unless not_found_ok is True.
self.client.delete_table(table_id, not_found_ok=True)
print("Deleted table '{}'.".format(table_id))
Migration naming conventions
| Pattern | yyyy_mm_dd_hhmmss_your_class_name.py |
|---|---|
| Example filename | 2024_12_10_120000_create_users_table.py |
| Example class name | CreateUsersTable |
Changelog
0.4.3
Documentation
- README.md
- GCP Service account creation process updated
0.4.2
Documentation
- README.md
- sample code: import correction
- new sections:
- GCP Service account creation process
- Migration naming convention
0.4.1
Documentation
- README.md sample code: removed unnecessary lines of code
0.4.0
This is the first release which uses the CHANGELOG file.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bigquery_migrations-0.4.3.tar.gz.
File metadata
- Download URL: bigquery_migrations-0.4.3.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a420b7ea92c5d40527c3c6e5ab39512b643d7a26259f162c52d725dac0a54850
|
|
| MD5 |
1d335700566201da7cb38352f719be5b
|
|
| BLAKE2b-256 |
c509ffe78c38366a780971e141dce3239a21973cdde7c100709ea72d721cc2ab
|
File details
Details for the file bigquery_migrations-0.4.3-py3-none-any.whl.
File metadata
- Download URL: bigquery_migrations-0.4.3-py3-none-any.whl
- Upload date:
- Size: 12.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1008091b2a4542da786bd86f913ab332f9115a6ea73c03fffa621c093ced8b4d
|
|
| MD5 |
2e0c38fbf0d9d1e1cb87566d97a1c205
|
|
| BLAKE2b-256 |
1f4d11e9363fd4adb67ad12bcc82813bcc4407df29c8c41df8de82d2d621fbdb
|