use Try::Tiny;
use DBIx::Class ();
use Encode qw/encode/;
+use List::MoreUtils 'all';
use namespace::clean;
our $VERSION = '0.07010';
=head2 uniq_to_primary
-Automatically promotes the largest unique constraints on tables to primary
-keys, assuming there is only one largest unique constraint.
+Automatically promotes the largest unique constraints with non-nullable columns
+on tables to primary keys, assuming there is only one largest unique
+constraint.
=head1 METHODS
push @uniqs, [$name, $cols];
}
- if ((not @$pks) && @uniqs && $self->uniq_to_primary) {
+ my @non_nullable_uniqs = grep {
+ all { $col_info->{$_}{is_nullable} == 0 } @{ $_->[1] }
+ } @uniqs;
+
+ if ($self->uniq_to_primary && (not @$pks) && @non_nullable_uniqs) {
my @by_colnum = sort { $b->[0] <=> $a->[0] }
- map [ scalar @{ $_->[1] }, $_ ], @uniqs;
+ map [ scalar @{ $_->[1] }, $_ ], @non_nullable_uniqs;
if (not (@by_colnum > 1 && $by_colnum[0][0] == $by_colnum[1][0])) {
- @uniqs = map $_->[1], @by_colnum;
+ my @keys = map $_->[1], @by_colnum;
+
+ my $pk = $keys[0];
+
+ # remove the uniq from list
+ @uniqs = grep { $_->[0] ne $pk->[0] } @uniqs;
- $pks = (shift @uniqs)->[1];
+ $pks = $pk->[1];
}
}