Merge branch 'github/pr/55_reprise'
[freeside.git] / bin / rt-trim-whitespace
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use FS::Record;
6 use FS::UID qw(adminsuidsetup dbh driver_name);
7
8 # Remove trailing whitespace from custom field option lists and values.
9
10 my $dbh = adminsuidsetup(shift) or die "Usage: rt-trim-whitespace username\n";
11 die "rt-trim-whitespace only works on Pg databases" if driver_name ne 'Pg';
12
13 my @updates = (
14   customfieldvalues       => 'name',
15   objectcustomfieldvalues => 'content',
16 );
17
18 while(@updates) {
19   my $table = shift @updates;
20   my $field = shift @updates;
21   my $select = 
22 "SELECT $field FROM $table WHERE $field != substring($field from ".
23   q!E'^(.*\\\\S)\\\\s*$'! . ')';
24   
25   print "$select\n";
26   my $rows = $dbh->do($select);
27   print "$rows rows found.\n";
28   
29   if($rows) {
30     my $update =
31 "UPDATE $table SET $field = substring($field from ".q!E'^(.*\\\\S)\\\\s*$'!.')'.
32 " WHERE $field != substring($field from ".q!E'^(.*\\\\S)\\\\s*$'!.')';
33     print "$update\n";
34     my $rows = $dbh->do($update);
35     print "$rows updated.\n";
36   }
37 }
38 $dbh->commit or die $dbh->errstr;