Committing the inserts helps.
[freeside.git] / bin / add-history-records.pl
1 #!/usr/bin/perl
2
3
4 use strict;
5 use FS::UID qw(adminsuidsetup);
6 use FS::Record qw(qsearchs qsearch);
7
8 use Data::Dumper;
9
10 my @tables = qw(svc_acct svc_broadband svc_domain svc_external svc_forward svc_www cust_svc domain_record);
11
12 my $user = shift or die &usage;
13 my $dbh = adminsuidsetup($user);
14
15 my $dbdef = FS::Record::dbdef;
16
17 foreach my $table (@tables) {
18
19   my $h_table = 'h_' . $table;
20   my $cnt = 0;
21   my $t_cnt = 0;
22
23   eval "use FS::${table}";
24   die $@ if $@;
25   eval "use FS::${h_table}";
26   die $@ if $@;
27
28   print "Adding history records for ${table}...\n";
29
30   my $dbdef_table = $dbdef->table($table);
31   my $pkey = $dbdef_table->primary_key;
32
33   foreach my $rec (qsearch($table, {})) {
34
35     my $h_rec = qsearchs(
36       $h_table,
37       { $pkey => $rec->getfield($pkey) },
38       eval "FS::${h_table}->sql_h_searchs(time)",
39     );
40
41     unless ($h_rec) {
42       my $h_insert_rec = $rec->_h_statement('insert', 1);
43       print $h_insert_rec . "\n";
44       $dbh->do($h_insert_rec);
45       die $dbh->errstr if $dbh->err;
46       $dbh->commit or die $dbh->errstr;
47       $cnt++;
48     }
49
50
51   $t_cnt++;
52
53   }
54
55   print "History records inserted into $h_table: $cnt\n";
56   print "               Total records in $table: $t_cnt\n";
57
58   print "\n";
59
60 }
61
62 sub usage {
63   die "Usage:\n  add-history-records.pl user\n";
64 }
65