head	1.20;
access;
symbols
	TIGRIS_1_1_0RC2:1.16
	TIGRIS_1_1_0RC1:1.16
	TIGRIS_1_1:1.16.0.4
	TIGRIS_1_0_8:1.16
	TIGRIS_1_0_8RC3:1.16
	TIGRIS_1_0_8RC2:1.16
	TIGRIS_1_0_8RC1:1.16
	TIGRIS_1_0_7:1.16
	TIGRIS_1_0_7RC3:1.16
	TIGRIS_1_0_7RC2:1.16
	TIGRIS_1_0_7RC1:1.16
	TIGRIS_1_0_6:1.16
	TIGRIS_1_0_6RC5:1.16
	TIGRIS_1_0_6RC4:1.16
	TIGRIS_1_0_6RC3:1.16
	TIGRIS_1_0_6RC2:1.16
	TIGRIS_1_0_6RC1:1.16
	TIGRIS_1_0_5:1.16
	TIGRIS_1_0_5RC6:1.16
	TIGRIS_1_0_5RC5:1.16
	TIGRIS_1_0_5RC4:1.16
	TIGRIS_1_0_5RC3:1.16
	TIGRIS_1_0_5RC2:1.16
	TIGRIS_1_0_5RC1:1.16
	TIGRIS_1_0_4:1.16
	TIGRIS_1_0_3:1.16
	TIGRIS_1_0_2:1.16
	TIGRIS_1_0_1:1.16
	TIGRIS_1_0:1.16.0.6
	TIGRIS_1_0_0:1.16
	TIGRIS_1_0_0_RC1:1.16.0.2
	dlr:1.1.1
	TIGRIS_0_9_2_4:1.6
	TIGRIS_0_9_2_3:1.6
	HELM_PEER_PORT_BRANCH:1.1.1.2
	TURBINE_PEER:1.6
	TIGRIS_0_9_2_1:1.6
	TIGRIS_0_9_2:1.6.0.2
	TIGRIS_0_9_0:1.5.0.4
	TIGRIS_0_8_4:1.5.0.2
	NEWHELM:1.1.1.1
	TIGRIS_NOV_12_2000:1.1.1;
locks; strict;
comment	@# @;


1.20
date	2001.04.17.06.35.25;	author dlr;	state Exp;
branches;
next	1.19;

1.19
date	2001.04.16.22.36.54;	author dlr;	state Exp;
branches;
next	1.18;

1.18
date	2001.04.16.22.32.40;	author dlr;	state Exp;
branches;
next	1.17;

1.17
date	2001.03.30.00.42.08;	author kmaples;	state Exp;
branches;
next	1.16;

1.16
date	2001.03.22.20.53.23;	author dlr;	state Exp;
branches;
next	1.15;

1.15
date	2001.03.22.08.36.48;	author dlr;	state Exp;
branches;
next	1.14;

1.14
date	2001.03.22.08.21.09;	author dlr;	state Exp;
branches;
next	1.13;

1.13
date	2001.03.22.07.26.28;	author dlr;	state Exp;
branches;
next	1.12;

1.12
date	2001.02.23.05.11.37;	author dlr;	state Exp;
branches;
next	1.11;

1.11
date	2001.02.21.19.02.09;	author dlr;	state Exp;
branches;
next	1.10;

1.10
date	2001.02.21.18.42.09;	author dlr;	state Exp;
branches;
next	1.9;

1.9
date	2001.02.21.18.23.48;	author dlr;	state Exp;
branches;
next	1.8;

1.8
date	2001.02.21.01.08.28;	author dlr;	state Exp;
branches;
next	1.7;

1.7
date	2001.01.31.19.21.39;	author kmaples;	state Exp;
branches;
next	1.6;

1.6
date	2001.01.17.23.55.40;	author dlr;	state Exp;
branches
	1.6.4.1;
next	1.5;

1.5
date	2000.12.04.05.12.23;	author edk;	state Exp;
branches;
next	1.4;

1.4
date	2000.12.02.01.13.17;	author edk;	state Exp;
branches;
next	1.3;

1.3
date	2000.11.14.22.45.57;	author thom;	state Exp;
branches;
next	1.2;

1.2
date	2000.11.13.21.42.39;	author thom;	state Exp;
branches;
next	1.1;

1.1
date	2000.11.12.21.17.44;	author dlr;	state Exp;
branches
	1.1.1.1;
next	;

1.1.1.1
date	2000.11.12.21.17.44;	author dlr;	state Exp;
branches;
next	1.1.1.2;

1.1.1.2
date	2001.02.21.00.36.24;	author dlr;	state Exp;
branches;
next	;

1.6.4.1
date	2001.02.07.03.58.30;	author dlr;	state Exp;
branches;
next	1.6.4.2;

1.6.4.2
date	2001.02.07.04.03.05;	author dlr;	state Exp;
branches;
next	1.6.4.3;

1.6.4.3
date	2001.02.07.20.28.35;	author dlr;	state Exp;
branches;
next	1.6.4.4;

1.6.4.4
date	2001.02.07.20.36.07;	author dlr;	state Exp;
branches;
next	1.6.4.5;

1.6.4.5
date	2001.02.07.20.38.06;	author dlr;	state Exp;
branches;
next	1.6.4.6;

1.6.4.6
date	2001.02.07.20.51.21;	author dlr;	state Exp;
branches;
next	1.6.4.7;

1.6.4.7
date	2001.02.07.20.58.50;	author dlr;	state Exp;
branches;
next	1.6.4.8;

1.6.4.8
date	2001.02.07.21.10.52;	author dlr;	state Exp;
branches;
next	1.6.4.9;

1.6.4.9
date	2001.02.07.21.18.37;	author dlr;	state Exp;
branches;
next	1.6.4.10;

1.6.4.10
date	2001.02.08.01.15.08;	author dlr;	state Exp;
branches;
next	1.6.4.11;

1.6.4.11
date	2001.02.08.01.18.51;	author dlr;	state Exp;
branches;
next	1.6.4.12;

1.6.4.12
date	2001.02.08.01.24.13;	author dlr;	state Exp;
branches;
next	1.6.4.13;

1.6.4.13
date	2001.02.14.01.41.49;	author dlr;	state Exp;
branches;
next	1.6.4.14;

1.6.4.14
date	2001.02.14.02.00.56;	author dlr;	state Exp;
branches;
next	1.6.4.15;

1.6.4.15
date	2001.02.15.00.52.01;	author ms;	state Exp;
branches;
next	1.6.4.16;

1.6.4.16
date	2001.02.20.12.47.53;	author edk;	state Exp;
branches;
next	;


desc
@@


1.20
log
@* Using the new HelmDB package.

* Removed the dbConnection() and runSQL() functions--they are now
static methods of the HelmDB package.  Replaced calls appropriately.
@
text
@#!/usr/bin/perl -w
# -*- Perl -*- script which creates the Tigris database and schema.  This
# script assumes that the SQL files for all components that you wish to install
# have been copied to the $SANDBOX/site/sql directory.
#
# $Id: create_tigris_database,v 1.19 2001/04/16 22:36:54 dlr Exp $

use DBI;
use strict;

BEGIN
{
    unshift(@@INC, "$ENV{SANDBOX}/scripts");
}
use HelmDB;

sub bootstrap();
sub componentScriptPath(@@);
sub gatherComponents();
sub getScriptsForAllComponents($);
sub parseScriptNames($);
sub runPreprocessorIfMissingOutput($);
sub runScripts($$);
	
# Call the utility script to drop the old issues databases associated with
# this user.
print 'Running remove_old_issue_dbs:', "\n";
system($ENV{SANDBOX} . '/helm/scripts/remove_old_issue_dbs');
print 'Finished running remove_old_issue_dbs', "\n";

# The Helm component.
my $HELM_CMPNT = 'helm';

