|
| 1 | +# Copyright (c) 2021 The PyBigQuery Authors |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 4 | +# this software and associated documentation files (the "Software"), to deal in |
| 5 | +# the Software without restriction, including without limitation the rights to |
| 6 | +# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 7 | +# the Software, and to permit persons to whom the Software is furnished to do so, |
| 8 | +# subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in all |
| 11 | +# copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 15 | +# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 16 | +# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 17 | +# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 18 | +# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 19 | + |
| 20 | +import contextlib |
| 21 | + |
| 22 | +import pytest |
| 23 | +from sqlalchemy import Column, DateTime, Integer, String |
| 24 | + |
| 25 | +try: |
| 26 | + import alembic # noqa |
| 27 | +except ImportError: |
| 28 | + alembic = None |
| 29 | + |
| 30 | +import google.api_core.exceptions |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture |
| 34 | +def alembic_table(bigquery_dataset, bigquery_client): |
| 35 | + import sqlalchemy |
| 36 | + import alembic.migration |
| 37 | + import alembic.operations |
| 38 | + |
| 39 | + def get_table(table_name, data="table"): |
| 40 | + try: |
| 41 | + table_id = f"{bigquery_dataset}.{table_name}" |
| 42 | + if data == "rows": |
| 43 | + return [dict(r) for r in bigquery_client.list_rows(table_id)] |
| 44 | + else: |
| 45 | + table = bigquery_client.get_table(table_id) |
| 46 | + if data == "table": |
| 47 | + return table |
| 48 | + elif data == "schema": |
| 49 | + return [ |
| 50 | + repr(s).replace(", (), None)", ")").replace(", None)", ")") |
| 51 | + for s in table.schema |
| 52 | + ] |
| 53 | + else: |
| 54 | + raise ValueError(data) |
| 55 | + except google.api_core.exceptions.NotFound: |
| 56 | + return None |
| 57 | + |
| 58 | + engine = sqlalchemy.create_engine(f"bigquery:///{bigquery_dataset}") |
| 59 | + with contextlib.closing(engine.connect()) as conn: |
| 60 | + migration_context = alembic.migration.MigrationContext.configure(conn, {}) |
| 61 | + with alembic.operations.Operations.context(migration_context): |
| 62 | + yield get_table |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.skipif(alembic is None, reason="Alembic isn't installed.") |
| 66 | +def test_alembic_scenario(alembic_table): |
| 67 | + """ |
| 68 | + Exercise all of the operations we support. |
| 69 | +
|
| 70 | + It's a little awkward because we have to avoid doing too many |
| 71 | + operations on the same table to avoid tripping over limits on |
| 72 | + table mods within a short time. |
| 73 | + """ |
| 74 | + from alembic import op |
| 75 | + |
| 76 | + assert alembic_table("account") is None |
| 77 | + |
| 78 | + account = op.create_table( |
| 79 | + "account", |
| 80 | + Column("id", Integer, nullable=False), |
| 81 | + Column("name", String(50), nullable=False, comment="The name"), |
| 82 | + Column("description", String(200)), |
| 83 | + ) |
| 84 | + assert alembic_table("account", "schema") == [ |
| 85 | + "SchemaField('id', 'INTEGER', 'REQUIRED')", |
| 86 | + "SchemaField('name', 'STRING(50)', 'REQUIRED', 'The name')", |
| 87 | + "SchemaField('description', 'STRING(200)', 'NULLABLE')", |
| 88 | + ] |
| 89 | + |
| 90 | + op.bulk_insert( |
| 91 | + account, |
| 92 | + [ |
| 93 | + dict(id=1, name="home", description="the home account"), |
| 94 | + dict(id=2, name="operations", description="the ops account"), |
| 95 | + dict(id=3, name="savings", description=None), |
| 96 | + ], |
| 97 | + ) |
| 98 | + |
| 99 | + assert alembic_table("account", "rows") == [ |
| 100 | + {"description": "the home account", "id": 1, "name": "home"}, |
| 101 | + {"description": "the ops account", "id": 2, "name": "operations"}, |
| 102 | + {"description": None, "id": 3, "name": "savings"}, |
| 103 | + ] |
| 104 | + |
| 105 | + op.add_column( |
| 106 | + "account", Column("last_transaction_date", DateTime, comment="when updated") |
| 107 | + ) |
| 108 | + |
| 109 | + assert alembic_table("account", "schema") == [ |
| 110 | + "SchemaField('id', 'INTEGER', 'REQUIRED')", |
| 111 | + "SchemaField('name', 'STRING(50)', 'REQUIRED', 'The name')", |
| 112 | + "SchemaField('description', 'STRING(200)', 'NULLABLE')", |
| 113 | + "SchemaField('last_transaction_date', 'DATETIME', 'NULLABLE', 'when updated')", |
| 114 | + ] |
| 115 | + |
| 116 | + op.create_table( |
| 117 | + "account_w_comment", |
| 118 | + Column("id", Integer, nullable=False), |
| 119 | + Column("name", String(50), nullable=False, comment="The name"), |
| 120 | + Column("description", String(200)), |
| 121 | + comment="This table has comments", |
| 122 | + ) |
| 123 | + assert alembic_table("account_w_comment").description == "This table has comments" |
| 124 | + op.drop_table_comment("account_w_comment") |
| 125 | + assert alembic_table("account_w_comment").description is None |
| 126 | + |
| 127 | + op.drop_column("account_w_comment", "description") |
| 128 | + assert alembic_table("account_w_comment", "schema") == [ |
| 129 | + "SchemaField('id', 'INTEGER', 'REQUIRED')", |
| 130 | + "SchemaField('name', 'STRING(50)', 'REQUIRED', 'The name')", |
| 131 | + ] |
| 132 | + |
| 133 | + op.drop_table("account_w_comment") |
| 134 | + assert alembic_table("account_w_comment") is None |
| 135 | + |
| 136 | + op.rename_table("account", "accounts") |
| 137 | + assert alembic_table("account") is None |
| 138 | + assert alembic_table("accounts", "schema") == [ |
| 139 | + "SchemaField('id', 'INTEGER', 'REQUIRED')", |
| 140 | + "SchemaField('name', 'STRING(50)', 'REQUIRED', 'The name')", |
| 141 | + "SchemaField('description', 'STRING(200)', 'NULLABLE')", |
| 142 | + "SchemaField('last_transaction_date', 'DATETIME', 'NULLABLE', 'when updated')", |
| 143 | + ] |
| 144 | + op.drop_table("accounts") |
| 145 | + assert alembic_table("accounts") is None |
| 146 | + |
| 147 | + op.execute( |
| 148 | + """ |
| 149 | + create table transactions( |
| 150 | + account INT64 NOT NULL, |
| 151 | + transaction_time DATETIME NOT NULL, |
| 152 | + amount NUMERIC(11, 2) NOT NULL |
| 153 | + ) |
| 154 | + partition by DATE(transaction_time) |
| 155 | + """ |
| 156 | + ) |
| 157 | + |
| 158 | + # The only thing we can alter about a column is we can make it |
| 159 | + # nullable: |
| 160 | + op.alter_column("transactions", "amount", True) |
| 161 | + assert alembic_table("transactions", "schema") == [ |
| 162 | + "SchemaField('account', 'INTEGER', 'REQUIRED')", |
| 163 | + "SchemaField('transaction_time', 'DATETIME', 'REQUIRED')", |
| 164 | + "SchemaField('amount', 'NUMERIC(11, 2)', 'NULLABLE')", |
| 165 | + ] |
| 166 | + |
| 167 | + op.create_table_comment("transactions", "Transaction log") |
| 168 | + assert alembic_table("transactions").description == "Transaction log" |
| 169 | + |
| 170 | + op.drop_table("transactions") |
0 commit comments