head	1.1;
access;
symbols;
locks; strict;
comment	@# @;


1.1
date	2001.08.28.21.46.30;	author kmaples;	state Exp;
branches;
next	;


desc
@@


1.1
log
@Issue number: 4053
Removed 'platform' and 'operating system' from database enums(); both are
now abstracted into their own tables, though no interface is currently being
provided for making edits to these fields.
@
text
@#!/usr/bin/perl -w
# $Id: $

# Populates the 'opsys' and 'platform' tables with basic info
# from enums

use strict;
use DBI;
use Getopt::Long;

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

my $issues_database = '';

GetOptions(
    'issues_database=s'         => \$issues_database,
);

unless($issues_database){
    print "No issues db specified.  Use '--issues_database=[db name]' to specify db.\n";
    exit;
}

# Get db connections to helm and IZ:
my $IZDBC   = HelmDB::connect($issues_database, $ENV{DATABASE_USER}, $ENV{DATABASE_PASSWORD});

END {
    $IZDBC->disconnect()   if $IZDBC;
}

my %info = %{ HelmDB::getTableInfo($IZDBC, 'issues', 'Type') };

#foreach(keys %info){
#    print "->$_ = $info{$_}\n";  
#}

if( $info{op_sys} =~ m/enum\((.*?)\)/ ){
    my @@values = split(',', $1);
    @@values    = grep { s/^'?(.+?)'?$/\('$1'\)/ } @@values;
    my $values = join(',', @@values);
    my $sql;
    my %opsys;
    my $rv;
    my @@pairs;
    
#    foreach(@@values){ print "  -> $_ \n"; }    
#    
    $sql = qq[INSERT INTO opsys (name) VALUES $values];
#    print $sql . "\n";
    $rv = HelmDB::runSQL($IZDBC, $sql);
    
    $sql = qq[SELECT name, id FROM opsys];  
    @@pairs = HelmDB::runQuery($IZDBC, $sql);

    # Time to modify the columns:
    $sql = qq[ALTER TABLE issues DROP INDEX op_sys];
    $rv = HelmDB::runSQL($IZDBC, $sql);
    
    $sql = qq[ALTER TABLE issues MODIFY op_sys tinytext NOT NULL];
    $rv = HelmDB::runSQL($IZDBC, $sql);
    
    foreach(@@pairs){
#        print "p: $_\n";
#        print "[0] = $_->[0]; [1] = $_->[1]\n";
        $opsys{ $_->[0] } =  $_->[1];        
    }
    
    foreach(keys(%opsys)){
#        print "k = $_; v = $opsys{$_}\n";
        $sql = qq[UPDATE issues SET op_sys = $opsys{$_} WHERE op_sys = '$_'];
#        print "$sql\n";
        $rv = HelmDB::runSQL($IZDBC, $sql);
    }
 
    $sql = qq[ALTER table issues MODIFY op_sys mediumint(9) NOT NULL];
    $rv = HelmDB::runSQL($IZDBC, $sql);
}

if( $info{rep_platform} =~ m/enum\((.*?)\)/ ){
    my @@values = split(',', $1);
    @@values    = grep { s/^'?(.+?)'?$/\('$1'\)/ } @@values;
    my $values = join(',', @@values);
    my $sql;
    my %platform;
    my $rv;
    my @@pairs;
    
    foreach(@@values){ print "  -> $_ \n"; }    
    
    $sql = qq[INSERT INTO platform (name) VALUES $values];
#    print "$sql\n";
    $rv = HelmDB::runSQL($IZDBC, $sql);
    
    $sql = qq[SELECT name, id FROM platform];  
    @@pairs = HelmDB::runQuery($IZDBC, $sql);

    # Time to modify the columns:
    $sql = qq[ALTER TABLE issues MODIFY rep_platform tinytext NOT NULL];
#    print "$sql\n";
    $rv = HelmDB::runSQL($IZDBC, $sql);

    foreach(@@pairs){
        $platform{ $_->[0] } =  $_->[1];        
    }
    
    foreach(keys(%platform)){
        $sql = qq[UPDATE issues SET rep_platform = $platform{$_} WHERE rep_platform = '$_'];
        $rv = HelmDB::runSQL($IZDBC, $sql);
    }
    $sql = qq[ALTER table issues MODIFY rep_platform mediumint(9) NOT NULL];
    $rv = HelmDB::runSQL($IZDBC, $sql);

}

@
