3 # run dbdef-create first!
7 use DBIx::DBSchema 0.26;
8 use DBIx::DBSchema::Table;
9 use DBIx::DBSchema::Column;
10 use DBIx::DBSchema::ColGroup::Unique;
11 use DBIx::DBSchema::ColGroup::Index;
12 use FS::UID qw(adminsuidsetup driver_name);
13 use FS::Record qw(dbdef);
15 my $user = shift or die &usage;
16 my $dbh = adminsuidsetup $user;
20 #false laziness w/fs-setup
21 my @tables = scalar(@ARGV)
23 : grep { ! /^h_/ } $schema->tables;
24 foreach my $table ( @tables ) {
25 my $tableobj = $schema->table($table)
26 or die "unknown table $table (did you run dbdef-create?)\n";
28 my $primary_key = $tableobj->primary_key;
29 next unless $primary_key;
31 my $col = $tableobj->column($primary_key);
34 next unless uc($col->type) eq 'SERIAL'
35 || ( driver_name eq 'Pg'
36 && defined($col->default)
37 && $col->default =~ /^nextval\(/i
39 || ( driver_name eq 'mysql'
40 && defined($col->local)
41 && $col->local =~ /AUTO_INCREMENT/i
44 my $seq = "${table}_${primary_key}_seq";
45 if ( driver_name eq 'Pg'
46 && defined($col->default)
47 && $col->default =~ /^nextval\('"(public\.)?(\w+_seq)"'::text\)$/
52 warn "fixing sequence for $table\n";
55 my $sql = "SELECT setval( '$seq',
56 ( SELECT max($primary_key) FROM $table ) );";
58 #warn $col->default. " $seq\n$sql\n";
59 $dbh->do( $sql ) or die $dbh->errstr;
63 $dbh->commit or die $dbh->errstr;
64 $dbh->disconnect or die $dbh->errstr;
67 die "Usage:\n fix-sequences user [ table table ... ] \n";