-
Notifications
You must be signed in to change notification settings - Fork 44
Merge pull request #6308 from specify/issue-6266 #8094
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
Open
CarolineDenis
wants to merge
17
commits into
temp-pre-6266
Choose a base branch
from
retro-review-6266-base
base: temp-pre-6266
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
21ab7ab
Merge pull request #6308 from specify/issue-6266
acwhite211 f2a3516
Fix PR review issues for key migration pipeline
acwhite211 e8a005f
Make migration edits while preserving PR review fixes
acwhite211 7bc1f79
Revert "Make migration edits while preserving PR review fixes"
acwhite211 e05eeb5
Revert "Fix PR review issues for key migration pipeline"
acwhite211 20ce3ac
Mark existing catalog number rules as database constraints
acwhite211 150237d
Match uniqueness rules by exact field signatures
acwhite211 e31e838
Use migration database alias in patch helpers
acwhite211 244f6cd
Backfill permissions per user and collection
acwhite211 6999a4d
Gate stored query SQL debug logging
acwhite211 056f211
Gate geology time SQL debug logging
acwhite211 3480666
Tighten key migration command queries and logging
acwhite211 5b8d98b
Pair tectonic tree defs with disciplines one-to-one
acwhite211 6231791
Fix RelativeAgeCitation schema field id
acwhite211 5c839d1
Repair tectonic rank root handling
acwhite211 59c88e5
Make schema config backfills exact and reversible
acwhite211 1a57b82
Avoid literal-bound SQL in debug logs
acwhite211 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 hidden or 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 hidden or 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 hidden or 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,75 @@ | ||
| from typing import List | ||
|
|
||
| from specifyweb.backend.businessrules.uniqueness_rules import create_uniqueness_rule | ||
|
|
||
|
|
||
| def catnum_rule_editable(apps, schema_editor=None): | ||
| """ Find any CollectionObject catalogNumber must be unique to Collection | ||
| rules which are readonly on the frontend (have isDatabaseConstraint=True) | ||
| and set their isDatabaseConstraint=False. | ||
|
|
||
| Generally should be run only after migration businessrules/0003 has been | ||
| applied | ||
| """ | ||
| UniquenessRule = apps.get_model("businessrules", "UniquenessRule") | ||
|
|
||
| model_rules = UniquenessRule.objects.filter(modelName="Collectionobject", isDatabaseConstraint=True) | ||
|
|
||
| catalog_number_rules: List[int] = [] | ||
| for rule in model_rules: | ||
| rule_fields = rule.uniquenessrulefield_set.all() | ||
|
|
||
| fields = rule_fields.filter(isScope=False) | ||
| scopes = rule_fields.filter(isScope=True) | ||
|
|
||
| # We're only interested in the rule "CollectionObject catalogNumber | ||
| # must be unique to Collection" | ||
| # We check for length of fields and scopes because get() raises an | ||
| # exception if more than one result is returned | ||
| if (len(fields) == 1 and len(scopes) == 1) and (fields.get().fieldPath.lower() == "catalognumber" and scopes.get().fieldPath.lower() == "collection"): | ||
| catalog_number_rules.append(rule.id) | ||
|
|
||
| rules_to_update = UniquenessRule.objects.filter(id__in=catalog_number_rules) | ||
| rules_to_update.update(isDatabaseConstraint=False) | ||
|
|
||
|
|
||
| def catnum_rule_uneditable(apps, schema_editor=None): | ||
| """ Find any CollectionObject catalogNumber must be unique to Collection | ||
| rules which are editable on the frontend (have isDatabaseConstraint=False) | ||
| and set their isDatabaseConstraint=True. | ||
|
|
||
| Generally should be run when migration businessrules/0003 is being reverted | ||
| """ | ||
| Discipline = apps.get_model("specify", "Discipline") | ||
| UniquenessRule = apps.get_model("businessrules", "UniquenessRule") | ||
|
|
||
| for discipline in Discipline.objects.all(): | ||
| model_rules = UniquenessRule.objects.filter(modelName="Collectionobject", discipline_id=discipline.id, isDatabaseConstraint=False) | ||
|
|
||
| has_catalognumber_rule = False | ||
| matching_rule_ids: List[int] = [] | ||
| for rule in model_rules: | ||
| rule_fields = rule.uniquenessrulefield_set.all() | ||
|
|
||
| fields = rule_fields.filter(isScope=False) | ||
| scopes = rule_fields.filter(isScope=True) | ||
|
|
||
| # We're only interested in the rule "CollectionObject catalogNumber | ||
| # must be unique to Collection" | ||
| # We check for length of fields and scopes because get() raises an | ||
| # exception if more than one result is returned | ||
| if (len(fields) == 1 and len(scopes) == 1) and (fields.get().fieldPath.lower() == "catalognumber" and scopes.get().fieldPath.lower() == "collection"): | ||
| has_catalognumber_rule = True | ||
| matching_rule_ids.append(rule.id) | ||
|
|
||
| if has_catalognumber_rule: | ||
| UniquenessRule.objects.filter(id__in=matching_rule_ids).update(isDatabaseConstraint=True) | ||
| else: | ||
| create_uniqueness_rule( | ||
| "Collectionobject", | ||
| discipline=discipline, | ||
| is_database_constraint=True, | ||
| fields=["catalogNumber"], | ||
| scopes=["collection"], | ||
| registry=apps, | ||
| ) | ||
3 changes: 1 addition & 2 deletions
3
specifyweb/backend/businessrules/migrations/0004_catnum_uniquerule.py
This file contains hidden or 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 hidden or 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
18 changes: 18 additions & 0 deletions
18
specifyweb/backend/businessrules/migrations/0008_fix_global_default_rules.py
This file contains hidden or 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,18 @@ | ||
| import logging | ||
| from django.db import migrations | ||
| from specifyweb.backend.businessrules.uniqueness_rules import fix_global_default_rules | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| def apply_migration(apps, schema_editor): | ||
| fix_global_default_rules(apps) | ||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('businessrules', '0007_more_uniqueness_rules'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython(apply_migration, migrations.RunPython.noop, atomic=True) | ||
| ] |
This file contains hidden or 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 hidden or 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 hidden or 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,38 @@ | ||
| from django.db.models import F | ||
|
|
||
| # REFACTOR: Use ALL_TRESS in specify/tree_views.py? | ||
| SPECIFY_TREES = ["Taxon", "Geography", "Storage", | ||
| "Geologictimeperiod", "Lithostrat"] | ||
|
|
||
|
|
||
| def apply_migrations(app_registry, schema_editor=None): | ||
| update_is_accepted(app_registry, schema_editor) | ||
| update_coordinates(app_registry, schema_editor) | ||
|
|
||
| def update_is_accepted(app_registry, schema_editor=None): | ||
| db_alias = schema_editor.connection.alias if schema_editor is not None else "default" | ||
| for tree in SPECIFY_TREES: | ||
| tree_filters = { | ||
| "isaccepted": False, | ||
| "accepted" + tree.lower() + "__isnull": True | ||
| } | ||
|
|
||
| tree_model = app_registry.get_model("specify", tree) | ||
| tree_model._base_manager.using(db_alias).filter(**tree_filters).update(isaccepted=True) | ||
|
|
||
|
|
||
| def update_coordinates(app_registry, schema_editor=None): | ||
| db_alias = schema_editor.connection.alias if schema_editor is not None else "default" | ||
| Locality = app_registry.get_model("specify", "Locality") | ||
|
|
||
| Locality._base_manager.using(db_alias).filter(lat1text__isnull=True, latitude1__isnull=False) \ | ||
| .update(lat1text=F("latitude1")) | ||
|
|
||
| Locality._base_manager.using(db_alias).filter(long1text__isnull=True, longitude1__isnull=False) \ | ||
| .update(long1text=F("longitude1")) | ||
|
|
||
| Locality._base_manager.using(db_alias).filter(lat2text__isnull=True, latitude2__isnull=False) \ | ||
| .update(lat2text=F("latitude2")) | ||
|
|
||
| Locality._base_manager.using(db_alias).filter(long2text__isnull=True, longitude2__isnull=False) \ | ||
| .update(long2text=F("longitude2")) | ||
|
acwhite211 marked this conversation as resolved.
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.