X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=bin%2Fadd-history-records.pl;h=fbf9d09d956d63d89d4b957bbcf2a42e3f050120;hp=16f91a18ff839d68868d0e1a68a79b54b70af8e7;hb=5f0388ca15d79d964b1c6197d0841d8f5c708d15;hpb=36ebe36593d21235be392fa8c2732c8bd17cad96 diff --git a/bin/add-history-records.pl b/bin/add-history-records.pl index 16f91a18f..fbf9d09d9 100755 --- a/bin/add-history-records.pl +++ b/bin/add-history-records.pl @@ -1,45 +1,139 @@ #!/usr/bin/perl +die "This is broken. Don't use it!\n"; use strict; use FS::UID qw(adminsuidsetup); use FS::Record qw(qsearchs qsearch); -use FS::svc_domain; -use FS::h_svc_domain; -use FS::domain_record; -use FS::h_domain_record; use Data::Dumper; -adminsuidsetup(shift); +my @tables = qw(svc_acct svc_broadband svc_domain svc_external svc_forward svc_www cust_svc domain_record); +#my @tables = qw(svc_www); +my $user = shift or die &usage; +my $dbh = adminsuidsetup($user); -my $svcnum = shift; +my $dbdef = FS::Record::dbdef; -my $svc_domain = qsearchs('svc_domain', { svcnum => $svcnum }) or die "no svcnum '$svcnum'"; +foreach my $table (@tables) { -my $h_svc_domain = qsearchs( - 'h_svc_domain', - { 'svcnum' => $svc_domain->svcnum }, - FS::h_svc_domain->sql_h_searchs(time), -); + my $h_table = 'h_' . $table; + my $cnt = 0; + my $t_cnt = 0; + + eval "use FS::${table}"; + die $@ if $@; + eval "use FS::${h_table}"; + die $@ if $@; + + print "Adding history records for ${table}...\n"; + + my $dbdef_table = $dbdef->table($table); + my $pkey = $dbdef_table->primary_key; + + foreach my $rec (qsearch($table, {})) { + + #my $h_rec = qsearchs( + # $h_table, + # { $pkey => $rec->getfield($pkey) }, + # eval "FS::${h_table}->sql_h_searchs(time)", + #); + + my $h_rec = qsearchs( + $h_table, + { $pkey => $rec->getfield($pkey) }, + "DISTINCT ON ( $pkey ) *", + "AND history_action = 'insert' ORDER BY $pkey ASC, history_date DESC", + '', + 'AS maintable', + ); + + unless ($h_rec) { + my $h_insert_rec = $rec->_h_statement('insert', 1); + #print $h_insert_rec . "\n"; + $dbh->do($h_insert_rec); + die $dbh->errstr if $dbh->err; + $dbh->commit or die $dbh->errstr; + $cnt++; + } + + + $t_cnt++; + + } + + print "History records inserted into $h_table: $cnt\n"; + print " Total records in $table: $t_cnt\n"; + + print "\n"; -unless ($h_svc_domain) { - print $svc_domain->_h_statement('insert', 1) . "\n"; } -foreach my $rec ($svc_domain->domain_record) { - my $h_rec = qsearchs( - 'h_domain_record', - { 'svcnum' => $svc_domain->svcnum }, - FS::h_domain_record->sql_h_searchs(time), - ); +foreach my $table (@tables) { + + my $h_table = 'h_' . $table; + my $cnt = 0; + + eval "use FS::${table}"; + die $@ if $@; + eval "use FS::${h_table}"; + die $@ if $@; + + print "Adding insert records for unmatched delete records on ${table}...\n"; - #print Dumper($h_rec); + my $dbdef_table = $dbdef->table($table); + my $pkey = $dbdef_table->primary_key; - unless ($h_rec) { - print $rec->_h_statement('insert', 1) . "\n"; + #SELECT * FROM h_svc_www + #DISTINCT ON ( $pkey ) ? + my $where = " + WHERE ${pkey} in ( + SELECT ${h_table}1.${pkey} + FROM ${h_table} as ${h_table}1 + WHERE ( + SELECT count(${h_table}2.${pkey}) + FROM ${h_table} as ${h_table}2 + WHERE ${h_table}2.${pkey} = ${h_table}1.${pkey} + AND ${h_table}2.history_action = 'delete' + ) > 0 + AND ( + SELECT count(${h_table}3.${pkey}) + FROM ${h_table} as ${h_table}3 + WHERE ${h_table}3.${pkey} = ${h_table}1.${pkey} + AND ( ${h_table}3.history_action = 'insert' + OR ${h_table}3.history_action = 'replace_new' ) + ) = 0 + GROUP BY ${h_table}1.${pkey})"; + + + my @h_recs = qsearch( + $h_table, { }, + "DISTINCT ON ( $pkey ) *", + $where, + '', + '' + ); + + foreach my $h_rec (@h_recs) { + #print "Adding insert record for deleted record with pkey='" . $h_rec->getfield($pkey) . "'...\n"; + my $class = 'FS::' . $table; + my $rec = $class->new({ $h_rec->hash }); + my $h_insert_rec = $rec->_h_statement('insert', 1); + #print $h_insert_rec . "\n"; + $dbh->do($h_insert_rec); + die $dbh->errstr if $dbh->err; + $dbh->commit or die $dbh->errstr; + $cnt++; } + print "History records inserted into $h_table: $cnt\n"; + +} + + + +sub usage { + die "Usage:\n add-history-records.pl user\n"; }