icingadb/cmd/icingadb-migrate/embed/state_query.sql
Alvar Penning f94a65f459
icingadb-migrate: Explicit AS in SELECT
By introducing an explicit "AS" to set output names in the SELECT column
list, there are no issues with reserved names. Unfortunately, this
happened on PostgreSQL in the older version 13 with the reserved name
"name". Adding "AS" mitigates this issue.

Furthermore, I have put each column name in its own line for the SELECT
queries to ease the readability of the query itself and of future diffs.

Fixes #884.
2025-03-05 14:02:01 +01:00

22 lines
711 B
SQL

SELECT
sh.statehistory_id,
UNIX_TIMESTAMP(sh.state_time) AS state_time,
sh.state_time_usec,
sh.state,
sh.state_type,
sh.current_check_attempt,
sh.max_check_attempts,
sh.last_state,
sh.last_hard_state,
sh.output,
sh.long_output,
sh.check_source,
o.objecttype_id,
o.name1,
COALESCE(o.name2, '') AS name2
FROM icinga_statehistory sh USE INDEX (PRIMARY)
INNER JOIN icinga_objects o ON o.object_id=sh.object_id
WHERE sh.statehistory_id BETWEEN :fromid AND :toid
AND sh.statehistory_id <= :cache_limit AND sh.statehistory_id > :checkpoint -- where we were interrupted
ORDER BY sh.statehistory_id -- this way we know what has already been migrated from just the last row's ID
LIMIT :bulk