telemetrie per pv_array, fix predictinos
This commit is contained in:
56
db/migration/V073__pv_telemetry_source_def_fk.sql
Normal file
56
db/migration/V073__pv_telemetry_source_def_fk.sql
Normal file
@@ -0,0 +1,56 @@
|
||||
-- =============================================================
|
||||
-- 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;
|
||||
$$;
|
||||
|
||||
Reference in New Issue
Block a user