mistercrunch commented on code in PR #26654: URL: https://github.com/apache/superset/pull/26654#discussion_r1456518544
########## superset/migrations/versions/2024-01-17_13-09_96164e3017c6_tagged_object_unique_constraint.py: ########## @@ -0,0 +1,86 @@ +# 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 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 +) + + +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( Review Comment: this is a little scary, needs review, but should be ok -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
