Merge lp:~jderose/dmedia/import-update into lp:dmedia

Proposed by Jason Gerard DeRose
Status: Merged
Merged at revision: 944
Proposed branch: lp:~jderose/dmedia/import-update
Merge into: lp:dmedia
Diff against target: 124 lines (+81/-5)
3 files modified
dmedia/importer.py (+2/-5)
dmedia/metastore.py (+10/-0)
dmedia/tests/test_metastore.py (+69/-0)
To merge this branch: bzr merge lp:~jderose/dmedia/import-update
Reviewer Review Type Date Requested Status
David Jordan Approve
Review via email: mp+376667@code.launchpad.net

Commit message

This switches the importer to use Database.update() instead of Database.save(), which fixes an import failure that can occur when importing duplicate files (files already in Dmedia).

For details, see:
https://bugs.launchpad.net/dmedia/+bug/1856111

To post a comment you must log in.
Revision history for this message
David Jordan (dmj726) wrote :

Looks good. Thanks for finding this issue and documenting to problem and solution. Approved!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'dmedia/importer.py'
2--- dmedia/importer.py 2014-04-19 22:37:14 +0000
3+++ dmedia/importer.py 2019-12-12 05:25:21 +0000
4@@ -43,7 +43,7 @@
5 from dmedia.util import get_project_db
6 from dmedia.units import bytes10
7 from dmedia import workers, schema
8-from dmedia.metastore import MetaStore, create_stored, merge_stored
9+from dmedia.metastore import MetaStore, create_stored, update_duplicate_file
10 from dmedia.extractor import extract, merge_thumbnail
11
12
13@@ -270,10 +270,7 @@
14 stored = create_stored(ch.id, *filestores)
15 try:
16 doc = self.db.get(ch.id)
17- doc['origin'] = 'user'
18- doc['atime'] = int(timestamp)
19- merge_stored(doc['stored'], stored)
20- self.db.save(doc)
21+ self.db.update(update_duplicate_file, doc, timestamp, stored)
22 yield ('duplicate', file, ch)
23 except NotFound:
24 doc = schema.create_file(timestamp, ch, stored)
25
26=== modified file 'dmedia/metastore.py'
27--- dmedia/metastore.py 2019-12-11 18:43:39 +0000
28+++ dmedia/metastore.py 2019-12-12 05:25:21 +0000
29@@ -378,6 +378,16 @@
30 doc['bytes_avail'] = bytes_avail
31
32
33+# See https://bugs.launchpad.net/dmedia/+bug/1856111
34+def update_duplicate_file(doc, timestamp, stored):
35+ """
36+ Used by importer.ImportWorker.
37+ """
38+ doc['origin'] = 'user'
39+ doc['atime'] = int(timestamp)
40+ merge_stored(get_dict(doc, 'stored'), stored)
41+
42+
43 def relink_iter(fs, count=50):
44 buf = []
45 for st in fs:
46
47=== modified file 'dmedia/tests/test_metastore.py'
48--- dmedia/tests/test_metastore.py 2019-12-11 18:43:39 +0000
49+++ dmedia/tests/test_metastore.py 2019-12-12 05:25:21 +0000
50@@ -1453,6 +1453,75 @@
51 }
52 )
53
54+ def test_update_duplicate_file(self):
55+ doc = {}
56+ stored = {}
57+ self.assertIsNone(metastore.update_duplicate_file(doc, 12345.6, stored))
58+ self.assertEqual(doc,
59+ {
60+ 'origin': 'user',
61+ 'atime': 12345,
62+ 'stored': {},
63+ }
64+ )
65+ self.assertIsNot(doc['stored'], stored)
66+
67+ id1 = random_id()
68+ id2 = random_id()
69+ id3 = random_id()
70+ old = {
71+ id1: {
72+ 'copies': 2,
73+ 'mtime': 1234,
74+ 'verified': 2345,
75+ },
76+ id2: {
77+ 'should_be': 'ignored',
78+ 'copies': 2,
79+ 'mtime': 3456,
80+ 'verified': 4567,
81+ },
82+ }
83+ new = {
84+ id2: {
85+ 'copies': 1,
86+ 'mtime': 5678,
87+ },
88+ id3: {
89+ 'copies': 1,
90+ 'mtime': 6789,
91+ },
92+ }
93+ doc = {
94+ 'origin': 'strange',
95+ 'atime': 9876,
96+ 'stored': old,
97+ }
98+ self.assertIsNone(metastore.update_duplicate_file(doc, 12345.6, new))
99+ self.assertEqual(doc,
100+ {
101+ 'origin': 'user',
102+ 'atime': 12345,
103+ 'stored': {
104+ id1: { # Should be unmodified old:
105+ 'copies': 2,
106+ 'mtime': 1234,
107+ 'verified': 2345,
108+ },
109+ id2: { # Should be updated with new, plus 'verified' poped:
110+ 'should_be': 'ignored',
111+ 'copies': 1,
112+ 'mtime': 5678,
113+ },
114+ id3: { # Should be from new:
115+ 'copies': 1,
116+ 'mtime': 6789,
117+ },
118+ }
119+ }
120+ )
121+ self.assertIs(doc['stored'], old)
122+
123 def test_relink_iter(self):
124 fs = TempFileStore()
125

Subscribers

People subscribed via source and target branches