{"id":24591979,"url":"https://github.com/rolandbende/python-bigquery-migrations","last_synced_at":"2026-02-02T08:42:25.930Z","repository":{"id":268380397,"uuid":"904165728","full_name":"RolandBende/python-bigquery-migrations","owner":"RolandBende","description":"Python bigquery-migrations package is for creating and manipulating BigQuery databases easily.","archived":false,"fork":false,"pushed_at":"2024-12-23T16:58:43.000Z","size":63,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-30T10:16:05.778Z","etag":null,"topics":["bigquery","google","migration-automation","migration-scripts","migration-tool","migrations","python"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/bigquery-migrations/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RolandBende.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-16T11:24:31.000Z","updated_at":"2024-12-23T16:53:40.000Z","dependencies_parsed_at":"2024-12-16T20:21:49.307Z","dependency_job_id":null,"html_url":"https://github.com/RolandBende/python-bigquery-migrations","commit_stats":null,"previous_names":["rolandbende/bigquery-migrations","rolandbende/python-bigquery-migrations"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RolandBende%2Fpython-bigquery-migrations","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RolandBende%2Fpython-bigquery-migrations/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RolandBende%2Fpython-bigquery-migrations/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RolandBende%2Fpython-bigquery-migrations/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RolandBende","download_url":"https://codeload.github.com/RolandBende/python-bigquery-migrations/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251683372,"owners_count":21626955,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bigquery","google","migration-automation","migration-scripts","migration-tool","migrations","python"],"created_at":"2025-01-24T10:13:18.425Z","updated_at":"2026-02-02T08:42:25.879Z","avatar_url":"https://github.com/RolandBende.png","language":"Python","readme":"# python-bigquery-migrations\n\nThe `python-bigquery-migrations` package provides a streamlined way to create and manage BigQuery databases using intuitive CLI commands, such as the following:\n\n```bash\nbigquery-migrations run\n```\n\n**What are the benefits of using migrations?**\n\nMigrations are like version control for your database, allowing you to define and share the application's datasets and table schema definitions.\n\n## Getting Started\n\n## 0. Prerequisite\n\n- Google Cloud Project with enabled billing\n- Enabled Google Cloud BigQuery API\n- Google Cloud Service Account JSON file\n\n## 1. Install\n```\npip install bigquery-migrations\n```\n\n## 2. Create the project folder structure\n\nCreate two subdirectory:\n1. credentials\n2. migrations\n\n```\nyour-project-root-folder\n├── credentials\n├── migrations\n└── ...\n```\n\n## 3. Create the neccessary files in the folders\n\n### Google Cloud Service Account JSON file\n\nPut your Google Cloud Service Account JSON file in the credentials subdirectory. See more info in the [Authorize BigQuery Client section](#authorize-bigquery-client)\n\n```\nyour-project\n├── credentials\n│   ├── gcp-sa.json\n├── migrations\n└── ...\n```\n\nYou can use different folder name and file name but in that case you must specify them with command arguments, such as the following:\n\n```bash\nbigquery-migrations run --gcp-sa-json-dir  my-creds --gcp-sa-json-fname my-service-account.json\n```\n\n|argument             |description                                                |\n|---------------------|-----------------------------------------------------------|              \n|--gcp-sa-json-dir    |Name of the service account JSON file directory (optional) |\n|--gcp-sa-json-fname  |Name of the service account JSON file (optional)           |\n\n\u003e **IMPORTANT!**  \n\u003e Never check the Google Cloud Service Account JSON file into version control. This file contains sensitive credentials that could compromise your Google Cloud account if exposed.\n\nTo prevent accidental commits, make sure to add the file to your .gitignore configuration. For example:\n\n```bash\n# .gitignore\ngcp-sa.json\n```\n\nBy ignoring this file, you reduce the risk of unintentional leaks and maintain secure practices in your repository.\n\n### Migrations\n\nCreate your own migrations and put them in the migrations directory. See the [Migration structure section](#migration-structure) and [Migration naming conventions section](#migration-naming-conventions) for more info.\n\n```\nyour-project\n├── credentials\n│   ├── gcp-sa.json\n├── migrations\n│   ├── 2024_12_01_120000_create_users_table.py\n└── ...\n```\n\nYou can use different folder name but in that case you must specify it with a command argument:\n\n```bash\nbigquery-migrations run --migrations-dir bq-migrations\n```\n\n|argument             |description                                                |\n|---------------------|-----------------------------------------------------------|              \n|--migrations-dir     |Name of the migrations directory (optional)                |\n\n\n## Running migrations\n\n\u003e **IMPORTANT!**  \n\u003e You have to create your own Migrations first! [Jump to Creating Migrations section](#creating-migrations)\n\nTo run all of your outstanding migrations, execute the `run` command:\n\n```bash\nbigquery-migrations run\n```\n\nYou can specify the Google Cloud Project id with the `--gcp-project-id` argument:\n\n```bash\nbigquery-migrations run --gcp-project-id your-gcp-id\n```\n\n### Migration log\n\n\u003e **IMPORTANT!**  \n\u003e It's cruical to keep the migration_log.json file in place, and not to modify it manualy!\n\nAfter the first successful run a migration_log.json is created in the migrations directory.\n\n```\nyour-project\n├── migrations\n│   ├── 2024_12_01_120000_create_users_table.py\n    ├── migration_log.json\n...\n```\n\nThe migration_log.json file content should look like this:\n```json\n{\n    \"last_migration\": \"2024_12_10_121000_create_users_table\",\n    \"timestamp\": \"2024-12-18T12:25:54.318426+00:00\"\n}\n```\n\n\n## Rolling Back Migrations\n\n### Rollback the last migration\n\nTo reverse the last migration, execute the `rollback` command and pass `last` with the `--migration-name` argument:\n\n```bash\nbigquery-migrations rollback --migration-name last\n```\n\n### Rollback a specific migration\n\nTo reverse a specific migration, execute the `rollback` command and pass the migration name with the `--migration-name` argument:\n\n```bash\nbigquery-migrations rollback --migration-name 2024_12_10_121000_create_users_table\n```\n\n### Rollback all migrations\n\nTo reverse all of your migrations, execute the `reset` command:\n\n```bash\nbigquery-migrations reset\n```\n\n## Authorize BigQuery Client\n\nPut your service account JSON file in the credentials subdirectory in the root of your project.\n\n```\nyour-project\n├── credentials\n│   ├── gcp-sa.json\n...\n```\n\n### Creating a Service Account for Google BigQuery\n\nYou 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.\n\nTo create a BigQuery service account key\n\n1. Sign in to the [Google Cloud management console](https://console.cloud.google.com/).\n1. Make sure that you have API enabled on your [BigQuery API](https://console.cloud.google.com/apis/library/bigquery.googleapis.com) page. If you don’t see API Enabled, choose Enable.\n1. On the Service accounts page, choose your BigQuery project, and then choose Create service account.\n1. On the [Service account](https://console.cloud.google.com/iam-admin/serviceaccounts) 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.\n1. For Select a role, choose BigQuery, and then choose BigQuery Admin.\n1. Choose Continue, and then choose Done.\n1. On the [Service account](https://console.cloud.google.com/iam-admin/serviceaccounts) page, choose the service account that you created.\n1. Choose Keys, Add key, Create new key.\n1. Choose JSON, and then choose Create. Choose the folder to save your private key or check the default folder for downloads in your browser.\n\n## Creating migrations\n\nPut your migrations files in the migrations subdirectory of the root of your project.\n\n```\nyour-project\n├── migrations\n│   ├── 2024_12_01_120000_create_users_table.py\n...\n```\n\n### Migration structure\n\nThe migration class must contain two methods: `up` and `down`.\n\nThe `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.\n\n```python\nfrom google.cloud import bigquery\nfrom bigquery_migrations import Migration\n\nclass CreateUsersTable(Migration):\n    \"\"\"\n    See:\n    https://github.com/googleapis/python-bigquery/tree/main/samples\n    \"\"\"\n\n    def up(self):\n        # TODO: Set table_id to the ID of the table to create.\n        table_id = \"your_project.your_dataset.example_table\"\n        \n        # TODO: Define table schema\n        schema = [\n            bigquery.SchemaField(\"id\", \"INTEGER\", mode=\"REQUIRED\"),\n            bigquery.SchemaField(\"name\", \"STRING\", mode=\"REQUIRED\"),\n            bigquery.SchemaField(\"created_at\", \"TIMESTAMP\", mode=\"NULLABLE\"),\n        ]\n        table = bigquery.Table(table_id, schema=schema)\n        table = self.client.create_table(table)\n        print(\n            \"Created table {}.{}.{}\".format(table.project, table.dataset_id, table.table_id)\n        )\n\n    def down(self):\n        # TODO: Set table_id to the ID of the table to fetch.\n        table_id = \"your_project.your_dataset.example_table\"\n        \n        # If the table does not exist, delete_table raises\n        # google.api_core.exceptions.NotFound unless not_found_ok is True.\n        self.client.delete_table(table_id, not_found_ok=True)\n        print(\"Deleted table '{}'.\".format(table_id))\n```\n\n### Migration naming conventions\n\n|Pattern              |yyyy_mm_dd_hhmmss_your_class_name.py    |\n|---------------------|----------------------------------------|              \n|Example filename     |2024_12_10_120000_create_users_table.py |\n|Example class name   |CreateUsersTable                        |\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frolandbende%2Fpython-bigquery-migrations","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frolandbende%2Fpython-bigquery-migrations","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frolandbende%2Fpython-bigquery-migrations/lists"}