-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LA-199: Enhanced functionality around handling taxonomy active / de-a…
…ctive nodes (#5617) Co-authored-by: Adrian Galvan <adrian@ethyca.com>
- Loading branch information
1 parent
c8baf75
commit f319894
Showing
8 changed files
with
798 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
48 changes: 48 additions & 0 deletions
48
...ides/api/alembic/migrations/versions/10c6b7709be3_adding_foreign_key_on_fides_key_for_.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,48 @@ | ||
"""adding foreign key on fides key for taxonomy | ||
Revision ID: 10c6b7709be3 | ||
Revises: b63ecb007556 | ||
Create Date: 2024-12-17 14:54:02.325442 | ||
""" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "10c6b7709be3" | ||
down_revision = "b63ecb007556" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_foreign_key( | ||
"ctl_data_categories_parent_key_fkey", | ||
"ctl_data_categories", | ||
"ctl_data_categories", | ||
["parent_key"], | ||
["fides_key"], | ||
ondelete="RESTRICT", | ||
) | ||
op.create_foreign_key( | ||
"ctl_data_uses_parent_key_fkey", | ||
"ctl_data_uses", | ||
"ctl_data_uses", | ||
["parent_key"], | ||
["fides_key"], | ||
ondelete="RESTRICT", | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_constraint( | ||
"ctl_data_categories_parent_key_fkey", "ctl_data_categories", type_="foreignkey" | ||
) | ||
op.drop_constraint( | ||
"ctl_data_uses_parent_key_fkey", "ctl_data_uses", type_="foreignkey" | ||
) | ||
# ### end Alembic commands ### |
110 changes: 110 additions & 0 deletions
110
src/fides/api/alembic/migrations/versions/ae65da77c468_add_data_migration_to_reactivate_.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,110 @@ | ||
"""Add data migration to reactivate taxonomy nodes | ||
Revision ID: ae65da77c468 | ||
Revises: 10c6b7709be3 | ||
Create Date: 2024-12-18 12:27:47.239489 | ||
""" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
from sqlalchemy import text | ||
from sqlalchemy.engine import Connection | ||
from sqlalchemy.sql.elements import TextClause | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "ae65da77c468" | ||
down_revision = "10c6b7709be3" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
""" | ||
This is a data migration to activate taxonomy nodes if it is de-active and has any children that are active | ||
E.g. | ||
Taxonomy Tree: A----B----C | ||
\ | ||
----D | ||
Current Active Fields: A (false), B (false), C (true), D (false) | ||
Upgrade Active Fields: A (true), B (true), C (true), D (false) | ||
""" | ||
bind: Connection = op.get_bind() | ||
|
||
reactivate_data_categories_query: TextClause = text( | ||
""" | ||
WITH RECURSIVE leaf_nodes AS ( | ||
SELECT DISTINCT dc.fides_key | ||
FROM ctl_data_categories dc | ||
WHERE dc.fides_key NOT IN ( | ||
SELECT DISTINCT parent_key | ||
FROM ctl_data_categories | ||
WHERE parent_key IS NOT NULL | ||
) | ||
AND dc.active = true | ||
), | ||
parent_hierarchy AS ( | ||
SELECT dc.fides_key, dc.parent_key | ||
FROM ctl_data_categories dc | ||
INNER JOIN leaf_nodes ln ON dc.fides_key = ln.fides_key | ||
UNION | ||
SELECT dc.fides_key, dc.parent_key | ||
FROM ctl_data_categories dc | ||
INNER JOIN parent_hierarchy ph ON dc.fides_key = ph.parent_key | ||
) | ||
UPDATE ctl_data_categories | ||
SET active = true | ||
WHERE fides_key IN ( | ||
SELECT fides_key FROM parent_hierarchy | ||
) | ||
AND active = false; | ||
""" | ||
) | ||
|
||
reactivate_data_uses_query: TextClause = text( | ||
""" | ||
WITH RECURSIVE leaf_nodes AS ( | ||
SELECT DISTINCT dc.fides_key | ||
FROM ctl_data_uses dc | ||
WHERE dc.fides_key NOT IN ( | ||
SELECT DISTINCT parent_key | ||
FROM ctl_data_uses | ||
WHERE parent_key IS NOT NULL | ||
) | ||
AND dc.active = true | ||
), | ||
parent_hierarchy AS ( | ||
SELECT dc.fides_key, dc.parent_key | ||
FROM ctl_data_uses dc | ||
INNER JOIN leaf_nodes ln ON dc.fides_key = ln.fides_key | ||
UNION | ||
SELECT dc.fides_key, dc.parent_key | ||
FROM ctl_data_uses dc | ||
INNER JOIN parent_hierarchy ph ON dc.fides_key = ph.parent_key | ||
) | ||
UPDATE ctl_data_uses | ||
SET active = true | ||
WHERE fides_key IN ( | ||
SELECT fides_key FROM parent_hierarchy | ||
) | ||
AND active = false; | ||
""" | ||
) | ||
|
||
# Update ctl_data_categories | ||
bind.execute(reactivate_data_categories_query) | ||
|
||
# Update ctl_data_uses | ||
bind.execute(reactivate_data_uses_query) | ||
|
||
|
||
def downgrade() -> None: | ||
""" | ||
This migration does not support downgrades. | ||
""" | ||
pass |
Oops, something went wrong.