Commit 7f19fbfd authored by Lukáš Lalinský's avatar Lukáš Lalinský

Remember merged track IDs

parent 36a7bace
......@@ -58,7 +58,6 @@ def insert_fingerprint(conn, data):
'fingerprint': data['fingerprint'],
'length': data['length'],
'bitrate': data.get('bitrate'),
'source_id': data.get('source_id'),
'format_id': data.get('format_id'),
'track_id': data['track_id'],
'meta_id': data.get('meta_id'),
......
......@@ -23,9 +23,9 @@ def insert_submission(conn, data):
'fingerprint': data['fingerprint'],
'length': data['length'],
'bitrate': data.get('bitrate'),
'source_id': data['source_id'],
'mbid': data.get('mbid'),
'puid': data.get('puid'),
'source_id': data.get('source_id'),
'format_id': data.get('format_id'),
'meta_id': data.get('meta_id'),
})
......@@ -65,7 +65,6 @@ def import_submission(conn, submission):
'fingerprint': submission['fingerprint'],
'length': submission['length'],
'bitrate': submission['bitrate'],
'source_id': submission['source_id'],
'format_id': submission['format_id'],
'meta_id': submission['meta_id'],
}
......
......@@ -100,9 +100,10 @@ def merge_tracks(conn, target_id, source_ids):
delete_stmt = schema.track_mbid.delete().where(
schema.track_mbid.c.track_id.in_(source_ids))
conn.execute(delete_stmt)
delete_track_stmt = schema.track.delete().where(
schema.track.c.id.in_(source_ids))
conn.execute(delete_track_stmt)
update_stmt = schema.track.update().where(
sql.or_(schema.track.c.id.in_(source_ids),
schema.track.c.new_id.in_(source_ids)))
conn.execute(update_stmt.values(new_id=target_id))
def insert_track(conn):
......
......@@ -31,6 +31,7 @@ application = Table('application', metadata,
track = Table('track', metadata,
Column('id', Integer, primary_key=True),
Column('created', DateTime),
Column('new_id', Integer),
)
format = Table('format', metadata,
......@@ -88,7 +89,6 @@ fingerprint = Table('fingerprint', metadata,
Column('fingerprint', ARRAY(Integer)),
Column('length', Integer),
Column('bitrate', Integer),
Column('source_id', Integer, ForeignKey('source.id')),
Column('format_id', Integer, ForeignKey('format.id')),
Column('track_id', Integer, ForeignKey('track.id')),
Column('meta_id', Integer, ForeignKey('meta.id')),
......
......@@ -26,6 +26,10 @@ ALTER TABLE fingerprint ADD CONSTRAINT fingerprint_fk_track_id
FOREIGN KEY (track_id)
REFERENCES track (id);
ALTER TABLE track ADD CONSTRAINT track_fk_new_id
FOREIGN KEY (new_id)
REFERENCES track (id);
ALTER TABLE track_mbid ADD CONSTRAINT track_mbid_fk_track_id
FOREIGN KEY (track_id)
REFERENCES track (id);
......
......@@ -49,7 +49,6 @@ CREATE TABLE fingerprint (
bitrate smallint CHECK (bitrate > 0),
format_id int,
created timestamp with time zone NOT NULL DEFAULT current_timestamp,
source_id int NOT NULL,
track_id int NOT NULL,
meta_id int,
hash_full bytea,
......@@ -64,6 +63,7 @@ CREATE TABLE fingerprint_index_queue (
CREATE TABLE track (
id serial NOT NULL,
created timestamp with time zone DEFAULT current_timestamp
new_id int,
);
CREATE TABLE track_mbid (
......
......@@ -6,6 +6,7 @@ ALTER TABLE submission ADD CONSTRAINT submission_fk_fingerprint_id
ALTER TABLE fingerprint ADD submission_count int;
ALTER TABLE fingerprint DROP COLUMN submission_id;
ALTER TABLE fingerprint DROP COLUMN source_id;
ALTER TABLE track_mbid ADD submission_count int ;
ALTER TABLE track_mbid DROP COLUMN submission_id;
......@@ -18,3 +19,7 @@ CREATE TABLE track_puid (
ALTER TABLE track_puid ADD CONSTRAINT track_puid_pkey PRIMARY KEY (track_id, puid);
ALTER TABLE track_puid ADD CONSTRAINT track_puid_fk_track_id FOREIGN KEY (track_id) REFERENCES track (id);
CREATE INDEX track_puid_idx_puid ON track_puid (puid);
ALTER TABLE track ADD new_id int;
ALTER TABLE track ADD CONSTRAINT track_fk_new_id FOREIGN KEY (new_id) REFERENCES track (id);
......@@ -93,8 +93,8 @@ def test_lookup_handler(conn):
assert_equals('200 OK', resp.status)
# one exact match
prepare_database(conn, """
INSERT INTO fingerprint (length, fingerprint, source_id, track_id)
VALUES (%s, %s, 1, 1);
INSERT INTO fingerprint (length, fingerprint, track_id)
VALUES (%s, %s, 1);
""", (TEST_1_LENGTH, TEST_1_FP_RAW))
handler = LookupHandler(connect=provider(conn))
resp = handler.handle(Request(builder.get_environ()))
......
......@@ -147,8 +147,8 @@ def test_lookup_handler(conn):
assert_equals('200 OK', resp.status)
# one exact match
prepare_database(conn, """
INSERT INTO fingerprint (length, fingerprint, source_id, track_id)
VALUES (%s, %s, 1, 1);
INSERT INTO fingerprint (length, fingerprint, track_id)
VALUES (%s, %s, 1);
""", (TEST_1_LENGTH, TEST_1_FP_RAW))
handler = LookupHandler(connect=provider(conn))
resp = handler.handle(Request(builder.get_environ()))
......@@ -236,8 +236,8 @@ INSERT INTO fingerprint (length, fingerprint, source_id, track_id)
assert_equals('200 OK', resp.status)
# duplicate fingerprint
prepare_database(conn, """
INSERT INTO fingerprint (length, fingerprint, source_id, track_id)
VALUES (%s, %s, 1, 1);
INSERT INTO fingerprint (length, fingerprint, track_id)
VALUES (%s, %s, 1);
""", (TEST_1_LENGTH, TEST_1_FP_RAW))
values = {'format': 'json', 'client': 'app1key', 'duration': str(TEST_1_LENGTH), 'fingerprint': TEST_1_FP}
builder = EnvironBuilder(method='POST', data=values)
......
......@@ -18,11 +18,11 @@ def test_insert_fingerprint(conn):
})
assert_equals(1, id)
rows = conn.execute("""
SELECT fingerprint, length, bitrate, source_id, format_id, track_id
SELECT fingerprint, length, bitrate, format_id, track_id
FROM fingerprint WHERE id=%s
""", (id,)).fetchall()
expected_rows = [
([1,2,3,4,5,6], 123, 192, 1, 1, 2),
([1,2,3,4,5,6], 123, 192, 1, 2),
]
assert_equals(expected_rows, rows)
......@@ -33,11 +33,11 @@ def test_insert_submission(conn):
})
assert_equals(1, id)
rows = conn.execute("""
SELECT fingerprint, length, bitrate, source_id, format_id
SELECT fingerprint, length, bitrate, format_id
FROM submission WHERE id=%s
""", (id,)).fetchall()
expected_rows = [
([1,2,3,4,5,6], 123, 192, 1, 1),
([1,2,3,4,5,6], 123, 192, 1),
]
assert_equals(expected_rows, rows)
......@@ -128,8 +128,8 @@ def test_import_submission(conn):
@with_database
def test_import_submission_reuse_fingerprint_97(conn):
prepare_database(conn, """
INSERT INTO fingerprint (fingerprint, length, source_id, track_id)
VALUES (%(fp)s, %(len)s, 1, 1);
INSERT INTO fingerprint (fingerprint, length, track_id)
VALUES (%(fp)s, %(len)s, 1);
""", dict(fp=TEST_1A_FP_RAW, len=TEST_1A_LENGTH))
id = insert_submission(conn, {
'fingerprint': TEST_1B_FP_RAW,
......@@ -149,8 +149,8 @@ def test_import_submission_reuse_fingerprint_97(conn):
@with_database
def test_import_submission_reuse_fingerprint_100(conn):
prepare_database(conn, """
INSERT INTO fingerprint (fingerprint, length, source_id, track_id)
VALUES (%(fp)s, %(len)s, 1, 1);
INSERT INTO fingerprint (fingerprint, length, track_id)
VALUES (%(fp)s, %(len)s, 1);
""", dict(fp=TEST_1A_FP_RAW, len=TEST_1A_LENGTH))
id = insert_submission(conn, {
'fingerprint': TEST_1A_FP_RAW,
......@@ -170,8 +170,8 @@ def test_import_submission_reuse_fingerprint_100(conn):
@with_database
def test_import_submission_reuse_track_93(conn):
prepare_database(conn, """
INSERT INTO fingerprint (fingerprint, length, source_id, track_id)
VALUES (%(fp)s, %(len)s, 1, 1);
INSERT INTO fingerprint (fingerprint, length, track_id)
VALUES (%(fp)s, %(len)s, 1);
""", dict(fp=TEST_1A_FP_RAW, len=TEST_1A_LENGTH))
id = insert_submission(conn, {
'fingerprint': TEST_1C_FP_RAW,
......@@ -191,8 +191,8 @@ def test_import_submission_reuse_track_93(conn):
@with_database
def test_import_submission_new_track_55(conn):
prepare_database(conn, """
INSERT INTO fingerprint (fingerprint, length, source_id, track_id)
VALUES (%(fp)s, %(len)s, 1, 1);
INSERT INTO fingerprint (fingerprint, length, track_id)
VALUES (%(fp)s, %(len)s, 1);
""", dict(fp=TEST_1A_FP_RAW, len=TEST_1A_LENGTH))
id = insert_submission(conn, {
'fingerprint': TEST_1D_FP_RAW,
......@@ -212,8 +212,8 @@ def test_import_submission_new_track_55(conn):
@with_database
def test_import_submission_new_track_different(conn):
prepare_database(conn, """
INSERT INTO fingerprint (fingerprint, length, source_id, track_id)
VALUES (%(fp)s, %(len)s, 1, 1);
INSERT INTO fingerprint (fingerprint, length, track_id)
VALUES (%(fp)s, %(len)s, 1);
""", dict(fp=TEST_1A_FP_RAW, len=TEST_1A_LENGTH))
id = insert_submission(conn, {
'fingerprint': TEST_2_FP_RAW,
......@@ -233,8 +233,8 @@ def test_import_submission_new_track_different(conn):
@with_database
def test_import_submission_merge_existing_tracks(conn):
prepare_database(conn, """
INSERT INTO fingerprint (fingerprint, length, source_id, track_id)
VALUES (%(fp1)s, %(len1)s, 1, 1), (%(fp2)s, %(len2)s, 1, 2);
INSERT INTO fingerprint (fingerprint, length, track_id)
VALUES (%(fp1)s, %(len1)s, 1), (%(fp2)s, %(len2)s, 2);
""", dict(fp1=TEST_1A_FP_RAW, len1=TEST_1A_LENGTH,
fp2=TEST_1B_FP_RAW, len2=TEST_1B_LENGTH))
id = insert_submission(conn, {
......@@ -255,7 +255,7 @@ def test_import_submission_merge_existing_tracks(conn):
assert_equals(2, fingerprint['track_id'])
query = tables.track.select(tables.track.c.id == 1)
track = conn.execute(query).fetchone()
assert_equals(None, track)
assert_equals(2, track['new_id'])
@with_database
......
......@@ -61,8 +61,8 @@ def test_insert_track(conn):
@with_database
def test_merge_tracks(conn):
prepare_database(conn, """
INSERT INTO fingerprint (fingerprint, length, track_id, source_id)
VALUES (%(fp1)s, %(len1)s, 1, 1), (%(fp2)s, %(len2)s, 2, 1);
INSERT INTO fingerprint (fingerprint, length, track_id)
VALUES (%(fp1)s, %(len1)s, 1), (%(fp2)s, %(len2)s, 2);
INSERT INTO track_mbid (track_id, mbid) VALUES (1, '97edb73c-4dac-11e0-9096-0025225356f3');
INSERT INTO track_mbid (track_id, mbid) VALUES (1, 'd575d506-4da4-11e0-b951-0025225356f3');
INSERT INTO track_mbid (track_id, mbid) VALUES (2, 'd575d506-4da4-11e0-b951-0025225356f3');
......@@ -88,9 +88,9 @@ INSERT INTO track_mbid (track_id, mbid) VALUES (4, '5d0290a6-4dad-11e0-a47a-0025
@with_database
def test_track_fingerprint_matrix(conn):
prepare_database(conn, """
INSERT INTO fingerprint (fingerprint, length, track_id, source_id)
VALUES (%(fp1)s, %(len1)s, 1, 1), (%(fp2)s, %(len2)s, 1, 1),
(%(fp3)s, %(len3)s, 1, 1);
INSERT INTO fingerprint (fingerprint, length, track_id)
VALUES (%(fp1)s, %(len1)s, 1), (%(fp2)s, %(len2)s, 1),
(%(fp3)s, %(len3)s, 1);
""", dict(fp1=TEST_1A_FP_RAW, len1=TEST_1A_LENGTH,
fp2=TEST_1B_FP_RAW, len2=TEST_1B_LENGTH,
fp3=TEST_1C_FP_RAW, len3=TEST_1C_LENGTH))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment