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)
        class_name = self.__class__.__name__
        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)
        class_name = self.__class__.__name__
        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

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.0.tar.gz (10.1 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.0-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: bigquery_migrations-0.4.0.tar.gz
  • Upload date:
  • Size: 10.1 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.0.tar.gz
Algorithm Hash digest
SHA256 23c76c7f7a9d89710d81329d6ad87788506d0700fa602856d376ad880f5f6644
MD5 0c002c0b60d52195fbaef81309c136d3
BLAKE2b-256 3ae5f6c62e15dfe18dac770f4449a719ee279ba581204fd98afc066ae9d75ad7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bigquery_migrations-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a8029d2f81a1192400ebd49226106619199bf8e0f81546df07cc704535ae23b9
MD5 c8d2aefabdafa488e6dd74ca5fdfee09
BLAKE2b-256 39f8e4bc35bc99257bfd129216b9d69eea27f3325cc120a361b1a4302897ad86

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