Class: Wukong::Migrate::MigrateRunner

Inherits:
Runner
  • Object
show all
Includes:
Logging, Plugin
Defined in:
lib/wukong-migrate/migrate_runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configure(env, prog) ⇒ Object



19
20
21
22
23
# File 'lib/wukong-migrate/migrate_runner.rb', line 19

def configure(env, prog)
  env.define :debug, type: :boolean, default: false, description: 'Run in debug mode'
  env.define :db,    required: true,                 description: 'The database to apply the migration to'
  env.define :force, type: :boolean, default: false, description: 'Continue migrating through errors'
end

Instance Method Details

#commandObject



26
27
28
# File 'lib/wukong-migrate/migrate_runner.rb', line 26

def command
  args.first
end

#database_optionsObject



38
39
40
41
# File 'lib/wukong-migrate/migrate_runner.rb', line 38

def database_options
  opts = settings.to_hash
  opts.merge(opts.delete(settings.db.to_sym) || {})
end

#generate_migration_file(name, database) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/wukong-migrate/migrate_runner.rb', line 43

def generate_migration_file(name, database)
  m_file = migration_file_dir.join(name + '.rb').to_s
  log.info "Creating migration: #{m_file}"
  case database
  when 'elasticsearch'
    File.open(m_file, 'w'){ |f| f.puts EsMigration.template(name) }
  when 'hbase'
    File.open(m_file, 'w'){ |f| f.puts HbaseMigration.template(name) }
  end
end

#load_all_migration_files!Object



54
55
56
57
58
# File 'lib/wukong-migrate/migrate_runner.rb', line 54

def load_all_migration_files!
  migration_file_dir.children.each do |m_file|
    Kernel.load m_file.to_s if m_file.extname == '.rb'
  end
end

#migration_file_dirObject



34
35
36
# File 'lib/wukong-migrate/migrate_runner.rb', line 34

def migration_file_dir
  Wukong::Deploy.root.join('db/migrate')
end

#perform_migration(*names, options) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/wukong-migrate/migrate_runner.rb', line 60

def perform_migration(*names, options)
  names.each do |name|
    migration = Wukong::Migration.retrieve(name)
    migration.write_attribute(:log, self.log)
    migration.perform(options)
  end
end

#runObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/wukong-migrate/migrate_runner.rb', line 68

def run
  case command
  when 'generate'
    generate_migration_file(specified_migration, settings.db)
  when 'perform'
    load_all_migration_files!
    perform_migration(specified_migration, database_options)
  when 'all'
    load_all_migration_files!
    perform_migration(*Wukong::Migration.all_migrations, database_options)
  else
    log.error "Please specify a valid command"
    dump_help_and_exit!
  end
end

#specified_migrationObject



30
31
32
# File 'lib/wukong-migrate/migrate_runner.rb', line 30

def specified_migration
  args[1] or die('Must specify a migration when using this command', 1)
end