5 use Date::Parse 'str2time';
6 use Date::Format 'time2str';
7 use FS::UID qw(adminsuidsetup dbh);
13 getopts('H:U:P:D:T:', \%opt);
14 my $user = shift or die &usage;
16 my $dsn = 'dbi:mysql';
17 $dsn .= ":database=$opt{D}" if $opt{D};
18 $dsn .= ":host=$opt{H}" if $opt{H};
20 my $mysql = DBI->connect($dsn, $opt{U}, $opt{P})
25 my $fsdbh = FS::UID::dbh;
27 # check for existence of freesidestatus
28 my $table = $opt{T} || 'cdr';
29 my $status = $mysql->selectall_arrayref("SHOW COLUMNS FROM $table WHERE Field = 'freesidestatus'");
31 print "Adding freesidestatus column...\n";
32 $mysql->do("ALTER TABLE $table ADD COLUMN freesidestatus varchar(32)")
33 or die $mysql->errstr;
36 print "freesidestatus column present\n";
40 calldate clid src dst dcontext channel lastapp lastdata duration
41 billsec disposition amaflags accountcode uniqueid userfield) );
42 my $sql = 'SELECT '.join(',', @cols). " FROM $table WHERE freesidestatus IS NULL";
43 my $sth = $mysql->prepare($sql);
45 print "Importing ".$sth->rows." records...\n";
47 my $cdr_batch = new FS::cdr_batch({
48 'cdrbatch' => 'mysql-import-'. time2str('%Y/%m/%d-%T',time),
50 my $error = $cdr_batch->insert;
52 my $cdrbatchnum = $cdr_batch->cdrbatchnum;
57 while ( $row = $sth->fetchrow_hashref ) {
58 my $cdr = FS::cdr->new($row);
59 $cdr->startdate(str2time($cdr->calldate));
60 $cdr->cdrbatchnum($cdrbatchnum);
61 my $error = $cdr->insert;
63 print "failed import: $error\n";
67 if( $mysql->do("UPDATE cdr SET freesidestatus = 'done'
68 WHERE calldate = ? AND src = ? AND dst = ?",
78 print "failed to set status: ".$mysql->errstr."\n";
82 print "Done.\nImported $imports CDRs, marked $updates CDRs as done.\n";
86 "Usage: \n cdr-mysql.import\n\t[ -H host ]\n\t-D database\n\t-U user\n\t-P password\n\tfreesideuser\n";