Skip to content
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
wants to merge 4 commits into
base: production
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
def apply_default_rules(apps, schema_editor):
Discipline = apps.get_model('specify', 'Discipline')
for disp in Discipline.objects.all():
apply_default_uniqueness_rules(disp)
apply_default_uniqueness_rules(disp, registry=apps)


def remove_default_rules(apps, schema_editor):
Expand Down
57 changes: 57 additions & 0 deletions specifyweb/businessrules/migrations/0003_catnum_constraint.py
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'
Copy link
Member

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!

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 specifyweb/businessrules/migrations/0004_catnum_uniquerule.py
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)
]
1 change: 0 additions & 1 deletion specifyweb/interactions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def preps_available_rs(request, recordset_id):
sql += " GROUP BY 1,2,3,4,5 ORDER BY 1;"

cursor.execute(sql, [request.specify_collection.id, recordset_id])

rows = cursor.fetchall()

return http.HttpResponse(toJson(rows), content_type='application/json')
Expand Down
Loading