alter table eventhub.event
    drop constraint if exists event_vehicle_entity_id_fkey;

alter table eventhub.event
    drop constraint if exists fk_event_vehicle;

do $migration$
begin
    if exists (
        select 1
        from information_schema.columns
        where table_schema = 'eventhub'
          and table_name = 'event'
          and column_name = 'vehicle_id'
    )
    then
        alter table eventhub.event
            add constraint fk_event_vehicle
            foreign key (vehicle_id)
            references eventhub.vehicle(id)
            not valid;
    end if;
end
$migration$;
