Class: Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/sfb_scripts/migrations/migrator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo:, shell:, queue:, folder_guard:, drop_dbs: false) ⇒ Migrator

Returns a new instance of Migrator.



4
5
6
7
8
9
10
# File 'lib/sfb_scripts/migrations/migrator.rb', line 4

def initialize(repo:, shell:, queue:, folder_guard:, drop_dbs: false)
  @shell = shell
  @repo = repo
  @queue = queue
  @drop_dbs = drop_dbs
  @folder_guard = folder_guard
end

Instance Attribute Details

#drop_dbsObject

Returns the value of attribute drop_dbs.



2
3
4
# File 'lib/sfb_scripts/migrations/migrator.rb', line 2

def drop_dbs
  @drop_dbs
end

#folder_guardObject

Returns the value of attribute folder_guard.



2
3
4
# File 'lib/sfb_scripts/migrations/migrator.rb', line 2

def folder_guard
  @folder_guard
end

#queueObject

Returns the value of attribute queue.



2
3
4
# File 'lib/sfb_scripts/migrations/migrator.rb', line 2

def queue
  @queue
end

#repoObject

Returns the value of attribute repo.



2
3
4
# File 'lib/sfb_scripts/migrations/migrator.rb', line 2

def repo
  @repo
end

#shellObject

Returns the value of attribute shell.



2
3
4
# File 'lib/sfb_scripts/migrations/migrator.rb', line 2

def shell
  @shell
end

Instance Method Details

#directories_to_migrateObject



22
23
24
25
# File 'lib/sfb_scripts/migrations/migrator.rb', line 22

def directories_to_migrate
  migrate_dirs = repo.files_changed.select {|f| f.match("/migrate/") }.map {|f| File.dirname(f) }.map {|dir| dir.gsub(/\/db\/migrate$/, '')}.uniq
  migrate_dirs.select {|d| folder_guard.allowed?(d) };
end

#migrate_where_necessaryObject



12
13
14
15
16
17
18
19
20
# File 'lib/sfb_scripts/migrations/migrator.rb', line 12

def migrate_where_necessary
  shell.notify "\nMigrating:"
  migrations.each do |migration|
    queue.enqueue_b do
      shell.run "RAILS_ENV=#{migration[:env]} bundle exec rake #{drop_dbs ? 'db:drop' : ''} db:create 2> /dev/null;\n RAILS_ENV=#{migration[:env]} bundle exec rake db:migrate", dir: migration[:dir]
    end
  end
  queue.join
end