diff --git a/package-lock.json b/package-lock.json index b96d1d9..b565766 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "coryd.dev", - "version": "2.0.2", + "version": "2.1.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "coryd.dev", - "version": "2.0.2", + "version": "2.1.2", "license": "MIT", "dependencies": { "html-minifier-terser": "7.2.0", diff --git a/package.json b/package.json index aaf89b4..8d5aff4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "coryd.dev", - "version": "2.0.2", + "version": "2.1.2", "description": "The source for my personal site. Built using 11ty (and other tools).", "type": "module", "engines": { diff --git a/queries/functions/parse_country_field.psql b/queries/functions/parse_country_field.psql index d399881..b2d9269 100644 --- a/queries/functions/parse_country_field.psql +++ b/queries/functions/parse_country_field.psql @@ -1,14 +1,21 @@ +CREATE OR REPLACE FUNCTION normalize_country_field(countryField TEXT) +RETURNS TEXT AS $$ DECLARE delimiters TEXT[] := ARRAY[',', '/', '&', 'and']; countries TEXT[]; + temp TEXT[]; result TEXT := ''; + i INTEGER; BEGIN countries := string_to_array(countryField, ','); - FOR i IN 1..array_length(delimiters, 1) LOOP - countries := array_cat(countries, string_to_array(result, delimiters[i])); + temp := ARRAY[]::TEXT[]; + FOREACH result IN ARRAY countries LOOP + temp := array_cat(temp, string_to_array(result, delimiters[i])); + END LOOP; + countries := temp; END LOOP; - result := array_to_string(countries, ' '); RETURN trim(result); -END +END; +$$ LANGUAGE plpgsql; diff --git a/queries/functions/search.psql b/queries/functions/search.psql index 374e9c7..4192803 100644 --- a/queries/functions/search.psql +++ b/queries/functions/search.psql @@ -1,4 +1,4 @@ -CREATE OR REPLACE FUNCTION public.search_optimized_index(search_query text, page_size integer, page_offset integer, types text[]) +CREATE OR REPLACE FUNCTION search_optimized_index(search_query text, page_size integer, page_offset integer, types text[]) RETURNS TABLE( result_id integer, url text, diff --git a/queries/functions/slugify.psql b/queries/functions/slugify.psql index 86bf1dd..e6ce8d8 100644 --- a/queries/functions/slugify.psql +++ b/queries/functions/slugify.psql @@ -1,4 +1,13 @@ -SELECT lower(regexp_replace( - unaccent(regexp_replace($1, '[^\\w\\s-]', '', 'g')), - '\\s+', '-', 'g' -)); +CREATE OR REPLACE FUNCTION slugify(input TEXT) +RETURNS TEXT AS $$ +BEGIN + RETURN lower( + regexp_replace( + unaccent( + regexp_replace(input, '[^\w\s-]', '', 'g') + ), + '\s+', '-', 'g' + ) + ); +END; +$$ LANGUAGE plpgsql IMMUTABLE; diff --git a/queries/functions/update_album_key.psql b/queries/functions/update_album_key.psql index 5bb5263..5da8434 100644 --- a/queries/functions/update_album_key.psql +++ b/queries/functions/update_album_key.psql @@ -1,5 +1,8 @@ +CREATE OR REPLACE FUNCTION update_album_key(old_album_key TEXT, new_album_key TEXT) +RETURNS void AS $$ BEGIN UPDATE listens SET album_key = new_album_key WHERE album_key = old_album_key; END; +$$ LANGUAGE plpgsql; diff --git a/queries/functions/update_artist_key.psql b/queries/functions/update_artist_key.psql new file mode 100644 index 0000000..cc386f2 --- /dev/null +++ b/queries/functions/update_artist_key.psql @@ -0,0 +1,11 @@ +CREATE OR REPLACE FUNCTION update_artist_name(old_artist_name TEXT, new_artist_name TEXT) +RETURNS void AS $$ +BEGIN + UPDATE listens + SET artist_name = new_artist_name + WHERE artist_name = old_artist_name; + UPDATE albums + SET artist_name = new_artist_name + WHERE artist_name = old_artist_name; +END; +$$ LANGUAGE plpgsql; diff --git a/queries/functions/update_listen_totals.psql b/queries/functions/update_listen_totals.psql index 7b78f3c..827ba33 100644 --- a/queries/functions/update_listen_totals.psql +++ b/queries/functions/update_listen_totals.psql @@ -1,6 +1,8 @@ +CREATE OR REPLACE FUNCTION update_listen_totals() +RETURNS void AS $$ BEGIN WITH artist_plays AS ( - SELECT artist_name, COUNT(*)::integer as total_plays + SELECT artist_name, COUNT(*)::integer AS total_plays FROM listens GROUP BY artist_name ) @@ -8,9 +10,8 @@ BEGIN SET total_plays = COALESCE(ap.total_plays, 0) FROM artist_plays ap WHERE artists.name_string = ap.artist_name; - WITH album_plays AS ( - SELECT album_key, artist_name, COUNT(*)::integer as total_plays + SELECT album_key, artist_name, COUNT(*)::integer AS total_plays FROM listens GROUP BY album_key, artist_name ) @@ -19,9 +20,8 @@ BEGIN FROM album_plays ap WHERE albums.key = ap.album_key AND albums.artist_name = ap.artist_name; - WITH genre_plays AS ( - SELECT g.id, COUNT(*)::integer as total_plays + SELECT g.id, COUNT(*)::integer AS total_plays FROM listens l JOIN artists a ON l.artist_name = a.name_string JOIN genres g ON a.genres::text = g.id::text @@ -31,6 +31,5 @@ BEGIN SET total_plays = COALESCE(gp.total_plays, 0) FROM genre_plays gp WHERE genres.id = gp.id; - - RAISE NOTICE 'All listen totals are up to date'; END; +$$ LANGUAGE plpgsql;