Files
ems/db/migration/V073__pv_telemetry_source_def_fk.sql
Dusan Vojacek 9d37efb991
Some checks failed
CI and deploy / migration-check (push) Failing after 24s
CI and deploy / deploy (push) Has been skipped
telemetrie per pv_array, fix predictinos
2026-04-29 13:03:41 +02:00

57 lines
2.3 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- =============================================================
-- V073 číselník PV telemetrie + FK na asset_pv_array.telemetry_source
--
-- Cíl: referenční integrita pro telemetry_source (povolené kódy),
-- aby se zabránilo překlepům a nekonzistentním datům.
-- =============================================================
create table if not exists ems.pv_telemetry_source_def (
code text primary key,
description text not null,
telemetry_inverter_expr text null,
active boolean not null default true
);
comment on table ems.pv_telemetry_source_def is
'Číselník zdrojů PV telemetrie (kanálů) pro asset_pv_array.telemetry_source.';
comment on column ems.pv_telemetry_source_def.code is
'Stabilní kód zdroje telemetrie (FK z asset_pv_array.telemetry_source).';
comment on column ems.pv_telemetry_source_def.telemetry_inverter_expr is
'Volitelně: lidsky čitelný výraz, jak se kanál počítá z telemetry_inverter (informativní; runtime logika je v routines).';
insert into ems.pv_telemetry_source_def (code, description, telemetry_inverter_expr) values
('gen_port', 'AC-coupled výroba na GEN portu (souhrn).', 'gen_port_power_w'),
('pv1', 'DC string/MPPT 1 (samostatně).', 'pv1_power_w'),
('pv2', 'DC string/MPPT 2 (samostatně).', 'pv2_power_w'),
('pv_strings', 'Součet DC stringů (pv1+pv2).', 'pv1_power_w + pv2_power_w'),
('pv_total', 'Souhrnná PV výroba (pokud nelze rozlišit).','pv_power_w')
on conflict (code) do update
set description = excluded.description,
telemetry_inverter_expr = excluded.telemetry_inverter_expr,
active = true;
-- FK (idempotentně): NULL povolen (pole bez přímé telemetrie / fallback na forecast).
do $$
begin
if not exists (
select 1
from pg_constraint c
join pg_class t on t.oid = c.conrelid
join pg_namespace n on n.oid = t.relnamespace
where n.nspname = 'ems'
and t.relname = 'asset_pv_array'
and c.conname = 'asset_pv_array_telemetry_source_fk'
) then
alter table ems.asset_pv_array
add constraint asset_pv_array_telemetry_source_fk
foreign key (telemetry_source)
references ems.pv_telemetry_source_def(code)
on update cascade
on delete restrict;
end if;
end;
$$;