Skip to content

Commit

Permalink
LA-199: Enhanced functionality around handling taxonomy active / de-a…
Browse files Browse the repository at this point in the history
…ctive nodes (#5617)

Co-authored-by: Adrian Galvan <adrian@ethyca.com>
  • Loading branch information
eastandwestwind and galvana authored Dec 19, 2024
1 parent c8baf75 commit f319894
Show file tree
Hide file tree
Showing 8 changed files with 798 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ The types of changes are:
### Changed
- Adjusted Ant's Select component colors and icon [#5594](https://github.com/ethyca/fides/pull/5594)
- Replaced taxonomies page with new UI based on an interactive tree visualization [#5602](https://github.com/ethyca/fides/pull/5602)
- Adjusted functionality around updating taxonomy active field, includes data migration to re-activate taxonomy nodes [#5617](https://github.com/ethyca/fides/pull/5617)
- Migrated breadcrumbs to Ant Design [#5610](https://github.com/ethyca/fides/pull/5610)
- Updated `CustomReportConfig` to be more intuitive on its contents [#5543](https://github.com/ethyca/fides/pull/5543)

Expand Down
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 ###
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
Loading

0 comments on commit f319894

Please sign in to comment.