 $$ 
 LANGUAGE plpgsql;
 
-SELECT migrateWebstreamHistory() as output;+SELECT migrateWebstreamHistory() as output;
+
+DROP FUNCTION migrateWebstreamHistory();
+DROP VIEW ws_history;
FROM cc_schedule
WHERE media_item_played = true;

drop view ws_history;
create view ws_history as

select 
wm.start_time as starts, 
ws.name as creator,
wm.liquidsoap_data as title,
sched.instance_id as instance_id

from cc_webstream_metadata as wm
left join cc_schedule as sched
on sched.id = wm.instance_id
left join cc_webstream as ws
on sched.stream_id = ws.id;


CREATE OR REPLACE FUNCTION migrateWebstreamHistory() RETURNS int4 AS $$

            insert into cc_playout_history_metadata (history_id, key, value)
            values (hisid, 'artist_name', r.creator);

            insert into cc_playout_history_metadata (history_id, key, value)
            values (hisid, 'showname', r.showname);
            
    END LOOP;
return 1;
