Commit 6d546757 authored by Leigh B Stoller's avatar Leigh B Stoller

Add DBKeyExists() function for use in sql update scripts, to fix

error in protogeni/updates/14
parent ac6a35f1
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2008, 2009 University of Utah and the Flux Group.
# Copyright (c) 2008-2010 University of Utah and the Flux Group.
# All rights reserved.
#
package emdb;
......@@ -34,7 +34,7 @@ else {
NewTBDBHandle DBQueryN DBQueryFatalN DBQueryWarnN DBErrN
DBQuerySingleFatal DBQuerySingleFatalN TBDBDisconnect
DBTableExists DBSlotExists DBSlotType DBQuoteSpecial
TBDBConnect TBDBReconnect);
DBKeyExists TBDBConnect TBDBReconnect);
@EXPORT = (@EXPORT, @emdbi::EXPORT);
sub TBDBConnect($) { return emdbi::TBDBConnect($_[0], $DBNAME); }
......@@ -54,6 +54,7 @@ sub DBErrN($) { return emdbi::DBErrN($_[0]); }
sub DBErr() { return emdbi::DBErr(); }
sub DBTableExists($) { return emdbi::DBTableExists($_[0]); }
sub DBSlotExists($$) { return emdbi::DBSlotExists($_[0], $_[1]); }
sub DBKeyExists($$) { return emdbi::DBKeyExists($_[0], $_[1]); }
sub DBSlotType($$) { return emdbi::DBSlotType($_[0], $_[1]); }
# These are handled differently cause of tblog stuff.
sub DBWarn($;$) { DBError(\&tbwarn, $_[0], $_[1]); }
......
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2008, 2009 University of Utah and the Flux Group.
# Copyright (c) 2008-2010 University of Utah and the Flux Group.
# All rights reserved.
#
package emdbi;
......@@ -471,6 +471,21 @@ sub DBSlotTypeN($$$)
}
sub DBSlotType($$) { return DBSlotTypeN(0,$_[0],$_[1]); }
sub DBKeyExistsN($$$)
{
my($dbnum, $table, $keyname) = @_;
my $result =
DBQueryFatalN($dbnum, "show index from `$table`");
while (my (undef,undef,$kname,undef,$colname) = $result->fetchrow_array()){
return 1
if ($kname eq $keyname);
}
return 0;
}
sub DBKeyExists($$) { return DBKeyExistsN(0,$_[0],$_[1]); }
END {
# Call it here otherwise may get:
# (in cleanup) Can't call method "FETCH" on an undefined value at
......
#!/usr/bin/perl -w
#
# GENIPUBLIC-COPYRIGHT
# Copyright (c) 2008-2009 University of Utah and the Flux Group.
# Copyright (c) 2008-2010 University of Utah and the Flux Group.
# All rights reserved.
#
package GeniDB;
......@@ -12,7 +12,8 @@ use vars qw(@ISA @EXPORT);
@ISA = "Exporter";
@EXPORT = qw(DBQuery DBQueryFatal DBQueryWarn DBWarn DBFatal DBErr
DBQuoteSpecial DBTableExists DBSlotExists DBSetDefault
DBSlotType DBConnect GENISA_DBNAME GENICH_DBNAME GENICM_DBNAME);
DBSlotType DBKeyExists
DBConnect GENISA_DBNAME GENICH_DBNAME GENICM_DBNAME);
# Configure variables
my $TB = "@prefix@";
......@@ -35,6 +36,7 @@ sub DBFatal($;$) { emdbi::DBFatal($_[0], $_[1]); }
sub DBErr() { return emdbi::DBErrN($dbnum); }
sub DBTableExists($) { return emdbi::DBTableExistsN($dbnum, $_[0]); }
sub DBSlotExists($$) { return emdbi::DBSlotExistsN($dbnum, $_[0], $_[1]); }
sub DBKeyExists($$) { return emdbi::DBKeyExistsN($dbnum, $_[0], $_[1]); }
sub DBSlotType($$) { return emdbi::DBSlotTypeN($dbnum, $_[0], $_[1]); }
$emdbi::DBQUERY_DEBUG = 0;
......@@ -124,6 +126,8 @@ sub TableExists($$) {
return emdbi::DBTableExistsN($_[0]->DBnum(), $_[0]); }
sub SlotExists($$$) {
return emdbi::DBSlotExistsN($_[0]->DBnum(), $_[0], $_[1]); }
sub KeyExists($$$) {
return emdbi::DBKeyExistsN($_[0]->DBnum(), $_[0], $_[1]); }
sub SlotType($$$) {
return emdbi::DBSlotExistsN($_[0]->DBnum(), $_[0], $_[1]); }
......
......@@ -10,8 +10,10 @@ sub DoUpdate($$$)
DBSetDefault($dbhandle);
DBQueryFatal("ALTER TABLE `geni_authorities` " .
"add unique key `urn` (`urn`(256))");
if (! DBKeyExists("geni_authorities", "urn")) {
DBQueryFatal("ALTER TABLE `geni_authorities` " .
"add unique key `urn` (`urn`(255))");
}
DBQueryFatal("ALTER TABLE `geni_slices` " .
"ADD COLUMN `creator_urn` tinytext after creator_uuid")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment