mirror of
https://github.com/rommapp/romm.git
synced 2026-12-02 10:08:40 +01:00
The virtual_collections view joined roms_metadata (itself a heavy COALESCE/JSON view) once per collection type, evaluating it ~5x per listing. Factor the roms<->roms_metadata join into a single base CTE that each collection CTE unnests from. Output is identical. Generated-By: PostHog Code Task-Id: 10289bf4-b0d0-4856-80c9-1be93c926f37
419 lines
17 KiB
Python
419 lines
17 KiB
Python
"""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 base AS (
|
|
SELECT
|
|
r.id as rom_id,
|
|
r.path_cover_s as path_cover_s,
|
|
r.path_cover_l as path_cover_l,
|
|
rm.genres as genres,
|
|
rm.franchises as franchises,
|
|
rm.collections as collections,
|
|
rm.game_modes as game_modes,
|
|
rm.companies as companies
|
|
FROM roms r
|
|
JOIN roms_metadata rm ON rm.rom_id = r.id
|
|
),
|
|
genres_collection AS (
|
|
SELECT
|
|
rom_id,
|
|
path_cover_s,
|
|
path_cover_l,
|
|
jsonb_array_elements_text(genres) as collection_name,
|
|
'genre' as collection_type
|
|
FROM base
|
|
),
|
|
franchises_collection AS (
|
|
SELECT
|
|
rom_id,
|
|
path_cover_s,
|
|
path_cover_l,
|
|
jsonb_array_elements_text(franchises) as collection_name,
|
|
'franchise' as collection_type
|
|
FROM base
|
|
),
|
|
collection_collection AS (
|
|
SELECT
|
|
rom_id,
|
|
path_cover_s,
|
|
path_cover_l,
|
|
jsonb_array_elements_text(collections) as collection_name,
|
|
'collection' as collection_type
|
|
FROM base
|
|
),
|
|
modes_collection AS (
|
|
SELECT
|
|
rom_id,
|
|
path_cover_s,
|
|
path_cover_l,
|
|
jsonb_array_elements_text(game_modes) as collection_name,
|
|
'mode' as collection_type
|
|
FROM base
|
|
),
|
|
companies_collection AS (
|
|
SELECT
|
|
rom_id,
|
|
path_cover_s,
|
|
path_cover_l,
|
|
jsonb_array_elements_text(companies) as collection_name,
|
|
'company' as collection_type
|
|
FROM base
|
|
)
|
|
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 base AS (
|
|
SELECT
|
|
r.id as rom_id,
|
|
r.path_cover_s as path_cover_s,
|
|
r.path_cover_l as path_cover_l,
|
|
rm.genres as genres,
|
|
rm.franchises as franchises,
|
|
rm.collections as collections,
|
|
rm.game_modes as game_modes,
|
|
rm.companies as companies
|
|
FROM
|
|
roms r
|
|
JOIN roms_metadata rm ON rm.rom_id = r.id
|
|
),
|
|
genres AS (
|
|
SELECT
|
|
base.rom_id as rom_id,
|
|
base.path_cover_s as path_cover_s,
|
|
base.path_cover_l as path_cover_l,
|
|
CONCAT(j.genre) as collection_name,
|
|
'genre' as collection_type
|
|
FROM
|
|
base
|
|
CROSS JOIN JSON_TABLE(
|
|
base.genres,
|
|
'$[*]' COLUMNS (genre VARCHAR(255) PATH '$')
|
|
) j
|
|
),
|
|
franchises AS (
|
|
SELECT
|
|
base.rom_id as rom_id,
|
|
base.path_cover_s as path_cover_s,
|
|
base.path_cover_l as path_cover_l,
|
|
CONCAT(j.franchise) as collection_name,
|
|
'franchise' as collection_type
|
|
FROM
|
|
base
|
|
CROSS JOIN JSON_TABLE(
|
|
base.franchises,
|
|
'$[*]' COLUMNS (franchise VARCHAR(255) PATH '$')
|
|
) j
|
|
),
|
|
collections AS (
|
|
SELECT
|
|
base.rom_id as rom_id,
|
|
base.path_cover_s as path_cover_s,
|
|
base.path_cover_l as path_cover_l,
|
|
CONCAT(j.collection) as collection_name,
|
|
'collection' as collection_type
|
|
FROM
|
|
base
|
|
CROSS JOIN JSON_TABLE(
|
|
base.collections,
|
|
'$[*]' COLUMNS (collection VARCHAR(255) PATH '$')
|
|
) j
|
|
),
|
|
modes AS (
|
|
SELECT
|
|
base.rom_id as rom_id,
|
|
base.path_cover_s as path_cover_s,
|
|
base.path_cover_l as path_cover_l,
|
|
CONCAT(j.mode) as collection_name,
|
|
'mode' as collection_type
|
|
FROM
|
|
base
|
|
CROSS JOIN JSON_TABLE(
|
|
base.game_modes,
|
|
'$[*]' COLUMNS (mode VARCHAR(255) PATH '$')
|
|
) j
|
|
),
|
|
companies AS (
|
|
SELECT
|
|
base.rom_id as rom_id,
|
|
base.path_cover_s as path_cover_s,
|
|
base.path_cover_l as path_cover_l,
|
|
CONCAT(j.company) as collection_name,
|
|
'company' as collection_type
|
|
FROM
|
|
base
|
|
CROSS JOIN JSON_TABLE(
|
|
base.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;
|
|
"""),
|
|
)
|