fix wa_tax_rate_update script to skip zero rates, #73226
[freeside.git] / bin / add-history-records.pl
1 #!/usr/bin/perl
2
3 die "This is broken.  Don't use it!\n";
4
5 use strict;
6 use FS::UID qw(adminsuidsetup);
7 use FS::Record qw(qsearchs qsearch);
8
9 use Data::Dumper;
10
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);
13
14 my $user = shift or die &usage;
15 my $dbh = adminsuidsetup($user);
16
17 my $dbdef = FS::Record::dbdef;
18
19 foreach my $table (@tables) {
20
21   my $h_table = 'h_' . $table;
22   my $cnt = 0;
23   my $t_cnt = 0;
24
25   eval "use FS::${table}";
26   die $@ if $@;
27   eval "use FS::${h_table}";
28   die $@ if $@;
29
30   print "Adding history records for ${table}...\n";
31
32   my $dbdef_table = $dbdef->table($table);
33   my $pkey = $dbdef_table->primary_key;
34
35   foreach my $rec (qsearch($table, {})) {
36
37     #my $h_rec = qsearchs(
38     #  $h_table,
39     #  { $pkey => $rec->getfield($pkey) },
40     #  eval "FS::${h_table}->sql_h_searchs(time)",
41     #);
42
43     my $h_rec = qsearchs(
44       $h_table,
45       { $pkey => $rec->getfield($pkey) },
46       "DISTINCT ON ( $pkey ) *",
47       "AND history_action = 'insert' ORDER BY $pkey ASC, history_date DESC",
48       '',
49       'AS maintable',
50     );
51
52     unless ($h_rec) {
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;
58       $cnt++;
59     }
60
61
62   $t_cnt++;
63
64   }
65
66   print "History records inserted into $h_table: $cnt\n";
67   print "               Total records in $table: $t_cnt\n";
68
69   print "\n";
70
71 }
72
73 foreach my $table (@tables) {
74
75   my $h_table = 'h_' . $table;
76   my $cnt = 0;
77
78   eval "use FS::${table}";
79   die $@ if $@;
80   eval "use FS::${h_table}";
81   die $@ if $@;
82
83   print "Adding insert records for unmatched delete records on ${table}...\n";
84
85   my $dbdef_table = $dbdef->table($table);
86   my $pkey = $dbdef_table->primary_key;
87
88   #SELECT * FROM h_svc_www
89   #DISTINCT ON ( $pkey ) ?
90   my $where = "
91   WHERE ${pkey} in (
92     SELECT ${h_table}1.${pkey}
93       FROM ${h_table} as ${h_table}1
94       WHERE (
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'
99       ) > 0
100       AND (
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' )
106       ) = 0
107       GROUP BY ${h_table}1.${pkey})";
108
109
110   my @h_recs = qsearch(
111     $h_table, { },
112     "DISTINCT ON ( $pkey ) *",
113     $where,
114     '',
115     ''
116   );
117
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;
127     $cnt++;
128   }
129
130   print "History records inserted into $h_table: $cnt\n";
131
132 }
133
134
135
136 sub usage {
137   die "Usage:\n  add-history-records.pl user\n";
138 }
139