-
Notifications
You must be signed in to change notification settings - Fork 13.8k
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
chore(dao): Add explicit ON DELETE CASCADE for ownership #24628
Merged
john-bodley
merged 3 commits into
apache:master
from
john-bodley:john-bodley--dao-owners-ondelete-cascade
Jul 11, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -535,7 +535,6 @@ def create_dashboard(slices: list[Slice]) -> Dashboard: | |
dash = db.session.query(Dashboard).filter_by(slug="births").first() | ||
if not dash: | ||
dash = Dashboard() | ||
dash.owners = [] | ||
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. No need to initialize this. |
||
db.session.add(dash) | ||
|
||
dash.published = True | ||
|
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,57 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
|
||
from alembic import op | ||
from sqlalchemy.engine.reflection import Inspector | ||
|
||
from superset.utils.core import generic_find_fk_constraint_name | ||
|
||
|
||
@dataclass(frozen=True) | ||
class ForeignKey: | ||
table: str | ||
referent_table: str | ||
local_cols: list[str] | ||
remote_cols: list[str] | ||
|
||
@property | ||
def constraint_name(self) -> str: | ||
return f"fk_{self.table}_{self.local_cols[0]}_{self.referent_table}" | ||
|
||
|
||
def redefine( | ||
foreign_key: ForeignKey, | ||
on_delete: str | None = None, | ||
on_update: str | None = None, | ||
) -> None: | ||
""" | ||
Redefine the foreign key constraint to include the ON DELETE and ON UPDATE | ||
constructs for cascading purposes. | ||
|
||
:params foreign_key: The foreign key constraint | ||
:param ondelete: If set, emit ON DELETE <value> when issuing DDL operations | ||
:param onupdate: If set, emit ON UPDATE <value> when issuing DDL operations | ||
""" | ||
|
||
bind = op.get_bind() | ||
insp = Inspector.from_engine(bind) | ||
conv = {"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s"} | ||
|
||
with op.batch_alter_table(foreign_key.table, naming_convention=conv) as batch_op: | ||
if constraint := generic_find_fk_constraint_name( | ||
table=foreign_key.table, | ||
columns=set(foreign_key.remote_cols), | ||
referenced=foreign_key.referent_table, | ||
insp=insp, | ||
): | ||
batch_op.drop_constraint(constraint, type_="foreignkey") | ||
|
||
batch_op.create_foreign_key( | ||
constraint_name=foreign_key.constraint_name, | ||
referent_table=foreign_key.referent_table, | ||
local_cols=foreign_key.local_cols, | ||
remote_cols=foreign_key.remote_cols, | ||
ondelete=on_delete, | ||
onupdate=on_update, | ||
) |
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 |
---|---|---|
|
@@ -21,74 +21,46 @@ | |
Create Date: 2023-06-22 13:39:47.989373 | ||
|
||
""" | ||
from __future__ import annotations | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "6fbe660cac39" | ||
down_revision = "90139bf715e4" | ||
|
||
import sqlalchemy as sa | ||
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. Same logic as before, but using the more generalized shared constraints logic. |
||
from alembic import op | ||
|
||
from superset.utils.core import generic_find_fk_constraint_name | ||
|
||
|
||
def migrate(ondelete: str | None) -> None: | ||
""" | ||
Redefine the foreign key constraints, via a successive DROP and ADD, for all tables | ||
related to the `tables` table to include the ON DELETE construct for cascading | ||
purposes. | ||
|
||
:param ondelete: If set, emit ON DELETE <value> when issuing DDL for this constraint | ||
""" | ||
|
||
bind = op.get_bind() | ||
insp = sa.engine.reflection.Inspector.from_engine(bind) | ||
|
||
conv = { | ||
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", | ||
} | ||
|
||
for table in ("sql_metrics", "table_columns"): | ||
with op.batch_alter_table(table, naming_convention=conv) as batch_op: | ||
if constraint := generic_find_fk_constraint_name( | ||
table=table, | ||
columns={"id"}, | ||
referenced="tables", | ||
insp=insp, | ||
): | ||
batch_op.drop_constraint(constraint, type_="foreignkey") | ||
|
||
batch_op.create_foreign_key( | ||
constraint_name=f"fk_{table}_table_id_tables", | ||
referent_table="tables", | ||
local_cols=["table_id"], | ||
remote_cols=["id"], | ||
ondelete=ondelete, | ||
) | ||
|
||
with op.batch_alter_table("sqlatable_user", naming_convention=conv) as batch_op: | ||
for table, column in zip(("ab_user", "tables"), ("user_id", "table_id")): | ||
if constraint := generic_find_fk_constraint_name( | ||
table="sqlatable_user", | ||
columns={"id"}, | ||
referenced=table, | ||
insp=insp, | ||
): | ||
batch_op.drop_constraint(constraint, type_="foreignkey") | ||
|
||
batch_op.create_foreign_key( | ||
constraint_name=f"fk_sqlatable_user_{column}_{table}", | ||
referent_table=table, | ||
local_cols=[column], | ||
remote_cols=["id"], | ||
ondelete=ondelete, | ||
) | ||
from superset.migrations.shared.constraints import ForeignKey, redefine | ||
|
||
foreign_keys = [ | ||
ForeignKey( | ||
table="sql_metrics", | ||
referent_table="tables", | ||
local_cols=["table_id"], | ||
remote_cols=["id"], | ||
), | ||
ForeignKey( | ||
table="table_columns", | ||
referent_table="tables", | ||
local_cols=["table_id"], | ||
remote_cols=["id"], | ||
), | ||
ForeignKey( | ||
table="sqlatable_user", | ||
referent_table="ab_user", | ||
local_cols=["user_id"], | ||
remote_cols=["id"], | ||
), | ||
ForeignKey( | ||
table="sqlatable_user", | ||
referent_table="tables", | ||
local_cols=["table_id"], | ||
remote_cols=["id"], | ||
), | ||
] | ||
|
||
|
||
def upgrade(): | ||
migrate(ondelete="CASCADE") | ||
for foreign_key in foreign_keys: | ||
redefine(foreign_key, on_delete="CASCADE") | ||
|
||
|
||
def downgrade(): | ||
migrate(ondelete=None) | ||
for foreign_key in foreign_keys: | ||
redefine(foreign_key) |
78 changes: 78 additions & 0 deletions
78
...ons/versions/2023-07-11_15-51_6d05b0a70c89_add_on_delete_cascade_for_owners_references.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,78 @@ | ||
# 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 on delete cascade for owners references | ||
|
||
Revision ID: 6d05b0a70c89 | ||
Revises: f92a3124dd66 | ||
Create Date: 2023-07-11 15:51:57.964635 | ||
|
||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "6d05b0a70c89" | ||
down_revision = "f92a3124dd66" | ||
|
||
from superset.migrations.shared.constraints import ForeignKey, redefine | ||
|
||
foreign_keys = [ | ||
ForeignKey( | ||
table="dashboard_user", | ||
referent_table="ab_user", | ||
local_cols=["user_id"], | ||
remote_cols=["id"], | ||
), | ||
ForeignKey( | ||
table="dashboard_user", | ||
referent_table="dashboards", | ||
local_cols=["dashboard_id"], | ||
remote_cols=["id"], | ||
), | ||
ForeignKey( | ||
table="report_schedule_user", | ||
referent_table="ab_user", | ||
local_cols=["user_id"], | ||
remote_cols=["id"], | ||
), | ||
ForeignKey( | ||
table="report_schedule_user", | ||
referent_table="report_schedule", | ||
local_cols=["report_schedule_id"], | ||
remote_cols=["id"], | ||
), | ||
ForeignKey( | ||
table="slice_user", | ||
referent_table="ab_user", | ||
local_cols=["user_id"], | ||
remote_cols=["id"], | ||
), | ||
ForeignKey( | ||
table="slice_user", | ||
referent_table="slices", | ||
local_cols=["slice_id"], | ||
remote_cols=["id"], | ||
), | ||
] | ||
|
||
|
||
def upgrade(): | ||
for foreign_key in foreign_keys: | ||
redefine(foreign_key, on_delete="CASCADE") | ||
|
||
|
||
def downgrade(): | ||
for foreign_key in foreign_keys: | ||
redefine(foreign_key) |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm not really sure why the reports were refetched as opposed to using the
items
provides. Note after removing the need to remove the owners this logic simply reduced to theBaseDAO.delete
method and thus is unnecessary.