step5_newids.pl 6.04 KB
Newer Older
1 2 3
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2006, 2007, 2009 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
# All rights reserved.
#
use English;

use lib "/usr/testbed/lib";
use libdb;
use libtestbed;

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

#
# 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;
}

# users
DBQueryFatal("lock tables users write, user_stats write");
if (! TableChanged("users", "uid_uuid")) {
    DBQueryFatal("alter table users ".
	"add `uid_uuid` varchar(40) NOT NULL default '' after uid_idx, ".
	"add KEY uid_uuid (`uid_uuid`)");
}
if (! TableChanged("user_stats", "uid_uuid")) {
    DBQueryFatal("alter table user_stats ".
	"add `uid_uuid` varchar(40) NOT NULL default '' after uid_idx, ".
	"add KEY uid_uuid (`uid_uuid`)");
}
my $query_result = DBQueryFatal("select uid_idx from users ".
				"where uid_uuid=''");
while (my ($uid_idx) = $query_result->fetchrow_array()) {
47
    my $uuid = NewUUID();
48 49 50 51 52 53 54 55 56 57 58 59 60

    if (!defined($uuid)) {
	die("*** $0:\n".
	    "    Could not generate a UUID for user: $uid_idx\n");
    }
    DBQueryFatal("update users set uid_uuid='$uuid' where uid_idx='$uid_idx'");
    DBQueryFatal("update user_stats set uid_uuid='$uuid' ".
		 "where uid_idx='$uid_idx'");
    select(undef, undef, undef, 0.02);
}
$query_result = DBQueryFatal("select uid_idx from user_stats ".
			     "where uid_uuid=''");
while (my ($uid_idx) = $query_result->fetchrow_array()) {
61
    my $uuid = NewUUID();
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 87

    if (!defined($uuid)) {
	die("*** $0:\n".
	    "    Could not generate a UUID for user: $uid_idx\n");
    }
    DBQueryFatal("update user_stats set uid_uuid='$uuid' ".
		 "where uid_idx='$uid_idx'");
    select(undef, undef, undef, 0.02);
}
DBQueryFatal("unlock tables");

# groups (no point in doing the project).
DBQueryFatal("lock tables groups write, group_stats write");
if (! TableChanged("groups", "gid_uuid")) {
    DBQueryFatal("alter table groups ".
	"add `gid_uuid` varchar(40) NOT NULL default '' after gid_idx, ".
	"add KEY gid_uuid (`gid_uuid`)");
}
if (! TableChanged("group_stats", "gid_uuid")) {
    DBQueryFatal("alter table group_stats ".
	"add `gid_uuid` varchar(40) NOT NULL default '' after gid_idx, ".
	"add KEY gid_uuid (`gid_uuid`)");
}
$query_result = DBQueryFatal("select gid_idx from groups ".
			     "where gid_uuid=''");
while (my ($gid_idx) = $query_result->fetchrow_array()) {
88
    my $uuid = NewUUID();
89 90 91 92 93 94 95 96 97 98 99 100 101 102

    if (!defined($uuid)) {
	die("*** $0:\n".
	    "    Could not generate a UUID for group: $gid_idx\n");
    }
    DBQueryFatal("update groups set gid_uuid='$uuid' ".
		 "where gid_idx='$gid_idx'");
    DBQueryFatal("update group_stats set gid_uuid='$uuid' ".
		 "where gid_idx='$gid_idx'");
    select(undef, undef, undef, 0.02);
}
$query_result = DBQueryFatal("select gid_idx from group_stats ".
			     "where gid_uuid=''");
while (my ($gid_idx) = $query_result->fetchrow_array()) {
103
    my $uuid = NewUUID();
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

    if (!defined($uuid)) {
	die("*** $0:\n".
	    "    Could not generate a UUID for group: $gid_idx\n");
    }
    DBQueryFatal("update group_stats set gid_uuid='$uuid' ".
		 "where gid_idx='$gid_idx'");
    select(undef, undef, undef, 0.02);
}
DBQueryFatal("unlock tables");

# experiments 
DBQueryFatal("lock tables experiments write, experiment_stats write");
if (! TableChanged("experiments", "eid_uuid")) {
    DBQueryFatal("alter table experiments ".
	"add `eid_uuid` varchar(40) NOT NULL default '' after eid, ".
	"add KEY eid_uuid (`eid_uuid`)");
}
if (! TableChanged("experiment_stats", "eid_uuid")) {
    DBQueryFatal("alter table experiment_stats ".
	"add `eid_uuid` varchar(40) NOT NULL default '' after eid, ".
	"add KEY eid_uuid (`eid_uuid`)");
}
$query_result = DBQueryFatal("select idx from experiments ".
			     "where eid_uuid=''");
while (my ($exptidx) = $query_result->fetchrow_array()) {
130
    my $uuid = NewUUID();
131 132 133 134 135 136 137 138 139 140 141 142 143 144

    if (!defined($uuid)) {
	die("*** $0:\n".
	    "    Could not generate a UUID for exptidx: $exptidx\n");
    }
    DBQueryFatal("update experiments set eid_uuid='$uuid' ".
		 "where idx='$exptidx'");
    DBQueryFatal("update experiment_stats set eid_uuid='$uuid' ".
		 "where exptidx='$exptidx'");
    select(undef, undef, undef, 0.02);
}
$query_result = DBQueryFatal("select exptidx from experiment_stats ".
			     "where eid_uuid=''");
while (my ($exptidx) = $query_result->fetchrow_array()) {
145
    my $uuid = NewUUID();
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

    if (!defined($uuid)) {
	die("*** $0:\n".
	    "    Could not generate a UUID for exptidx: $exptidx\n");
    }
    DBQueryFatal("update experiment_stats set eid_uuid='$uuid' ".
		 "where exptidx='$exptidx'");
    select(undef, undef, undef, 0.02);
}
DBQueryFatal("unlock tables");

# images
DBQueryFatal("lock tables images write");
if (! TableChanged("images", "uuid")) {
    DBQueryFatal("alter table images ".
	"add `uuid` varchar(40) NOT NULL default '' after imageid, ".
	"add KEY uuid (`uuid`)");
}
$query_result = DBQueryFatal("select imageid from images where uuid=''");
while (my ($imageid) = $query_result->fetchrow_array()) {
166
    my $uuid = NewUUID();
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

    if (!defined($uuid)) {
	die("*** $0:\n".
	    "    Could not generate a UUID for imageid: $imageid\n");
    }
    DBQueryFatal("update images set uuid='$uuid' ".
		 "where imageid='$imageid'");
    select(undef, undef, undef, 0.02);
}
DBQueryFatal("unlock tables");

# os_info
DBQueryFatal("lock tables os_info write");
if (! TableChanged("os_info", "uuid")) {
    DBQueryFatal("alter table os_info ".
	"add `uuid` varchar(40) NOT NULL default '' after osid, ".
	"add KEY uuid (`uuid`)");
}
$query_result = DBQueryFatal("select osid from os_info where uuid=''");
while (my ($osid) = $query_result->fetchrow_array()) {
187
    my $uuid = NewUUID();
188 189 190 191 192 193 194 195 196 197 198

    if (!defined($uuid)) {
	die("*** $0:\n".
	    "    Could not generate a UUID for osid: $osid\n");
    }
    DBQueryFatal("update os_info set uuid='$uuid' ".
		 "where osid='$osid'");
    select(undef, undef, undef, 0.02);
}
DBQueryFatal("unlock tables");