 -- N O N   P O L Y C O M   P H O N E S
 create or replace function migrate_non_polycom_phones() returns integer as '
 declare
-  phone record;
+  my_phone record;
   my_group_id int;
   my_phone_type record;
   next_id int;

-- notes:
--  Dont use apostrophes in comments
--  Dont use variable names that match column names when setting variable (see my_* usages)
--  primative logging by raising notices. level debug is useless w/o modifications to postgres.conf
--  some functions preserve primary keys from PDS, others do not.  depends on how easy it is to adjust
--    and if destination table has to merge values from multiple tables in old schema.  if primary keys
--    are preserved, sequences must be updated manually
--
-- todos:
-- bail on any error
-- workout logging that can keep a audit trail of migration incase there was a bad translation
--   discovered post migration AND after users have added new data and running another migration is
--   not possible and custom repair script can be written

delete from line;
delete from phone_group;
delete from phone;

-- todo, translate superuser as well
delete from user_group;
delete from users where user_id != 1;

delete from group_storage;
delete from setting_value;
delete from value_storage;

-- U S E R   G R O U P S
create or replace function migrate_user_groups() returns integer as '

select dblink_connect('dbname=PDS');

-- todo migrate folder and folder values
delete from line;
delete from phone_group;
delete from phone;

-- todo, translate superuser as well
delete from user_group;
delete from users where user_id != 1;

delete from group_storage;
delete from setting_value;
delete from value_storage;

select migrate_settings();
select migrate_user_groups();
