#!/usr/bin/perl
$|++;
my
$VERSION
=
'0.09'
;
use
lib
qw(./lib ../lib)
;
my
%options
;
init_options();
rebuild();
sub
rebuild {
my
$next
;
my
$next
=
$options
{source}->iterator(
'hash'
,
"SELECT * FROM users"
);
while
(
my
$row
=
$next
->()) {
$options
{source}->do_query(
'DELETE FROM ixaddress WHERE userid=?'
,
$row
->{userid})
if
(
$options
{run});
_log(
"delete old entries for $row->{realname} [$row->{userid}]"
);
my
$next2
=
$options
{source}->iterator(
'hash'
,
"SELECT * FROM ixtester WHERE userid=? AND confirmed=1"
,
$row
->{userid});
while
(
my
$row2
=
$next2
->()) {
my
@rows
=
$options
{source}->get_query(
'hash'
,
'SELECT addressid FROM testers.address WHERE email=?'
,
$row2
->{email});
for
my
$addr
(
@rows
) {
$options
{source}->do_query(
'INSERT INTO ixaddress SET userid=?,addressid=?'
,
$row
->{userid},
$addr
->{addressid})
if
(
$options
{run});
_log(
"mapped $row->{userid} to $addr->{addressid} for $row2->{email}"
);
}
}
}
}
sub
init_options {
GetOptions( \
%options
,
'config=s'
,
'run'
,
'verbose'
,
'help|h'
,
);
_help(1)
if
(
$options
{help});
die
"Configuration file [$options{config}] not found\n"
unless
(-f
$options
{config});
my
$cfg
= Config::IniFiles->new(
-file
=>
$options
{config} );
my
%opts
=
map
{
$_
=>
$cfg
->val(
'DATABASE'
,
$_
);}
qw(driver database dbfile dbhost dbport dbuser dbpass)
;
$options
{source} = CPAN::Testers::Common::DBUtils->new(
%opts
);
die
"Cannot configure SOURCE database\n"
unless
(
$options
{source});
}
sub
_help {
my
$full
=
shift
;
if
(
$full
) {
print
<<HERE;
Usage: $0 \\
[-config=<file>] [-h] [-v]
--config=<file> database configuration file
-h this help screen
-v verbose mode
HERE
}
print
"$0 v$VERSION\n"
;
exit
(0);
}
sub
_log {
return
unless
(
$options
{verbose});
print
join
(
' '
,
@_
) .
"\n"
;
}