-
Notifications
You must be signed in to change notification settings - Fork 138
feat: Alembic support #183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
758e41f
0d64fda
38da253
ec4fc1f
738462a
03e2bfb
6ce50bc
c257cfa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
Alembic support | ||
--------------- | ||
|
||
`Alembic <https://alembic.sqlalchemy.org>`_ is a lightweight database | ||
migration tool for usage with the SQLAlchemy Database Toolkit for | ||
Python. It can use this BigQuery SQLAlchemy support to manage | ||
BigQuery shemas. | ||
|
||
Some features, like management of constrains and indexes, aren't | ||
supported because `BigQuery doesn't support them | ||
<https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language>`_. | ||
|
||
Supported operations: | ||
|
||
`add_column(table_name, column, schema=None) | ||
<https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.add_column>`_ | ||
|
||
`alter_column(table_name, column_name, nullable=None, schema=None) | ||
<https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.alter_column>`_ | ||
|
||
`bulk_insert(table, rows, multiinsert=True) | ||
<https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.bulk_insert>`_ | ||
|
||
`create_table(table_name, *columns, **kw) | ||
<https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.create_table>`_ | ||
|
||
`create_table_comment(table_name, comment, schema=None) | ||
<https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.create_table_comment>`_ | ||
|
||
`drop_column(table_name, column_name, schema=None) | ||
<https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.drop_column>`_ | ||
|
||
`drop_table(table_name, schema=None) | ||
<https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.drop_table>`_ | ||
|
||
`drop_table_comment(table_name, schema=None) | ||
<https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.drop_table_comment>`_ | ||
|
||
`execute(sqltext, execution_options=None) | ||
<https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.execute>`_ | ||
|
||
`rename_table(old_table_name, new_table_name, schema=None) | ||
<https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.rename_table>`_ | ||
|
||
Note that some of the operations above have limited capability, again | ||
do to `BigQuery limitations | ||
<https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language>`_. | ||
|
||
The `execute` operation allows access to BigQuery-specific | ||
`data-definition-language | ||
<https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language>`_. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
|
||
.. include:: multiprocessing.rst | ||
|
||
.. include:: alembic.rst | ||
|
||
API Reference | ||
------------- | ||
.. toctree:: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
# Copyright (c) 2021 The PyBigQuery Authors | ||
# | ||
# 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. | ||
|
||
import contextlib | ||
|
||
import pytest | ||
from sqlalchemy import Column, DateTime, Integer, String | ||
|
||
try: | ||
import alembic # noqa | ||
except ImportError: | ||
alembic = None | ||
|
||
import google.api_core.exceptions | ||
|
||
|
||
@pytest.fixture | ||
def alembic_table(bigquery_dataset, bigquery_client): | ||
import sqlalchemy | ||
import alembic.migration | ||
import alembic.operations | ||
|
||
def get_table(table_name, data="table"): | ||
try: | ||
table_id = f"{bigquery_dataset}.{table_name}" | ||
if data == "rows": | ||
return [dict(r) for r in bigquery_client.list_rows(table_id)] | ||
else: | ||
table = bigquery_client.get_table(table_id) | ||
if data == "table": | ||
return table | ||
elif data == "schema": | ||
return [ | ||
repr(s).replace(", (), None)", ")").replace(", None)", ")") | ||
for s in table.schema | ||
] | ||
else: | ||
raise ValueError(data) | ||
except google.api_core.exceptions.NotFound: | ||
return None | ||
|
||
engine = sqlalchemy.create_engine(f"bigquery:///{bigquery_dataset}") | ||
with contextlib.closing(engine.connect()) as conn: | ||
migration_context = alembic.migration.MigrationContext.configure(conn, {}) | ||
with alembic.operations.Operations.context(migration_context): | ||
yield get_table | ||
|
||
|
||
@pytest.mark.skipif(alembic is None, reason="Alembic isn't installed.") | ||
def test_alembic_scenario(alembic_table): | ||
""" | ||
Exercise all of the operations we support. | ||
|
||
It's a little awkward because we have to avoid doing too many | ||
operations on the same table to avoid tripping over limits on | ||
table mods within a short time. | ||
""" | ||
from alembic import op | ||
|
||
assert alembic_table("account") is None | ||
|
||
account = op.create_table( | ||
"account", | ||
Column("id", Integer, nullable=False), | ||
Column("name", String(50), nullable=False, comment="The name"), | ||
Column("description", String(200)), | ||
) | ||
assert alembic_table("account", "schema") == [ | ||
"SchemaField('id', 'INTEGER', 'REQUIRED')", | ||
"SchemaField('name', 'STRING(50)', 'REQUIRED', 'The name')", | ||
"SchemaField('description', 'STRING(200)', 'NULLABLE')", | ||
] | ||
|
||
op.bulk_insert( | ||
account, | ||
[ | ||
dict(id=1, name="home", description="the home account"), | ||
dict(id=2, name="operations", description="the ops account"), | ||
dict(id=3, name="savings", description=None), | ||
], | ||
) | ||
|
||
assert alembic_table("account", "rows") == [ | ||
{"description": "the home account", "id": 1, "name": "home"}, | ||
{"description": "the ops account", "id": 2, "name": "operations"}, | ||
{"description": None, "id": 3, "name": "savings"}, | ||
] | ||
|
||
op.add_column( | ||
"account", Column("last_transaction_date", DateTime, comment="when updated") | ||
) | ||
|
||
assert alembic_table("account", "schema") == [ | ||
"SchemaField('id', 'INTEGER', 'REQUIRED')", | ||
"SchemaField('name', 'STRING(50)', 'REQUIRED', 'The name')", | ||
"SchemaField('description', 'STRING(200)', 'NULLABLE')", | ||
"SchemaField('last_transaction_date', 'DATETIME', 'NULLABLE', 'when updated')", | ||
] | ||
|
||
op.create_table( | ||
"account_w_comment", | ||
Column("id", Integer, nullable=False), | ||
Column("name", String(50), nullable=False, comment="The name"), | ||
Column("description", String(200)), | ||
comment="This table has comments", | ||
) | ||
assert alembic_table("account_w_comment").description == "This table has comments" | ||
op.drop_table_comment("account_w_comment") | ||
assert alembic_table("account_w_comment").description is None | ||
|
||
op.drop_column("account_w_comment", "description") | ||
assert alembic_table("account_w_comment", "schema") == [ | ||
"SchemaField('id', 'INTEGER', 'REQUIRED')", | ||
"SchemaField('name', 'STRING(50)', 'REQUIRED', 'The name')", | ||
] | ||
|
||
op.drop_table("account_w_comment") | ||
assert alembic_table("account_w_comment") is None | ||
|
||
op.rename_table("account", "accounts") | ||
assert alembic_table("account") is None | ||
assert alembic_table("accounts", "schema") == [ | ||
"SchemaField('id', 'INTEGER', 'REQUIRED')", | ||
"SchemaField('name', 'STRING(50)', 'REQUIRED', 'The name')", | ||
"SchemaField('description', 'STRING(200)', 'NULLABLE')", | ||
"SchemaField('last_transaction_date', 'DATETIME', 'NULLABLE', 'when updated')", | ||
] | ||
op.drop_table("accounts") | ||
assert alembic_table("accounts") is None | ||
|
||
op.execute( | ||
""" | ||
create table transactions( | ||
account INT64 NOT NULL, | ||
transaction_time DATETIME NOT NULL, | ||
amount NUMERIC(11, 2) NOT NULL | ||
) | ||
partition by DATE(transaction_time) | ||
""" | ||
) | ||
|
||
# The only thing we can alter about a column is we can make it | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will change, but we're not currently watching the changes for the DDL params closely. Will need to figure out how to notify on changes like this. https://cloud.google.com/bigquery/docs/release-notes is how the team signals these changes for syntax historically. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hah! They just added table rename the other day. |
||
# nullable: | ||
op.alter_column("transactions", "amount", True) | ||
assert alembic_table("transactions", "schema") == [ | ||
"SchemaField('account', 'INTEGER', 'REQUIRED')", | ||
"SchemaField('transaction_time', 'DATETIME', 'REQUIRED')", | ||
"SchemaField('amount', 'NUMERIC(11, 2)', 'NULLABLE')", | ||
] | ||
|
||
op.create_table_comment("transactions", "Transaction log") | ||
assert alembic_table("transactions").description == "Transaction log" | ||
|
||
op.drop_table("transactions") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW you could appease flake8 by just importing
impl
above, rather than the bareimport alembic
.