-
Notifications
You must be signed in to change notification settings - Fork 36
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
Make default catalogNumber uniqueness rule editable #5400
Open
melton-jason
wants to merge
4
commits into
production
Choose a base branch
from
issue-5229
base: production
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.
+100
−2
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5e52295
Introduce migration to remove catalognum + collection unique constraint
melton-jason 06d4b8c
Make catalognumber + collection rule editable
melton-jason 3e69783
Merge remote-tracking branch 'origin/production' into issue-5229
melton-jason 12e5533
Merge branch 'production' into issue-5229
melton-jason 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 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
57 changes: 57 additions & 0 deletions
57
specifyweb/businessrules/migrations/0003_catnum_constraint.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,57 @@ | ||
from typing import Optional, Tuple | ||
from django.db import migrations | ||
from django.db.models import UniqueConstraint | ||
|
||
|
||
DEFAULT_INDEX_NAME = 'CollectionID' | ||
|
||
def get_index_names(connection) -> Tuple[str]: | ||
db_name = connection.settings_dict['NAME'] | ||
cursor = connection.cursor() | ||
sql = """ | ||
SELECT stat.INDEX_NAME | ||
FROM information_schema.statistics stat | ||
WHERE stat.TABLE_SCHEMA = %s | ||
AND stat.TABLE_NAME = 'collectionobject' | ||
AND stat.NON_UNIQUE = 0 | ||
GROUP BY stat.INDEX_NAME | ||
HAVING MAX(stat.SEQ_IN_INDEX) = 2 | ||
AND SUM( | ||
stat.COLUMN_NAME IN ('catalognumber', 'collectionid') | ||
) = 2; | ||
""" | ||
args = [db_name] | ||
cursor.execute(sql, args) | ||
rows: Tuple[Tuple[str], ...] = cursor.fetchall() | ||
return [row[0] for row in rows] | ||
|
||
def remove_constraint(apps, schema_editor): | ||
connection = schema_editor.connection | ||
index_names = get_index_names(connection) | ||
CollectionObject = apps.get_model('specify', 'Collectionobject') | ||
in_atomic_block = connection.in_atomic_block | ||
connection.in_atomic_block = False | ||
for index_name in index_names: | ||
schema_editor.remove_constraint(CollectionObject, UniqueConstraint(fields=["catalognumber", 'collection'], name=index_name)) | ||
connection.in_atomic_block = in_atomic_block | ||
|
||
|
||
def add_constraint(apps, schema_editor): | ||
connection = schema_editor.connection | ||
CollectionObject = apps.get_model('specify', 'Collectionobject') | ||
index_names = get_index_names(connection) | ||
if len(index_names) == 0: | ||
in_atomic_block = connection.in_atomic_block | ||
connection.in_atomic_block = False | ||
schema_editor.add_constraint(CollectionObject, UniqueConstraint(fields=['catalognumber', 'collection'], name=DEFAULT_INDEX_NAME)) | ||
connection.in_atomic_block = in_atomic_block | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
('businessrules', '0002_default_unique_rules') | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(remove_constraint, add_constraint, atomic=True) | ||
] |
42 changes: 42 additions & 0 deletions
42
specifyweb/businessrules/migrations/0004_catnum_uniquerule.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,42 @@ | ||
from typing import Tuple | ||
|
||
from django.db import migrations | ||
|
||
from specifyweb.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)) | ||
|
||
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)) | ||
|
||
candidate_rules = UniquenessRule.objects.filter(id__in=candidate_rules_with_scope) | ||
candidate_rules.update(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') | ||
|
||
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)) | ||
|
||
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)) | ||
|
||
candidate_rules = UniquenessRule.objects.filter(id__in=candidate_rules_with_scope) | ||
if len(candidate_rules) == 0: | ||
create_uniqueness_rule('Collectionobject', discipline=discipline, is_database_constraint=True, 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) | ||
] |
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.
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.
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.
You took the complicated route! I hope there aren't many instances where it differs from our current database schema, but at least it's managed. Nice work!