-
Notifications
You must be signed in to change notification settings - Fork 74
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
Migrate remaining data categories #5073
Migrate remaining data categories #5073
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Skipped Deployment
|
Passing run #9175 ↗︎
Details:
Review all test suite changes for PR #5073 ↗︎ |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #5073 +/- ##
=======================================
Coverage 86.56% 86.56%
=======================================
Files 357 357
Lines 22349 22349
Branches 2954 2954
=======================================
+ Hits 19346 19347 +1
Misses 2480 2480
+ Partials 523 522 -1 ☔ View full report in Codecov by Sentry. |
src/fides/api/alembic/migrations/helpers/fideslang_migration_functions.py
Outdated
Show resolved
Hide resolved
src/fides/api/alembic/migrations/helpers/fideslang_migration_functions.py
Outdated
Show resolved
Hide resolved
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.
The only change here is that I'm pulling out the shared functions
src/fides/api/alembic/migrations/versions/a6d9cdfcc7dc_migrate_remaining_data_categories.py
Show resolved
Hide resolved
src/fides/api/alembic/migrations/versions/a6d9cdfcc7dc_migrate_remaining_data_categories.py
Outdated
Show resolved
Hide resolved
|
||
|
||
class TestDataCategoryMigrationFunctions: | ||
def test_remove_conflicting_rule_targets(self, db): |
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.
This is just a straightforward test with one "bad" data category. Let me know if there are any additional scenarios I should cover.
Starting review - |
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.
thanks for this needed cleanup @galvana. my main suggestion is around not using the sqlalchemy models but raw sql in the data migration in your two new functions -
existing_target = RuleTarget.filter( | ||
db=db, | ||
conditions=( | ||
(RuleTarget.rule_id == rule.id) | ||
& (RuleTarget.data_category == data_category) | ||
), | ||
).first() |
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 generally leery of using sqlalchemy models in migrations. If this model gets updated in the future, old migrations will start using this new model, with fields that don't yet exist on it, which can cause issues. I'd consider refactoring to not use the sqlalchemy models directly, like the other functions that were copied to this file.
src/fides/api/alembic/migrations/versions/a6d9cdfcc7dc_migrate_remaining_data_categories.py
Outdated
Show resolved
Hide resolved
src/fides/api/alembic/migrations/helpers/fideslang_migration_functions.py
Show resolved
Hide resolved
erasure_rules = Rule.filter( | ||
db=db, conditions=(Rule.action_type == ActionType.erasure) | ||
).all() |
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'd refactor to not use the Rule model here, and write raw sql queries instead
) | ||
db.delete(rule_target) | ||
|
||
db.commit() |
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'd try to avoid using this commit()- it shouldn't be needed, or should be rewritten so it isn't needed - see the other functions in this file.
I think the way we run migrations, it's all wrapped in a single transaction, so all migration scripts are run as part of one transaction and the commit happens at the end. We've had issues where say we're running migrations and something fails or takes too long and we have to bail in the middle - it's pretty straightforward to stop and roll back because nothing was actually committed. We could get in a weird state by deviating from this pattern where we get some of the migration committed but not all of it in the event of a failure.
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.
Got it, I'll remove this commit and move it to the test instead, that way the context this function is running can take care of when to commit (either Alembic or the tests)
src/fides/api/alembic/migrations/helpers/fideslang_migration_functions.py
Show resolved
Hide resolved
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.
Ready for another pass! I updated the manual testing steps and the setup_script.txt
so make sure you have the latest version
src/fides/api/alembic/migrations/helpers/fideslang_migration_functions.py
Show resolved
Hide resolved
) | ||
db.delete(rule_target) | ||
|
||
db.commit() |
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.
Got it, I'll remove this commit and move it to the test instead, that way the context this function is running can take care of when to commit (either Alembic or the tests)
src/fides/api/alembic/migrations/helpers/fideslang_migration_functions.py
Show resolved
Hide resolved
src/fides/api/alembic/migrations/versions/a6d9cdfcc7dc_migrate_remaining_data_categories.py
Outdated
Show resolved
Hide resolved
Starting re-review! |
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.
OK i've tested and reviewed the code, this looks good to me. I think we did backups or similar last time before this migration, I'd recommend the same here, since we can't roll back and it touches a lot of places in the database.
I also haven't independently verified if we're changing all the right data category locations/are there any we shouldn't be updating/any we've forgotten -
text("DELETE FROM ruletarget WHERE id IN :target_ids"), | ||
{"target_ids": tuple(target_ids)}, | ||
) | ||
logger.info(f"Removed {len(target_ids)} conflicting rule targets") |
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.
Ah right, and you're only doing this for erasure rules, I was confused in testing why a conflicting access rule target was hanging around.
Passing run #9176 ↗︎
Details:
Review all test suite changes for PR #5073 ↗︎ |
Closes PROD-2210
Description Of Changes
The following data categories weren't migrated during the last Fideslang migration:
user.biometric_health
->user.biometric.health
user.credentials.biometric_credentials
->user.authorization.biometric
user.credentials.password
->user.authorization.password
Additionally, the default access and erasure polices could be missing the following data categories if they were created before Fideslang 2.0 was released.
user.behavior
user.content
user.privacy_preferences
This PR migrates the missed data categories for all policies but only adds the missing data categories to the default access and erasure policies. We don't want to make any assumptions about any policies that were created by the users.
Code Changes
update_default_dsr_policies
andremove_conflicting_rule_targets
Steps to Confirm
nox -s dev
and connect to the Postgres database.We should see these records containing the old data categories +
user.biometric
which already existed in Fideslang 1.4nox -s shell
and navigate to the Alembic directorycd src/fides/api/alembic
.alembic downgrade -1
. This won't reset anything (that was done by the commands in step 2) but it will allow us to run the migration again.alembic upgrade head
and take note of the messages in the logs.We should see this:
user.behavior
,user.content
,user.privacy_preference
)user.authorization.biometric
,user.authorization.password
,user.biometric.health
)user.biometric.health
data category removed from thedefault_erasure_policy_rule
but notdefault_access_policy_rule
Pre-Merge Checklist
CHANGELOG.md
main
downgrade()
migration is correct and works