Skip to content

Commit dc82727

Browse files
uu1tctran
authored andcommitted
Refactor hooking ActiveRecord migration tasks (#588)
- Use Rake::Task#enhance insteaad of defining same tasks again - Remove hooking db:migrate:change task which doesn't exist - Fix hooking db:migrate:reset task so that the annotation runs after all migration tasks (#548)
1 parent b249e8a commit dc82727

File tree

2 files changed

+75
-12
lines changed

2 files changed

+75
-12
lines changed

lib/tasks/annotate_models_migrate.rake

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,12 @@
44
# Append annotations to Rake tasks for ActiveRecord, so annotate automatically gets
55
# run after doing db:migrate.
66

7-
namespace :db do
8-
[:migrate, :rollback].each do |cmd|
9-
task cmd do
7+
%w(db:migrate db:migrate:up db:migrate:down db:migrate:reset db:migrate:redo db:rollback).each do |task|
8+
Rake::Task[task].enhance do
9+
Rake::Task[Rake.application.top_level_tasks.last].enhance do
1010
Rake::Task['set_annotation_options'].invoke
1111
Annotate::Migration.update_annotations
1212
end
13-
14-
namespace cmd do
15-
[:change, :up, :down, :reset, :redo].each do |t|
16-
task t do
17-
Rake::Task['set_annotation_options'].invoke
18-
Annotate::Migration.update_annotations
19-
end
20-
end
21-
end
2213
end
2314
end
2415

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
require_relative '../spec_helper'
2+
3+
describe 'ActiveRecord migration rake task hooks' do
4+
before do
5+
Rake.application = Rake::Application.new
6+
7+
# Stub migration tasks
8+
%w(db:migrate db:migrate:up db:migrate:down db:migrate:reset db:rollback).each do |task|
9+
Rake::Task.define_task(task)
10+
end
11+
Rake::Task.define_task('db:migrate:redo') do
12+
Rake::Task['db:rollback'].invoke
13+
Rake::Task['db:migrate'].invoke
14+
end
15+
16+
Rake::Task.define_task('set_annotation_options')
17+
Rake.load_rakefile('tasks/annotate_models_migrate.rake')
18+
19+
Rake.application.instance_variable_set(:@top_level_tasks, [subject])
20+
end
21+
22+
describe 'db:migrate' do
23+
it 'should update annotations' do
24+
expect(Annotate::Migration).to receive(:update_annotations)
25+
Rake.application.top_level
26+
end
27+
end
28+
29+
describe 'db:migrate:up' do
30+
it 'should update annotations' do
31+
expect(Annotate::Migration).to receive(:update_annotations)
32+
Rake.application.top_level
33+
end
34+
end
35+
36+
describe 'db:migrate:down' do
37+
it 'should update annotations' do
38+
expect(Annotate::Migration).to receive(:update_annotations)
39+
Rake.application.top_level
40+
end
41+
end
42+
43+
describe 'db:migrate:reset' do
44+
it 'should update annotations' do
45+
expect(Annotate::Migration).to receive(:update_annotations)
46+
Rake.application.top_level
47+
end
48+
end
49+
50+
describe 'db:rollback' do
51+
it 'should update annotations' do
52+
expect(Annotate::Migration).to receive(:update_annotations)
53+
Rake.application.top_level
54+
end
55+
end
56+
57+
describe 'db:migrate:redo' do
58+
it 'should update annotations after all migration tasks' do
59+
allow(Annotate::Migration).to receive(:update_annotations)
60+
61+
# Confirm that update_annotations isn't called when the original redo task finishes
62+
Rake::Task[subject].enhance do
63+
expect(Annotate::Migration).not_to have_received(:update_annotations)
64+
end
65+
66+
Rake.application.top_level
67+
68+
# Hooked 3 times by db:rollback, db:migrate, and db:migrate:redo tasks
69+
expect(Annotate::Migration).to have_received(:update_annotations).exactly(3).times
70+
end
71+
end
72+
end

0 commit comments

Comments
 (0)