xinbinhuang commented on a change in pull request #9067: URL: https://github.com/apache/airflow/pull/9067#discussion_r432726322
########## File path: airflow/migrations/versions/8d48763f6d53_add_unique_constraint_to_conn_id.py ########## @@ -0,0 +1,76 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +"""add unique constraint to conn_id + +Revision ID: 8d48763f6d53 +Revises: 952da73b5eff +Create Date: 2020-05-03 16:55:01.834231 + +""" + +import sqlalchemy as sa +from alembic import op + +# revision identifiers, used by Alembic. +revision = '8d48763f6d53' +down_revision = '952da73b5eff' +branch_labels = None +depends_on = None + + +def upgrade(): + """Apply add unique constraint to conn_id""" + conn = op.get_bind() + try: + if conn.dialect.name in ["mysql", "postgresql", "mssql"]: + op.create_unique_constraint( + constraint_name="unique_conn_id", + table_name="connection", + columns=["conn_id"]) + + elif conn.dialect.name == "sqlite": + # use batch_alter_table to support SQLite workaround + with op.batch_alter_table('connection') as batch_op: + batch_op.create_unique_constraint( + constraint_name="unique_conn_id", + columns=["conn_id"] + ) Review comment: ```suggestion with op.batch_alter_table('connection') as batch_op: batch_op.create_unique_constraint( constraint_name="unique_conn_id", columns=["conn_id"] ) ``` You don't need to create your own branching logic. [Alembic](https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.batch_alter_table) will handle it for you internally. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