# The name of the file containing the order in which the schema and table
# population scripts must be executed.
my $LOAD_ORDER_FILE = 'LoadOrder.lst';

my $dbh = bootstrap();

my $components = gatherComponents();

# Go about creating the database once we have assured that all generated files
# are in place.
runPreprocessorIfMissingOutput($components);
runScripts($dbh, $components);

# Cleanup and return success.
$dbh->disconnect();
exit 0;



# Prints a message and exits with error status.  Called on fatal error
# condition.  Prints trailing newline.
#
# ex. fatalError("Gratuitous documentation detected");  -->
#       create_tigris_database: Gratuitous documentation detected
sub fatalError
{
    print "FATAL: $0: ", @@_, "\n";
    exit 1;
}

# Gathers a list of components and returns an array reference to it.
sub gatherComponents()
{
    my @@components = ($HELM_CMPNT);
    # TODO: Allow a '-i' command line argument to specify alternate instance.
    my $instance = undef;
    if (! -z $ENV{INSTANCE}) {
        $instance = $ENV{INSTANCE};
    }
    push(@@components, $instance);
    foreach my $cmpnt (glob(componentScriptPath('*')))
    {
        chomp($cmpnt = `basename $cmpnt`);
        #print "cmpnt=$cmpnt\n";
        unless ($cmpnt eq $HELM_CMPNT ||
                (defined($instance) && $cmpnt eq $instance))
        {
            push @@components, $cmpnt;
        }
    }
    return \@@components;
}

