update schema, optimized db ops a bit

This commit is contained in:
DataHoarder 2022-02-01 11:49:35 +01:00
parent fd326df5e5
commit af4823f730
3 changed files with 21 additions and 13 deletions

View file

@ -116,6 +116,10 @@ func GetMimeTypeFromExtension(ext string) string {
//Text subtitles
case "lrc":
return "text/x-subtitle-lrc"
case "ssa":
return "text/x-subtitle-ssa"
case "ass":
return "text/x-subtitle-ass"
case "srt":
return "text/x-subtitle-subrip"
@ -170,6 +174,10 @@ func AddAudioToDatabase(release *database.Release, tx *PendingTransaction, size
log.Printf("handling %s", pathEntry)
defer tx.WaitGroup.Done()
if database.GetResourceFromDatabaseByPath(db, pathEntry) != nil {
return
}
f, err := OpenFileWithLimit(pathEntry)
if err != nil { //TODO
log.Panic(err)
@ -189,7 +197,7 @@ func AddAudioToDatabase(release *database.Release, tx *PendingTransaction, size
resourceCreationMutex.Lock()
var resource = database.GetResourceFromDatabaseByHash(db, hash)
var exists = resource != nil
var exists = resource != nil && len(resource.GetReleases()) > 0
if resource == nil {
//insert

View file

@ -15,7 +15,7 @@ Check its native dependencies that must be installed before usage.
## Test setup
```bash
# Create postgres database
# Create ephemeral postgres database
docker run --rm -p 5432:5432 --name metanoia-postgres \
-e POSTGRES_PASSWORD=metanoia -e POSTGRES_DB=metanoia -e POSTGRES_USER=metanoia -d \
postgres:14

View file

@ -8,15 +8,15 @@ CREATE TYPE artist_kind AS ENUM ('main', 'original', 'performer', 'composer', 'a
-- groups resources in specific releases / groupings
CREATE TABLE releases (
id BIGSERIAL PRIMARY KEY,
id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
identifiers TEXT[] NOT NULL DEFAULT '{}'::TEXT[], -- array of "semi-unique" identifiers, like site sources or identifiers
metadata jsonb NOT NULL DEFAULT '{}'::jsonb
);
CREATE INDEX idx_releases_identifiers_gin ON releases USING GIN (identifiers) INCLUDE (id);
CREATE INDEX idx_releases_identifiers_gin ON releases USING GIN (identifiers);
CREATE TABLE resources (
id BIGSERIAL PRIMARY KEY,
id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
hash bytea UNIQUE NOT NULL, -- hash of the resource, SHA256
size BIGINT NOT NULL,
path bytea, -- can be null if not available locally
@ -25,7 +25,7 @@ CREATE TABLE resources (
CREATE INDEX idx_resources_hash ON resources USING BTREE (hash); -- TODO: check if you can do partial prefix queries
CREATE INDEX idx_resources_path ON resources USING BTREE (path);
CREATE INDEX idx_resources_path_gin ON resources USING GIN (path gin_trgm_ops); -- Allows for partial path queries
CREATE INDEX idx_resources_path_gin ON resources USING GIN (path); -- Allows for partial path queries
CREATE TABLE resource_alternate_identifiers (
resource BIGINT NOT NULL,
@ -51,7 +51,7 @@ CREATE INDEX idx_resource_releases_resource ON resource_releases USING BTREE (re
CREATE INDEX idx_resource_releases_release ON resource_releases USING BTREE (release) INCLUDE (resource);
CREATE TABLE albums (
id SERIAL PRIMARY KEY,
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
cover BIGINT, -- can be null if not available
identifiers TEXT[] NOT NULL DEFAULT '{}'::TEXT[], -- array of "semi-unique" identifiers, like catalog number
metadata jsonb NOT NULL DEFAULT '{}'::jsonb,
@ -69,11 +69,11 @@ CREATE TABLE albums_names (
CONSTRAINT fk_album FOREIGN KEY (album) REFERENCES albums(id)
);
CREATE INDEX idx_albums_names_album ON albums_names USING BTREE (album);
CREATE INDEX idx_albums_names_name_gin ON albums_names USING GIN (name gin_trgm_ops) INCLUDE (album);
CREATE INDEX idx_albums_names_name_gin ON albums_names USING GIN (name gin_trgm_ops);
CREATE TABLE artists (
id SERIAL PRIMARY KEY,
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
metadata jsonb NOT NULL DEFAULT '{}'::jsonb
);
@ -85,10 +85,10 @@ CREATE TABLE artists_names (
CONSTRAINT fk_artist FOREIGN KEY (artist) REFERENCES artists(id)
);
CREATE INDEX idx_artists_names_artist ON artists_names USING BTREE (artist);
CREATE INDEX idx_artists_names_name_gin ON artists_names USING GIN (name gin_trgm_ops) INCLUDE (artist);
CREATE INDEX idx_artists_names_name_gin ON artists_names USING GIN (name gin_trgm_ops);
CREATE TABLE songs (
id BIGSERIAL PRIMARY KEY,
id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
resource BIGINT NOT NULL,
cover BIGINT, -- set to override album cover. can be null if not available
album INTEGER, -- can be null
@ -109,7 +109,7 @@ CREATE TABLE songs_names (
CONSTRAINT fk_song FOREIGN KEY (song) REFERENCES songs(id)
);
CREATE INDEX idx_songs_names_song ON songs_names USING BTREE (song);
CREATE INDEX idx_songs_names_name_gin ON songs_names USING GIN (name gin_trgm_ops) INCLUDE (song);
CREATE INDEX idx_songs_names_name_gin ON songs_names USING GIN (name gin_trgm_ops);
-- extra mapping table of artists <-> song
CREATE TABLE album_artists (
@ -140,7 +140,7 @@ CREATE INDEX idx_song_artists_artist ON song_artists USING BTREE (artist) INCLUD
-- CREATE INDEX idx_song_artists_artist_kind ON song_artists (artist, kind);
CREATE TABLE tags (
id SERIAL PRIMARY KEY,
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
name TEXT UNIQUE NOT NULL
);