forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add unique constraint to tagged_objects (apache#26654)
- Loading branch information
1 parent
effd73f
commit 1283803
Showing
9 changed files
with
335 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# 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. | ||
|
||
from alembic.operations import BatchOperations, Operations | ||
|
||
naming_convention = { | ||
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", | ||
"uq": "uq_%(table_name)s_%(column_0_name)s", | ||
} | ||
|
||
|
||
def create_unique_constraint( | ||
op: Operations, index_id: str, table_name: str, uix_columns: list[str] | ||
) -> None: | ||
with op.batch_alter_table( | ||
table_name, naming_convention=naming_convention | ||
) as batch_op: | ||
batch_op.create_unique_constraint(index_id, uix_columns) | ||
|
||
|
||
def drop_unique_constraint(op: Operations, index_id: str, table_name: str) -> None: | ||
dialect = op.get_bind().dialect.name | ||
|
||
with op.batch_alter_table( | ||
table_name, naming_convention=naming_convention | ||
) as batch_op: | ||
if dialect == "mysql": | ||
# MySQL requires specifying the type of constraint | ||
batch_op.drop_constraint(index_id, type_="unique") | ||
else: | ||
# For other databases, a standard drop_constraint call is sufficient | ||
batch_op.drop_constraint(index_id) |
89 changes: 89 additions & 0 deletions
89
...rset/migrations/versions/2024-01-17_13-09_96164e3017c6_tagged_object_unique_constraint.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# 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. | ||
import enum | ||
|
||
import migration_utils as utils | ||
import sqlalchemy as sa | ||
from alembic import op | ||
from sqlalchemy import Column, Enum, Integer, MetaData, Table | ||
from sqlalchemy.sql import and_, func, select | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "96164e3017c6" | ||
down_revision = "59a1450b3c10" | ||
|
||
|
||
class ObjectType(enum.Enum): | ||
# pylint: disable=invalid-name | ||
query = 1 | ||
chart = 2 | ||
dashboard = 3 | ||
dataset = 4 | ||
|
||
|
||
# Define the tagged_object table structure | ||
metadata = MetaData() | ||
tagged_object_table = Table( | ||
"tagged_object", | ||
metadata, | ||
Column("id", Integer, primary_key=True), | ||
Column("tag_id", Integer), | ||
Column("object_id", Integer), | ||
Column("object_type", Enum(ObjectType)), # Replace ObjectType with your Enum | ||
) | ||
|
||
index_id = "uix_tagged_object" | ||
table_name = "tagged_object" | ||
uix_columns = ["tag_id", "object_id", "object_type"] | ||
|
||
|
||
def upgrade(): | ||
bind = op.get_bind() # Get the database connection bind | ||
|
||
# Reflect the current database state to get existing tables | ||
metadata.reflect(bind=bind) | ||
|
||
# Delete duplicates if any | ||
min_id_subquery = ( | ||
select( | ||
[ | ||
func.min(tagged_object_table.c.id).label("min_id"), | ||
tagged_object_table.c.tag_id, | ||
tagged_object_table.c.object_id, | ||
tagged_object_table.c.object_type, | ||
] | ||
) | ||
.group_by( | ||
tagged_object_table.c.tag_id, | ||
tagged_object_table.c.object_id, | ||
tagged_object_table.c.object_type, | ||
) | ||
.alias("min_ids") | ||
) | ||
|
||
delete_query = tagged_object_table.delete().where( | ||
tagged_object_table.c.id.notin_(select([min_id_subquery.c.min_id])) | ||
) | ||
|
||
bind.execute(delete_query) | ||
|
||
# Create unique constraint | ||
utils.create_unique_constraint(op, index_id, table_name, uix_columns) | ||
|
||
|
||
def downgrade(): | ||
utils.drop_unique_constraint(op, index_id, table_name) |
38 changes: 38 additions & 0 deletions
38
superset/migrations/versions/2024-01-18_12-12_15a2c68a2e6b_merging_two_heads.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# 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. | ||
"""merging two heads | ||
Revision ID: 15a2c68a2e6b | ||
Revises: ('96164e3017c6', 'a32e0c4d8646') | ||
Create Date: 2024-01-18 12:12:52.174742 | ||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "15a2c68a2e6b" | ||
down_revision = ("96164e3017c6", "a32e0c4d8646") | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
|
||
def upgrade(): | ||
pass | ||
|
||
|
||
def downgrade(): | ||
pass |
38 changes: 38 additions & 0 deletions
38
superset/migrations/versions/2024-01-19_08-42_1cf8e4344e2b_merging.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# 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. | ||
"""merging | ||
Revision ID: 1cf8e4344e2b | ||
Revises: ('e863403c0c50', '15a2c68a2e6b') | ||
Create Date: 2024-01-19 08:42:37.694192 | ||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "1cf8e4344e2b" | ||
down_revision = ("e863403c0c50", "15a2c68a2e6b") | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
|
||
def upgrade(): | ||
pass | ||
|
||
|
||
def downgrade(): | ||
pass |
Oops, something went wrong.