From: Justin Hunter Date: Tue, 27 Jul 2010 03:18:53 +0000 (-0700) Subject: fix up some ordering issues X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=160f1f551395c24929216b166a3eb45c3096f435;p=dbsrgits%2FSQL-Translator-2.0-ish.git fix up some ordering issues --- diff --git a/lib/SQL/Translator/Object/Column.pm b/lib/SQL/Translator/Object/Column.pm index 80aea11..aaee8ea 100644 --- a/lib/SQL/Translator/Object/Column.pm +++ b/lib/SQL/Translator/Object/Column.pm @@ -59,7 +59,7 @@ class SQL::Translator::Object::Column extends SQL::Translator::Object is dirty { is => 'rw', isa => Bit, required => 1, - default => 1 + default => 1, ); has 'is_unique' => ( @@ -137,7 +137,7 @@ class SQL::Translator::Object::Column extends SQL::Translator::Object is dirty { my $order = $self->_order; unless (defined $order && $order) { my $columns = Tie::IxHash->new( map { $_->name => $_ } $self->table->get_columns ); - $order = ($columns->Indices($self->name) + 1) || 0; + $order = $columns->Indices($self->name) || 0; $order++; $self->_order($order); } return $order; diff --git a/lib/SQL/Translator/Object/Constraint.pm b/lib/SQL/Translator/Object/Constraint.pm index d8f0bfe..f8913c9 100644 --- a/lib/SQL/Translator/Object/Constraint.pm +++ b/lib/SQL/Translator/Object/Constraint.pm @@ -1,6 +1,6 @@ use MooseX::Declare; class SQL::Translator::Object::Constraint extends SQL::Translator::Object { - use MooseX::Types::Moose qw(ArrayRef Bool HashRef Maybe Str Undef); + use MooseX::Types::Moose qw(ArrayRef Bool HashRef Int Maybe Str Undef); use MooseX::MultiMethods; use SQL::Translator::Types qw(Column MatchType Table); @@ -74,6 +74,11 @@ class SQL::Translator::Object::Constraint extends SQL::Translator::Object { default => '' ); + has '_order' => ( + isa => Int, + is => 'rw', + ); + has 'on_delete' => ( is => 'rw', required => 0); has 'on_update' => ( is => 'rw', required => 0); @@ -84,5 +89,16 @@ class SQL::Translator::Object::Constraint extends SQL::Translator::Object { $self->$orig($column->name, $column) } + multi method order(Int $order) { $self->_order($order); } + multi method order { + my $order = $self->_order; + unless (defined $order && $order) { + my $tables = Tie::IxHash->new( map { $_->name => $_ } $self->schema->get_tables ); + $order = $tables->Indices($self->name) || 0; $order++; + $self->_order($order); + } + return $order; + } + method is_valid { return $self->has_type && scalar $self->column_ids ? 1 : undef } } diff --git a/lib/SQL/Translator/Object/Table.pm b/lib/SQL/Translator/Object/Table.pm index 13f83a7..878cd11 100644 --- a/lib/SQL/Translator/Object/Table.pm +++ b/lib/SQL/Translator/Object/Table.pm @@ -1,6 +1,6 @@ use MooseX::Declare; class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty { - use MooseX::Types::Moose qw(Any Bool HashRef Str); + use MooseX::Types::Moose qw(Any Bool HashRef Int Str); use MooseX::MultiMethods; use SQL::Translator::Types qw(Column Constraint Index Schema Sequence); use SQL::Translator::Object::Column; @@ -92,6 +92,11 @@ class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty { default => 0 ); + has '_order' => ( + is => 'rw', + isa => Int, + ); + around add_column(Column $column does coerce) { die "Can't use column name " . $column->name if $self->exists_column($column->name) || $column->name eq ''; $column->table($self); @@ -146,8 +151,18 @@ class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty { return $primary_key; } + multi method order(Int $order) { $self->_order($order); } + multi method order { + my $order = $self->_order; + unless (defined $order && $order) { + my $tables = Tie::IxHash->new( map { $_->name => $_ } $self->schema->get_tables ); + $order = $tables->Indices($self->name) || 0; $order++; + $self->_order($order); + } + return $order; + } + method is_valid { return $self->get_columns ? 1 : undef } - method order { } before name($name?) { die "Can't use table name $name, table already exists" if $name && $self->schema->exists_table($name) && $name ne $self->name } diff --git a/lib/SQL/Translator/Object/Trigger.pm b/lib/SQL/Translator/Object/Trigger.pm index f2a744e..5e324b5 100644 --- a/lib/SQL/Translator/Object/Trigger.pm +++ b/lib/SQL/Translator/Object/Trigger.pm @@ -1,6 +1,6 @@ use MooseX::Declare; class SQL::Translator::Object::Trigger extends SQL::Translator::Object { - use MooseX::Types::Moose qw(Any ArrayRef HashRef Str); + use MooseX::Types::Moose qw(Any ArrayRef HashRef Int Str); use MooseX::MultiMethods; use SQL::Translator::Types qw(Column Schema Table); @@ -67,11 +67,27 @@ class SQL::Translator::Object::Trigger extends SQL::Translator::Object { required => 1, ); + has '_order' => ( + isa => Int, + is => 'rw', + ); + around add_column(Column $column) { $self->$orig($column->name, $column) } multi method database_events(Str $database_event) { $self->add_database_event($database_event); $self->database_events } multi method database_events(ArrayRef $database_events) { $self->add_database_event($_) for @$database_events; $self->database_events } multi method database_events { $self->_database_events } + multi method order(Int $order) { $self->_order($order); } + multi method order { + my $order = $self->_order; + unless (defined $order && $order) { + my $triggers = Tie::IxHash->new( map { $_->name => $_ } $self->schema->get_triggers ); + $order = $triggers->Indices($self->name) || 0; $order++; + $self->_order($order); + } + return $order; + } + method is_valid { 1 } }