24 5.1 KB
Newer Older
1 2 3 4 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 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
#
# Add more cross referencing between geni and emulab DBs.
#
use strict;
use GeniDB;
use GeniUser;
use GeniHRN;

sub DoUpdate($$$)
{
    my ($dbhandle, $dbname, $version) = @_;

    DBSetDefault($dbhandle);

    if (!DBSlotExists("aggregate_history", "urn")) {
        DBQueryFatal("ALTER TABLE aggregate_history ADD ".
		     "  `urn` tinytext ".
		     "after hrn");
    }
    if (!DBSlotExists("aggregate_history", "slice_urn")) {
        DBQueryFatal("ALTER TABLE aggregate_history ADD ".
		     "  `slice_urn` tinytext ".
		     "after exptidx");
    }
    if (!DBSlotExists("aggregate_history", "creator_urn")) {
        DBQueryFatal("ALTER TABLE aggregate_history ADD ".
		     "  `creator_urn` tinytext ".
		     "after slice_hrn");
    }
    if (!DBSlotExists("aggregate_history", "reported")) {
        DBQueryFatal("ALTER TABLE aggregate_history ADD ".
		     " `reported` datetime default NULL ".
		     "after destroyed");
    }
    if (! DBKeyExists("aggregate_history", "slice_urn")) {
	DBQueryFatal("ALTER TABLE `aggregate_history` " .
		     "add index `slice_urn` (`slice_urn`(255))");
    }
    if (! DBKeyExists("aggregate_history", "slice")) {
	DBQueryFatal("ALTER TABLE `aggregate_history` " .
		     "add index `slice` (`slice_uuid`,`slice_urn`(255))");
    }
    if (! DBKeyExists("aggregate_history", "urn")) {
	DBQueryFatal("ALTER TABLE `aggregate_history` " .
		     "add index `urn` (`urn`(255))");
    }
    if (! DBKeyExists("aggregate_history", "aggregate")) {
	DBQueryFatal("ALTER TABLE `aggregate_history` " .
		     "add index `aggregate` (`uuid`,`urn`(255))");
    }

    #
    # The manifest history table needs a urn too, since the clearinghouse
    # is going to store these for all CMs.
    #
    if (!DBSlotExists("manifest_history", "aggregate_urn")) {
        DBQueryFatal("ALTER TABLE manifest_history ADD ".
		     "  `aggregate_urn` tinytext ".
		     "after aggregate_uuid");
    }
    if (!DBSlotExists("manifest_history", "reported")) {
        DBQueryFatal("ALTER TABLE manifest_history ADD ".
		     " `reported` datetime default NULL ".
		     "after created");
    }
    # Also store original rspec.
    if (!DBSlotExists("manifest_history", "rspec")) {
        DBQueryFatal("ALTER TABLE manifest_history ADD ".
		     "  `rspec` text ".
		     "after reported");
    }
    if (! DBKeyExists("manifest_history", "aggregate_urn")) {
	DBQueryFatal("ALTER TABLE `manifest_history` " .
		     "add index `aggregate_urn` (`aggregate_urn`(255))");
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
76
    if (! DBKeyExists("manifest_history", "created")) {
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 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 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
	DBQueryFatal("ALTER TABLE `manifest_history` " .
		     "add KEY `created` (`created`)");
    }

    #
    # Also the ticket_history table.
    #
    if (!DBSlotExists("ticket_history", "slice_urn")) {
        DBQueryFatal("ALTER TABLE ticket_history ADD ".
		     "  `slice_urn` tinytext ".
		     "after owner_hrn");
    }
    if (!DBSlotExists("ticket_history", "owner_urn")) {
        DBQueryFatal("ALTER TABLE ticket_history ADD ".
		     "  `owner_urn` tinytext ".
		     "after uuid");
    }
    if (! DBKeyExists("ticket_history", "slice_urn")) {
	DBQueryFatal("ALTER TABLE `ticket_history` " .
		     "add index `slice_urn` (`slice_urn`(255))");
    }
    if (! DBKeyExists("ticket_history", "slice")) {
	DBQueryFatal("ALTER TABLE `ticket_history` " .
		     "add index `slice` (`slice_uuid`,`slice_urn`(255))");
    }

    #
    # Add last activity to geni users. This is just to record the last time
    # the user created a slice, nothing else. 
    #
    if (!DBSlotExists("geni_users", "last_activity")) {
        DBQueryFatal("ALTER TABLE geni_users ADD ".
		     " `last_activity` datetime default NULL ".
		     "after expires");
    }

    #
    # In the CM database, try to match up users so we can set the creator_urn.
    # Not really possible to do the slice_urn since we do not store that
    # in any of the history tables. It just so happens that we do not delete
    # users from the geni_users table. 
    #
    if ($dbname eq "geni-cm") {
	my $query_result =
	    DBQueryFatal("select idx,slice_uuid,creator_uuid ".
			 "  from aggregate_history");

	while (my ($idx,$slice_uuid,$creator_uuid) =
	       $query_result->fetchrow_array()) {
	    # We want to include local users.
	    my $geniuser = GeniUser->Lookup($creator_uuid, 1);
	    next
		if (!defined($geniuser));

	    my $creator_urn = $geniuser->urn();
	    next
		if (! GeniHRN::IsValid($creator_urn));
	    my $safe_urn = DBQuoteSpecial($creator_urn);

	    DBQueryFatal("update aggregate_history set creator_urn=$safe_urn ".
			 "where idx='$idx'");
	}

	$query_result =
	    DBQueryFatal("select idx,slice_uuid,owner_uuid ".
			 "  from ticket_history");

	while (my ($idx,$slice_uuid,$owner_uuid) =
	       $query_result->fetchrow_array()) {
	    # We want to include local users.
	    my $geniuser = GeniUser->Lookup($owner_uuid, 1);
	    next
		if (!defined($geniuser));

	    my $owner_urn = $geniuser->urn();
	    next
		if (! GeniHRN::IsValid($owner_urn));
	    my $safe_urn = DBQuoteSpecial($owner_urn);

	    DBQueryFatal("update ticket_history set owner_urn=$safe_urn ".
			 "where idx='$idx'");
	}
    }
    return 0;
}
1;

# Local Variables:
# mode:perl
# End: