Merge lp:~jamesh/mediascanner2/bug-1501990 into lp:mediascanner2

Proposed by James Henstridge
Status: Merged
Approved by: Michi Henning
Approved revision: 309
Merged at revision: 310
Proposed branch: lp:~jamesh/mediascanner2/bug-1501990
Merge into: lp:mediascanner2
Diff against target: 98 lines (+34/-8)
2 files modified
src/mediascanner/MediaStore.cc (+7/-4)
test/test_mediastore.cc (+27/-4)
To merge this branch: bzr merge lp:~jamesh/mediascanner2/bug-1501990
Reviewer Review Type Date Requested Status
Michi Henning (community) Approve
Review via email: mp+273649@code.launchpad.net

Commit message

Remove the id column from media_attic so that we don't get conflicts when copying data between media and media_attic.

Description of the change

When removable media is plugged or unplugged, the media scanner copies rows from the media table to or from the media_attic table (the idea being to avoid the need to keep the metadata from the removable media around while it is unplugged but not in the search results).

In 0.106, we added an explicit integer primary key field to media and media_attic, since previously it was using the implicit rowid as a foreign key in the FTS index, which is not guaranteed to stay unchanged over various operations (e.g. vacuum). However, it seems the sequence used to allocate these IDs could be reuse values when they were removed from the table leading to duplicates. This meant that a future copy operation could fail.

Since nothing references the media_attic table, the easy solution was to simply remove its id column so IDs will be stripped when copying data to the attic, and new IDs assigned when copying back to the media table.

To post a comment you must log in.
Revision history for this message
Michi Henning (michihenning) wrote :

Looks good!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/mediascanner/MediaStore.cc'
--- src/mediascanner/MediaStore.cc 2015-07-14 01:34:56 +0000
+++ src/mediascanner/MediaStore.cc 2015-10-07 07:13:13 +0000
@@ -46,7 +46,7 @@
4646
47// Increment this whenever changing db schema.47// Increment this whenever changing db schema.
48// It will cause dbstore to rebuild its tables.48// It will cause dbstore to rebuild its tables.
49static const int schemaVersion = 9;49static const int schemaVersion = 10;
5050
51struct MediaStorePrivate {51struct MediaStorePrivate {
52 sqlite3 *db;52 sqlite3 *db;
@@ -322,7 +322,6 @@
322CREATE INDEX media_mtime_idx ON media(type, mtime);322CREATE INDEX media_mtime_idx ON media(type, mtime);
323323
324CREATE TABLE media_attic (324CREATE TABLE media_attic (
325 id INTEGER PRIMARY KEY,
326 filename TEXT UNIQUE NOT NULL,325 filename TEXT UNIQUE NOT NULL,
327 content_type TEXT,326 content_type TEXT,
328 etag TEXT,327 etag TEXT,
@@ -918,7 +917,9 @@
918917
919void MediaStorePrivate::archiveItems(const std::string &prefix) {918void MediaStorePrivate::archiveItems(const std::string &prefix) {
920 const char *templ = R"(BEGIN TRANSACTION;919 const char *templ = R"(BEGIN TRANSACTION;
921INSERT INTO media_attic SELECT * FROM media WHERE filename LIKE %s;920INSERT INTO media_attic (filename, content_type, etag, title, date, artist, album, album_artist, genre, disc_number, track_number, duration, width, height, latitude, longitude, has_thumbnail, mtime, type)
921 SELECT filename, content_type, etag, title, date, artist, album, album_artist, genre, disc_number, track_number, duration, width, height, latitude, longitude, has_thumbnail, mtime, type
922 FROM media WHERE filename LIKE %s;
922DELETE FROM media WHERE filename LIKE %s;923DELETE FROM media WHERE filename LIKE %s;
923COMMIT;924COMMIT;
924)";925)";
@@ -935,7 +936,9 @@
935936
936void MediaStorePrivate::restoreItems(const std::string &prefix) {937void MediaStorePrivate::restoreItems(const std::string &prefix) {
937 const char *templ = R"(BEGIN TRANSACTION;938 const char *templ = R"(BEGIN TRANSACTION;
938INSERT INTO media SELECT * FROM media_attic WHERE filename LIKE %s;939INSERT INTO media (filename, content_type, etag, title, date, artist, album, album_artist, genre, disc_number, track_number, duration, width, height, latitude, longitude, has_thumbnail, mtime, type)
940 SELECT filename, content_type, etag, title, date, artist, album, album_artist, genre, disc_number, track_number, duration, width, height, latitude, longitude, has_thumbnail, mtime, type
941 FROM media_attic WHERE filename LIKE %s;
939DELETE FROM media_attic WHERE filename LIKE %s;942DELETE FROM media_attic WHERE filename LIKE %s;
940COMMIT;943COMMIT;
941)";944)";
942945
=== modified file 'test/test_mediastore.cc'
--- test/test_mediastore.cc 2015-07-14 01:34:56 +0000
+++ test/test_mediastore.cc 2015-10-07 07:13:13 +0000
@@ -24,9 +24,10 @@
24#include <mediascanner/MediaStore.hh>24#include <mediascanner/MediaStore.hh>
25#include <mediascanner/internal/utils.hh>25#include <mediascanner/internal/utils.hh>
2626
27#include<stdexcept>27#include <algorithm>
28#include<cstdio>28#include <stdexcept>
29#include<string>29#include <cstdio>
30#include <string>
30#include <gtest/gtest.h>31#include <gtest/gtest.h>
3132
32using namespace std;33using namespace std;
@@ -488,7 +489,23 @@
488TEST_F(MediaStoreTest, unmount) {489TEST_F(MediaStoreTest, unmount) {
489 MediaFile audio1 = MediaFileBuilder("/media/username/dir/fname.ogg")490 MediaFile audio1 = MediaFileBuilder("/media/username/dir/fname.ogg")
490 .setType(AudioMedia)491 .setType(AudioMedia)
491 .setTitle("bbb bbb");492 .setETag("etag")
493 .setContentType("audio/ogg")
494 .setTitle("bbb bbb")
495 .setDate("2015-01-01")
496 .setAuthor("artist")
497 .setAlbum("album")
498 .setAlbumArtist("album_artist")
499 .setGenre("genre")
500 .setDiscNumber(5)
501 .setTrackNumber(10)
502 .setDuration(42)
503 .setWidth(640)
504 .setHeight(480)
505 .setLatitude(8.0)
506 .setLongitude(4.0)
507 .setHasThumbnail(true)
508 .setModificationTime(10000);
492 MediaFile audio2 = MediaFileBuilder("/home/username/Music/fname.ogg")509 MediaFile audio2 = MediaFileBuilder("/home/username/Music/fname.ogg")
493 .setType(AudioMedia)510 .setType(AudioMedia)
494 .setTitle("bbb bbb");511 .setTitle("bbb bbb");
@@ -507,6 +524,12 @@
507 store.restoreItems("/media/username");524 store.restoreItems("/media/username");
508 result = store.query("bbb", AudioMedia, filter);525 result = store.query("bbb", AudioMedia, filter);
509 ASSERT_EQ(result.size(), 2);526 ASSERT_EQ(result.size(), 2);
527 std::sort(result.begin(), result.end(),
528 [](const MediaFile &m1, const MediaFile &m2) -> bool {
529 return m1.getFileName() < m2.getFileName();
530 });
531 EXPECT_EQ(result[0], audio2);
532 EXPECT_EQ(result[1], audio1);
510}533}
511534
512TEST_F(MediaStoreTest, utils) {535TEST_F(MediaStoreTest, utils) {

Subscribers

People subscribed via source and target branches