step2_newids.pl 9.22 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2006-2008 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25
#
use English;

26
use lib "/usr/testbed/lib";
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
use libdb;
use libtestbed;
use User;

#
# Untaint the path
# 
$ENV{'PATH'} = '/bin:/usr/bin:/usr/sbin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Grab the ids that were generated earlier with init_newids and save them.
#
my %uids = ();

# Ick, this turns up in various places!
$uids{'root'} = 0;

my $query_result =
    DBQueryFatal("select uid,uid_idx from users");

while (my ($uid,$uid_idx) = $query_result->fetchrow_array()) {
    $uids{$uid} = $uid_idx;
}

#
# See if a table has been changed by looking for the existence of a field.
#
sub TableChanged($$)
{
    my ($table, $slot) = @_;

    my $describe_result =
	DBQueryFatal("describe $table $slot");

    return $describe_result->numrows;
}

#
# Change the primary key for a table
#
sub ChangePrimaryKey($$)
{
    my ($table, $newkey) = @_;

    DBQuery("alter table $table drop primary key");
    DBQueryFatal("alter table $table add PRIMARY KEY ($newkey)");
}

#
# Update table
#
sub UpdateTable($$$)
{
    my ($table, $newslot, $oldslot) = @_;

    print "Updating '$newslot' from '$oldslot' in table '$table'\n";

    my $query_result =
	DBQueryFatal("select distinct $oldslot from $table ".
87
		     "where ${newslot}='0' or ${newslot} is null");
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

    while (my ($oldvalue) = $query_result->fetchrow_array()) {
	next
	    if (!defined($oldvalue));
	
	my $idx = $uids{$oldvalue};

	#
	# Create an archived user as a placeholder for this person.
	# This can be a problem if a uid was deleted and then reused.
	#
	if (!defined($idx)) {
	    print "*** Creating archived user for '$oldvalue'\n";
	    
	    my %newuser_args = ("notes" =>
				'Deleted user restored as Archived');
	    
	    my $newuser = User->Create($oldvalue,
106 107
				       $User::NEWUSER_FLAGS_ARCHIVED|
				       $User::NEWUSER_FLAGS_NOUUID,
108 109 110 111 112 113 114
				       \%newuser_args);
	    
	    $idx = $newuser->uid_idx();
	    $uids{$oldvalue} = $idx;
	}
	
	DBQueryFatal("update $table set ${newslot}='${idx}' ".
115 116
		     "where ${oldslot}='${oldvalue}' and ".
		     "      (${newslot}='0' or ${newslot} is null)");
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    }
}

# user_sslcerts
if (! TableChanged("user_sslcerts", "uid_idx")) {
    DBQueryFatal("alter table user_sslcerts add ".
		 "  uid_idx mediumint(8) unsigned NOT NULL default '0' ".
		 "after uid");
}
UpdateTable("user_sslcerts", "uid_idx", "uid");

# user_sfskeys
if (! TableChanged("user_sfskeys", "uid_idx")) {
    DBQueryFatal("alter table user_sfskeys add ".
		 "  uid_idx mediumint(8) unsigned NOT NULL default '0' ".
		 "after uid");
    UpdateTable("user_sfskeys", "uid_idx", "uid");
    DBQueryFatal("alter table user_sfskeys drop PRIMARY KEY");
    DBQueryFatal("alter table user_sfskeys add PRIMARY KEY (uid_idx,comment)");
    DBQueryFatal("alter table user_sfskeys add INDEX uid (uid,comment)");
}
UpdateTable("user_sfskeys", "uid_idx", "uid");

# user_pubkeys
if (! TableChanged("user_pubkeys", "uid_idx")) {
    DBQueryFatal("alter table user_pubkeys add ".
		 "  uid_idx mediumint(8) unsigned NOT NULL default '0' ".
		 "after uid");
    UpdateTable("user_pubkeys", "uid_idx", "uid");
    DBQueryFatal("alter table user_pubkeys add INDEX uid (uid,idx)");
    DBQueryFatal("alter table user_pubkeys drop PRIMARY KEY");
    DBQueryFatal("alter table user_pubkeys add PRIMARY KEY (uid_idx,idx)");
}
UpdateTable("user_pubkeys", "uid_idx", "uid");

# Projects table.
if (! TableChanged("projects", "head_idx")) {
    DBQueryFatal("alter table projects add ".
		 "  head_idx mediumint(8) unsigned NOT NULL default '0' ".
		 "after head_uid");
}
UpdateTable("projects", "head_idx", "head_uid");

# Groups table.
if (! TableChanged("groups", "leader_idx")) {
    DBQueryFatal("alter table groups add ".
		 "  leader_idx mediumint(8) unsigned NOT NULL default '0' ".
		 "after leader");
}
UpdateTable("groups", "leader_idx", "leader");

# experiments table
if (! TableChanged("experiments", "creator_idx")) {
    DBQueryFatal("alter table experiments add ".
		 "  creator_idx mediumint(8) unsigned NOT NULL default '0' ".
		 "after gid");
    DBQueryFatal("alter table experiments add ".
		 "  swapper_idx mediumint(8) unsigned default NULL ".
		 "after creator_idx");
}
UpdateTable("experiments", "creator_idx", "expt_head_uid");
UpdateTable("experiments", "swapper_idx", "expt_swap_uid");

