--remove broken records
DELETE FROM entrance_level_flat_range
    WHERE
        flat_first IS NULL OR
        flat_last IS NULL  OR
        level_universal_id IS NULL OR
        flat_first='' OR
        flat_last = '' OR
        level_universal_id = '';

-- keep original records in entrance_flat_range
INSERT INTO entrance_flat_range (flat_range_id, flat_first, flat_last, ft_id, is_exact)
SELECT (level_universal_id::jsonb->>'flat_range_id')::bigint, flat_first, flat_last, ft_id, is_exact
   FROM entrance_level_flat_range
   WHERE level_universal_id::jsonb->>'level_universal_id' = '';
DELETE FROM entrance_level_flat_range
   WHERE level_universal_id::jsonb->>'level_universal_id' = '';
-- update ids from json meta
UPDATE entrance_level_flat_range
    SET level_flat_range_id = (level_universal_id::jsonb->>'flat_range_id')::bigint
    WHERE level_universal_id::jsonb ? 'flat_range_id';
UPDATE entrance_level_flat_range
  SET level_universal_id = level_universal_id::jsonb->>'level_universal_id'
  WHERE level_universal_id::jsonb ? 'level_universal_id';
