mirror of
https://github.com/rommapp/romm.git
synced 2026-11-30 02:22:44 +01:00
fix: generate virtual collections from all metadata sources
The virtual_collections view read only roms.igdb_metadata, so franchises, genres, companies and play modes set manually or provided by non-IGDB sources (ScreenScraper, Flashpoint, gamelist) never produced auto-generated collections, even though the same values already drove /api/roms filtering and the game detail page. Redefine the view to source each grouping from the roms_metadata view, which coalesces manual and provider metadata in priority order. Matching values now merge into a single collection regardless of their source. The 3-ROM minimum threshold is unchanged. Fixes #3187 Generated-By: PostHog Code Task-Id: 10289bf4-b0d0-4856-80c9-1be93c926f37
This commit is contained in:
401
backend/alembic/versions/0095_virtual_collections_source.py
Normal file
401
backend/alembic/versions/0095_virtual_collections_source.py
Normal file
@@ -0,0 +1,401 @@
|
||||
"""Source virtual_collections view from roms_metadata (manual + all providers)
|
||||
|
||||
Revision ID: 0095_virtual_collections_source
|
||||
Revises: 0094_track_meta_table
|
||||
Create Date: 2026-07-06 12:00:00.000000
|
||||
|
||||
"""
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
from utils.database import is_postgresql
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "0095_virtual_collections_source"
|
||||
down_revision = "0094_track_meta_table"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
connection = op.get_bind()
|
||||
|
||||
if is_postgresql(connection):
|
||||
connection.execute(
|
||||
sa.text("""
|
||||
CREATE OR REPLACE VIEW virtual_collections AS
|
||||
WITH genres_collection AS (
|
||||
SELECT
|
||||
r.id as rom_id,
|
||||
r.path_cover_s as path_cover_s,
|
||||
r.path_cover_l as path_cover_l,
|
||||
jsonb_array_elements_text(rm.genres) as collection_name,
|
||||
'genre' as collection_type
|
||||
FROM roms r
|
||||
JOIN roms_metadata rm ON rm.rom_id = r.id
|
||||
),
|
||||
franchises_collection AS (
|
||||
SELECT
|
||||
r.id as rom_id,
|
||||
r.path_cover_s as path_cover_s,
|
||||
r.path_cover_l as path_cover_l,
|
||||
jsonb_array_elements_text(rm.franchises) as collection_name,
|
||||
'franchise' as collection_type
|
||||
FROM roms r
|
||||
JOIN roms_metadata rm ON rm.rom_id = r.id
|
||||
),
|
||||
collection_collection AS (
|
||||
SELECT
|
||||
r.id as rom_id,
|
||||
r.path_cover_s as path_cover_s,
|
||||
r.path_cover_l as path_cover_l,
|
||||
jsonb_array_elements_text(rm.collections) as collection_name,
|
||||
'collection' as collection_type
|
||||
FROM roms r
|
||||
JOIN roms_metadata rm ON rm.rom_id = r.id
|
||||
),
|
||||
modes_collection AS (
|
||||
SELECT
|
||||
r.id as rom_id,
|
||||
r.path_cover_s as path_cover_s,
|
||||
r.path_cover_l as path_cover_l,
|
||||
jsonb_array_elements_text(rm.game_modes) as collection_name,
|
||||
'mode' as collection_type
|
||||
FROM roms r
|
||||
JOIN roms_metadata rm ON rm.rom_id = r.id
|
||||
),
|
||||
companies_collection AS (
|
||||
SELECT
|
||||
r.id as rom_id,
|
||||
r.path_cover_s as path_cover_s,
|
||||
r.path_cover_l as path_cover_l,
|
||||
jsonb_array_elements_text(rm.companies) as collection_name,
|
||||
'company' as collection_type
|
||||
FROM roms r
|
||||
JOIN roms_metadata rm ON rm.rom_id = r.id
|
||||
)
|
||||
SELECT
|
||||
collection_name as name,
|
||||
collection_type as type,
|
||||
'Autogenerated ' || collection_type || ' collection' AS description,
|
||||
NOW() AS created_at,
|
||||
NOW() AS updated_at,
|
||||
jsonb_agg(rom_id) as rom_ids,
|
||||
jsonb_agg(path_cover_s) as path_covers_s,
|
||||
jsonb_agg(path_cover_l) as path_covers_l
|
||||
FROM (
|
||||
SELECT * FROM genres_collection
|
||||
UNION ALL
|
||||
SELECT * FROM franchises_collection
|
||||
UNION ALL
|
||||
SELECT * FROM collection_collection
|
||||
UNION ALL
|
||||
SELECT * FROM modes_collection
|
||||
UNION ALL
|
||||
SELECT * FROM companies_collection
|
||||
) combined
|
||||
GROUP BY collection_type, collection_name
|
||||
HAVING COUNT(DISTINCT rom_id) > 2
|
||||
ORDER BY collection_type, collection_name;
|
||||
"""), # nosec B608
|
||||
)
|
||||
else:
|
||||
connection.execute(
|
||||
sa.text("""
|
||||
CREATE OR REPLACE VIEW virtual_collections AS
|
||||
WITH genres AS (
|
||||
SELECT
|
||||
r.id as rom_id,
|
||||
r.path_cover_s as path_cover_s,
|
||||
r.path_cover_l as path_cover_l,
|
||||
CONCAT(j.genre) as collection_name,
|
||||
'genre' as collection_type
|
||||
FROM
|
||||
roms r
|
||||
JOIN roms_metadata rm ON rm.rom_id = r.id
|
||||
CROSS JOIN JSON_TABLE(
|
||||
rm.genres,
|
||||
'$[*]' COLUMNS (genre VARCHAR(255) PATH '$')
|
||||
) j
|
||||
),
|
||||
franchises AS (
|
||||
SELECT
|
||||
r.id as rom_id,
|
||||
r.path_cover_s as path_cover_s,
|
||||
r.path_cover_l as path_cover_l,
|
||||
CONCAT(j.franchise) as collection_name,
|
||||
'franchise' as collection_type
|
||||
FROM
|
||||
roms r
|
||||
JOIN roms_metadata rm ON rm.rom_id = r.id
|
||||
CROSS JOIN JSON_TABLE(
|
||||
rm.franchises,
|
||||
'$[*]' COLUMNS (franchise VARCHAR(255) PATH '$')
|
||||
) j
|
||||
),
|
||||
collections AS (
|
||||
SELECT
|
||||
r.id as rom_id,
|
||||
r.path_cover_s as path_cover_s,
|
||||
r.path_cover_l as path_cover_l,
|
||||
CONCAT(j.collection) as collection_name,
|
||||
'collection' as collection_type
|
||||
FROM
|
||||
roms r
|
||||
JOIN roms_metadata rm ON rm.rom_id = r.id
|
||||
CROSS JOIN JSON_TABLE(
|
||||
rm.collections,
|
||||
'$[*]' COLUMNS (collection VARCHAR(255) PATH '$')
|
||||
) j
|
||||
),
|
||||
modes AS (
|
||||
SELECT
|
||||
r.id as rom_id,
|
||||
r.path_cover_s as path_cover_s,
|
||||
r.path_cover_l as path_cover_l,
|
||||
CONCAT(j.mode) as collection_name,
|
||||
'mode' as collection_type
|
||||
FROM
|
||||
roms r
|
||||
JOIN roms_metadata rm ON rm.rom_id = r.id
|
||||
CROSS JOIN JSON_TABLE(
|
||||
rm.game_modes,
|
||||
'$[*]' COLUMNS (mode VARCHAR(255) PATH '$')
|
||||
) j
|
||||
),
|
||||
companies AS (
|
||||
SELECT
|
||||
r.id as rom_id,
|
||||
r.path_cover_s as path_cover_s,
|
||||
r.path_cover_l as path_cover_l,
|
||||
CONCAT(j.company) as collection_name,
|
||||
'company' as collection_type
|
||||
FROM
|
||||
roms r
|
||||
JOIN roms_metadata rm ON rm.rom_id = r.id
|
||||
CROSS JOIN JSON_TABLE(
|
||||
rm.companies,
|
||||
'$[*]' COLUMNS (company VARCHAR(255) PATH '$')
|
||||
) j
|
||||
)
|
||||
SELECT
|
||||
collection_name as name,
|
||||
collection_type as type,
|
||||
CONCAT('Autogenerated ', collection_name, ' collection') AS description,
|
||||
NOW() AS created_at,
|
||||
NOW() AS updated_at,
|
||||
JSON_ARRAYAGG(rom_id) as rom_ids,
|
||||
JSON_ARRAYAGG(path_cover_s) as path_covers_s,
|
||||
JSON_ARRAYAGG(path_cover_l) as path_covers_l
|
||||
FROM
|
||||
(
|
||||
SELECT * FROM genres
|
||||
UNION ALL
|
||||
SELECT * FROM franchises
|
||||
UNION ALL
|
||||
SELECT * FROM collections
|
||||
UNION ALL
|
||||
SELECT * FROM modes
|
||||
UNION ALL
|
||||
SELECT * FROM companies
|
||||
) combined
|
||||
GROUP BY collection_type, collection_name
|
||||
HAVING COUNT(DISTINCT rom_id) > 2
|
||||
ORDER BY collection_type, collection_name;
|
||||
"""),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
connection = op.get_bind()
|
||||
|
||||
if is_postgresql(connection):
|
||||
connection.execute(
|
||||
sa.text("""
|
||||
CREATE OR REPLACE VIEW virtual_collections AS
|
||||
WITH genres_collection AS (
|
||||
SELECT
|
||||
r.id as rom_id,
|
||||
r.path_cover_s as path_cover_s,
|
||||
r.path_cover_l as path_cover_l,
|
||||
jsonb_array_elements_text(igdb_metadata -> 'genres') as collection_name,
|
||||
'genre' as collection_type
|
||||
FROM roms r
|
||||
WHERE igdb_metadata->'genres' IS NOT NULL
|
||||
),
|
||||
franchises_collection AS (
|
||||
SELECT
|
||||
r.id as rom_id,
|
||||
r.path_cover_s as path_cover_s,
|
||||
r.path_cover_l as path_cover_l,
|
||||
jsonb_array_elements_text(igdb_metadata -> 'franchises') as collection_name,
|
||||
'franchise' as collection_type
|
||||
FROM roms r
|
||||
WHERE igdb_metadata->'franchises' IS NOT NULL
|
||||
),
|
||||
collection_collection AS (
|
||||
SELECT
|
||||
r.id as rom_id,
|
||||
r.path_cover_s as path_cover_s,
|
||||
r.path_cover_l as path_cover_l,
|
||||
jsonb_array_elements_text(igdb_metadata -> 'collections') as collection_name,
|
||||
'collection' as collection_type
|
||||
FROM roms r
|
||||
WHERE igdb_metadata->'collections' IS NOT NULL
|
||||
),
|
||||
modes_collection AS (
|
||||
SELECT
|
||||
r.id as rom_id,
|
||||
r.path_cover_s as path_cover_s,
|
||||
r.path_cover_l as path_cover_l,
|
||||
jsonb_array_elements_text(igdb_metadata -> 'game_modes') as collection_name,
|
||||
'mode' as collection_type
|
||||
FROM roms r
|
||||
WHERE igdb_metadata->'game_modes' IS NOT NULL
|
||||
),
|
||||
companies_collection AS (
|
||||
SELECT
|
||||
r.id as rom_id,
|
||||
r.path_cover_s as path_cover_s,
|
||||
r.path_cover_l as path_cover_l,
|
||||
jsonb_array_elements_text(igdb_metadata -> 'companies') as collection_name,
|
||||
'company' as collection_type
|
||||
FROM roms r
|
||||
WHERE igdb_metadata->'companies' IS NOT NULL
|
||||
)
|
||||
SELECT
|
||||
collection_name as name,
|
||||
collection_type as type,
|
||||
'Autogenerated ' || collection_type || ' collection' AS description,
|
||||
NOW() AS created_at,
|
||||
NOW() AS updated_at,
|
||||
jsonb_agg(rom_id) as rom_ids,
|
||||
jsonb_agg(path_cover_s) as path_covers_s,
|
||||
jsonb_agg(path_cover_l) as path_covers_l
|
||||
FROM (
|
||||
SELECT * FROM genres_collection
|
||||
UNION ALL
|
||||
SELECT * FROM franchises_collection
|
||||
UNION ALL
|
||||
SELECT * FROM collection_collection
|
||||
UNION ALL
|
||||
SELECT * FROM modes_collection
|
||||
UNION ALL
|
||||
SELECT * FROM companies_collection
|
||||
) combined
|
||||
GROUP BY collection_type, collection_name
|
||||
HAVING COUNT(DISTINCT rom_id) > 2
|
||||
ORDER BY collection_type, collection_name;
|
||||
"""), # nosec B608
|
||||
)
|
||||
else:
|
||||
connection.execute(
|
||||
sa.text("""
|
||||
CREATE OR REPLACE VIEW virtual_collections AS
|
||||
WITH genres AS (
|
||||
SELECT
|
||||
r.id as rom_id,
|
||||
r.path_cover_s as path_cover_s,
|
||||
r.path_cover_l as path_cover_l,
|
||||
CONCAT(j.genre) as collection_name,
|
||||
'genre' as collection_type
|
||||
FROM
|
||||
roms r
|
||||
CROSS JOIN JSON_TABLE(
|
||||
JSON_EXTRACT(igdb_metadata, '$.genres'),
|
||||
'$[*]' COLUMNS (genre VARCHAR(255) PATH '$')
|
||||
) j
|
||||
WHERE
|
||||
JSON_EXTRACT(igdb_metadata, '$.genres') IS NOT NULL
|
||||
),
|
||||
franchises AS (
|
||||
SELECT
|
||||
r.id as rom_id,
|
||||
r.path_cover_s as path_cover_s,
|
||||
r.path_cover_l as path_cover_l,
|
||||
CONCAT(j.franchise) as collection_name,
|
||||
'franchise' as collection_type
|
||||
FROM
|
||||
roms r
|
||||
CROSS JOIN JSON_TABLE(
|
||||
JSON_EXTRACT(igdb_metadata, '$.franchises'),
|
||||
'$[*]' COLUMNS (franchise VARCHAR(255) PATH '$')
|
||||
) j
|
||||
WHERE
|
||||
JSON_EXTRACT(igdb_metadata, '$.franchises') IS NOT NULL
|
||||
),
|
||||
collections AS (
|
||||
SELECT
|
||||
r.id as rom_id,
|
||||
r.path_cover_s as path_cover_s,
|
||||
r.path_cover_l as path_cover_l,
|
||||
CONCAT(j.collection) as collection_name,
|
||||
'collection' as collection_type
|
||||
FROM
|
||||
roms r
|
||||
CROSS JOIN JSON_TABLE(
|
||||
JSON_EXTRACT(igdb_metadata, '$.collections'),
|
||||
'$[*]' COLUMNS (collection VARCHAR(255) PATH '$')
|
||||
) j
|
||||
WHERE
|
||||
JSON_EXTRACT(igdb_metadata, '$.collections') IS NOT NULL
|
||||
),
|
||||
modes AS (
|
||||
SELECT
|
||||
r.id as rom_id,
|
||||
r.path_cover_s as path_cover_s,
|
||||
r.path_cover_l as path_cover_l,
|
||||
CONCAT(j.mode) as collection_name,
|
||||
'mode' as collection_type
|
||||
FROM
|
||||
roms r
|
||||
CROSS JOIN JSON_TABLE(
|
||||
JSON_EXTRACT(igdb_metadata, '$.game_modes'),
|
||||
'$[*]' COLUMNS (mode VARCHAR(255) PATH '$')
|
||||
) j
|
||||
WHERE
|
||||
JSON_EXTRACT(igdb_metadata, '$.game_modes') IS NOT NULL
|
||||
),
|
||||
companies AS (
|
||||
SELECT
|
||||
r.id as rom_id,
|
||||
r.path_cover_s as path_cover_s,
|
||||
r.path_cover_l as path_cover_l,
|
||||
CONCAT(j.company) as collection_name,
|
||||
'company' as collection_type
|
||||
FROM
|
||||
roms r
|
||||
CROSS JOIN JSON_TABLE(
|
||||
JSON_EXTRACT(igdb_metadata, '$.companies'),
|
||||
'$[*]' COLUMNS (company VARCHAR(255) PATH '$')
|
||||
) j
|
||||
WHERE
|
||||
JSON_EXTRACT(igdb_metadata, '$.companies') IS NOT NULL
|
||||
)
|
||||
SELECT
|
||||
collection_name as name,
|
||||
collection_type as type,
|
||||
CONCAT('Autogenerated ', collection_name, ' collection') AS description,
|
||||
NOW() AS created_at,
|
||||
NOW() AS updated_at,
|
||||
JSON_ARRAYAGG(rom_id) as rom_ids,
|
||||
JSON_ARRAYAGG(path_cover_s) as path_covers_s,
|
||||
JSON_ARRAYAGG(path_cover_l) as path_covers_l
|
||||
FROM
|
||||
(
|
||||
SELECT * FROM genres
|
||||
UNION ALL
|
||||
SELECT * FROM franchises
|
||||
UNION ALL
|
||||
SELECT * FROM collections
|
||||
UNION ALL
|
||||
SELECT * FROM modes
|
||||
UNION ALL
|
||||
SELECT * FROM companies
|
||||
) combined
|
||||
GROUP BY collection_type, collection_name
|
||||
HAVING COUNT(DISTINCT rom_id) > 2
|
||||
ORDER BY collection_type, collection_name;
|
||||
"""),
|
||||
)
|
||||
@@ -1,8 +1,54 @@
|
||||
from handler.database import db_collection_handler
|
||||
from handler.database import db_collection_handler, db_rom_handler
|
||||
from models.collection import SmartCollection
|
||||
from models.platform import Platform
|
||||
from models.rom import Rom
|
||||
from models.user import User
|
||||
|
||||
|
||||
def _add_rom(platform: Platform, index: int, manual_metadata: dict) -> Rom:
|
||||
rom = Rom(
|
||||
platform_id=platform.id,
|
||||
name=f"manual_rom_{index}",
|
||||
slug=f"manual_rom_{index}",
|
||||
fs_name=f"manual_rom_{index}.zip",
|
||||
fs_name_no_tags=f"manual_rom_{index}",
|
||||
fs_name_no_ext=f"manual_rom_{index}",
|
||||
fs_extension="zip",
|
||||
fs_path=f"{platform.slug}/roms",
|
||||
manual_metadata=manual_metadata,
|
||||
)
|
||||
return db_rom_handler.add_rom(rom)
|
||||
|
||||
|
||||
def test_get_virtual_collections_includes_manual_franchises(
|
||||
platform: Platform, admin_user: User
|
||||
):
|
||||
# The virtual_collections view sources from roms_metadata, so manually-set
|
||||
# franchises (with no IGDB match) must generate a franchise collection once
|
||||
# shared by more than 2 ROMs.
|
||||
for index in range(3):
|
||||
_add_rom(platform, index, {"franchises": ["Grow"]})
|
||||
|
||||
collections = db_collection_handler.get_virtual_collections(type="franchise")
|
||||
|
||||
grow = next((c for c in collections if c.name == "Grow"), None)
|
||||
assert grow is not None
|
||||
assert grow.type == "franchise"
|
||||
assert len(grow.rom_ids) == 3
|
||||
|
||||
|
||||
def test_get_virtual_collections_respects_min_rom_threshold(
|
||||
platform: Platform, admin_user: User
|
||||
):
|
||||
# Fewer than 3 ROMs sharing a value must not surface as a collection.
|
||||
for index in range(2):
|
||||
_add_rom(platform, index, {"franchises": ["Sparse"]})
|
||||
|
||||
collections = db_collection_handler.get_virtual_collections(type="franchise")
|
||||
|
||||
assert all(c.name != "Sparse" for c in collections)
|
||||
|
||||
|
||||
def test_get_smart_collection_roms_normalizes_legacy_selected_status_lists(
|
||||
admin_user: User, mocker
|
||||
):
|
||||
|
||||
Reference in New Issue
Block a user