Name | bigquery-migrations JSON |
Version |
0.5.1
JSON |
| download |
home_page | None |
Summary | Simple tool for writing Google BigQuery migrations |
upload_time | 2024-12-18 13:32:48 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.9 |
license | MIT License Copyright (c) 2024 Roland Bende Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
keywords |
migrations
bigquery
|
VCS |
|
bugtrack_url |
|
requirements |
autopep8
cachetools
certifi
charset-normalizer
colorama
coverage
flake8
google-api-core
google-auth
google-cloud-bigquery
google-cloud-core
google-crc32c
google-resumable-media
googleapis-common-protos
grpcio
grpcio-status
idna
mccabe
packaging
proto-plus
protobuf
pyasn1
pyasn1_modules
pycodestyle
pyflakes
python-dateutil
requests
rsa
ruff
six
urllib3
|
Travis-CI |
No Travis.
|
coveralls test coverage |
|
# 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.
## Prerequisite
- Google Cloud Project with enabled billing
- Enabled Google Cloud BigQuery API
- Google Cloud Service Account JSON file
## Getting Started
## Install
```
pip install bigquery-migrations
```
## Create the project folder structure
Create two subdirectory:
1. credentials
2. 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](#authorize-bigquery-client)
Create 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.
```
your-project
├── credentials
│ ├── gcp-sa.json
├── migrations
│ ├── 2024_12_01_120000_create_users_table.py
└── ...
```
You can use different folder names but in that case you must specify them with command arguments:
|argument |description |
|---------------------|-----------------------------------------------------------|
|--gcp-sa-json-dir |Name of the service account JSON file directory (optional) |
|--gcp-sa-json-fname |Name of the service account JSON file (optional) |
|--migrations-dir |Name of the migrations directory (optional) |
## Running migrations
> **IMPORTANT!**
> You have to create your own Migrations first! [Jump to Creating Migrations section](#creating-migrations)
To run all of your outstanding migrations, execute the `run` command:
```bash
bigquery-migrations run
```
You can specify the Google Cloud Project id with the `--gcp-project-id` argument:
```bash
bigquery-migrations run --gcp-project-id your-gcp-id
```
### Migration log
> **IMPORTANT!**
> It's cruical to keep the migration_log.json file in place, and not to modify it manualy!
After the first successful run a migration_log.json is created in the migrations directory.
```
your-project
├── migrations
│ ├── 2024_12_01_120000_create_users_table.py
├── migration_log.json
...
```
The migration_log.json file content should look like this:
```json
{
"last_migration": "2024_12_10_121000_create_users_table",
"timestamp": "2024-12-18T12:25:54.318426+00:00"
}
```
## Rolling Back Migrations
To reverse a specific migration, execute the `rollback` command and pass the migration name with the `--migration-name` argument:
```bash
bigquery-migrations rollback --migration-name 2024_12_10_121000_create_users_table
```
To reverse all of your migrations, execute the `reset` command:
```bash
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
1. Sign in to the [Google Cloud management console](https://console.cloud.google.com/).
1. 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.
1. On the Service accounts page, choose your BigQuery project, and then choose Create service account.
1. 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.
1. For Select a role, choose BigQuery, and then choose BigQuery Admin.
1. Choose Continue, and then choose Done.
1. On the [Service account](https://console.cloud.google.com/iam-admin/serviceaccounts) page, choose the service account that you created.
1. Choose Keys, Add key, Create new key.
1. 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.
```python
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.5.1
### Documentation
- README.md
- Modified sections
- Create the neccessary files in the folders
## 0.5.0
### Feature
- Rollback to a specific migration
### Documentation
- README.md
- New sections
- Migration log
- Modified sections
- Running migrations
- Rollback migrations
## 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.
Raw data
{
"_id": null,
"home_page": null,
"name": "bigquery-migrations",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "migrations, bigquery",
"author": null,
"author_email": "Roland Bende <benderoland+bigquery-migrations@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/bb/8e/8221747b78fe4f071d3356f6d64bb77f584d14c48ff93d4f0a8395474950/bigquery_migrations-0.5.1.tar.gz",
"platform": null,
"description": "# python-bigquery-migrations\n\nPython bigquery-migrations package is for creating and manipulating BigQuery databases easily.\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## Prerequisite\n\n- Google Cloud Project with enabled billing\n- Enabled Google Cloud BigQuery API\n- Google Cloud Service Account JSON file\n\n## Getting Started\n\n## Install\n```\npip install bigquery-migrations\n```\n\n## Create the project folder structure\n\nCreate two subdirectory:\n1. credentials\n2. migrations\n\n```\nyour-project-root-folder\n\u251c\u2500\u2500 credentials\n\u251c\u2500\u2500 migrations\n\u2514\u2500\u2500 ...\n```\n\n## Create the neccessary files in the folders\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\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\u251c\u2500\u2500 credentials\n\u2502 \u251c\u2500\u2500 gcp-sa.json\n\u251c\u2500\u2500 migrations\n\u2502 \u251c\u2500\u2500 2024_12_01_120000_create_users_table.py\n\u2514\u2500\u2500 ...\n```\n\nYou can use different folder names but in that case you must specify them with command arguments:\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|--migrations-dir |Name of the migrations directory (optional) |\n\n\n## Running migrations\n\n> **IMPORTANT!** \n> 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> **IMPORTANT!** \n> 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\u251c\u2500\u2500 migrations\n\u2502 \u251c\u2500\u2500 2024_12_01_120000_create_users_table.py\n \u251c\u2500\u2500 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\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\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\u251c\u2500\u2500 credentials\n\u2502 \u251c\u2500\u2500 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.\n\nService accounts don\u2019t 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.\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\u2019t 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\u251c\u2500\u2500 migrations\n\u2502 \u251c\u2500\u2500 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\n# Changelog\n\n## 0.5.1\n\n### Documentation\n\n- README.md\n - Modified sections\n - Create the neccessary files in the folders\n\n## 0.5.0\n\n### Feature\n\n- Rollback to a specific migration\n\n### Documentation\n\n- README.md\n - New sections\n - Migration log\n - Modified sections\n - Running migrations\n - Rollback migrations\n\n## 0.4.3\n\n### Documentation\n\n- README.md\n - GCP Service account creation process updated\n\n## 0.4.2\n\n### Documentation\n\n- README.md\n - Sample code: import correction\n - New sections:\n - GCP Service account creation process\n - Migration naming convention\n\n## 0.4.1\n\n### Documentation\n\n- README.md sample code: removed unnecessary lines of code\n\n## 0.4.0\n\nThis is the first release which uses the `CHANGELOG` file.\n",
"bugtrack_url": null,
"license": "MIT License Copyright (c) 2024 Roland Bende Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
"summary": "Simple tool for writing Google BigQuery migrations",
"version": "0.5.1",
"project_urls": {
"Homepage": "https://github.com/RolandBende/python-bigquery-migrations"
},
"split_keywords": [
"migrations",
" bigquery"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "df63803d223ab95874368f9b3ef4ea5dc14456326db1112729bae7aebdafa8f6",
"md5": "400e0e3f58ad0d4a256bd7e5348822b1",
"sha256": "28798e20f42cef9ca83beaa9c1bab4d30488661a6dd1a58310117c988cd8926e"
},
"downloads": -1,
"filename": "bigquery_migrations-0.5.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "400e0e3f58ad0d4a256bd7e5348822b1",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 13077,
"upload_time": "2024-12-18T13:32:46",
"upload_time_iso_8601": "2024-12-18T13:32:46.256025Z",
"url": "https://files.pythonhosted.org/packages/df/63/803d223ab95874368f9b3ef4ea5dc14456326db1112729bae7aebdafa8f6/bigquery_migrations-0.5.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "bb8e8221747b78fe4f071d3356f6d64bb77f584d14c48ff93d4f0a8395474950",
"md5": "9840020ed618c8010b34023925b52ca0",
"sha256": "f344f423b120625048c4d34196c373c6cce670b1d2bac80eb3313a7309a4db27"
},
"downloads": -1,
"filename": "bigquery_migrations-0.5.1.tar.gz",
"has_sig": false,
"md5_digest": "9840020ed618c8010b34023925b52ca0",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 13002,
"upload_time": "2024-12-18T13:32:48",
"upload_time_iso_8601": "2024-12-18T13:32:48.475029Z",
"url": "https://files.pythonhosted.org/packages/bb/8e/8221747b78fe4f071d3356f6d64bb77f584d14c48ff93d4f0a8395474950/bigquery_migrations-0.5.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-18 13:32:48",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "RolandBende",
"github_project": "python-bigquery-migrations",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"requirements": [
{
"name": "autopep8",
"specs": [
[
"==",
"2.3.1"
]
]
},
{
"name": "cachetools",
"specs": [
[
"==",
"5.5.0"
]
]
},
{
"name": "certifi",
"specs": [
[
"==",
"2024.12.14"
]
]
},
{
"name": "charset-normalizer",
"specs": [
[
"==",
"3.4.0"
]
]
},
{
"name": "colorama",
"specs": [
[
"==",
"0.4.6"
]
]
},
{
"name": "coverage",
"specs": [
[
"==",
"7.6.9"
]
]
},
{
"name": "flake8",
"specs": [
[
"==",
"7.1.1"
]
]
},
{
"name": "google-api-core",
"specs": [
[
"==",
"2.24.0"
]
]
},
{
"name": "google-auth",
"specs": [
[
"==",
"2.37.0"
]
]
},
{
"name": "google-cloud-bigquery",
"specs": [
[
"==",
"3.27.0"
]
]
},
{
"name": "google-cloud-core",
"specs": [
[
"==",
"2.4.1"
]
]
},
{
"name": "google-crc32c",
"specs": [
[
"==",
"1.6.0"
]
]
},
{
"name": "google-resumable-media",
"specs": [
[
"==",
"2.7.2"
]
]
},
{
"name": "googleapis-common-protos",
"specs": [
[
"==",
"1.66.0"
]
]
},
{
"name": "grpcio",
"specs": [
[
"==",
"1.68.1"
]
]
},
{
"name": "grpcio-status",
"specs": [
[
"==",
"1.68.1"
]
]
},
{
"name": "idna",
"specs": [
[
"==",
"3.10"
]
]
},
{
"name": "mccabe",
"specs": [
[
"==",
"0.7.0"
]
]
},
{
"name": "packaging",
"specs": [
[
"==",
"24.2"
]
]
},
{
"name": "proto-plus",
"specs": [
[
"==",
"1.25.0"
]
]
},
{
"name": "protobuf",
"specs": [
[
"==",
"5.29.1"
]
]
},
{
"name": "pyasn1",
"specs": [
[
"==",
"0.6.1"
]
]
},
{
"name": "pyasn1_modules",
"specs": [
[
"==",
"0.4.1"
]
]
},
{
"name": "pycodestyle",
"specs": [
[
"==",
"2.12.1"
]
]
},
{
"name": "pyflakes",
"specs": [
[
"==",
"3.2.0"
]
]
},
{
"name": "python-dateutil",
"specs": [
[
"==",
"2.9.0.post0"
]
]
},
{
"name": "requests",
"specs": [
[
"==",
"2.32.3"
]
]
},
{
"name": "rsa",
"specs": [
[
"==",
"4.9"
]
]
},
{
"name": "ruff",
"specs": [
[
"==",
"0.8.3"
]
]
},
{
"name": "six",
"specs": [
[
"==",
"1.17.0"
]
]
},
{
"name": "urllib3",
"specs": [
[
"==",
"2.2.3"
]
]
}
],
"lcname": "bigquery-migrations"
}