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


1.3
date	2001.08.23.19.05.27;	author kmaples;	state Exp;
branches;
next	1.2;

1.2
date	2001.07.25.01.26.40;	author kmaples;	state Exp;
branches;
next	1.1;

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


desc
@@


1.3
log
@Misc. table changes to support optimization of Helm/IZ api.
@
text
@#!/usr/bin/perl -w
# $Id: map_iz_to_helm.pl,v 1.2 2001/07/25 01:26:40 kmaples Exp $ 

# Populates the 'profiles' table with Helm user ids.

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});
my $HELMDBC = HelmDB::connect($ENV{DATABASE_NAME}, $ENV{DATABASE_USER}, $ENV{DATABASE_PASSWORD});

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

my %iz_users_by_id     = ();
my %helm_users_by_name = ();
my @@insert_pairs;
my @@move_users;

my @@r1 = HelmDB::runQuery($IZDBC, 'SELECT userid, login_name FROM profiles');

foreach(@@r1){
    $iz_users_by_id{$_->[0]} = $_->[1];
#    print "$_->[0] , $iz_users_by_id{$_->[0]}\n";  
}

my @@r2 = HelmDB::runQuery($HELMDBC, 'SELECT LOGIN_NAME, USER_ID FROM HELM_USER');

# Populate a hash of helm users: 
foreach(@@r2){
    $helm_users_by_name{$_->[0]} = $_->[1];
#    print "$_->[0] , $helm_users_by_name{$_->[0]}\n";  
}

foreach(keys(%iz_users_by_id)){
    my $cur_name = clean_username($iz_users_by_id{$_}); 
    if( $helm_users_by_name{ $cur_name } ){
        push(@@insert_pairs, "$_:$helm_users_by_name{ $cur_name }");
    } else {
        # Conundrum.  The user didn't have an account in helm.  This may mean 
        # that the user is a programmatic user, like the issues mailing lists,
        # or an account in IZ that was deleted from helm, etc.
        1;
        push(@@move_users, " ($_, '$cur_name') ");
    }
}

my $rv;

foreach(@@insert_pairs){
    my ($izid, $huid) = split(':',$_);
    $rv = HelmDB::runSQL($IZDBC, 
        "UPDATE profiles SET helm_user_id = $huid WHERE userid = $izid"
    );
}
    
if(@@move_users){
    $rv = HelmDB::runSQL($IZDBC, 
        "INSERT INTO profiles_internal VALUES " . join(',', @@move_users)
    );
}

# It's now safe to drop columns:
$rv = HelmDB::runSQL($IZDBC, "ALTER IGNORE TABLE profiles DROP COLUMN realname");
$rv = HelmDB::runSQL($IZDBC, "ALTER IGNORE TABLE profiles DROP COLUMN login_name");

print "Done populating profiles.helm_user_id for $issues_database\n";

sub clean_username {
    my $n = shift;

    $n =~ s/[\.\@@]$ENV{DOMAIN}$//;
    $n =~ s/^ex-//;
    $n =~ s/^$ENV{MAIL_PREFIX}// if $ENV{MAIL_PREFIX};
    return $n;
}




@


1.2
log
@Sorting out migration of IZ user record data in the case of programmatic
users
@
text
@d2 1
a2 1
# $Id: map_iz_to_helm.pl,v 1.1 2001/07/24 23:16:42 kmaples Exp $ 
d4 1
a4 1
# Populates the 'issuezilla_x_helm' lookup table
d50 1
d59 1
a59 1
        push(@@insert_pairs, " ($_,  $helm_users_by_name{ $cur_name }) ");
d65 1
a65 1
        push(@@move_users, " ($_, '$iz_users_by_id{$_}') ");
d69 6
a74 2
my $rv = HelmDB::runSQL($IZDBC, 
    "INSERT INTO issuezilla_x_helm VALUES " . join(',', @@insert_pairs)
d76 1
d85 2
a86 2
$rv = HelmDB::runSQL($IZDBC, "ALTER TABLE profiles DROP COLUMN realname");
$rv = HelmDB::runSQL($IZDBC, "ALTER TABLE profiles DROP COLUMN login_name");
d88 1
a88 1
print "Done populating issuezilla_x_helm for $issues_database\n";
d93 1
a93 1
    $n =~ s/\@@.*$//;
a95 1

@


1.1
log
@Added bootstrapping script for iz-to-helm lookup table; fixed table defs.
@
text
@d2 1
a2 1
# $Id: $ 
d39 1
a44 2
    $iz_users_by_id{$_->[0]} =~ s/\@@.*$//;
    $iz_users_by_id{$_->[0]} =~ s/^$ENV{MAIL_PREFIX}// if $ENV{MAIL_PREFIX};
d56 9
a64 2
    if( $helm_users_by_name{ $iz_users_by_id{$_} } ){
        push(@@insert_pairs, " ($_,  $helm_users_by_name{ $iz_users_by_id{$_} }) ");
d72 10
d83 14
@

