mojolicious / mojo-pg

Mojolicious :heart: PostgreSQL
https://metacpan.org/release/Mojo-Pg
Artistic License 2.0
101 stars 46 forks source link

bugfix for $pg->migration->latest version #5

Closed hernan604 closed 9 years ago

hernan604 commented 9 years ago

BUG: I have migrations up from 1 to 11. Mojo::Pg stopped getting my migrations at 9. The problem is at how the version sort is performed (It is not sorting numerically.)

Current sort: 1,10,11,2,3,4,5,6,7,8,9

Correct sort: 1,2,3,4,5,6,7,8,9,10,11 with { $a <=> $b }

to reproduce:

1. Create migrations string with 12 up

2. Instanciate mojo pg and load migrations string.

3. Check the latest which should be 12. But returns 9.

This is the proof that Mojo::Pg::Migrations is not numerically sorting

use lib './lib'; use Mojo::Pg; my $pg = Mojo::Pg->new; my $migration = <<MIGRATIONS;

-- 1 up CREATE TABLE "MyTable" ( col1 text );

-- 2 up ALTER TABLE "MyTable" ADD COLUMN col2 text;

-- 3 up ALTER TABLE "MyTable" ADD COLUMN col3 text;

-- 4 up ALTER TABLE "MyTable" ADD COLUMN col4 text;

-- 5 up ALTER TABLE "MyTable" ADD COLUMN col5 text;

-- 6 up ALTER TABLE "MyTable" ADD COLUMN col6 text;

-- 7 up ALTER TABLE "MyTable" ADD COLUMN col7 text;

-- 8 up ALTER TABLE "MyTable" ADD COLUMN col8 text;

-- 9 up ALTER TABLE "MyTable" ADD COLUMN col9 text;

-- 10 up ALTER TABLE "MyTable" ADD COLUMN col10 text;

-- 11 up ALTER TABLE "MyTable" ADD COLUMN col11 text;

-- 12 up ALTER TABLE "MyTable" ADD COLUMN col12 text;

MIGRATIONS

my $latest = $pg->migrations->from_string( $migration )->latest; warn "WRONG : $latest * * * should be 12";

*Mojo::Pg::Migrations::latest = sub { (sort { $a <=> $b } keys %{shift->{migrations}{up}})[-1] || 0 };

my $latest_correct = $pg->migrations->from_string( $migration )->latest; warn "CORRECT: $latest_correct";

kraih commented 9 years ago

Thanks, applied (with some small changes).