CREATE TABLE enapo_pre_metadata_history (
	ean varchar NOT NULL,
	"year" int4 NOT NULL,
	"month" int4 NOT NULL,
	month_name varchar NULL,
	days_in_stored_month int4 NULL,
	consumption_point varchar NULL,
	address varchar NULL,
	location_type varchar NULL,
	company_name varchar NULL,
	company_id varchar NULL,
	tarif_type varchar NULL,
	tarif_1t2t varchar NULL,
	phases varchar NULL,
	circuit_breaker varchar NULL,
	type_b_meter varchar NULL,
	meter_replaced varchar NULL,
	created_at timestamp NULL,
	updated_at timestamp NULL,
	CONSTRAINT enapo_log_pre_pk PRIMARY KEY (ean, "year", "month")
);

COMMENT ON COLUMN enapo_pre_metadata_history.meter_replaced IS 'Změna elekroměru na odběrném místě v daném měsíci je indikována takto:
"meter_replaced": "53435572"  ==> beze změny
"meter_replaced": "2025-06-19|54292510|55505332"  ==> změna "datumZměny|starý|nový"
"meter_replaced": "2025-06-19|54292510|55505332|2025-06-30|55505332|96933662"  ==> případně dvě a více změn';

ALTER TABLE enapo_measurements RENAME COLUMN eic TO id;
ALTER TABLE enapo_measurements ADD id_type varchar DEFAULT 'eic' NOT NULL;
ALTER TABLE enapo_measurements ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz;
ALTER TABLE enapo_measurements ALTER COLUMN updated_at TYPE timestamptz USING updated_at::timestamptz;
