Class: Kanal::Plugins::ActiveRecord::Tasks::MigrateTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/kanal/plugins/active_record/tasks/migrate_task.rb

Instance Method Summary collapse

Constructor Details

#initialize(name = :kanal) ⇒ MigrateTask

Returns a new instance of MigrateTask.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kanal/plugins/active_record/tasks/migrate_task.rb', line 14

def initialize(name = :kanal)
  super()

  desc "Run migrations for Kanal plugin for ActiveRecord with all added migration directories. Use --yes to execute without y/n question."

  namespace name do
    namespace :active_record do
      task :migrate do |t, args|
        options = {}
        opts = OptionParser.new
        opts.banner = "Usage: kanal:active_record:migrate [options]"
        opts.on "-y", "--yes", "Don't ask for confirmation, just run migrations" do |y|
          options[:yes] = y
        end

        args = opts.order! ARGV do
        end
        opts.parse!

        execute_migration_task options[:yes]
      end
    end
  end
end

Instance Method Details

#execute_migration_task(yes = false) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/kanal/plugins/active_record/tasks/migrate_task.rb', line 39

def execute_migration_task(yes = false)
  plugin = Kanal::Plugins::ActiveRecord::ActiveRecordPlugin
  
  # The problem with this is connection is not REALLY (look NOTE below) established only
  # thanks to ::ActiveRecord::Base.establish_connection
  # Migrations can proceed and be executed without checking for connection
  # NOTE: we can look here
  # https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionHandler.html#method-i-establish_connection
  # and learn that there seems no real connection happening. At least, I assume so

#             unless plugin.connected?
#               puts "ActiveRecord is not connected to the database.
# Directive 🤖: You should execute this rake task with application or something that has
# this plugin added and initialized (via setup method). In other words: how could I execute migrations,
# if there is no connection to database? 😭"
#               return
#             end

  migration_dirs = plugin.migration_directories

  if migration_dirs.empty?
    puts "There are no migration directories for migrating. 👀
Try adding them yourself or add some plugins with them! 😉"
    return
  end

  unless yes
    puts "You are about to execute #{migration_dirs.size} directories with migrations to execute.
Are you sure you want to execute them?

Please enter y or Y as yes, other values will be considered as no"

    result = $stdin.gets.chomp

    if result != "y" && result != "Y"
      puts "Stopping migrations!"
      return
    end
  end


  puts "Executing migrations..."

  plugin.migrate

  puts "Done! 🥳"
end