Skip to main content

bqmigrate

BigQuery schema migrations for Python.

Status: pre-alpha, under construction. Not ready for production use.

Works today

  • Tables declared in Python. Each table's columns, modes, nested records, partitioning and clustering live in one spec, checked where it is declared, so a spec BigQuery would refuse fails on import.
  • Alembic revisions that know BigQuery. In-place changes rendered from the spec (add_column, drop_column, rename_column, widen_type, defaults and descriptions), each safe to rerun. A change BigQuery cannot make in place, such as adding a REQUIRED column, is refused.
  • A cost limit on every statement. Each query job is capped at a scan limit and labelled with the run id; a revision raises the limit explicitly, only around the statements that need more.
  • A ledger that cannot be rewritten. Every applied revision is recorded with the hash of its file, and a run refuses to start when an applied revision was edited.
  • Table rebuilds for what ALTER cannot do. The table is snapshotted, a copy is written as the spec declares it and checked against the spec and the row count, and only then swapped in. A table whose rows or attributes the rebuild would lose is refused before anything is written, and so is a swap when the table was written during the rebuild.
  • Drift checks per environment. Each spec is compared with its live table, columns at every depth, description, partitioning and clustering. Drift an environment has accepted is listed, and the check fails on anything else, and on listed drift that is gone.
  • A plan for the next revision. Each drift is named with how BigQuery can make it good: in place with a named operation, through a schema update, or by a rebuild, and which tables need rebuilding.
  • A command line. upgrade, pending, drift and plan, each against one environment.

Usage

Declare the tables:

# myapp/schemas.py
from bqmigrate.spec import Column, Dataset, Table
from bqmigrate.types import ColumnType, Mode

EVENTS = Table(
    name="events",
    columns=(
        Column(name="id", type=ColumnType.STRING, mode=Mode.REQUIRED),
        Column(name="country", type=ColumnType.STRING),
    ),
    clustering=("id",),
)
ANALYTICS = Dataset(name="analytics", tables=(EVENTS,))
DATASETS = (ANALYTICS,)

Describe the project in bqmigrate.toml:

revisions = "migrations"
datasets = "myapp.schemas:DATASETS"

[environments.staging]
project = "acme-staging"
dataset_prefix = "staging_"
known_drift = "drift/staging.json"

[environments.production]
project = "acme"

migrations/env.py hands Alembic to the run:

from bqmigrate.run import MigrationRun

MigrationRun.current().run_migrations()

A revision in migrations/versions/ states its change in the spec's terms:

from alembic.op import execute

from bqmigrate import operations
from bqmigrate.run import MigrationRun
from myapp.schemas import ANALYTICS, EVENTS

revision = "0002"
down_revision = "0001"


def upgrade() -> None:
    events = MigrationRun.current().environment.dataset(ANALYTICS).table(EVENTS)
    execute(operations.add_column(events, column=EVENTS.column("country")))

Then, per environment:

bqmigrate plan staging                       # what differs from the specs, and how to make it good
bqmigrate upgrade staging --release v1.4.0   # apply the pending revisions
bqmigrate drift staging                      # fail on drift the environment has not accepted

Tests

uv run pytest runs the unit tests. The statements are also run against a real BigQuery project when BQMIGRATE_TEST_PROJECT names one the default credentials can create datasets in:

BQMIGRATE_TEST_PROJECT=my-sandbox uv run pytest tests/test_bigquery_integration.py

Each run creates a dataset of its own and deletes it afterwards. They use no DML, so a BigQuery sandbox project without billing is enough. upgrade is not among them: Alembic's version table and the ledger are written with DML, which the sandbox refuses.

Metadata

Release files for bqmigrate 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bqmigrate 0.1.0
File Size Uploaded Provenance Actions
bqmigrate-0.1.0.tar.gz 120.5 kB Without provenance Details for bqmigrate-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bqmigrate 0.1.0
File Interpreter ABI Platform Actions
bqmigrate-0.1.0-py3-none-any.whl Python 3 none any Details for bqmigrate-0.1.0-py3-none-any.whl

Total release size: 162.1 kB

Release files / bqmigrate-0.1.0.tar.gz

Download URL bqmigrate-0.1.0.tar.gz
Size 120.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ba88b364db9b3461005e1b7ccc2b291a62e817220665b7c89712baf401a0a1cc
BLAKE2b-256 checksum
How to use checksums
ebfa9f29c775baf1e1bd030d2cae74ae5cc38b68f5d09366bcc615162fd67456
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.13 {"installer":{"name":"uv","version":"0.13.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / bqmigrate-0.1.0-py3-none-any.whl

Download URL bqmigrate-0.1.0-py3-none-any.whl
Size 41.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
745e176e76cc3da956578c17776fb58e2578659b8547120addf739208575f28e
BLAKE2b-256 checksum
How to use checksums
2e99a58f21fae591ce3183f12d1880ed09efb4c568e0544a385baedf98610c16
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.13 {"installer":{"name":"uv","version":"0.13.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for bqmigrate 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page