Class: Batteries::Tasks::Migrations

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options: {}) {|_self| ... } ⇒ Migrations

Returns a new instance of Migrations.

Yields:

  • (_self)

Yield Parameters:



12
13
14
15
16
17
18
19
20
21
# File 'lib/batteries/tasks/migrations.rb', line 12

def initialize(options: {})
  self.migrations_path = options.fetch(:migrations_path) { "migrate" }
  self.database = options.fetch(:database) { DB if defined?(DB) }
  self.logger = options.fetch(:logger) { Logger.new($stdout) }
  self.setup_hook = options[:setup_hook]

  yield self if block_given?

  define
end

Instance Attribute Details

#databaseObject



67
68
69
70
71
72
# File 'lib/batteries/tasks/migrations.rb', line 67

def database
  case @database
  when String then Object.const_get(@database)
  else @database
  end
end

#loggerObject

Returns the value of attribute logger.



9
10
11
# File 'lib/batteries/tasks/migrations.rb', line 9

def logger
  @logger
end

#migrations_pathObject

Returns the value of attribute migrations_path.



9
10
11
# File 'lib/batteries/tasks/migrations.rb', line 9

def migrations_path
  @migrations_path
end

#setup_hookObject

Returns the value of attribute setup_hook.



9
10
11
# File 'lib/batteries/tasks/migrations.rb', line 9

def setup_hook
  @setup_hook
end

Instance Method Details

#defineObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
# File 'lib/batteries/tasks/migrations.rb', line 23

def define
  desc "Migrate test database to latest version"
  task :test_up do
    migrate("test", nil)
  end

  desc "Migrate test database all the way down"
  task :test_down do
    migrate("test", 0)
  end

  desc "Migrate test database all the way down and then back up"
  task :test_bounce do
    migrate("test", 0)
    migrate("test", nil)
  end

  desc "Migrate development database to latest version"
  task :dev_up, [:version] do |_t, args|
    version = args[:version]
    version = version ? version.to_i : nil

    migrate("development", version)
  end

  desc "Migrate development database to all the way down"
  task :dev_down, [:version] do |_t, args|
    version = args[:version].to_i

    migrate("development", version)
  end

  desc "Migrate development database all the way down and then back up"
  task :dev_bounce do
    migrate("development", 0)
    migrate("development", nil)
  end

  desc "Migrate production database to latest version"
  task :prod_up do
    migrate("production", nil)
  end
end

#migrate(env, version) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/batteries/tasks/migrations.rb', line 74

def migrate(env, version)
  ENV["RACK_ENV"] = env
  setup_hook.call if setup_hook.respond_to?(:call)
  Sequel.extension :migration
  database.loggers << logger if logger
  Sequel::Migrator.apply(database, migrations_path, version)
end