From: Graham Barr Date: Fri, 9 Oct 2009 05:15:55 +0000 (-0500) Subject: preserve column order X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=aef7f568c40fc14904c16729d8ef1b2b3a4b134b;p=dbsrgits%2FSQL-Translator-2.0-ish.git preserve column order columns returned from db are not always in the order that they are defined in a table. So sort them before adding them to the table --- diff --git a/lib/SQL/Translator/Parser/DBI.pm b/lib/SQL/Translator/Parser/DBI.pm index a93ad3e..fe03f4c 100644 --- a/lib/SQL/Translator/Parser/DBI.pm +++ b/lib/SQL/Translator/Parser/DBI.pm @@ -82,6 +82,7 @@ role SQL::Translator::Parser::DBI { method _add_columns(Table $table) { my $sth = $self->dbh->column_info($self->catalog_name, $self->schema_name, $table->name, '%'); + my @columns; while (my $column_info = $sth->fetchrow_hashref) { my $column = SQL::Translator::Object::Column->new({ name => $column_info->{COLUMN_NAME}, data_type => $self->_column_data_type($column_info), @@ -90,8 +91,9 @@ role SQL::Translator::Parser::DBI { is_auto_increment => $self->_is_auto_increment($column_info), is_nullable => $column_info->{NULLABLE}, }); - $table->add_column($column); + push @columns, { column => $column, pos => $column_info->{ORDINAL_POSITION} || $#columns }; } + $table->add_column($_->{column}) for sort { $a->{pos} <=> $b->{pos} } @columns; } method _add_primary_key(Table $table) {