
-- rhnServerGroup and dependencies

SELECT .clear_log_id();

DELETE FROM rhnOrgExtGroupMapping
  WHERE server_group_id in (
    SELECT id
      FROM rhnServerGroup
      WHERE group_type = (
        SELECT id
          FROM rhnServerGroupType
          WHERE label = 'sw_mgr_entitled'
      )
  );

DELETE FROM rhnRegTokenGroups
  WHERE server_group_id in (
    SELECT id
      FROM rhnServerGroup
      WHERE group_type = (
        SELECT id
          FROM rhnServerGroupType
          WHERE label = 'sw_mgr_entitled'
      )
  );

-- create server group for 'enterprise_entitled' everywhere
insert into rhnServerGroup ( id, name, description, group_type, org_id )
  select nextval('rhn_server_group_id_seq'), sgt.name, sgt.name, sgt.id, X.org_id
  from rhnServerGroupType sgt,
     (select distinct msg.org_id
        from rhnServerGroup msg
       where msg.org_id not in (select org_id
                                  from rhnServerGroup sg
                                  join rhnServerGroupType sgt ON sgt.id = sg.group_type
                                 where sgt.label = 'enterprise_entitled')
     ) X
  where sgt.label = 'enterprise_entitled';

-- add membership to 'enterprise_entitled' for all server which
-- are currently in the group 'sw_mgr_entitled'
  insert into rhnServerGroupMembers (server_id, server_group_id)
    select * from (
      select sgm.server_id,
        (select nsg.id
           from rhnServerGroup nsg
           join rhnServerGroupType nsgt on nsg.group_type = nsgt.id
          where nsgt.label = 'enterprise_entitled'
            and nsg.org_id = sg.org_id) new_group_id
        from rhnServerGroupMembers sgm
        join rhnServerGroup sg on sgm.server_group_id = sg.id
        join rhnServerGroupType sgt on sg.group_type = sgt.id
       where sgt.label = 'sw_mgr_entitled'
    ) X
    where not exists (select 1
                       from rhnServerGroupMembers csgm
                      where csgm.server_id = X.server_id
                        and csgm.server_group_id = X.new_group_id);

-- remove 'sw_mgr_entitled'
DELETE FROM rhnServerGroupMembers
  WHERE server_group_id in (
    SELECT id
      FROM rhnServerGroup
      WHERE group_type = (
        SELECT id
          FROM rhnServerGroupType
          WHERE label = 'sw_mgr_entitled'
      )
  );


DELETE FROM rhnSnapshotServerGroup
  WHERE server_group_id in (
    SELECT id
      FROM rhnServerGroup
      WHERE group_type = (
        SELECT id
          FROM rhnServerGroupType
          WHERE label = 'sw_mgr_entitled'
      )
  );

DELETE FROM rhnUserDefaultSystemGroups
  WHERE system_group_id in (
    SELECT id
      FROM rhnServerGroup
      WHERE group_type = (
        SELECT id
          FROM rhnServerGroupType
          WHERE label = 'sw_mgr_entitled'
      )
  );

DELETE FROM rhnUserServerGroupPerms
  WHERE server_group_id in (
    SELECT id
      FROM rhnServerGroup
      WHERE group_type = (
        SELECT id
          FROM rhnServerGroupType
          WHERE label = 'sw_mgr_entitled'
      )
  );

DELETE FROM rhnServerGroup
  WHERE group_type = (
    SELECT id
      FROM rhnServerGroupType
      WHERE label = 'sw_mgr_entitled'
  );

-- rhnServerGroupType and dependencies

DELETE FROM rhnRegTokenEntitlement
  WHERE server_group_type_id = (
    SELECT id
      FROM rhnServerGroupType
      WHERE label = 'sw_mgr_entitled'
  );

DELETE FROM rhnServerGroupTypeFeature
  WHERE server_group_type_id = (
    SELECT id
      FROM rhnServerGroupType
      WHERE label = 'sw_mgr_entitled'
  );

DELETE FROM rhnServerServerGroupArchCompat
  WHERE server_group_type = (
    SELECT id
      FROM rhnServerGroupType
      WHERE label = 'sw_mgr_entitled'
  );

DELETE FROM rhnSGTypeBaseAddonCompat
  WHERE base_id = (
    SELECT id
      FROM rhnServerGroupType
      WHERE label = 'sw_mgr_entitled'
  );

DELETE FROM rhnServerGroupType
  WHERE label = 'sw_mgr_entitled';
