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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/migration_tools/tasks.rb', line 119

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
              self.group = migration_group.to_s
              Rake::Task["db:migrate:#{ns}"].invoke
              Rake::Task["db:migrate:#{ns}"].reenable
            end
          end
        end
      end
    end

    namespace :abort_if_pending_migrations do
      MigrationTools::MIGRATION_GROUPS.each do |migration_group|
        desc "Raises an error if there are pending #{migration_group} migrations"
        task migration_group do
          self.group = migration_group.to_s
          Rake::Task["db:migrate:list"].invoke
          Rake::Task["db:migrate:list"].reenable
          if pending_migrations.any?
            abort "Run \"rake db:migrate\" to update your database then try again."
          end
        end
      end
    end
  end
end

#define_migrate_groupObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/migration_tools/tasks.rb', line 91

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|
            migrator(migration.version).run
          end

          schema_format = if ActiveRecord::VERSION::MAJOR >= 7
            ActiveRecord.schema_format
          else
            ActiveRecord::Base.schema_format
          end

          Rake::Task["db:schema:dump"].invoke if schema_format == :ruby
          Rake::Task["db:structure:dump"].invoke if schema_format == :sql
        end
      end
    end
  end
end

#define_migrate_listObject



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

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 defined?(@group) && @group

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

#group=(group) ⇒ Object



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

def group=(group)
  @group = nil
  @pending_migrations = nil
  ENV["GROUP"] = group
end

#migrate_up(migrations, target_version) ⇒ Object



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

def migrate_up(migrations, target_version)
  if ActiveRecord::VERSION::MAJOR == 7 && ActiveRecord::VERSION::MINOR == 1
    ActiveRecord::Migrator.new(:up, migrations,
      ActiveRecord::Base.connection.schema_migration,
      ActiveRecord::Base.connection.,
      target_version)
  elsif ActiveRecord.gem_version >= Gem::Version.new("7.2")
    ActiveRecord::Migrator.new(:up, migrations,
      ActiveRecord::Base.connection_pool.schema_migration,
      ActiveRecord::Base.connection_pool.,
      target_version)
  else
    ActiveRecord::Migrator.new(:up, migrations, ActiveRecord::SchemaMigration, target_version)
  end
end

#migrations_pathsObject



26
27
28
# File 'lib/migration_tools/tasks.rb', line 26

def migrations_paths
  ActiveRecord::Migrator.migrations_paths
end

#migrator(target_version = nil) ⇒ Object



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

def migrator(target_version = nil)
  if ActiveRecord::VERSION::MAJOR == 7 && ActiveRecord::VERSION::MINOR == 1
    migrate_up(ActiveRecord::MigrationContext.new(
      migrations_paths,
      ActiveRecord::Base.connection.schema_migration
    ).migrations, target_version)
  elsif ActiveRecord.gem_version >= Gem::Version.new("7.2")
    migrate_up(ActiveRecord::MigrationContext.new(
      migrations_paths,
      ActiveRecord::Base.connection_pool.schema_migration
    ).migrations, target_version)
  else
    migrate_up(ActiveRecord::MigrationContext.new(
      migrations_paths,
      ActiveRecord::SchemaMigration
    ).migrations, target_version)
  end
end

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



152
153
154
155
156
157
158
# File 'lib/migration_tools/tasks.rb', line 152

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

#pending_migrationsObject



65
66
67
68
69
70
71
# File 'lib/migration_tools/tasks.rb', line 65

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

  @pending_migrations
end