Refactor Falukant certificate processing logic: Introduced a new SQL query to check for the existence of the completion_count column in the production log, allowing for conditional selection of certificate input rows. Added a legacy query for environments lacking this column, enhancing compatibility and robustness in certificate processing.
All checks were successful
Deploy yourpart (blue-green) / deploy (push) Successful in 1m34s

This commit is contained in:
Torsten Schulz (local)
2026-04-21 15:34:58 +02:00
parent 397b612bf1
commit 05f713c5e1
2 changed files with 84 additions and 1 deletions

View File

@@ -4015,6 +4015,75 @@ pub const QUERY_GET_PRODUCTION_CERTIFICATE_INPUT_ROWS: &str = r#"
ORDER BY fu.id, c.id DESC;
"#;
/// Fallback für Bestands-DBs ohne `falukant_log.production.completion_count`:
/// nutzt `COUNT(*)` statt `SUM(completion_count)`.
pub const QUERY_GET_PRODUCTION_CERTIFICATE_INPUT_ROWS_LEGACY: &str = r#"
SELECT DISTINCT ON (fu.id)
fu.id AS falukant_user_id,
COALESCE(fu.user_id, fu.id)::int AS app_user_id,
COALESCE(fu.certificate, 1)::int AS certificate,
COALESCE(fu.money, 0)::float8 AS money,
c.id AS character_id,
COALESCE(c.reputation, 50)::float8 AS reputation,
COALESCE(t.level, 0)::int AS title_level,
COALESCE((
SELECT AVG(k.knowledge)::float8
FROM falukant_data.knowledge k
WHERE k.character_id = c.id
), 0.0) AS avg_knowledge,
COALESCE((
SELECT COUNT(*)::bigint
FROM falukant_log.production pl
WHERE (pl.producer_id = fu.id OR pl.producer_id = c.id)
AND (
fu.certificate_productions_count_since IS NULL
OR COALESCE(
pl.production_timestamp,
pl.production_date::timestamp
) >= fu.certificate_productions_count_since
)
), 0) AS completed_production_count,
GREATEST(
COALESCE(c.highest_church_hierarchy_ever, 0)::int,
COALESCE((
SELECT MAX(cot.hierarchy_level)::int
FROM falukant_data.church_office co
JOIN falukant_type.church_office_type cot ON cot.id = co.office_type_id
WHERE co.character_id = c.id
), 0)
) AS max_church_hierarchy,
COALESCE((
SELECT STRING_AGG(DISTINCT pot.name, '|')
FROM falukant_data.political_office po
JOIN falukant_type.political_office_type pot ON pot.id = po.office_type_id
WHERE po.character_id = c.id
AND (po.created_at + (pot.term_length * INTERVAL '1 day')) > NOW()
), '') AS political_office_names,
COALESCE((
SELECT h.position::int
FROM falukant_data.user_house uh
JOIN falukant_type.house h ON h.id = uh.house_type_id
WHERE uh.user_id = fu.id
ORDER BY uh.id
LIMIT 1
), 0) AS house_position
FROM falukant_data.falukant_user fu
JOIN falukant_data.character c ON c.user_id = fu.id AND c.health > 0
LEFT JOIN falukant_type.title t ON t.id = c.title_of_nobility
ORDER BY fu.id, c.id DESC;
"#;
/// Prüft, ob `falukant_log.production.completion_count` existiert.
pub const QUERY_PRODUCTION_COMPLETION_COUNT_READY: &str = r#"
SELECT EXISTS (
SELECT 1
FROM information_schema.columns
WHERE table_schema = 'falukant_log'
AND table_name = 'production'
AND column_name = 'completion_count'
) AS ready;
"#;
/// Setzt bei jeder Stufenänderung `certificate_productions_count_since` (Mindest-Produktionen / PP neu ab Aufstieg).
pub const QUERY_UPDATE_FALUKANT_USER_CERTIFICATE: &str = r#"
UPDATE falukant_data.falukant_user