* Fix ignored option to script/sqlt-diagram (RT#5992)
* Fix t/17sqlfxml-producer.t failures due to whitespace differences introduced by
environment config snippets (RT#70786)
+* Fix assembly of Table objects with numbered columns being added out of order
+ (RT#74771) (based on patch from Jonathan Otsuka)
* Deprecate SQL::Translator::Schema::Graph and the as_graph() schema method
* Bump minimum supported perl version to 5.8.1 (mostly due to Moo)
use SQL::Translator::Schema::Constraint;
use SQL::Translator::Schema::Field;
use SQL::Translator::Schema::Index;
-use Data::Dumper;
+
+use Carp::Clan '^SQL::Translator';
+use List::Util 'max';
use base 'SQL::Translator::Schema::Object';
=cut
-sub new {
- my $class = shift;
- my $self = $class->SUPER::new (@_)
- or return;
-
- $self->{_order} = { map { $_ => 0 } qw/
- field
- /};
-
- return $self;
-}
-
sub add_constraint {
=pod
$self->error( $field_class->error );
}
- $field->order( ++$self->{_order}{field} );
+ my $existing_order = { map { $_->order => $_->name } $self->get_fields };
+
+ # supplied order, possible unordered assembly
+ if ( $field->order ) {
+ if($existing_order->{$field->order}) {
+ croak sprintf
+ "Requested order '%d' for column '%s' conflicts with already existing column '%s'",
+ $field->order,
+ $field->name,
+ $existing_order->{$field->order},
+ ;
+ }
+ }
+ else {
+ my $last_field_no = max(keys %$existing_order) || 0;
+ if ( $last_field_no != scalar keys %$existing_order ) {
+ croak sprintf
+ "Table '%s' field order incomplete - unable to auto-determine order for newly added field",
+ $self->name,
+ ;
+ }
+
+ $field->order( $last_field_no + 1 );
+ }
+
# We know we have a name as the Field->new above errors if none given.
my $field_name = $field->name;
- if ( exists $self->{'fields'}{ $field_name } ) {
+ if ( $self->get_field($field_name) ) {
return $self->error(qq[Can't create field: "$field_name" exists]);
}
else {
$| = 1;
use strict;
-use Test::More tests => 238;
+use Test::More tests => 245;
+use Test::Exception;
use SQL::Translator::Schema::Constants;
require_ok( 'SQL::Translator' );
$s->add_procedure($p);
}
+
+#
+# Test field order
+#
+{
+ my $s = SQL::Translator::Schema->new;
+ my $t = $s->add_table( name => 'person' );
+ my $f3 = $t->add_field( name => 'age', order => 3 );
+ my $f1 = $t->add_field( name => 'person_id', order => 1 );
+ my $f2 = $t->add_field( name => 'name', order => 2 );
+ my $f4 = $t->add_field( name => 'gender' );
+ my $f5 = $t->add_field( name => 'alias' );
+
+ is( $f1->order, 1, 'Field order is passed, order is 1' );
+ is( $f2->order, 2, 'Field order is passed, order is 2' );
+ is( $f3->order, 3, 'Field order is passed, order is 3' );
+ is( $f4->order, 4, 'Field order is not passed, order is 4' );
+ is( $f5->order, 5, 'Field order is not passed, order is 5' );
+
+ my $t2 = $s->add_table( name => 'place' );
+ $f2 = $t2->add_field( name => 'name', order => 2 );
+
+ throws_ok { my $f22 = $t2->add_field( name => 'name2', order => 2 ) }
+ qr/\QRequested order '2' for column 'name2' conflicts with already existing column 'name'/;
+
+ throws_ok { $f1 = $t2->add_field( name => 'location' ) }
+ qr/field order incomplete/;
+}