sitevarsdiff 5.3 KB
Newer Older
1 2 3
#!/usr/bin/perl -w

#
4
# Copyright (c) 2000-2002, 2006, 2008 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
# 
# {{{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/>.
# 
# }}}
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
#


#
# schemadiff - Find the differences between two database schemas, as reported
# by mysqldump. Can print out a summary of changes, or SQL commands to make
# the necessary changes
#

use strict;
use Getopt::Std; 

#
# Default settings for the options
#
my $debug = 0;
my $sql = 1;
my $quiet = 0;

#
# Return value - number of differences between the two files
#
my $diff = 0;

#
# Process command-line args
#
my %opt;
getopts('hdsq', \%opt); 
if ($opt{h}) {
    exit &usage;
}
if ($opt{d}) {
    $debug = 1;
}
if ($opt{s}) {
    $sql = 0;
}
if ($opt{q}) {
    $quiet = 1;
    $sql = 0;
    $debug = 0;
}

if (@ARGV != 2) {
    exit &usage
}

72 73
sub read_sitevars($$);

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
my ($file1,$file2) = @ARGV;

my %sitevars1 = ();
my %sitevars2 = ();

my %sitevars  = ();

read_sitevars(\%sitevars1, $file1);
read_sitevars(\%sitevars2, $file2);

#
# Take a union of the keynames.
#
foreach my $i (keys %sitevars1) { $sitevars{$i} = 1; }
foreach my $i (keys %sitevars2) { $sitevars{$i} = 1; }

foreach my $i (keys %sitevars) {
    if (exists $sitevars1{$i} && exists $sitevars2{$i}) {
	my @setClause = ();
	if ($sitevars1{$i}{"dval"} ne $sitevars2{$i}{"dval"}) {	
	    push @setClause, "defaultvalue=" . $sitevars2{$i}{"dval"};
	}
	if ($sitevars1{$i}{"desc"} ne $sitevars2{$i}{"desc"}) {	
	    push @setClause, "description=" . $sitevars2{$i}{"desc"};
	}
99 100 101
	if ($sitevars1{$i}{"ns_include"} ne $sitevars2{$i}{"ns_include"}) {
	    push @setClause, "ns_include=" . $sitevars2{$i}{"ns_include"};
	}
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
	if (@setClause) {
	    $diff++;
	    if (!$quiet) { print "# sitevar $i was changed.\n"; }
	    if ($sql) {
		print "UPDATE sitevariables SET " . 
		      join(",",@setClause) . " " .
		      "WHERE name=$i;\n\n";
	    }
	}
    } elsif (exists $sitevars1{$i} && ! exists $sitevars2{$i}) {
	$diff++;
	if (!$quiet) { print "# sitevar $i was deleted.\n"; }
	if ($sql) {
	    print "DELETE FROM sitevariables WHERE name=$i;\n\n";
	}
    } elsif (! exists $sitevars1{$i} && exists $sitevars2{$i}) {
	$diff++;
	if (!$quiet) { print "# sitevar $i was added.\n"; }
	if ($sql) {
	    print "INSERT INTO sitevariables VALUES ".
		  "(".
	          "$i,".
	          "NULL" . ",".
	          $sitevars2{$i}{"dval"} . ",".
126 127
	          $sitevars2{$i}{"desc"} . ",".
		  $sitevars2{$i}{"ns_include"} .
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
	          ");\n\n";
	}
    }
}

if ($diff > 0) {
    exit(1);
} else {
    exit(0);
}

######################################################################
# Subs
######################################################################

sub read_sitevars($$) {

    my $hashref = shift;
    
    #
    # Open the file
    #
    my $filename = shift;
    open(FH,"<$filename") or die "Unable to open $filename: $!\n";

    while(<FH>) {
154
	my ($name, $val, $dval, $desc, $ns_include);
155

156
	if (/^INSERT( IGNORE)? INTO `?sitevariables`?/) {
157 158 159 160 161 162 163 164
	    #
	    # Either match NULL, or
	    #   a quote followed by some number of:
	    #     + characters which aren't backslashes or quotes, or
	    #     + slashes followed by any character
	    #   followed by a closed quote.
	    #
	    my $match = "^(('([^\\']|(\\\\.))*')|(NULL))";
165 166 167 168 169 170 171

	    #
	    # Either match NULL, or
	    #   a nonempty sequence of digits
            #
	    my $matchint = "^(([0-9]+)|(NULL))";

172
	    s/^INSERT( IGNORE)? INTO `?sitevariables`? VALUES \(//;
173
	    ($name) = /${match},/;
174
	    s/${match},// or die "bad name parse $_";
175
	    ($val) = /${match},/;
176
	    s/${match},// or die "bad val parse $_";
177
	    ($dval) = /${match},/;
178 179 180 181 182
	    s/${match},// or die "bad dval parse $_";
	    ($desc) = /${match},/;
	    s/${match},// or die "bad desc parse $_";
	    ($ns_include) = /${matchint}\);/;
	    s/${matchint}\);// or die "bad ns_include parse $_";
183 184 185 186

            debug("Adding variable $name...\n");
            ${$hashref}{$name}{"dval"} = $dval;
            ${$hashref}{$name}{"desc"} = $desc;
187
	    ${$hashref}{$name}{"ns_include"} = $ns_include;
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
        }
    }

    close (FH);
}

#
# Print the message, only if the gloabal $debug variable is set
#
sub debug(@) {
    if ($debug) {
	print @_;
    }
}

#
# Usage message
#
sub usage() {
    print "usage: $0 [-h] [-s] [-d] <file1> <file2>\n";
    print "Prints on stdout a set of SQL commands to change the database\n";
    print "from file1 so that it has the same sitevars as the database from\n";
    print "file2 (same argument order as diff)\n\n";
    print "-h    This message\n";
    print "-s    Summary: Don't print SQL statments to apply changes\n";
    print "-q    Quiet: Don't print anything, just return a value\n";
    print "-d    Turn on debugging output\n";
    return 1;
}