summaryrefslogtreecommitdiff
path: root/bin/fix-sequences
diff options
context:
space:
mode:
authorivan <ivan>2003-06-12 13:32:15 +0000
committerivan <ivan>2003-06-12 13:32:15 +0000
commitb77d5769face5c38dd34c37e98a8fce47c4e15de (patch)
treebf30d7ebf05714e3616ad3c2338423651eca3345 /bin/fix-sequences
parentfaea8c040128dcace598fb224cf9f7e9a2e256c9 (diff)
adding fix-sequences
Diffstat (limited to 'bin/fix-sequences')
-rwxr-xr-xbin/fix-sequences55
1 files changed, 55 insertions, 0 deletions
diff --git a/bin/fix-sequences b/bin/fix-sequences
new file mode 100755
index 000000000..f193e21d4
--- /dev/null
+++ b/bin/fix-sequences
@@ -0,0 +1,55 @@
+#!/usr/bin/perl -Tw
+
+use strict;
+use DBI;
+use DBIx::DBSchema 0.21;
+use DBIx::DBSchema::Table;
+use DBIx::DBSchema::Column;
+use DBIx::DBSchema::ColGroup::Unique;
+use DBIx::DBSchema::ColGroup::Index;
+use FS::UID qw(adminsuidsetup);
+use FS::Record qw(dbdef);
+
+my $user = shift or die &usage;
+my $dbh = adminsuidsetup $user;
+
+my $schema = dbdef();
+
+#false laziness w/fs-setup
+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);
+
+ next unless uc($col->type) eq 'SERIAL'
+ || ( driver_name eq 'Pg'
+ && defined($col->default)
+ && $col->default =~ /^nextval\(/i
+ )
+ || ( driver_name eq 'mysql'
+ && defined($col->local)
+ && $col->local =~ /AUTO_INCREMENT/i
+ );
+
+ warn "fixing sequence for $table\n";
+
+ $dbh->do( "SELECT setval( '${table}_${primary_key}_seq',
+ ( SELECT max($primary_key) FROM $table ) );" )
+ or die $dbh->errstr;
+
+}
+
+$dbh->commit or die $dbh->errstr;
+$dbh->disconnect or die $dbh->errstr;
+
+sub usage {
+ die "Usage:\n create-history-tables user [ table table ... ] \n";
+}
+