Skip to main content

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.

Typical project folder structure

your-project
├── credentials
│   ├── gcp-sa.json
├── migrations
│   ├── 2024_12_01_120000_create_users_table.py
├── src
│   ├── my_project_file.py
└── README.md

Authorize Google 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 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 src.bigquery_migrations.migration 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))

Running migrations

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

Changelog

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

bigquery_migrations-0.4.1.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bigquery_migrations-0.4.1-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file bigquery_migrations-0.4.1.tar.gz.

File metadata

  • Download URL: bigquery_migrations-0.4.1.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.7

File hashes

Hashes for bigquery_migrations-0.4.1.tar.gz
Algorithm Hash digest
SHA256 b427085883d70bb8f44a876981f2cb33c4a21aef417a0bff47d68faccef016b4
MD5 348cd357e9880f1ac84a7df255412442
BLAKE2b-256 58efd165efbc67d7c48707fbf99a0f2dfc54045a4037ab5b1101ba426b84ca07

See more details on using hashes here.

File details

Details for the file bigquery_migrations-0.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for bigquery_migrations-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d031b9bdcb393b2eedfd8506cf541fc96b3db3c85208c613c115d76b81fa715e
MD5 00fbac019c688b29f037028a85ecf781
BLAKE2b-256 d818b97aa50598d0a2440c5bafb8c94e0e875e328a0a6a1ba40790db552edadc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page