-
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.
[Backend] Add Required Notice Key to Privacy Notices (#3337)
- Loading branch information
Showing
16 changed files
with
379 additions
and
8 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
68 changes: 68 additions & 0 deletions
68
src/fides/api/ctl/migrations/versions/8a71872089e4_add_notice_key_to_notices.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,68 @@ | ||
"""add_notice_key_to_notices | ||
Revision ID: 8a71872089e4 | ||
Revises: 2661f31daffb | ||
Create Date: 2023-05-18 19:48:33.268790 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
from fideslang.validation import FidesKey, FidesValidationError | ||
|
||
# revision identifiers, used by Alembic. | ||
from sqlalchemy import text | ||
from sqlalchemy.engine import ResultProxy | ||
|
||
revision = "8a71872089e4" | ||
down_revision = "2661f31daffb" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def validate_fides_key_suitability(names: ResultProxy, table_name: str) -> None: | ||
for row in names: | ||
name: str = row["name"].strip(" ").replace(" ", "_") | ||
try: | ||
FidesKey.validate(name) | ||
except FidesValidationError as exc: | ||
raise Exception( | ||
f"Cannot auto-migrate, adjust existing {table_name} name: '{name}' to remove invalid characters: {exc}." | ||
) | ||
|
||
|
||
def upgrade(): | ||
"""Add new non-nullable notice_key fields to privacy notice and notice history tables | ||
and automatically create existing notice keys from the notice names. | ||
Notice keys are not unique. | ||
""" | ||
bind = op.get_bind() | ||
existing_history_names: ResultProxy = bind.execute( | ||
text("select name from privacynoticehistory;") | ||
) | ||
validate_fides_key_suitability(existing_history_names, "privacynoticehistory") | ||
|
||
existing_notice_names: ResultProxy = bind.execute( | ||
text("select name from privacynotice;") | ||
) | ||
validate_fides_key_suitability(existing_notice_names, "privacynotice") | ||
|
||
op.add_column("privacynotice", sa.Column("notice_key", sa.String(), nullable=True)) | ||
op.add_column( | ||
"privacynoticehistory", sa.Column("notice_key", sa.String(), nullable=True) | ||
) | ||
|
||
op.execute( | ||
"update privacynoticehistory set notice_key = LOWER(REPLACE(TRIM(name), ' ', '_'));" | ||
) | ||
op.execute( | ||
"update privacynotice set notice_key = LOWER(REPLACE(TRIM(name), ' ', '_'));" | ||
) | ||
|
||
op.alter_column("privacynotice", "notice_key", nullable=False) | ||
op.alter_column("privacynoticehistory", "notice_key", nullable=False) | ||
|
||
|
||
def downgrade(): | ||
op.drop_column("privacynoticehistory", "notice_key") | ||
op.drop_column("privacynotice", "notice_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
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
Oops, something went wrong.