summaryrefslogtreecommitdiff
path: root/bin/makecounters
diff options
context:
space:
mode:
authorIvan Kohler <ivan@freeside.biz>2016-03-01 09:08:43 -0800
committerIvan Kohler <ivan@freeside.biz>2016-03-01 09:08:43 -0800
commitb946b5a2acce328ac0e163dbf7bf8e83e7eb677a (patch)
tree63be209382b73841290f4eaf956ca0042120ade7 /bin/makecounters
parent71bfc1b5f19f25d0e330664d53bf8f641e5088d9 (diff)
making counters for ancient sequence-less installs
Diffstat (limited to 'bin/makecounters')
-rw-r--r--bin/makecounters47
1 files changed, 47 insertions, 0 deletions
diff --git a/bin/makecounters b/bin/makecounters
new file mode 100644
index 0000000..0490ef5
--- /dev/null
+++ b/bin/makecounters
@@ -0,0 +1,47 @@
+#!/usr/bin/perl
+
+use strict;
+use FS::UID qw( adminsuidsetup driver_name );
+use FS::Record qw( dbdef );
+
+my $user = shift or die;
+my $dbh = adminsuidsetup $user;
+
+my $schema = dbdef();
+
+my @tables = scalar(@ARGV)
+ ? @ARGV
+ : grep { ! /^h_/ } $schema->tables;
+
+foreach my $table ( @tables ) {
+ my $tableobj = $schema->table($table)
+ or die "unknown table $table (did you run dbdef-create?)\n";
+
+ my $primary_key = $tableobj->primary_key;
+ next unless $primary_key;
+
+ my $col = $tableobj->column($primary_key);
+
+#warn "$table ". $col->type;
+#warn "$table ". $col->default;
+
+ next if uc($col->type) eq 'SERIAL'
+ || ( driver_name eq 'Pg'
+ && defined($col->default)
+ && $col->quoted_default =~ /^nextval\(/i
+ )
+ || ( driver_name eq 'mysql'
+ && defined($col->local)
+ && $col->local =~ /AUTO_INCREMENT/i
+ );
+
+ my $value = FS::Record->scalar_sql("SELECT max($primary_key) FROM $table")
+ or next;
+
+ warn "setting counter for $table to $value\n";
+ open(FILE, ">/usr/local/etc/freeside/counters.DBI:Pg:dbname=freeside/$table.$primary_key")
+ or die $!;
+ print FILE "#COUNTER-1.0\n$value\n";
+ close FILE or die $!;
+
+}