-
Notifications
You must be signed in to change notification settings - Fork 48
Issue 8124 8126 fix 7660 7682 2 6266 after review #8154
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
base: v7_12_0_7_base
Are you sure you want to change the base?
Changes from 51 commits
2a5eda8
e8466af
25bf8b2
063a7c6
6cc5ffa
1ee0f1e
f0dd766
6dfe4eb
8893687
32e2972
6d1403c
d60939d
470d99d
befb73b
8a1e35b
82bb520
8edb112
efc8228
2366664
4c0f9f8
fac87e9
c8663e9
a940612
4e1d45a
e228051
aff97b8
1e13637
624deee
cd740a7
05f6298
cf79196
c341f55
72414b6
7112c84
eda65ab
6e37e1e
48face1
fdfe21b
96bf0cc
cfad41f
2e1835e
f4d6491
f792d5e
91aaf38
6b4d796
ab8320b
4196c1e
6e1902e
39ec012
9f9df79
a95bc71
1136e52
5418c64
419d69b
19f5c1a
9ddf13e
3396fe4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,71 +1,31 @@ | ||
| from typing import Tuple, 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: | ||
| 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 | ||
| # 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 | ||
| # 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 | ||
| 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 | ||
|
|
||
| if not has_catalognumber_rule: | ||
| create_uniqueness_rule( | ||
| model_name="Collectionobject", | ||
| discipline=discipline, | ||
| is_database_constraint=True, | ||
| fields=["catalogNumber"], | ||
| scopes=["collection"], | ||
| registry=apps, | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,64 @@ | ||
| from django.db import migrations | ||
|
|
||
| from specifyweb.backend.businessrules.migration_utils import catnum_rule_editable, catnum_rule_uneditable | ||
| from specifyweb.backend.businessrules.migration_utils import catnum_rule_editable | ||
| from specifyweb.backend.businessrules.uniqueness_rules import create_uniqueness_rule | ||
|
|
||
|
|
||
| def catnum_rule_editable(apps, schema_editor): | ||
| UniquenessRule = apps.get_model('businessrules', 'UniquenessRule') | ||
| UniquenessRuleField = apps.get_model('businessrules', 'UniquenessRuleField') | ||
|
|
||
| candidate_rules_with_field: tuple[int] = tuple(UniquenessRuleField.objects.filter(uniquenessrule__modelName__iexact='collectionobject', uniquenessrule__isDatabaseConstraint=True, fieldPath__iexact='catalognumber', isScope=False).values_list('uniquenessrule_id', flat=True)) | ||
| def catnum_rule_uneditable(apps, schema_editor): | ||
| """ Find any CollectionObject catalogNumber must be unique to Collection | ||
| rules which are editable on the frontend (have isDatabaseConstraint=False) | ||
| and set their isDatabaseConstraint=True. | ||
|
|
||
| candidate_rules_with_scope: tuple[int] = tuple(UniquenessRuleField.objects.filter(uniquenessrule_id__in=candidate_rules_with_field, fieldPath__iexact='collection', isScope=True).values_list('uniquenessrule_id', flat=True)) | ||
| Generally should be run when migration businessrules/0003 is being reverted | ||
| """ | ||
| Discipline = apps.get_model("specify", "Discipline") | ||
| UniquenessRule = apps.get_model("businessrules", "UniquenessRule") | ||
|
|
||
| candidate_rules = UniquenessRule.objects.filter(id__in=candidate_rules_with_scope) | ||
| candidate_rules.update(isDatabaseConstraint=False) | ||
| for discipline in Discipline.objects.all(): | ||
| # REFACTOR: Some of these queries should be able to be combined to | ||
| # improve performance and limit how often we need to hit the database | ||
| model_rules = UniquenessRule.objects.filter( | ||
| modelName="Collectionobject", | ||
| discipline_id=discipline.id, | ||
| isDatabaseConstraint=False | ||
| ) | ||
|
|
||
| def catnum_rule_uneditable(apps, schema_editor): | ||
| Discipline = apps.get_model('specify', 'Discipline') | ||
| UniquenessRule = apps.get_model('businessrules', 'UniquenessRule') | ||
| UniquenessRuleField = apps.get_model('businessrules', 'UniquenessRuleField') | ||
| has_catalognumber_rule = False | ||
| matching_rule_ids: list[int] = [] | ||
| for rule in model_rules: | ||
| rule_fields = rule.uniquenessrulefield_set.all() | ||
|
|
||
| for discipline in Discipline.objects.all(): | ||
| candidate_rules_with_field: tuple[int] = tuple(UniquenessRuleField.objects.filter(uniquenessrule__modelName__iexact='collectionobject', uniquenessrule__discipline=discipline.id, uniquenessrule__isDatabaseConstraint=False, fieldPath__iexact='catalognumber', isScope=False).values_list('uniquenessrule_id', flat=True)) | ||
| fields = rule_fields.filter(isScope=False) | ||
| scopes = rule_fields.filter(isScope=True) | ||
|
|
||
| candidate_rules_with_scope: tuple[int] = tuple(UniquenessRuleField.objects.filter(uniquenessrule_id__in=candidate_rules_with_field, fieldPath__iexact='collection', isScope=True).values_list('uniquenessrule_id', flat=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) | ||
|
|
||
| candidate_rules = UniquenessRule.objects.filter(id__in=candidate_rules_with_scope) | ||
| if len(candidate_rules) == 0: | ||
| if has_catalognumber_rule: | ||
| UniquenessRule.objects.filter( | ||
| id__in=matching_rule_ids).update(isDatabaseConstraint=True) | ||
| else: | ||
| create_uniqueness_rule( | ||
| model_name='Collectionobject', | ||
| model_name="Collectionobject", | ||
| discipline=discipline, | ||
| is_database_constraint=True, | ||
| fields=['catalogNumber'], | ||
| scopes=['collection'], | ||
| registry=apps | ||
| fields=["catalogNumber"], | ||
| scopes=["collection"], | ||
| registry=apps, | ||
| ) | ||
| else: | ||
| candidate_rules.update(isDatabaseConstraint=True) | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ('businessrules', '0003_catnum_constraint') | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython(catnum_rule_editable, catnum_rule_uneditable, atomic=True) | ||
| migrations.RunPython(catnum_rule_editable, | ||
| catnum_rule_uneditable, atomic=True) | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,13 +53,28 @@ def create_admins(apps=apps) -> None: | |
| UserPolicy = apps.get_model('permissions', 'UserPolicy') | ||
| Specifyuser = apps.get_model('specify', 'Specifyuser') | ||
|
|
||
| if UserPolicy.objects.filter(collection__isnull=True, resource='%', action='%').exists(): | ||
| # don't do anything if there is already any admin. | ||
| return | ||
|
|
||
| users = Specifyuser.objects.all() | ||
| for user in users: | ||
| if is_sp6_user_permissions_migrated(user, apps): | ||
| # REFACTOR: Try and fold the following checks into a single query to | ||
| # avoid making multiple queries per user. | ||
| # Ideally, we only make a single query to fetch all users that: | ||
| # - Are not already Institution Admins | ||
| # - Have not already seen activity in Sp 7 (don't have Sp7 permissions) | ||
| # - (The Institution Admin permission could have been intentionally | ||
| # removed) | ||
| # - Are admins in Sp 6 | ||
|
|
||
| # The ordering here for checks here is intentional: it's more likely a | ||
| # user has Sp 7 permissions than being an admin, so we do the former | ||
| # check first | ||
| if is_sp6_user_permissions_migrated(user=user, apps=apps): | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (note here on why I brought back I brought back
Previously, because this is a part of Run Key Migration Functions, the user in question would be re-made into an Institutional Admin, even if they were explicitly removed as an admin. With |
||
| continue | ||
| if UserPolicy.objects.filter( | ||
| collection__isnull=True, | ||
| specifyuser_id=user.id, | ||
| resource="%", | ||
| action="%", | ||
| ).exists(): | ||
| continue | ||
| if is_legacy_admin(user): | ||
| UserPolicy.objects.get_or_create( | ||
|
|
@@ -92,14 +107,6 @@ def assign_users_to_roles(apps=apps) -> None: | |
|
|
||
| results = [] | ||
| with connection.cursor() as cursor: | ||
| cursor.execute(""" | ||
| SELECT COUNT(*) | ||
| FROM information_schema.tables | ||
| WHERE table_name IN ('specifyuser_spprincipal', 'spuserrole') | ||
| AND table_schema = DATABASE(); | ||
| """) | ||
| if cursor.fetchone()[0] < 2: | ||
| return # Newly created sp7 databases don't have these sp6 specific tables. | ||
| cursor.execute(""" | ||
| SELECT | ||
| u.SpecifyUserID as user_id, | ||
|
|
@@ -112,37 +119,34 @@ def assign_users_to_roles(apps=apps) -> None: | |
| JOIN spprincipal p ON p.SpPrincipalID = up.SpPrincipalID | ||
| JOIN collection c ON c.UserGroupScopeId = p.userGroupScopeID | ||
| WHERE p.groupType IS NULL | ||
| AND u.SpecifyUserID NOT IN ( | ||
| SELECT ur.specifyuser_id | ||
| AND NOT EXISTS ( | ||
| SELECT 1 | ||
| FROM spuserrole ur | ||
| JOIN sprole r ON r.id = ur.role_id | ||
| WHERE r.collection_id = p.usergroupscopeid | ||
| ) | ||
| AND c.UserGroupScopeId NOT IN ( | ||
| SELECT DISTINCT r.collection_id | ||
| FROM spuserrole ur | ||
| JOIN sprole r ON r.id = ur.role_id | ||
| JOIN collection c ON c.UserGroupScopeId = r.collection_id | ||
| WHERE r.collection_id = c.UserGroupScopeId | ||
| AND ur.specifyuser_id = u.SpecifyUserID | ||
| ); | ||
| """) | ||
|
|
||
| results = cursor.fetchall() | ||
|
|
||
| for user_id, user_name, user_type, collection_id, collection_name in results: | ||
| if user_type not in {'Manager', 'FullAccess', 'LimitedAccess', 'Guest'}: | ||
| # REFACTOR: If we want to exlcude all other roles, why don't we write | ||
| # the exlcusion in the query rather than evaluate in Python? | ||
| if user_type not in ROLE_NAMES.keys(): | ||
| continue | ||
|
|
||
| role_name = ROLE_NAMES.get(user_type, f"{user_type} - {collection_name}") | ||
| role_description = ROLE_DESCRIPTIONS.get(user_type, "No description available.") | ||
| logger.info(f"Assigned user {user_name} to role {role_name} for collection {collection_name}.") | ||
|
|
||
| role, is_new_role = Role.objects.get_or_create( | ||
| role, _ = Role.objects.get_or_create( | ||
| collection_id=collection_id, | ||
| name=role_name | ||
| name=role_name, | ||
| defaults={ | ||
| "description": role_description | ||
| } | ||
| ) | ||
| if is_new_role: | ||
| role.description = role_description | ||
| role.save() | ||
| UserRole.objects.get_or_create( | ||
| specifyuser_id=user_id, | ||
| role=role | ||
|
|
||
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.
(note for discussion)
I left this comment in the code, but I'll leave this as a comment on the PR for visibility.
There was a noticeable change in the asymptotic growth (time complexity) with this change.
Noticeably, the previous implementation of
fix_global_default_rules:Importantly, the number of Queries and iteration only grew on the number of Disciplines. So assuming the number of Disciplines remained the same, there could be any number of added Uniqueness Rules within those Disciplines without seeing a slowdown or additional database queries.
(This is only partly/theoretically true: there will be a slowdown for those queries if there is a sufficiently large amount of Uniqueness Rules and the queries are performed on unindexed fields or in a way that can't utilize the indexes, though I'm holding that slowdown as negligible or constant between the two approaches).
I've cleaned up the new approach and eliminated extra queries in
6e37e1e(this PR), but there still is a change in the approach:Our "bottleneck" is now largely on the amount of scoped Uniqueness Rules, rather than the number of Disciplines.
Practically, I'm not sure how much of a slowdown this would introduce.
If we have the time, we can try and resolve this slowdown (I imagine we can use and filter on annotations on the query for scoped rules for duplicate rules, or just revert back to something like the past approach if there are no bugs), and/or we can evaluate how much of a slowdown this introduces.