Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another Migrations sort bug #18

Merged
merged 1 commit into from
Apr 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/Mojo/mysql/Migrations.pm
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ sub migrate {
# Up
my @sql;
if ($active < $target) {
foreach (sort keys %$up) {
foreach (sort { $a <=> $b } keys %$up) {
push @sql, @{$up->{$_}} if $_ <= $target && $_ > $active;
}
}
# Down
else {
foreach (reverse sort keys %$down) {
foreach (reverse sort { $a <=> $b } keys %$down) {
push @sql, @{$down->{$_}} if $_ > $target && $_ <= $active;
}
}
Expand Down
14 changes: 7 additions & 7 deletions t/migrations.t
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,24 @@ create table if not exists migration_test_two (bar varchar(255));
-- 3 DOWN
drop table if exists migration_test_two;

-- 4 up (not down)
-- 10 up (not down)
insert into migration_test_two values ('works too');
-- 4 down (not up)
-- 10 down (not up)
delete from migration_test_two where bar = 'works too';
EOF
is $mysql->migrations->latest, 4, 'latest version is 4';
is $mysql->migrations->latest, 10, 'latest version is 10';
is $mysql->migrations->active, 0, 'active version is 0';
is $mysql->migrations->migrate->active, 4, 'active version is 4';
is $mysql->migrations->migrate->active, 10, 'active version is 10';
is_deeply $mysql->db->query('select * from migration_test_one')->hash,
{foo => 'works ♥'}, 'right structure';
is $mysql->migrations->migrate->active, 4, 'active version is 4';
is $mysql->migrations->migrate->active, 10, 'active version is 10';
is $mysql->migrations->migrate(1)->active, 1, 'active version is 1';
is $mysql->db->query('select * from migration_test_one')->hash, undef,
'no result';
is $mysql->migrations->migrate(3)->active, 3, 'active version is 3';
is $mysql->db->query('select * from migration_test_two')->hash, undef,
'no result';
is $mysql->migrations->migrate->active, 4, 'active version is 4';
is $mysql->migrations->migrate->active, 10, 'active version is 10';
is_deeply $mysql->db->query('select * from migration_test_two')->hash,
{bar => 'works too'}, 'right structure';
is $mysql->migrations->migrate(0)->active, 0, 'active version is 0';
Expand All @@ -94,7 +94,7 @@ like $@, qr/does_not_exist/, 'right error';
is $mysql2->migrations->migrate(3)->active, 3, 'active version is 3';
is $mysql2->migrations->migrate(2)->active, 2, 'active version is 3';
is $mysql->migrations->active, 0, 'active version is still 0';
is $mysql->migrations->migrate->active, 4, 'active version is 4';
is $mysql->migrations->migrate->active, 10, 'active version is 10';
is_deeply $mysql2->db->query('select * from migration_test_three')
->hashes->to_array, [{baz => 'just'}, {baz => 'works ♥'}],
'right structure';
Expand Down