Main Tables Views Materialized Views Indexes Constraints Triggers Procedures Functions Packages Sequences Java Sources Jobs Sanity Check Index DDL scrips
Arguments Source

DELETE_SERVER

Arguments:

NameData TypeDefault ValueIn/Out
SERVER_ID_INNUMBER(38) IN
DDL script

Source

Legend: comment string keyword reserved word operator
     1: procedure delete_server (
     2: 	server_id_in in number
     3: ) is
     4: 	cursor servergroups is
     5: 		select	server_id, server_group_id
     6: 		from	rhnServerGroupMembers sgm
     7: 		where	sgm.server_id = server_id_in;
     8: 	cursor configchannels is
     9: 		select	cc.id
    10: 		from	rhnConfigChannel cc,
    11: 			rhnConfigChannelType cct,
    12: 			rhnServerConfigChannel scc
    13: 		where	1=1
    14: 			and scc.server_id = server_id_in
    15: 			and scc.config_channel_id = cc.id
    16: 			-- these config channel types are reserved
    17: 			-- for use by a single server, so we don't
    18: 			-- need to check for other servers subscribed
    19: 			and cct.label in
    20: 				('local_override','server_import')
    21: 			and cct.id = cc.confchan_type_id;
    22:         type filelistsid_t is table of rhnServerPreserveFileList.file_list_id%type;
    23:         filelistsid_c filelistsid_t;
    24: 
    25:     update_lock number;
    26: begin
    27:     -- lock the rhnServer row to prevent deadlocks
    28:     -- we want rhnServer to be locked first, followed by tables that depend on it
    29:     select id into update_lock from rhnServer where id = server_id_in for update;
    30: 
    31:         -- filelists
    32: 	select	spfl.file_list_id id bulk collect into filelistsid_c
    33: 	  from	rhnServerPreserveFileList spfl
    34: 	 where	spfl.server_id = server_id_in
    35: 			and not exists (
    36: 				select	1
    37: 				from	rhnServerPreserveFileList
    38: 				where	file_list_id = spfl.file_list_id
    39: 					and server_id != server_id_in
    40: 				union
    41: 				select	1
    42: 				from	rhnKickstartPreserveFileList
    43: 				where	file_list_id = spfl.file_list_id
    44: 			);
    45:         if filelistsid_c.first is not null then
    46:             forall i in filelistsid_c.first..filelistsid_c.last
    47:                 delete from rhnFileList where id = filelistsid_c(i);
    48:         end if;
    49: 
    50: 	for configchannel in configchannels loop
    51: 		rhn_config.delete_channel(configchannel.id);
    52: 	end loop;
    53: 
    54: 	for sgm in servergroups loop
    55: 		rhn_server.delete_from_servergroup(
    56: 			sgm.server_id, sgm.server_group_id);
    57: 	end loop;
    58: 
    59: 	-- we're handling this instead of letting an "on delete
    60: 	-- set null" do it so that we don't run the risk
    61: 	-- of setting off the triggers and killing us with a
    62: 	-- mutating table
    63: 
    64: 	-- this is merge of two single updates:
    65:         --  update ... set old_server_id = null where old_server_id = server_id_in;
    66:         --  update ... set new_server_id = null where new_server_id = server_id_in;
    67:         -- so we scan rhnKickstartSession table only once
    68: 	update rhnKickstartSession
    69: 		set old_server_id = case when old_server_id = server_id_in then null else old_server_id end,
    70: 		    new_server_id = case when new_server_id = server_id_in then null else new_server_id end
    71: 		where old_server_id = server_id_in
    72: 		   or new_server_id = server_id_in;
    73: 
    74: 	rhn_channel.clear_subscriptions(server_id_in, 1);
    75: 
    76:     	-- A little complicated here, but the goal is to
    77: 	-- delete records from rhnVirtualInstace only if we don't
    78: 	-- care about them anymore.  We don't care about records
    79: 	-- in rhnVirtualInstance if we are deleting the host
    80: 	-- system and the virtual system is already null, or
    81: 	-- vice-versa.  We *do* care about them if either the
    82: 	-- host or virtual system is still registered because we
    83: 	-- still want them to show up in the UI.
    84:     -- If there's a newer row in rhnVirtualInstance with the same
    85:     -- uuid, this guest must have been re-registered, so we can clean
    86:     -- this data up.
    87: 
    88:         delete from rhnVirtualInstance vi
    89: 	      where (host_system_id = server_id_in and virtual_system_id is null)
    90:                  or (virtual_system_id = server_id_in and host_system_id is null)
    91:                  or (vi.virtual_system_id = server_id_in and vi.modified < (select max(vi2.modified)
    92:                     from rhnVirtualInstance vi2 where vi2.uuid = vi.uuid));
    93: 
    94:         -- this is merge of two single updates:
    95:         --  update ... set host_system_id = null where host_system_id = server_id_in;
    96:         --  update ... set virtual_system_id = null where virtual_system_id = server_id_in;
    97:         -- so we scan rhnVirtualInstance table only once
    98:         update rhnVirtualInstance
    99: 	   set host_system_id = case when host_system_id = server_id_in then null else host_system_id end,
   100: 	       virtual_system_id = case when virtual_system_id = server_id_in then null else virtual_system_id end
   101: 	 where host_system_id = server_id_in
   102: 	    or virtual_system_id = server_id_in;
   103: 
   104:         -- this is merge of two single updates:
   105:         --  update ... set old_host_system_id = null when old_host_system_id = server_id_in;
   106:         --  update ... set new_host_system_id = null when new_host_system_id = server_id_in;
   107:         -- so we scan rhnVirtualInstanceEventLog table only once
   108: 	update rhnVirtualInstanceEventLog
   109: 	   set old_host_system_id = case when old_host_system_id = server_id_in then null else old_host_system_id end,
   110:                new_host_system_id = case when new_host_system_id = server_id_in then null else new_host_system_id end
   111:          where old_host_system_id = server_id_in
   112:             or new_host_system_id = server_id_in;
   113: 
   114: 	-- We're deleting everything with a foreign key to rhnServer
   115: 	-- here, now.  I'm hoping this will help aleviate our deadlock
   116: 	-- problem.
   117: 
   118: 	delete from rhnActionConfigChannel where server_id = server_id_in;
   119: 	delete from rhnActionConfigRevision where server_id = server_id_in;
   120: 	delete from rhnActionPackageRemovalFailure where server_id = server_id_in;
   121: 	delete from rhnClientCapability where server_id = server_id_in;
   122: 	delete from rhnCpu where server_id = server_id_in;
   123: 	-- there's still a cascade here, because the constraint keeps the
   124: 	-- table locked for too long to rebuild it.  Ugh...
   125: 	delete from rhnDevice where server_id = server_id_in;
   126: 	delete from rhnProxyInfo where server_id = server_id_in;
   127: 	delete from rhnRam where server_id = server_id_in;
   128: 	delete from rhnRegToken where server_id = server_id_in;
   129: 	delete from rhnSatelliteInfo where server_id = server_id_in;
   130: 	-- this cascades to rhnActionConfigChannel and rhnActionConfigFileName
   131: 	delete from rhnServerAction where server_id = server_id_in;
   132: 	delete from rhnServerActionPackageResult where server_id = server_id_in;
   133: 	delete from rhnServerActionScriptResult where server_id = server_id_in;
   134: 	delete from rhnServerActionVerifyResult where server_id = server_id_in;
   135: 	delete from rhnServerActionVerifyMissing where server_id = server_id_in;
   136: 	-- counts are handled above.  this should be a delete_ function.
   137: 	delete from rhnServerChannel where server_id = server_id_in;
   138: 	delete from rhnServerConfigChannel where server_id = server_id_in;
   139: 	delete from rhnServerCustomDataValue where server_id = server_id_in;
   140: 	delete from rhnServerDMI where server_id = server_id_in;
   141: 	delete from rhnServerEvent where server_id = server_id_in;
   142: 	delete from rhnServerHistory where server_id = server_id_in;
   143: 	delete from rhnServerInfo where server_id = server_id_in;
   144: 	delete from rhnServerInstallInfo where server_id = server_id_in;
   145: 	delete from rhnServerLocation where server_id = server_id_in;
   146: 	delete from rhnServerLock where server_id = server_id_in;
   147: 	delete from rhnServerNeededCache where server_id = server_id_in;
   148: 	delete from rhnServerNetwork where server_id = server_id_in;
   149: 	delete from rhnServerNotes where server_id = server_id_in;
   150: 	-- I'm not removing the foreign key from rhnServerPackage; that'll
   151: 	-- take forever.  Do the delete anyway.
   152: 	delete from rhnServerPackage where server_id = server_id_in;
   153: 	delete from rhnServerTokenRegs where server_id = server_id_in;
   154: 	delete from rhnSnapshotTag where server_id = server_id_in;
   155: 	-- this cascades to:
   156: 	--   rhnSnapshotChannel, rhnSnapshotConfigChannel, rhnSnapshotPackage,
   157: 	--   rhnSnapshotConfigRevision, rhnSnapshotServerGroup,
   158: 	--   rhnSnapshotTag.
   159: 	-- We may want to consider delete_snapshot() at some point, but
   160: 	--   I don't think we need to yet.
   161: 	delete from rhnSnapshot where server_id = server_id_in;
   162: 	delete from rhnUserServerPrefs where server_id = server_id_in;
   163: 	-- hrm, this one's interesting... we _probably_ should delete
   164: 	-- everything for the parent server_id when we delete the proxy,
   165: 	-- but we don't currently.
   166: 	delete from rhnServerPath where server_id_in in (server_id, proxy_server_id);
   167: 	delete from rhnUserServerPerms where server_id = server_id_in;
   168: 
   169: 	delete from rhnServerNetInterface where server_id = server_id_in;
   170: 
   171: 	delete from rhnServerUuid where server_id = server_id_in;
   172: 
   173:     delete from rhnPushClient where server_id = server_id_in;
   174: 
   175: 	-- now get rhnServer itself.
   176: 	delete
   177: 	from	rhnServer
   178: 		where id = server_id_in;
   179: 
   180: 	delete
   181: 	from	rhnSet
   182: 	where	label = 'system_list'
   183: 		and element = server_id_in;
   184: end delete_server;