# Runs the content of the named SQL, Perl, or executable script.  If SQL, uses
# the provided database connection.  Returns success as a boolean.
#
# ex. my $rv = runScript($dbh, 'setup_database.sql');
sub runScript($$)
{
    my ($dbh, $script, $sql, $rv) = (@@_, undef, 0);

    # Parse file extension.
    (my $ext = $script) =~ s/^.*\.([a-zA-Z]*)$/$1/;

    if (-f $script) {
        print "  Running $script\n";

        # Do the right thing for the file type implied by the extension.
        if ($ext eq 'sql') {
	    # Concat the file's contents, ignoring comment lines.
	    open(IN, $script);
	    while (<IN>) {
	        if (! (m/^\s*\#/ or m/^\s*\-+/)) {
	            s/\#.*$//;
	            $sql .= $_;
	        }
	    }
            close(IN);
	  
            if (defined($sql)) {
                foreach (split(/\;\s*$/m, $sql)) {
                    $rv = HelmDB::runSQL($dbh, $_);
                    if (!$rv) {
                        last;
                    }
                }
	    }
	    else {
                fatalError("$script has no executable statements");
	    }
        }
        elsif ($ext eq 'pl') {
	    $rv = (system('perl', '-w', "$script") == 0);
        }
        else {
            if (-x $script) {
                $rv = (system($script) == 0);
	    }
	    else {
                fatalError
                  ("Unknown file extension on non-executable file: $script");
            }
        }

        #print "rv=$rv\n";  # debug
        return $rv;
    }
    else {
        fatalError("File does not exist: $script");
    }
}

# Opens administrative and user database connections, creates the database
# (with no schema), sets up access for the user, and returns a reference to the
# user database.
# TODO: Commandilne flag to not do initial admin setup--only return $dbh.
#
# ex. my $dbh = bootstrap();
sub bootstrap()
{
    my $admin = HelmDB::connect('mysql', $ENV{DATABASE_ADMIN_USER},
                                $ENV{DATABASE_ADMIN_PASSWORD});
    print 'Bootstrapping Tigris database', "\n";
    runScript($admin, componentScriptPath($HELM_CMPNT, 'setup_database.sql'));
    HelmDB::runSQL($admin, 'flush privileges');
    runScript($admin, componentScriptPath($HELM_CMPNT, 'setup_user.sql'));
    $admin->disconnect();

    return HelmDB::connect($ENV{DATABASE_NAME}, $ENV{DATABASE_USER},
                          $ENV{DATABASE_PASSWORD});
}

# Constructs the path to the specified component's script directory.  If 
# provided, a second path snippet is appended.
#
# ex. my $loadOrder = componentScriptPath('tigrisinst', 'LoadOrder.lst');
sub componentScriptPath(@@)
{
    my $path = "$ENV{SANDBOX}/site/sql";
    foreach my $pathElement (@@_) {
        $path .= "/$pathElement";
    }
    #print "componentScriptPath=$path\n";  # debug
    return $path;
}

# Run configure.pl, if necessary.
#
# ex. my $components = [ $HELM_CMPNT, 'tigrisinst' ];
#     runPreprocessorIfMissingOutput($components);
sub runPreprocessorIfMissingOutput($)
{
    my ($components, $cmpnt, $f) = ($_[0]);
    foreach $cmpnt (@@$components) {
        foreach $f (glob(componentScriptPath($cmpnt, "*.sql.in"))) {
            $f =~ s/\.in$//;
            if (! -r $f) {
                print 'Configuring in preparation for Tigris schema update',
		  "\n";
                if (system("$ENV{SANDBOX}/scripts/configure.pl") != 0) {
                    fatalError("Configuration error: $?");
                }
                return;
            }
        }
    }
}

# Parses a list of SQL scripts from the configuration file, then returns the 
# list.
#
# ex. my @@scripts = parseScriptNames('tigrisinst');
sub parseScriptNames($)
{
    my ($path) = @@_;
    my $fullPath = componentScriptPath($path, $LOAD_ORDER_FILE);
    my @@scripts = ();
    if (-f $fullPath) {
        open(IN, "$fullPath");
        foreach (<IN>) {
            s/(\s*)\#.*$/$1/;
            if (! m/^\s*$/) {
                chomp;
                push(@@scripts, $_);
            }
        }
        close(IN);
    }
    else {
        print " WARNING: $LOAD_ORDER_FILE does not exist in $path\n";
    }
    return @@scripts;
}

# Parses list of the full path to scripts across all specified components.
#
# ex. my $components = [ $HELM_CMPNT, 'tigrisinst' ];
#     my $scripts = getScriptsForAllComponents($components);
sub getScriptsForAllComponents($)
{
    my $components = $_[0];
    my @@scripts = ();
    while (@@$components) {
        my $cmpntName = $components->[0];
        $cmpntName =~ s/\b(\w)/\U$1/;
	print "Determining scripts to run for $cmpntName\n";
        foreach my $script (parseScriptNames($components->[0])) {
            # Check for script existance in remaining components.
            foreach my $cmpnt (@@$components) {
                #print "Looking in $cmpnt for $script\n";  # debug
                my $f = componentScriptPath($cmpnt, $script);
                if (-f $f) {
                    push @@scripts, $f;
                }
	    }
	}
        shift @@$components;
    }
    return \@@scripts;
}

# Runs the scripts specified in the load order list of the first component in 
# the provided list of components.  Looks for the script in the component SQL
# files in reverse order (i.e. last in the list is the first checked).
#
# ex. my $components = [ $HELM_CMPNT, 'tigrisinst' ];
#     runScripts($dbh, $components);
sub runScripts($$)
{
    my ($dbh, $components) = @@_;
    print "Setting up Tigris schema\n";

    my $scripts = getScriptsForAllComponents($components);
    if (@@$scripts) {
        foreach (@@$scripts) {
            if (-f "$_") {
                runScript($dbh, "$_");
            }
            else {
                print " WARNING: couldn't find script $_\n";
            }
        }
    }
}
@


1.19
log
@For print, I assume a comma separating data to print is preferable over a
string concatentation.
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.18 2001/04/16 22:32:40 dlr Exp $
d11 6
a86 28
# Creates and returns a handle to the database (i.e. connection reference).
#
# ex. my $conn = dbConnection('tigris', 'user', 'passwd');
sub dbConnection($$$)
{
    my ($dbName, $dbUser, $dbPasswd) = @@_;
    #print "dsn=DBI:mysql:$dbName:$ENV{DATABASE_HOST}:$ENV{DATABASE_PORT}\n";
    return DBI->connect
      ("DBI:mysql:$dbName:$ENV{DATABASE_HOST}:$ENV{DATABASE_PORT}",
       $dbUser, $dbPasswd,
       # Die on error and ignore transactions.
       { RaiseError => 1, AutoCommit => 1 });
}

# Runs a SQL statement.
#
# ex. my $rv = runSQL($dbh, 'flush priviledges');
sub runSQL($$)
{
    my ($dbh, $sql) = @@_;
    #print "sql=$sql\n";  # debug
    $sql =~ s/(.*)\;\s*$/$1/s;
    my $sth = $dbh->prepare($sql);
    my $rv = $sth->execute();
    $sth->finish();
    return $rv;
}

d115 2
a116 2
                    $rv = runSQL($dbh, $_);
	            if (!$rv) {
d154 2
a155 2
    my $admin = dbConnection('mysql', $ENV{DATABASE_ADMIN_USER},
			     $ENV{DATABASE_ADMIN_PASSWORD});
d158 1
a158 1
    runSQL($admin, 'flush privileges');
d162 2
a163 2
    return dbConnection($ENV{DATABASE_NAME}, $ENV{DATABASE_USER},
			$ENV{DATABASE_PASSWORD});
@


1.18
log
@No longer need any Joist tables--Message has been deprecated by
HELM_MESSAGE.
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.17 2001/03/30 00:42:08 kmaples Exp $
d19 3
a21 4
# HACK: directly call the utility script to drop the old issues databases
# associated with this user (could be better integrated with the rest of
# this script):
print 'Running remove_old_issue_dbs:' . "\n";
d23 1
a23 1
print 'Finished running remove_old_issue_dbs' . "\n";
@


1.17
log
@Added call to remove_old_issue_dbs directly (apologies to dlr); this should
reduce the number of phantom bugs produced in sandboxes due to bad data/
old data.
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.16 2001/03/22 20:53:23 dlr Exp $
a15 1
sub runPartialJoistScripts($$);
a34 4
# HACK: Add the few necessary Joist tables.  Remove this when no longer
# dependant upon UnsecureServlet.
runPartialJoistScripts($dbh, "$ENV{SANDBOX}/joist/sql/org/joist");

a186 18
}

# Create the pieces of the Joist database and schema.
#
# ex. runPartialJoistScripts($dbh, "/full/path/to/sql");
sub runPartialJoistScripts($$)
{
    my ($dbh, $path) = @@_;
    print 'Setting up Joist schema', "\n";
    # Joist's request logging is currently turned off.
    #my @@scripts = ('create_Message.sql', 'create_Request.sql');
    my @@scripts = ('create_Message.sql');
    foreach my $script (@@scripts) {
        $script = "$path/$script";
        if (!runScript($dbh, $script)) {
            fatalError("Setup of Joist tables failed");
	}
    }
@


1.16
log
@Corrected comment.
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.15 2001/03/22 08:36:48 dlr Exp $
d20 7
@


1.15
log
@* Reorganzied function prototypes alphabetically whilst adding
componentScriptPath(@@).

* Refactored componentScriptPath function to treat all arguments as
path elements.  This is what it was already doing explictly for either
one or two arguments, but is a simpler and more accurate reflection of
how the function is now being used.
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.14 2001/03/22 08:21:09 dlr Exp $
d20 1
a20 1
# Helm's isntance.
@


1.14
log
@Added support for Tigris components other than just Helm and an
instance via a new gatherComponents() function.  As an added bonus,
now passing around array references rather than large arrays by value.
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.13 2001/03/22 07:26:28 dlr Exp $
d12 1
d14 2
a17 2
sub parseScriptNames($);
sub getScriptsForAllComponents($);
d209 1
a209 1
sub componentScriptPath
d211 2
a212 4
    my ($component, $pathElement) = @@_;
    my $path = "$ENV{SANDBOX}/site/sql/$component";
    #print "componentScriptPath: component=$component, path=$path\n";  # debug
    if (defined($pathElement)) {
d215 1
@


1.13
log
@Changed "instance" references to "component" where appropriate (which
was everywhere except for your configured $INSTANCE.
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.12 2001/02/23 05:11:37 dlr Exp $
d12 1
d14 1
a14 1
sub runPreprocessorIfMissingOutput(@@);
d16 2
a17 2
sub getScriptsForAllComponents(@@);
sub runScripts($@@);
d32 1
a32 6
my @@components = ($HELM_CMPNT);
# TODO: Allow a '-i' command line argument to specify alternate
# instance.
if (! -z $ENV{INSTANCE}) {
    push(@@components, $ENV{INSTANCE});
}
d36 2
a37 2
runPreprocessorIfMissingOutput(@@components);
runScripts($dbh, @@components);
d48 2
a49 1
# ex. fatalError("UH OH!");  -->  create_tigris_database: UH OH!
d56 23
d210 1
a210 1
    my ($component, $file) = @@_;
d213 2
a214 2
    if (defined($file)) {
        $path .= "/$file";
d221 3
a223 3
# ex. my @@components = ($HELM_CMPNT, 'tigrisinst');
#     runPreprocessorIfMissingOutput(@@components);
sub runPreprocessorIfMissingOutput(@@)
d225 2
a226 2
    my ($cmpnt, $f);
    foreach $cmpnt (@@_) {
d233 1
a233 1
                    fatalError('Configuration error');
d269 3
a271 3
# ex. my @@components = ($HELM_CMPNT, 'tigrisinst');
#     my @@scripts = getScriptsForAllComponents(@@components);
sub getScriptsForAllComponents(@@)
d273 1
a273 1
    my @@components = @@_;
d275 2
a276 2
    while (@@components) {
        my $cmpntName = $components[0];
d279 1
a279 1
        foreach my $script (parseScriptNames($components[0])) {
d281 1
a281 1
            foreach my $cmpnt (@@components) {
d289 1
a289 1
        shift @@components;
d291 1
a291 1
    return @@scripts;
d298 3
a300 3
# ex. my @@components = ($HELM_CMPNT, 'tigrisinst');
#     runScripts($dbh, @@components);
sub runScripts($@@)
d302 1
a302 3
    my $dbh = shift;
    my @@components = @@_;

d305 3
a307 3
    my @@scripts = getScriptsForAllComponents(@@components);
    if (@@scripts) {
        foreach (@@scripts) {
@


1.12
log
@Now creates a list of scripts to run and runs them all at once.
The intended behavior is as follows;
1. Connect to database and do initial bootstrapping.
2. Hack to create legacy Joist tables.
3. LoadOrder.lst for Helm is read (it includes helm-schema.sql as its
   first entry).
4. For every script in the list, append the one from Helm (if it exists),
   and then the one from the instance (if it exists).
5. Check for LoadOrder.lst in instance and append each script to the
   end of the list of scripts to run (if it exists).
@
text
@d3 1
a3 1
# script assumes that the SQL files for all instances that you wish to install
d6 1
a6 1
# $Id: create_tigris_database,v 1.11 2001/02/21 19:02:09 dlr Exp $
a10 1
# Function prototypes for Perl 6.
d15 1
a15 1
sub getScriptsForAllInstances(@@);
d19 1
a19 1
my $HELM_INST = 'helm';
d31 1
a31 1
my @@instances = ($HELM_INST);
d35 1
a35 1
    push(@@instances, $ENV{INSTANCE});
d40 2
a41 2
runPreprocessorIfMissingOutput(@@instances);
runScripts($dbh, @@instances);
d157 1
a157 1
    runScript($admin, instanceScriptPath($HELM_INST, 'setup_database.sql'));
d159 1
a159 1
    runScript($admin, instanceScriptPath($HELM_INST, 'setup_user.sql'));
d184 1
a184 1
# Constructs the path to the specified instance's script directory.  If 
d187 2
a188 2
# ex. my $loadOrder = instanceScriptPath('tigrisinst', 'LoadOrder.lst');
sub instanceScriptPath
d190 3
a192 3
    my ($instance, $file) = @@_;
    my $path = "$ENV{SANDBOX}/site/sql/$instance";
    #print "instanceScriptPath: instance=$instance, path=$path\n";  # debug
d201 2
a202 2
# ex. my @@instances = ($HELM_INST, 'tigrisinst');
#     runPreprocessorIfMissingOutput(@@instances);
d205 3
a207 3
    my ($inst, $f);
    foreach $inst (@@_) {
        foreach $f (glob(instanceScriptPath($inst, "*.sql.in"))) {
d228 1
a228 1
    my $fullPath = instanceScriptPath($path, $LOAD_ORDER_FILE);
d247 1
a247 1
# Parses list of the full path to scripts across all specified instances.
d249 3
a251 3
# ex. my @@instances = ($HELM_INST, 'tigrisinst');
#     my @@scripts = getScriptsForAllInstances(@@instances);
sub getScriptsForAllInstances(@@)
d253 1
a253 1
    my @@instances = @@_;
d255 9
a263 9
    while (@@instances) {
        my $instName = $instances[0];
        $instName =~ s/\b(\w)/\U$1/;
	print "Determining scripts to run for $instName\n";
        foreach my $script (parseScriptNames($instances[0])) {
            # Check for script existance in remaining instances.
            foreach my $inst (@@instances) {
                #print "Looking in $inst for $script\n";  # debug
                my $f = instanceScriptPath($inst, $script);
d269 1
a269 1
        shift @@instances;
d274 2
a275 2
# Runs the scripts specified in the load order list of the first instance in 
# the provided list of instances.  Looks for the script in the instance SQL
d278 2
a279 2
# ex. my @@instances = ($HELM_INST, 'tigrisinst');
#     runScripts($dbh, @@instances);
d283 1
a283 1
    my @@instances = @@_;
d287 1
a287 1
    my @@scripts = getScriptsForAllInstances(@@instances);
@


1.11
log
@Do what the last commit intended to do.
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.10 2001/02/21 18:42:09 dlr Exp $
d11 7
a17 6
# function prototype definitions 

sub bootstrap();			
sub runPartialJoistScripts($$);		
sub runPreprocessorIfMissingOutput(@@);	
sub runScripts($$);		
d32 1
a32 1
my @@instances = ();
a37 1
push(@@instances, $HELM_INST);
d42 1
d44 1
a44 5
runScript($dbh, instanceScriptPath($HELM_INST, 'helm-schema.sql'));
foreach (@@instances) {
    runScripts($dbh, $_);
}

a49 1

d174 3
a176 1
    my @@scripts = ('create_Message.sql', 'create_Request.sql');
d193 1
d225 5
a229 3
# ex. my ($scripts) = parseSQLScripts('tigrisinst');
sub parseSQLScripts($) {
    my $fullPath = instanceScriptPath($_, $LOAD_ORDER_FILE);
d243 1
a243 1
        print " WARNING: $LOAD_ORDER_FILE does not exist in $_\n";
d248 1
a248 3
# Runs the scripts for a specific instance (as specified in its load order
# list), dying if any of the scripts don't exist.  If no arguments are
# specified, the default instance is $HELM_INST.
d250 32
a281 2
# ex. runScripts($dbh, 'tigrisinst');
sub runScripts($$)
d284 1
a284 2
    my $instance = shift || $HELM_INST;
    my $path = instanceScriptPath($instance);
d286 3
a288 3
    $instance =~ s/\b(\w)/\U$1/;
    print "Setting up $instance schema\n";
    my (@@scripts, $file, $ext) = parseSQLScripts($instance);
d290 3
a292 3
        foreach $file (@@scripts) {
            if (-f "$path/$file") {
                runScript($dbh, "$path/$file");
d295 1
a295 2
                print " WARNING: couldn't find script $file\n";

@


1.10
log
@Handle interleaved scripts by running the instances first.
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.9 2001/02/21 18:23:48 dlr Exp $
d31 3
a33 2
my @@instances = ($HELM_INST);
# TODO: Allow a '-i' command line argument to specify instance.
d35 1
a35 1
    push($ENV{INSTANCE}, @@instances);
d37 1
@


1.9
log
@Removed location of script relative to the $SANDBOX directory.  We
might want to add this back in later, but for now the script can do
without it.
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.8 2001/02/21 01:08:28 dlr Exp $
d34 1
a34 1
    push(@@instances, $ENV{INSTANCE});
d41 1
@


1.8
log
@This is the highest revision from the HELM_PEER_PORT_BRANCH.  The previous import caused too many conflicts to resolve manually
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.6.4.16 2001/02/20 12:47:53 edk Exp $
a238 1
        #fatalError("File $fullPath does not exist");
a259 2
            # if the script isn't here, perhaps the path is based from $SANDBOX
            my $script = undef;
d261 1
a261 8
                $script = "$path/$file";
            }
            elsif (-f "$ENV{SANDBOX}/$file" ) {
                $script = "$ENV{SANDBOX}/$file";
            }

            if (defined($script)) {
                runScript($dbh, "$script");
@


1.7
log
@Changed script to honor DATABASE_HOST and DATABASE_PORT
@
text
@d1 4
a4 2
#!/bin/sh
# Creates the Tigris database and schema.
d6 1
a6 1
# $Id: create_tigris_database,v 1.6 2001/01/17 23:55:40 dlr Exp $
d8 48
a55 2
# Called on fatal error condition.
function fatal_error ()
d57 2
a58 2
    echo "${0}: ${1}"
    exit 1
d61 13
a73 6
# Type of SQL schema and data loading (used to select load list).
if [ $# -gt 0 ]; then
    LIST_TYPE=${1}
else
    LIST_TYPE='INSTALL'
fi
d75 13
a87 3
# Location of the Helm SQL scripts.
SCRIPT_DIR="${SANDBOX}/helm/sql/org/tigris/helm"
INSTANCE_SCRIPT_DIR="${SANDBOX}/site/sql/${INSTANCE}";
d89 10
a98 2
# The command used to run MySQL.
MYSQL_CMD="mysql -u${DATABASE_USER} -p${DATABASE_PASSWORD} --host=${DATABASE_HOST} --port=${DATABASE_PORT}"
d100 97
a196 2
# Create the Joist database and schema.
${SANDBOX}/joist/scripts/create_joist_database ${LIST_TYPE}
d199 20
a218 83
for sql in ${SCRIPT_DIR}/*.sql.in; do
    sql=`echo ${sql} | sed -e 's/\.in$//'`
    if [ ! -f ${sql} ]; then
        echo 'Configuring in preparation for Tigris schema update'
        ${SANDBOX}/scripts/configure.pl || fatal_error 'Configuration error'
        break
    fi
done

# The order in which the schema and table population scripts must be executed.
if [ -z "${LIST_TYPE}" ]; then
    LOAD_ORDER="${SCRIPT_DIR}/LoadOrder.lst"
else
    LOAD_ORDER="${SCRIPT_DIR}/${LIST_TYPE}.LoadOrder.lst"
fi

# Run the necessary SQL scripts to update to the Tigris schema.
echo "Updating to Tigris schema using ${LOAD_ORDER} list"

# Run each script, dying if the script doesn't exist.
if [ -f ${LOAD_ORDER} ]; then
    for file in `sed -e 's/#.*$//' ${LOAD_ORDER}`; do
        tname=`echo $file | sed -e 's/^.*\.\([a-zA-Z]*\)$/\1/'`
        case "$tname" in
            sql) if [ -f "${SCRIPT_DIR}/${file}" ]; then
                    echo "Running sql script ${SCRIPT_DIR}/${file}"
#                    echo "--running ${MYSQL_CMD} ${DATABASE_NAME} < ${SCRIPT_DIR}/${file}"
                    ${MYSQL_CMD} ${DATABASE_NAME} < ${SCRIPT_DIR}/${file}
                    if [ -f "${INSTANCE_SCRIPT_DIR}/${file}" ]; then
                        echo "Running sql script ${INSTANCE_SCRIPT_DIR}/${file}"
                        ${MYSQL_CMD} ${DATABASE_NAME} < \
                                ${INSTANCE_SCRIPT_DIR}/${file}
                    fi
                 elif [ -f "${SANDBOX}/${file}" ]; then
                    echo "Running sql script ${SANDBOX}/${file}"
                    ${MYSQL_CMD} ${DATABASE_NAME} < ${SANDBOX}/${file}
                 else
                    echo "SQL script ${file} not found in ${SANDBOX} or ${SCRIPT_DIR}"
                 fi
            ;;
            pl) if [ -f "${SCRIPT_DIR}/${file}" ]; then
                   echo "Running perl script ${SCRIPT_DIR}/${file}"
                   perl ${SCRIPT_DIR}/${file}
                else 
                   echo "SQL script ${file} not found in ${SANDBOX} or ${SCRIPT_DIR}"
                fi
            ;;
        esac
    done
else
    fatal_error "Can't determine load order: ${LOAD_ORDER} does not exist"
fi


if [ -z "${LOAD_ORDER}" ]; then
    INSTANCE_LOAD_ORDER="${INSTANCE_SCRIPT_DIR}/LoadOrder.lst"
else
    INSTANCE_LOAD_ORDER="${INSTANCE_SCRIPT_DIR}/${LIST_TYPE}.LoadOrder.lst"
fi
if [ -f ${INSTANCE_LOAD_ORDER} ]; then
    for file in `sed -e 's/#.*$//' ${INSTANCE_LOAD_ORDER}`; do
        tname=`echo $file | sed -e 's/^.*\.\([a-zA-Z]*\)$/\1/'`
        case "$tname" in 
            sql) if [ -f "${INSTANCE_SCRIPT_DIR}/${file}" ]; then
                    echo "Running sql script ${SCRIPT_DIR}/${file}"
                    ${MYSQL_CMD} ${DATABASE_NAME} < \
                        ${INSTANCE_SCRIPT_DIR}/${file}
                 else
                    echo "SQL script ${file} not found ${INSTANCE_SCRIPT_DIR}" 
                 fi
            ;;
            pl) if [ -f "${INSTANCE_SCRIPT_DIR}/${file}" ]; then
                   echo "Running perl script ${INSTANCE_SCRIPT_DIR}/${file}"
                   perl ${INSTANCE_SCRIPT_DIR}/${file}
                else 
                   echo "SQL script ${file} not found in ${INSTANCE_SCRIPT_DIR}"
                fi
            ;;
        esac
    done
else
    fatal_error "Can't determine load order for instance: ${INSTANCE_LOAD_ORDER} does not exist"
fi
d220 60
a279 1
exit 0
@


1.6
log
@Removed pointless comment.
@
text
@d4 1
a4 1
# $Id: create_tigris_database,v 1.5 2000/12/04 05:12:23 edk Exp $
d25 1
a25 1
MYSQL_CMD="mysql -u${DATABASE_USER} -p${DATABASE_PASSWORD}"
d57 1
@


1.6.4.1
log
@This rewrite of create_tigris_database in Perl *almost* works.  I'm
checking it in now to solicit feedback.
@
text
@d1 2
a2 4
#!/usr/bin/perl -w
# -*- Perl -*- script which creates the Tigris database and schema.  This
# script assumes that the SQL files for all instances that you wish to install
# have been copied to the $SANDBOX/site/sql directory.
d4 1
a4 1
# $Id: $
d6 2
a7 62
use DBI;

# Helm's isntance.
my $HELM_INST = 'helm';

# The name of the file containing the order in which the schema and table
# population scripts must be executed.
my $LOAD_ORDER_FILE = 'LoadOrder.lst';

my $dbh = bootstrap();

# HACK: Add the few necessary Joist tables.  Remove this when no longer
# dependant upon UnsecureServlet.
runPartialJoistScripts($dbh, "$ENV{SANDBOX}/joist/sql/org/joist");

my @@instances = ($HELM_INST);
# TODO: Allow a '-i' command line argument to specify instance.
if (! -z $ENV{INSTANCE}) {
    push(@@instances, $ENV{INSTANCE});
}

# Go about creating the database once we have assured that all generated files
# are in place.
runPreprocessorIfMissingOutput(@@instances);

foreach (@@instances) {
    runScripts($dbh, $_);
}

$dbh->disconnect();
exit 0;




# Prints a message and exits with error status.  Called on fatal error
# condition.  
#
# ex. fatalError("UH OH!");  -->  create_tigris_database: UH OH!
sub fatalError
{
    print "$0: ", @@_, "\n";
    exit 1;
}

# Creates and returns a handle to the database (i.e. connection reference).
#
# ex. my $conn = dbConnection('tigris', 'user', 'passwd');
sub dbConnection($$$)
{
    my ($dbName, $dbUser, $dbPasswd) = @@_;
    return DBI->connect
      ("DBI:mysql:$dbName:$ENV{DATABASE_HOST}:$ENV{DATABASE_PORT}",
       $dbUser, $dbPasswd,
       # Die on error and ignore transactions.
       { RaiseError => 1, AutoCommit => 1 });
}

# Runs a SQL statement.
#
# ex. my $rv = runSQL($dbh, 'flush priviledges');
sub runSQL($$)
d9 2
a10 10
    my ($dbh, $sql) = @@_;
    if (defined($sql)) {
        print "sql=$sql\n";  # debug
        $sql =~ s/(.*)\;\s*$/$1/s;
        my $sth = $dbh->prepare($sql);
        return $sth->execute();
    }
    else {
        # HELP
    }
d13 6
a18 19
# Runs the content of the named SQL script using the provided database
# connection.  Returns the return value of the SQL execution.
#
# ex. my $rv = runScript($dbh, 'setup_database.sql');
sub runScript($$)
{
    my ($dbh, $script, $sql) = (@@_, undef);
    open(IN, $script);
    while (<IN>) {
        if ($_ !~ m/^\s*\#/) {
            s/\#.*$//;
            $sql .= $_;
	}
    }
    close(IN);
    foreach (split(/\;\s*$/m, $sql)) {
        runSQL($dbh, $_);
    }
}
d20 3
a22 17
# Opens administrative and user database connections, creates the database
# (with no schema), sets up access for the user, and returns a reference to the
# user database.
# TODO: Commandilne flag to not do initial setup--only return $dbh.
#
# ex. my $dbh = bootstrap();
sub bootstrap()
{
    my $admin = dbConnection('mysql', $ENV{DATABASE_ADMIN_USER},
			     $ENV{DATABASE_ADMIN_PASSWORD});
    runScript($admin, instanceScriptPath($HELM_INST, "setup_database.sql"));
    runSQL($admin, 'flush privileges');
    runScript($admin, 'setup_user.sql');
    $admin->disconnect();
    return dbConnection($ENV{DATABASE_NAME}, $ENV{DATABASE_USER},
			$ENV{DATABASE_PASSWORD});
}
d24 2
a25 13
# Create the pieces of the Joist database and schema.
#
# ex. runPartialJoistScripts($dbh, "/full/path/to/sql");
sub runPartialJoistScripts($$)
{
    my ($dbh, $path) = @@_;
    my @@scripts = ('create_Message.sql', 'create_Request.sql');
    foreach my $script (@@scripts) {
        $script = "$path/$script";
        # TODO: Check return value.
        runScript($dbh, $script);
    }
}
d27 2
a28 13
# Constructs the path to the specified instance's script directory.  If 
# provided, a second path snippet is appended.
#
# ex. my $loadOrder = instanceScriptPath('tigrisinst', 'LoadOrder.lst');
sub instanceScriptPath
{
    my ($instance, $file) = @@_;
    my $path = "$ENV{SANDBOX}/site/sql/$instance";
    if (defined($file)) {
        $path .= "/$file";
    }
    return $path;
}
d31 82
a112 19
#
# ex. my @@instances = ($HELM_INST, 'tigrisinst');
#     runPreprocessorIfMissingOutput(@@instances);
sub runPreprocessorIfMissingOutput
{
    my ($inst, $f);
    foreach $inst (@@_) {
        foreach $f (glob(instanceScriptPath($inst, "*.sql.in"))) {
            $f =~ s/\.in$//;
            if (! -r $f) {
                print 'Configuring in preparation for Tigris schema update', "\n";
                if (system("$ENV{SANDBOX}/scripts/configure.pl") != 0) {
                    fatalError('Configuration error');
                }
                return;
            }
        }
    }
}
d114 1
a114 67
# Parses a list of SQL scripts from the configuration file, then returns the 
# list.
#
# ex. my ($scripts) = parseSQLScripts('tigrisinst');
sub parseSQLScripts($) {
    my $fullPath = instanceScriptPath($_, $LOAD_ORDER_FILE);
    if (-f $fullPath) {
        my @@scripts = ();
        open(IN, "$fullPath");
        foreach (<IN>) {
            s/(\s*)\#.*$/$1/;
            if (! m/^\s*$/) {
                chomp;
                push(@@scripts, $_);
            }
        }
        close(IN);
        return @@scripts;
    }
    else {
        fatalError("File $fullPath does not exist");
    }
}

# Runs the scripts for a specific instance (as specified in its load order
# list), dying if any of the scripts don't exist.  If no arguments are
# specified, the default instance is $HELM_INST.
#
# ex. runScripts($dbh, 'tigrisinst');
sub runScripts
{
    my $dbh = shift;
    my $instance = shift || $HELM_INST;
    my $path = instanceScriptPath($instance);
    my (@@scripts, $file, $ext) = parseSQLScripts($instance);
    foreach $file (@@scripts) {
        # Parse file extension.
        $ext = $file;
        $ext =~ s/^.*\.([a-zA-Z]*)$/$1/;
        #print "[path=$path] [file=$file] [ext=$ext]\n"; last;  # debug

        # Do the right thing for the file type implied by the extension.
        # TODO: Handle or rework INSTANCE script directory checks.
        if ($ext eq 'sql') {
            if (-f "$path/$file") {
                #print "Running SQL script $path/$file\n";
                # TODO: FIXME
	        runScript($dbh, "$path/$file");
            }
            else {
                print "SQL script $file not found in $path\n";
            }
        }
        elsif ($ext eq 'pl') {
            if (-f "$path/$file") {
                #print "Running Perl script $path/$file\n";
                system('perl', "$path/$file");
            }
            else {
                print "SQL script $file not found in $path\n";
            }
        }
        else {
            fatalError('Unknown file extension');
        }
    }
}
@


1.6.4.2
log
@Added comment to regexp-full block.
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.6.4.1 2001/02/07 03:58:30 dlr Exp $
a89 2

    # Concat the file's contents, ignoring comment lines.
a97 1

@


1.6.4.3
log
@* Removed defined($sql) check from runSQL() function.
* Revamped runScript() function to run just about any type of script,
  instead of just ones containing SQL.  This functionality was moved
  here from the runScripts() function, then refined.
* Corrected path to setup_user.sql script in bootstrap().
* Slimmed down runScripts().
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.6.4.2 2001/02/07 04:03:05 dlr Exp $
d72 9
a80 4
    print "sql=$sql\n";  # debug
    $sql =~ s/(.*)\;\s*$/$1/s;
    my $sth = $dbh->prepare($sql);
    return $sth->execute();
d83 2
a84 3
# Runs the content of the named SQL, Perl, or executable script.  If SQL, uses
# the provided database connection and returns the return value of the first
# SQL failure (if any), or the return value of the executed script.
a89 4
    # Parse file extension.
    $ext = $script;
    $ext =~ s/^.*\.([a-zA-Z]*)$/$1/;
    #print "[script=$script] [ext=$ext]\n"; last;  # debug
d91 7
a97 33
    if (-f $script) {
        if ($ext eq 'sql') {
	    # Concat the file's contents, ignoring comment lines.
	    open(IN, $script);
	    while (<IN>) {
	        if (! (m/^\s*\#/ or m/^\s*\-+/)) {
	            s/\#.*$//;
	            $sql .= $_;
	        }
	    }
            close(IN);
	  
            if (defined($sql)) {
                foreach (split(/\;\s*$/m, $sql)) {
	            runSQL($dbh, $_);
                }
	    }
	    else {
                print "$0: Script has no executable statements: $script\n";
	    }
        }
        elsif ($ext eq 'pl') {
	    system('perl', '-w', "$script");
        }
        else {
            if (-x $script) {
                system($script);
	    }
	    else {
                fatalError
		  ("Unknown file extension on non-executable file: $script");
            }
        }
d99 4
a102 2
    else {
        fatalError("File does not exist: $script");
d109 1
a109 1
# TODO: Commandilne flag to not do initial admin setup--only return $dbh.
d116 1
a116 1
    runScript($admin, instanceScriptPath($HELM_INST, 'setup_database.sql'));
d118 1
a118 1
    runScript($admin, instanceScriptPath($HELM_INST, 'setup_user.sql'));
a119 1

d209 5
d216 18
a233 2
        if (-f "$path/$file") {
            runScript($dbh, "$path/$file");
d236 1
@


1.6.4.4
log
@Brought runScript() in line with its comments about return value.
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.6.4.3 2001/02/07 20:28:35 dlr Exp $
d85 1
a85 2
    my ($dbh, $script, $sql, $rv) = (@@_, undef, 0);

d105 1
a105 5
                    # TODO: Handle return value on error.
                    $rv = runSQL($dbh, $_);
	            if (!$rv) {
                        last;
                    }
d113 1
a113 1
	    $rv = system('perl', '-w', "$script");
d117 1
a117 1
                $rv = (system($script) == 0);
a123 3

        #print "rv=$rv\n";
        return $rv;
@


1.6.4.5
log
@Handled TODO item in runPartialJoistScripts() function.
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.6.4.4 2001/02/07 20:36:07 dlr Exp $
d166 2
a167 3
        if (!runScript($dbh, $script)) {
            fatalError("Setup of Joist tables failed");
	}
@


1.6.4.6
log
@Corrected comment.
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.6.4.5 2001/02/07 20:38:06 dlr Exp $
d79 2
a80 1
# the provided database connection.  Returns success as a boolean.
@


1.6.4.7
log
@Various minor refinements.
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.6.4.6 2001/02/07 20:51:21 dlr Exp $
d44 1
a44 1
# condition.  Prints trailing newline.
d125 1
a125 1
                  ("Unknown file extension on non-executable file: $script");
d129 1
a129 1
        #print "rv=$rv\n";  # debug
d196 1
a196 2
                print 'Configuring in preparation for Tigris schema update',
		  "\n";
@


1.6.4.8
log
@Provide useful output.
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.6.4.7 2001/02/07 20:58:50 dlr Exp $
d72 1
a72 1
    #print "sql=$sql\n";  # debug
d89 1
a89 1
    #print "ext=$ext\n"; last;  # debug
a91 3
        print "  Running $script\n";

        # Do the right thing for the file type implied by the extension.
a146 1
    print 'Bootstrapping Tigris database', "\n";
a161 1
    print 'Setting up Joist schema', "\n";
a240 2

    print "Setting up $instance schema\n";
d243 2
@


1.6.4.9
log
@Make it purrrrrrrrty.
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.6.4.8 2001/02/07 21:10:52 dlr Exp $
d247 1
a248 2
    $instance =~ s/\b(\w)/\U$1/;
    print "Setting up $instance schema\n";
@


1.6.4.10
log
@Removed implemented TODO.
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.6.4.9 2001/02/07 21:18:37 dlr Exp $
d108 1
@


1.6.4.11
log
@Set return code to evaluation of exit status against exit success.
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.6.4.10 2001/02/08 01:15:08 dlr Exp $
d119 1
a119 1
	    $rv = (system('perl', '-w', "$script") == 0);
@


1.6.4.12
log
@Explicitly close the statement object.
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.6.4.11 2001/02/08 01:18:51 dlr Exp $
d75 1
a75 3
    my $rv = $sth->execute();
    $sth->finish();
    return $rv;
@


1.6.4.13
log
@Added data source name debug trace (commented out).
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.6.4.12 2001/02/08 01:24:13 dlr Exp $
a58 1
    #print "dsn=DBI:mysql:$dbName:$ENV{DATABASE_HOST}:$ENV{DATABASE_PORT}\n";
@


1.6.4.14
log
@Now more lenient WRT missing LoadOrder.lst files, printing a warning
message instead of dying with a fatal error and looking ooooogly.
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.6.4.13 2001/02/14 01:41:49 dlr Exp $
a219 1
    my @@scripts = ();
d221 1
d231 1
d234 1
a234 2
        #fatalError("File $fullPath does not exist");
        print "  WARNING: $LOAD_ORDER_FILE does not exist in $_\n";
a235 1
    return @@scripts;
d249 1
d252 5
a256 8
    my (@@scripts, $file, $ext) = parseSQLScripts($instance);
    if (@@scripts) {
        foreach $file (@@scripts) {
            if (-f "$path/$file") {
                runScript($dbh, "$path/$file");
            }
            else {
            }
@


1.6.4.15
log
@small changes to prototypes, making them work in perl 5.6 .
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.6.4.14 2001/02/14 02:00:56 dlr Exp $
a8 1
use strict;
a9 7
# function prototype definitions 

sub bootstrap();			
sub runPartialJoistScripts($$);		
sub runPreprocessorIfMissingOutput(@@);	
sub runScripts($$);		
	
d90 3
a92 1
    (my $ext = $script) =~ s/^.*\.([a-zA-Z]*)$/$1/;
d118 1
a118 1
                fatalError("$script has no executable statements");
d196 1
a196 1
sub runPreprocessorIfMissingOutput(@@)
d244 1
a244 1
sub runScripts($$)
@


1.6.4.16
log
@this script did not implement all the functionality of the old
create_tigris_database script, which allowed sql script to be specified
relative to $SANDBOX.  if you don't like this implementation (which bugs
me mildly, but not enough to fix it right now), change it -- but please
ensure that all the sql scripts are actually run after the changes.
@
text
@d6 1
a6 1
# $Id: create_tigris_database,v 1.6.4.15 2001/02/15 00:52:01 ms Exp $
d57 1
a57 1
    print "FATAL: $0: ", @@_, "\n";
d240 1
a240 1
        print " WARNING: $LOAD_ORDER_FILE does not exist in $_\n";
a260 2
            # if the script isn't here, perhaps the path is based from $SANDBOX
            my $script = undef;
d262 1
a262 8
                $script = "$path/$file";
            }
            elsif (-f "$ENV{SANDBOX}/$file" ) {
                $script = "$ENV{SANDBOX}/$file";
            }

            if (defined($script)) {
                runScript($dbh, "$script");
a264 2
                print " WARNING: couldn't find script $file\n";

@


1.5
log
@d'oh ... fix syntax errors which would have prevented this block
from ever working
@
text
@d4 1
a4 1
# $Id: create_tigris_database,v 1.4 2000/12/02 01:13:17 edk Exp $
a82 1
# this script needs to be replaced with perl or something.  sh is *ugly*.
@


1.4
log
@handle instanciation specific sql
@
text
@d4 1
a4 1
# $Id: create_tigris_database,v 1.3 2000/11/14 22:45:57 thom Exp $
d91 1
a91 1
    for file in `set -e 's/#.*$//' ${LOAD_ORDER}`; do
@


1.3
log
@remove #
@
text
@d4 1
a4 1
# $Id: create_tigris_database,v 1.2 2000/11/13 21:42:39 thom Exp $
d22 1
d58 5
d81 32
@


1.2
log
@added generic script list processing
@
text
@d4 1
a4 1
# $Id: create_tigris_database,v 1.1.1.1 2000/11/12 21:17:44 dlr Exp $
d27 1
a27 1
#${SANDBOX}/joist/scripts/create_joist_database ${LIST_TYPE}
@


1.1
log
@Initial revision
@
text
@d4 1
a4 1
# $Id: create_tigris_database,v 1.14 2000/11/11 07:07:03 edk Exp $
d21 1
a21 1
HELM_DIR="${SANDBOX}/helm/sql/org/tigris/helm"
d27 1
a27 1
${SANDBOX}/joist/scripts/create_joist_database ${LIST_TYPE}
d30 1
a30 1
for sql in ${HELM_DIR}/*.sql.in; do
d41 1
a41 1
    LOAD_ORDER="${HELM_DIR}/LoadOrder.lst"
d43 1
a43 1
    LOAD_ORDER="${HELM_DIR}/${LIST_TYPE}.LoadOrder.lst"
d49 1
a49 1
# Run each SQL script, dying if the script doesn't exist.
d51 21
a71 10
    for sql in `sed -e 's/#.*$//' ${LOAD_ORDER}`; do
        if [ -f "${HELM_DIR}/${sql}" ]; then
           echo "Running ${HELM_DIR}/${sql}"
           ${MYSQL_CMD} ${DATABASE_NAME} < ${HELM_DIR}/${sql}
        elif [ -f "${SANDBOX}/${sql}" ]; then
           echo "Running ${SANDBOX}/${sql}"
           ${MYSQL_CMD} ${DATABASE_NAME} < ${SANDBOX}/${sql}
        else
           echo "SQL script ${sql} not found in ${SANDBOX} or ${HELM_DIR}"
        fi
@


1.1.1.1
log
@Merging newhelm tree into helm, as per IZ issue #557.  Previous major version of helm is tagged TIGRIS_NOV_11_2000.
@
text
@@


1.1.1.2
log
@Replacing the HEAD with the HELM_PEER_PORT_BRANCH.
@
text
@d1 2
a2 4
#!/usr/bin/perl -w
# -*- Perl -*- script which creates the Tigris database and schema.  This
# script assumes that the SQL files for all instances that you wish to install
# have been copied to the $SANDBOX/site/sql directory.
d4 1
a4 1
# $Id: create_tigris_database,v 1.6.4.16 2001/02/20 12:47:53 edk Exp $
d6 2
a7 48
use DBI;
use strict;

# function prototype definitions 

sub bootstrap();			
sub runPartialJoistScripts($$);		
sub runPreprocessorIfMissingOutput(@@);	
sub runScripts($$);		
	
# Helm's isntance.
my $HELM_INST = 'helm';

# The name of the file containing the order in which the schema and table
# population scripts must be executed.
my $LOAD_ORDER_FILE = 'LoadOrder.lst';

my $dbh = bootstrap();

# HACK: Add the few necessary Joist tables.  Remove this when no longer
# dependant upon UnsecureServlet.
runPartialJoistScripts($dbh, "$ENV{SANDBOX}/joist/sql/org/joist");

my @@instances = ($HELM_INST);
# TODO: Allow a '-i' command line argument to specify instance.
if (! -z $ENV{INSTANCE}) {
    push(@@instances, $ENV{INSTANCE});
}

# Go about creating the database once we have assured that all generated files
# are in place.
runPreprocessorIfMissingOutput(@@instances);

foreach (@@instances) {
    runScripts($dbh, $_);
}

$dbh->disconnect();
exit 0;




# Prints a message and exits with error status.  Called on fatal error
# condition.  Prints trailing newline.
#
# ex. fatalError("UH OH!");  -->  create_tigris_database: UH OH!
sub fatalError
d9 2
a10 2
    print "FATAL: $0: ", @@_, "\n";
    exit 1;
d13 6
a18 13
# Creates and returns a handle to the database (i.e. connection reference).
#
# ex. my $conn = dbConnection('tigris', 'user', 'passwd');
sub dbConnection($$$)
{
    my ($dbName, $dbUser, $dbPasswd) = @@_;
    #print "dsn=DBI:mysql:$dbName:$ENV{DATABASE_HOST}:$ENV{DATABASE_PORT}\n";
    return DBI->connect
      ("DBI:mysql:$dbName:$ENV{DATABASE_HOST}:$ENV{DATABASE_PORT}",
       $dbUser, $dbPasswd,
       # Die on error and ignore transactions.
       { RaiseError => 1, AutoCommit => 1 });
}
d20 2
a21 24
# Runs a SQL statement.
#
# ex. my $rv = runSQL($dbh, 'flush priviledges');
sub runSQL($$)
{
    my ($dbh, $sql) = @@_;
    #print "sql=$sql\n";  # debug
    $sql =~ s/(.*)\;\s*$/$1/s;
    my $sth = $dbh->prepare($sql);
    my $rv = $sth->execute();
    $sth->finish();
    return $rv;
}

# Runs the content of the named SQL, Perl, or executable script.  If SQL, uses
# the provided database connection.  Returns success as a boolean.
#
# ex. my $rv = runScript($dbh, 'setup_database.sql');
sub runScript($$)
{
    my ($dbh, $script, $sql, $rv) = (@@_, undef, 0);

    # Parse file extension.
    (my $ext = $script) =~ s/^.*\.([a-zA-Z]*)$/$1/;
d23 2
a24 2
    if (-f $script) {
        print "  Running $script\n";
d26 2
a27 94
        # Do the right thing for the file type implied by the extension.
        if ($ext eq 'sql') {
	    # Concat the file's contents, ignoring comment lines.
	    open(IN, $script);
	    while (<IN>) {
	        if (! (m/^\s*\#/ or m/^\s*\-+/)) {
	            s/\#.*$//;
	            $sql .= $_;
	        }
	    }
            close(IN);
	  
            if (defined($sql)) {
                foreach (split(/\;\s*$/m, $sql)) {
                    $rv = runSQL($dbh, $_);
	            if (!$rv) {
                        last;
                    }
                }
	    }
	    else {
                fatalError("$script has no executable statements");
	    }
        }
        elsif ($ext eq 'pl') {
	    $rv = (system('perl', '-w', "$script") == 0);
        }
        else {
            if (-x $script) {
                $rv = (system($script) == 0);
	    }
	    else {
                fatalError
                  ("Unknown file extension on non-executable file: $script");
            }
        }

        #print "rv=$rv\n";  # debug
        return $rv;
    }
    else {
        fatalError("File does not exist: $script");
    }
}

# Opens administrative and user database connections, creates the database
# (with no schema), sets up access for the user, and returns a reference to the
# user database.
# TODO: Commandilne flag to not do initial admin setup--only return $dbh.
#
# ex. my $dbh = bootstrap();
sub bootstrap()
{
    my $admin = dbConnection('mysql', $ENV{DATABASE_ADMIN_USER},
			     $ENV{DATABASE_ADMIN_PASSWORD});
    print 'Bootstrapping Tigris database', "\n";
    runScript($admin, instanceScriptPath($HELM_INST, 'setup_database.sql'));
    runSQL($admin, 'flush privileges');
    runScript($admin, instanceScriptPath($HELM_INST, 'setup_user.sql'));
    $admin->disconnect();

    return dbConnection($ENV{DATABASE_NAME}, $ENV{DATABASE_USER},
			$ENV{DATABASE_PASSWORD});
}

# Create the pieces of the Joist database and schema.
#
# ex. runPartialJoistScripts($dbh, "/full/path/to/sql");
sub runPartialJoistScripts($$)
{
    my ($dbh, $path) = @@_;
    print 'Setting up Joist schema', "\n";
    my @@scripts = ('create_Message.sql', 'create_Request.sql');
    foreach my $script (@@scripts) {
        $script = "$path/$script";
        if (!runScript($dbh, $script)) {
            fatalError("Setup of Joist tables failed");
	}
    }
}

# Constructs the path to the specified instance's script directory.  If 
# provided, a second path snippet is appended.
#
# ex. my $loadOrder = instanceScriptPath('tigrisinst', 'LoadOrder.lst');
sub instanceScriptPath
{
    my ($instance, $file) = @@_;
    my $path = "$ENV{SANDBOX}/site/sql/$instance";
    if (defined($file)) {
        $path .= "/$file";
    }
    return $path;
}
d30 35
a64 45
#
# ex. my @@instances = ($HELM_INST, 'tigrisinst');
#     runPreprocessorIfMissingOutput(@@instances);
sub runPreprocessorIfMissingOutput(@@)
{
    my ($inst, $f);
    foreach $inst (@@_) {
        foreach $f (glob(instanceScriptPath($inst, "*.sql.in"))) {
            $f =~ s/\.in$//;
            if (! -r $f) {
                print 'Configuring in preparation for Tigris schema update',
		  "\n";
                if (system("$ENV{SANDBOX}/scripts/configure.pl") != 0) {
                    fatalError('Configuration error');
                }
                return;
            }
        }
    }
}

# Parses a list of SQL scripts from the configuration file, then returns the 
# list.
#
# ex. my ($scripts) = parseSQLScripts('tigrisinst');
sub parseSQLScripts($) {
    my $fullPath = instanceScriptPath($_, $LOAD_ORDER_FILE);
    my @@scripts = ();
    if (-f $fullPath) {
        open(IN, "$fullPath");
        foreach (<IN>) {
            s/(\s*)\#.*$/$1/;
            if (! m/^\s*$/) {
                chomp;
                push(@@scripts, $_);
            }
        }
        close(IN);
    }
    else {
        #fatalError("File $fullPath does not exist");
        print " WARNING: $LOAD_ORDER_FILE does not exist in $_\n";
    }
    return @@scripts;
}
d66 1
a66 35
# Runs the scripts for a specific instance (as specified in its load order
# list), dying if any of the scripts don't exist.  If no arguments are
# specified, the default instance is $HELM_INST.
#
# ex. runScripts($dbh, 'tigrisinst');
sub runScripts($$)
{
    my $dbh = shift;
    my $instance = shift || $HELM_INST;
    my $path = instanceScriptPath($instance);

    $instance =~ s/\b(\w)/\U$1/;
    print "Setting up $instance schema\n";
    my (@@scripts, $file, $ext) = parseSQLScripts($instance);
    if (@@scripts) {
        foreach $file (@@scripts) {
            # if the script isn't here, perhaps the path is based from $SANDBOX
            my $script = undef;
            if (-f "$path/$file") {
                $script = "$path/$file";
            }
            elsif (-f "$ENV{SANDBOX}/$file" ) {
                $script = "$ENV{SANDBOX}/$file";
            }

            if (defined($script)) {
                runScript($dbh, "$script");
            }
            else {
                print " WARNING: couldn't find script $file\n";

            }
        }
    }
}
@

