3 die "This is broken. Don't use it!\n";
6 use FS::UID qw(adminsuidsetup);
7 use FS::Record qw(qsearchs qsearch);
11 my @tables = qw(svc_acct svc_broadband svc_domain svc_external svc_forward svc_www cust_svc domain_record);
12 #my @tables = qw(svc_www);
14 my $user = shift or die &usage;
15 my $dbh = adminsuidsetup($user);
17 my $dbdef = FS::Record::dbdef;
19 foreach my $table (@tables) {
21 my $h_table = 'h_' . $table;
25 eval "use FS::${table}";
27 eval "use FS::${h_table}";
30 print "Adding history records for ${table}...\n";
32 my $dbdef_table = $dbdef->table($table);
33 my $pkey = $dbdef_table->primary_key;
35 foreach my $rec (qsearch($table, {})) {
37 #my $h_rec = qsearchs(
39 # { $pkey => $rec->getfield($pkey) },
40 # eval "FS::${h_table}->sql_h_searchs(time)",
45 { $pkey => $rec->getfield($pkey) },
46 "DISTINCT ON ( $pkey ) *",
47 "AND history_action = 'insert' ORDER BY $pkey ASC, history_date DESC",
53 my $h_insert_rec = $rec->_h_statement('insert', 1);
54 #print $h_insert_rec . "\n";
55 $dbh->do($h_insert_rec);
56 die $dbh->errstr if $dbh->err;
57 $dbh->commit or die $dbh->errstr;
66 print "History records inserted into $h_table: $cnt\n";
67 print " Total records in $table: $t_cnt\n";
73 foreach my $table (@tables) {
75 my $h_table = 'h_' . $table;
78 eval "use FS::${table}";
80 eval "use FS::${h_table}";
83 print "Adding insert records for unmatched delete records on ${table}...\n";
85 my $dbdef_table = $dbdef->table($table);
86 my $pkey = $dbdef_table->primary_key;
88 #SELECT * FROM h_svc_www
89 #DISTINCT ON ( $pkey ) ?
92 SELECT ${h_table}1.${pkey}
93 FROM ${h_table} as ${h_table}1
95 SELECT count(${h_table}2.${pkey})
96 FROM ${h_table} as ${h_table}2
97 WHERE ${h_table}2.${pkey} = ${h_table}1.${pkey}
98 AND ${h_table}2.history_action = 'delete'
101 SELECT count(${h_table}3.${pkey})
102 FROM ${h_table} as ${h_table}3
103 WHERE ${h_table}3.${pkey} = ${h_table}1.${pkey}
104 AND ( ${h_table}3.history_action = 'insert'
105 OR ${h_table}3.history_action = 'replace_new' )
107 GROUP BY ${h_table}1.${pkey})";
110 my @h_recs = qsearch(
112 "DISTINCT ON ( $pkey ) *",
118 foreach my $h_rec (@h_recs) {
119 #print "Adding insert record for deleted record with pkey='" . $h_rec->getfield($pkey) . "'...\n";
120 my $class = 'FS::' . $table;
121 my $rec = $class->new({ $h_rec->hash });
122 my $h_insert_rec = $rec->_h_statement('insert', 1);
123 #print $h_insert_rec . "\n";
124 $dbh->do($h_insert_rec);
125 die $dbh->errstr if $dbh->err;
126 $dbh->commit or die $dbh->errstr;
130 print "History records inserted into $h_table: $cnt\n";
137 die "Usage:\n add-history-records.pl user\n";