summaryrefslogtreecommitdiff
path: root/bin/rt-trim-whitespace
diff options
context:
space:
mode:
authorcvs2git <cvs2git>2010-11-05 19:05:57 +0000
committercvs2git <cvs2git>2010-11-05 19:05:57 +0000
commitaaf8baf3662e16e9414de236a39f8801a8c41b01 (patch)
tree2cda603e4311b3e80f79b93d9bcce3a7c7c2d053 /bin/rt-trim-whitespace
parent995a145c931164347683071c95c6754379d36604 (diff)
parent9b2de4257b6a2877434008188e52b8ef71ff339d (diff)
This commit was manufactured by cvs2svn to create branch
'FREESIDE_2_1_BRANCH'.
Diffstat (limited to 'bin/rt-trim-whitespace')
-rwxr-xr-xbin/rt-trim-whitespace38
1 files changed, 38 insertions, 0 deletions
diff --git a/bin/rt-trim-whitespace b/bin/rt-trim-whitespace
new file mode 100755
index 000000000..503d9cff7
--- /dev/null
+++ b/bin/rt-trim-whitespace
@@ -0,0 +1,38 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use FS::Record;
+use FS::UID qw(adminsuidsetup dbh driver_name);
+
+# Remove trailing whitespace from custom field option lists and values.
+
+my $dbh = adminsuidsetup(shift) or die "Usage: rt-trim-whitespace username\n";
+die "rt-trim-whitespace only works on Pg databases" if driver_name ne 'Pg';
+
+my @updates = (
+ customfieldvalues => 'name',
+ objectcustomfieldvalues => 'content',
+);
+
+while(@updates) {
+ my $table = shift @updates;
+ my $field = shift @updates;
+ my $select =
+"SELECT $field FROM $table WHERE $field != substring($field from ".
+ q!E'^(.*\\\\S)\\\\s*$'! . ')';
+
+ print "$select\n";
+ my $rows = $dbh->do($select);
+ print "$rows rows found.\n";
+
+ if($rows) {
+ my $update =
+"UPDATE $table SET $field = substring($field from ".q!E'^(.*\\\\S)\\\\s*$'!.')'.
+" WHERE $field != substring($field from ".q!E'^(.*\\\\S)\\\\s*$'!.')';
+ print "$update\n";
+ my $rows = $dbh->do($update);
+ print "$rows updated.\n";
+ }
+}
+$dbh->commit or die $dbh->errstr;