Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Arkindex
Backend
Commits
4d48eff8
Commit
4d48eff8
authored
6 years ago
by
Bastien Abadie
Browse files
Options
Downloads
Patches
Plain Diff
Avoid doublons on transcriptions by adding a unique constraint
parent
777f50bc
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!105
Avoid doublons on transcriptions by adding a unique constraint
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
arkindex/documents/migrations/0025_avoid_doublons.py
+63
-0
63 additions, 0 deletions
arkindex/documents/migrations/0025_avoid_doublons.py
arkindex/documents/models.py
+8
-0
8 additions, 0 deletions
arkindex/documents/models.py
with
71 additions
and
0 deletions
arkindex/documents/migrations/0025_avoid_doublons.py
0 → 100644
+
63
−
0
View file @
4d48eff8
# Generated by Django 2.1 on 2018-09-17 08:35
from
django.db
import
migrations
,
models
from
django.db
import
connection
import
django.db.models.deletion
# This sql query delete all the transcriptions
# that have the same (element_id, zone_id, text)
# keeping only the first item
SQL_REMOVE
=
'''
delete from documents_transcription
where id in (
select unnest((array_agg(id))[2:])
from documents_transcription
group by (element_id, zone_id, text)
having count(*) > 1
);
'''
# This sql query create a unique together index
# on (element_id, zone_id, text)
# It cannot be automatically achieved by Django
# as it needs to make a md5 hash of text to fit in its index max size
SQL_UNIQUE
=
'''
create unique index documents_transcription_uniq_elt_zone_text
on documents_transcription (
zone_id,
element_id,
md5(text)
)
'''
def
remove_doublons
(
apps
,
schema_editor
):
'''
Run above sql queries
'''
with
connection
.
cursor
()
as
cursor
:
cursor
.
execute
(
SQL_REMOVE
)
cursor
.
execute
(
SQL_UNIQUE
)
# Custom index
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'
images
'
,
'
0005_zone_polygon_tmp
'
),
(
'
documents
'
,
'
0024_page_text
'
),
]
operations
=
[
migrations
.
AlterField
(
model_name
=
'
elementpath
'
,
name
=
'
element
'
,
field
=
models
.
ForeignKey
(
on_delete
=
django
.
db
.
models
.
deletion
.
CASCADE
,
related_name
=
'
paths
'
,
to
=
'
documents.Element
'
,
),
),
migrations
.
RunPython
(
remove_doublons
),
]
This diff is collapsed.
Click to expand it.
arkindex/documents/models.py
+
8
−
0
View file @
4d48eff8
...
...
@@ -474,6 +474,14 @@ class Transcription(models.Model):
text
=
models
.
TextField
(
null
=
True
,
blank
=
True
)
score
=
models
.
FloatField
(
null
=
True
,
blank
=
True
)
class
Meta
:
# This index is manually created in a migration to
# support the md5(text) operation
# unique_together = (
# ('element', 'zone', 'text')
# )
pass
def
__str__
(
self
):
return
'
Transcription: {}
'
.
format
(
self
.
text
[:
20
])
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment