   next_id int;
 begin
 
-  -- todo weights ought to be the lower the depth, the higher to weight
-  -- how do you find the depth though?
-  raise notice ''DATA LOSS: User group inheritance not preserved'';
-
   -- todo user permissions
   raise notice ''DATA LOSS: User group permissions not migrated'';
 

    where 
      u.cs_id = cs.id'') as (user_id int, password text)
  loop
    raise notice ''sippassword for % = %'', my_sip_password.user_id, my_sip_password.password;
    update users set sip_password = my_sip_password.password where user_id = my_sip_password.user_id;
  end loop;

  return 1;
end;

end;
' language plpgsql;


create or replace function migrate_group_weights() returns integer as '
declare
  my_group record;
  my_weight int := 0;
  next_id int;
begin

  -- default group has the most members and the lowest weight. 
  -- sort by member count descending and assign next weight seq.
  for my_group in 
    select g.group_id as id from user_group ug
      left join group_storage g on ug.group_id = g.group_id
      group by g.group_id order by count(*) desc loop

    update group_storage set weight=my_weight where group_id = my_group.id;
    my_weight := my_weight + 1;

  end loop;

  my_weight := 0;
  for my_group in 
    select g.group_id as id from phone_group pg
      left join group_storage g on pg.group_id = g.group_id
      group by g.group_id order by count(*) desc loop

    update group_storage set weight=my_weight where group_id = my_group.id;
    my_weight := my_weight + 1;

  end loop;

  next_id := max(weight) + 1 from group_storage;
  perform setval(''group_weight_seq'', next_id);

  return 1;
end;
' language plpgsql;


-- U S E R S
create or replace function migrate_users() returns integer as '
declare
