Class: MigrationTools::Tasks

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/migration_tools/tasks.rb

Instance Method Summary collapse

Constructor Details

#initializeTasks

Returns a new instance of Tasks.



6
7
8
9
10
# File 'lib/migration_tools/tasks.rb', line 6

def initialize
  define_migrate_list
  define_migrate_group
  define_convenience_tasks
end

Instance Method Details

#define_convenience_tasksObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/migration_tools/tasks.rb', line 70

def define_convenience_tasks
  namespace :db do
    namespace :migrate do
      [ :list, :group ].each do |ns|
        namespace ns do
          MigrationTools::MIGRATION_GROUPS.each do |migration_group|
            desc "#{ns == :list ? 'Lists' : 'Executes' } the migrations for group #{migration_group}"
            task migration_group => :environment do
              ENV['GROUP'] = migration_group.to_s
              Rake::Task["db:migrate:#{ns}"].invoke
            end
          end
        end
      end
    end
  end
end

#define_migrate_groupObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/migration_tools/tasks.rb', line 50

def define_migrate_group
  namespace :db do
    namespace :migrate do
      desc 'Runs pending migrations for a given group'
      task :group => :environment do
        if group.empty?
          notify "Please specify a migration group"
        elsif pending_migrations.empty?
          notify "Your database schema is up to date"
        else
          pending_migrations.each do |migration|
            migration.migrate(:up)
            migrator.send(:record_version_state_after_migrating, migration.version)
          end
        end
      end
    end
  end
end

#define_migrate_listObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/migration_tools/tasks.rb', line 32

def define_migrate_list
  namespace :db do
    namespace :migrate do
      desc 'Lists pending migrations'
      task :list => :environment do
        if pending_migrations.empty?
          notify "Your database schema is up to date", group
        else
          notify "You have #{pending_migrations.size} pending migrations", group
          pending_migrations.each do |migration|
            notify '  %4d %s %s' % [ migration.version, migration.migration_group.to_s[0..5].center(6), migration.name ]
          end
        end
      end
    end
  end
end

#groupObject



12
13
14
15
16
17
18
# File 'lib/migration_tools/tasks.rb', line 12

def group
  return @group if @group

  @group = ENV['GROUP'].to_s
  raise "Invalid group \"#{@group}\"" if !@group.empty? && !MIGRATION_GROUPS.member?(@group)
  @group
end

#migratorObject



20
21
22
# File 'lib/migration_tools/tasks.rb', line 20

def migrator
  @migrator ||= ActiveRecord::Migrator.new(:up, 'db/migrate')
end

#notify(string, group = "") ⇒ Object



88
89
90
91
92
93
94
# File 'lib/migration_tools/tasks.rb', line 88

def notify(string, group = "")
  if group.empty?
    puts string
  else
    puts string + " for group \""+group+"\""
  end
end

#pending_migrationsObject



24
25
26
27
28
29
30
# File 'lib/migration_tools/tasks.rb', line 24

def pending_migrations
  return @pending_migrations if @pending_migrations
  @pending_migrations = migrator.pending_migrations
  @pending_migrations = @pending_migrations.select { |proxy| group.empty? || proxy.migration_group == group }

  @pending_migrations
end