insert into groups_permissions
(id,
 project_id,
 parent_id,
 title,
 description,
 admin,
 permissions,
 total_groups,
 total_members,
 deleted,
 created_at,
 updated_at,
 deleted_at,
 options)
select id,
       project_id,
       parent_id,
       title,
       description,
       (case when admin is null then false else admin end),
       (case when permissions is null then '' else permissions end),
       total_groups,
       total_members,
       deleted,
       created_at,
       updated_at,
       deleted_at,
       (case when "options" is null then '' else "options" end)
from groups
where not exists(select 1 from groups_permissions where groups_permissions.id = groups.id);

update groups set options = '{"ReceiveProjectNotifications":false}' where options = '';
update groups_permissions set options = '{"ReceiveProjectNotifications":false}' where options = '';
select setval('groups_permissions_id_seq', (select max(id) from groups_permissions));
select setval('groups_id_seq', (select max(id) from groups));

insert into group_permissions_members (id, group_id, user_id, deleted, created_at, updated_at, deleted_at, restored_at)
select *
from group_members
where not exists(select 1 from group_permissions_members where group_permissions_members.id = group_members.id);
select setval('group_permissions_members_id_seq', (select max(id) from group_permissions_members));
select setval('group_members_id_seq', (select max(id) from group_members));