# experiment_stats table
if (! TableChanged("experiment_stats", "creator_idx")) {
    DBQueryFatal("alter table experiment_stats add ".
		 "  creator_idx mediumint(8) unsigned NOT NULL default '0' ".
		 "after creator");
}
UpdateTable("experiment_stats", "creator_idx", "creator");

# experiment_templates table
if (! TableChanged("experiment_templates", "uid_idx")) {
    DBQueryFatal("alter table experiment_templates add ".
		 "  uid_idx mediumint(8) unsigned NOT NULL default '0' ".
		 "after uid");
}
UpdateTable("experiment_templates", "uid_idx", "uid");

# experiment_template_metadata_items table
if (! TableChanged("experiment_template_metadata_items", "uid_idx")) {
    DBQueryFatal("alter table experiment_template_metadata_items add ".
		 "  uid_idx mediumint(8) unsigned NOT NULL default '0' ".
		 "after uid");
}
UpdateTable("experiment_template_metadata_items", "uid_idx", "uid");

# experiment_template_instances table
if (! TableChanged("experiment_template_instances", "uid_idx")) {
    DBQueryFatal("alter table experiment_template_instances add ".
		 "  uid_idx mediumint(8) unsigned NOT NULL default '0' ".
		 "after uid");
}
UpdateTable("experiment_template_instances", "uid_idx", "uid");

# datapository_databases table
if (! TableChanged("datapository_databases", "uid_idx")) {
    DBQueryFatal("alter table datapository_databases add ".
		 "  uid_idx mediumint(8) unsigned NOT NULL default '0' ".
		 "after uid");
}
UpdateTable("datapository_databases", "uid_idx", "uid");

# images table
if (! TableChanged("images", "creator_idx")) {
    DBQueryFatal("alter table images add ".
		 "  creator_idx mediumint(8) unsigned NOT NULL default '0' ".
		 "after creator");
}
UpdateTable("images", "creator_idx", "creator");

# os_info table
if (! TableChanged("os_info", "creator_idx")) {
    DBQueryFatal("alter table os_info add ".
		 "  creator_idx mediumint(8) unsigned NOT NULL default '0' ".
		 "after creator");
}
UpdateTable("os_info", "creator_idx", "creator");

# knowledge_base_entries
if (! TableChanged("knowledge_base_entries", "creator_idx")) {
    DBQueryFatal("alter table knowledge_base_entries add ".
		 "  creator_idx mediumint(8) unsigned NOT NULL default '0' ".
		 "after creator_uid");
    DBQueryFatal("alter table knowledge_base_entries add ".
		 "  modifier_idx mediumint(8) unsigned NOT NULL default '0' ".
		 "after modifier_uid");
    DBQueryFatal("alter table knowledge_base_entries add ".
		 "  archiver_idx mediumint(8) unsigned NOT NULL default '0' ".
		 "after archiver_uid");
}
UpdateTable("knowledge_base_entries", "creator_idx", "creator_uid");
UpdateTable("knowledge_base_entries", "modifier_idx", "modifier_uid");
UpdateTable("knowledge_base_entries", "archiver_idx", "archiver_uid");

# mailman_listnames
if (! TableChanged("mailman_listnames", "owner_idx")) {
    DBQueryFatal("alter table mailman_listnames add ".
		 "  owner_idx mediumint(8) unsigned NOT NULL default '0' ".
		 "after owner_uid");
}
UpdateTable("mailman_listnames", "owner_idx", "owner_uid");

# node_history
if (! TableChanged("node_history", "uid_idx")) {
    DBQueryFatal("alter table node_history add ".
		 "  uid_idx mediumint(8) unsigned NOT NULL default '0' ".
		 "after uid");
}
266
UpdateTable("node_history", "uid_idx", "uid");
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

# nodelog
if (! TableChanged("nodelog", "reporting_idx")) {
    DBQueryFatal("alter table nodelog add ".
		 "  reporting_idx mediumint(8) unsigned NOT NULL default '0' ".
		 "after reporting_uid");
}
UpdateTable("nodelog", "reporting_idx", "reporting_uid");

# testbed_stats
if (! TableChanged("testbed_stats", "uid_idx")) {
    DBQueryFatal("alter table testbed_stats add ".
		 "  uid_idx mediumint(8) unsigned NOT NULL default '0' ".
		 "after uid");
}
282
UpdateTable("testbed_stats", "uid_idx", "uid");
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299

# widearea_accounts
if (! TableChanged("widearea_accounts", "uid_idx")) {
    DBQueryFatal("alter table widearea_accounts add ".
		 "  uid_idx mediumint(8) unsigned NOT NULL default '0' ".
		 "after uid");
}
UpdateTable("widearea_accounts", "uid_idx", "uid");

# widearea_nodeinfo
if (! TableChanged("widearea_nodeinfo", "contact_idx")) {
    DBQueryFatal("alter table widearea_nodeinfo add ".
		 "  contact_idx mediumint(8) unsigned NOT NULL default '0' ".
		 "after contact_uid");
}
UpdateTable("widearea_nodeinfo", "contact_idx", "contact_uid");