From a63f2de60ba287d0875daafd23fc629d2269bc93 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Fri, 6 Jun 2025 21:05:44 +0100 Subject: [PATCH] Use ->> instead of json_extract in queries Improves readability of queries. --- docs/database_structure.md | 3 ++- lib/music_library/collection.ex | 6 +++--- lib/music_library/records.ex | 2 +- .../20241202095949_create_artist_records_view.exs | 2 +- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/docs/database_structure.md b/docs/database_structure.md index ef848c93..d6779647 100644 --- a/docs/database_structure.md +++ b/docs/database_structure.md @@ -126,7 +126,7 @@ A view that extracts artist information from the embedded JSON in the records ta ```sql CREATE VIEW artist_records AS - SELECT json_extract(json_each.value, '$.musicbrainz_id') AS musicbrainz_id, + SELECT json_each.value ->> '$.musicbrainz_id' AS musicbrainz_id, records.id AS record_id, json_each.value as artist FROM records, @@ -134,6 +134,7 @@ CREATE VIEW artist_records AS ``` This view is crucial for querying artist information as it: + - Extracts individual artists from the embedded JSON array in the records table - Provides a normalized view of the artist-record relationships - Makes it easier to query records by artist diff --git a/lib/music_library/collection.ex b/lib/music_library/collection.ex index 868dc27c..2c68ecad 100644 --- a/lib/music_library/collection.ex +++ b/lib/music_library/collection.ex @@ -78,11 +78,11 @@ defmodule MusicLibrary.Collection do q = from r in fragment("records, json_each(records.artists)"), where: fragment("records.purchased_at IS NOT NULL"), - group_by: fragment("json_extract(?, '$.name')", r.value), + group_by: fragment("? ->> '$.name'", r.value), order_by: [desc: fragment("count(1)")], select: %{ - id: fragment("json_extract(?, '$.musicbrainz_id')", r.value), - name: fragment("json_extract(?, '$.name')", r.value), + id: fragment("? ->> '$.musicbrainz_id'", r.value), + name: fragment("? ->> '$.name'", r.value), count: fragment("count(1)") }, limit: ^limit diff --git a/lib/music_library/records.ex b/lib/music_library/records.ex index 432e07c0..a98863fc 100644 --- a/lib/music_library/records.ex +++ b/lib/music_library/records.ex @@ -50,7 +50,7 @@ defmodule MusicLibrary.Records do defmacro order_alphabetically do quote do fragment( - "unaccent(json_extract(artists, '$[0].sort_name')) COLLATE NOCASE ASC, unaccent(title) COLLATE NOCASE ASC" + "unaccent(artists ->> '$[0].sort_name') COLLATE NOCASE ASC, unaccent(title) COLLATE NOCASE ASC" ) end end diff --git a/priv/repo/migrations/20241202095949_create_artist_records_view.exs b/priv/repo/migrations/20241202095949_create_artist_records_view.exs index 6a7d9bf0..b4327c9c 100644 --- a/priv/repo/migrations/20241202095949_create_artist_records_view.exs +++ b/priv/repo/migrations/20241202095949_create_artist_records_view.exs @@ -4,7 +4,7 @@ defmodule MusicLibrary.Repo.Migrations.CreateArtistRecordsView do def up do execute """ CREATE VIEW artist_records AS - SELECT json_extract(json_each.value, '$.musicbrainz_id') AS musicbrainz_id, + SELECT json_each.value ->> '$.musicbrainz_id' AS musicbrainz_id, records.id AS record_id, json_each.value as artist FROM records,