Skip to content

Commit

Permalink
tracks need a total order for each release_id
Browse files Browse the repository at this point in the history
  • Loading branch information
clrnd committed Aug 27, 2015
1 parent 93733ca commit 292eb9d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
7 changes: 3 additions & 4 deletions sql/indexes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ ALTER TABLE ONLY release_format ADD CONSTRAINT release_format_pkey PRIMARY KEY (
--ALTER TABLE ONLY release_identifier ADD CONSTRAINT release_identifier_pkey PRIMARY KEY (release_id, ??);
ALTER TABLE ONLY release_label ADD CONSTRAINT release_label_pkey PRIMARY KEY (release_id, catno);
ALTER TABLE ONLY release_video ADD CONSTRAINT release_video_pkey PRIMARY KEY (release_id, src);
ALTER TABLE ONLY track ADD CONSTRAINT track_pkey PRIMARY KEY (release_id, position);
ALTER TABLE ONLY track_artist ADD CONSTRAINT track_artist_pkey PRIMARY KEY (track_position, release_id, artist_id);
ALTER TABLE ONLY track_extraartist ADD CONSTRAINT track_extraartist_pkey PRIMARY KEY (track_position, release_id, artist_id);
ALTER TABLE ONLY track ADD CONSTRAINT track_pkey PRIMARY KEY (release_id, idx);
ALTER TABLE ONLY track_artist ADD CONSTRAINT track_artist_pkey PRIMARY KEY (track_idx, release_id, artist_id);
ALTER TABLE ONLY track_extraartist ADD CONSTRAINT track_extraartist_pkey PRIMARY KEY (track_idx, release_id, artist_id);

--ALTER TABLE ONLY release_artist ADD CONSTRAINT FOREIGN KEY (release_id) REFERENCES release(id);
--ALTER TABLE ONLY release_company ADD CONSTRAINT FOREIGN KEY (release_id) REFERENCES release(id);
Expand All @@ -34,7 +34,6 @@ CREATE INDEX release_extraartist_id_idx ON release_extraartist (artist_id);
CREATE INDEX release_extraartist_releaseid_idx ON release_extraartist (release_id);
CREATE INDEX track_artist_id_idx ON track_artist (artist_id);
CREATE INDEX track_extraartist_id_idx ON track_extraartist (artist_id);
CREATE INDEX track_releaseid_idx ON track (release_id);
CREATE INDEX release_label_name_idx ON release_label (label);
CREATE INDEX release_label_catno_idx ON release_label (catno);
CREATE INDEX label_name_idx ON label (name);
Expand Down
7 changes: 4 additions & 3 deletions sql/tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,14 @@ CREATE TABLE release_company (

CREATE TABLE track (
release_id integer NOT NULL,
position text NOT NULL,
idx integer NOT NULL,
position text,
title text,
duration text
);

CREATE TABLE track_artist (
track_position text NOT NULL,
track_idx text NOT NULL,
release_id integer NOT NULL,
artist_id integer NOT NULL,
anv text,
Expand All @@ -110,7 +111,7 @@ CREATE TABLE track_artist (
);

CREATE TABLE track_extraartist (
track_position text NOT NULL,
track_idx text NOT NULL,
release_id integer NOT NULL,
artist_id integer NOT NULL,
anv text,
Expand Down
28 changes: 15 additions & 13 deletions src/Discogs/Release.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Data.List (foldl')
import Data.Foldable (foldMap)
import Text.XML.Expat.Tree (NodeG(..), UNode, isElement)
import Data.ByteString (ByteString)
import Data.ByteString.Char8 (pack)

import Discogs.ArtistRelation
import Discogs.Build
Expand Down Expand Up @@ -52,7 +53,8 @@ data ReleaseFormat = ReleaseFormat
deriving Show

data ReleaseTrack = ReleaseTrack
{ _reTrkTitle :: ByteString
{ _reTrkIdx :: ByteString
, _reTrkTitle :: ByteString
, _reTrkPosition :: ByteString
, _reTrkDuration :: ByteString
, _reTrkArtists :: [ArtistRelation]
Expand Down Expand Up @@ -102,12 +104,11 @@ releaseStore x = Store
, TableInfo "release_label" ["release_id", "label", "catno"]
, TableInfo "release_format" ["release_id", "format_name",
"format_text", "qty", "descriptions"]
, TableInfo "track" ["release_id", "title",
"position", "duration"]
, TableInfo "track_artist" ["track_position", "release_id",
, TableInfo "track" ["release_id", "idx", "title", "position", "duration"]
, TableInfo "track_artist" ["track_idx", "release_id",
"artist_id", "anv",
"join_relation", "role"]
, TableInfo "track_extraartist" ["track_position", "release_id",
, TableInfo "track_extraartist" ["track_idx", "release_id",
"artist_id", "anv",
"join_relation", "role"]
, TableInfo "release_identifier" ["release_id", "description",
Expand Down Expand Up @@ -147,12 +148,12 @@ instance Table Release where
[escape i, escape l', escape c']
mkFormat (ReleaseFormat n' t' q' ds') =
[escape i, escape n', escape t', escape q', escapeList ds']
mkTracks (ReleaseTrack t' p' d' _ _) =
[escape i, escape t', escape p', escape d']
mksTrackArtists (ReleaseTrack _ p' _ as' _) =
foldMap (escapeRow . (escape p' :) . mkArtist) as'
mksTrackExArtists (ReleaseTrack _ p' _ _ es') =
foldMap (escapeRow . (escape p' :) . mkArtist) es'
mkTracks (ReleaseTrack i' t' p' d' _ _) =
[escape i, escape i', escape t', escape p', escape d']
mksTrackArtists (ReleaseTrack i' _ _ _ as' _) =
foldMap (escapeRow . (escape i' :) . mkArtist) as'
mksTrackExArtists (ReleaseTrack i' _ _ _ _ es') =
foldMap (escapeRow . (escape i' :) . mkArtist) es'
mkIdentifier (ReleaseIdentifier d' t' v') =
[escape i, escape d', escape t', escape v']
mkVideo (ReleaseVideo d' s' t') =
Expand Down Expand Up @@ -253,9 +254,10 @@ parseRelease' (Element "companies" [] ns) = releaseCompanies .~ (map parseCompan
parseCompany' (Element "entity_type" [] txt) = reComEntityType .~ getTexts txt
parseCompany' (Element "entity_type_name" [] txt) = reComEntityName .~ getTexts txt
parseCompany' _ = id
parseRelease' (Element "tracklist" [] ns) = releaseTracks .~ (map parseTrack $ filter isElement ns)
parseRelease' (Element "tracklist" [] ns) = releaseTracks .~ (map parseTrack . zip ones . filter isElement $ ns)
where
parseTrack (Element "track" [] ns') = foldl' (flip parseTrack') (ReleaseTrack "" "" "" [] []) ns'
ones = [1..] :: [Int]
parseTrack (idx, (Element "track" [] ns')) = foldl' (flip parseTrack') (ReleaseTrack (pack $ show idx) "" "" "" [] []) ns'
parseTrack _ = undefined
parseTrack' (Element "position" [] txt) = reTrkPosition .~ getTexts txt
parseTrack' (Element "title" [] txt) = reTrkTitle .~ getTexts txt
Expand Down

0 comments on commit 292eb9d

Please sign in to comment.