Class: Wukong::Migrate::MigrateRunner
- Inherits:
-
Runner
- Object
- Runner
- Wukong::Migrate::MigrateRunner
- Includes:
- Logging, Plugin
- Defined in:
- lib/wukong-migrate/migrate_runner.rb
Class Method Summary collapse
Instance Method Summary collapse
- #command ⇒ Object
- #database_options ⇒ Object
- #generate_migration_file(name, database) ⇒ Object
- #load_all_migration_files! ⇒ Object
- #migration_file_dir ⇒ Object
- #perform_migration(*names, options) ⇒ Object
- #run ⇒ Object
- #specified_migration ⇒ Object
Class Method Details
.configure(env, prog) ⇒ Object
29 30 31 32 33 |
# File 'lib/wukong-migrate/migrate_runner.rb', line 29 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
#command ⇒ Object
36 37 38 |
# File 'lib/wukong-migrate/migrate_runner.rb', line 36 def command args.first end |
#database_options ⇒ Object
48 49 50 51 |
# File 'lib/wukong-migrate/migrate_runner.rb', line 48 def opts = settings.to_hash opts.merge(opts.delete(settings.db.to_sym) || {}) end |
#generate_migration_file(name, database) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/wukong-migrate/migrate_runner.rb', line 53 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
64 65 66 67 68 |
# File 'lib/wukong-migrate/migrate_runner.rb', line 64 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_dir ⇒ Object
44 45 46 |
# File 'lib/wukong-migrate/migrate_runner.rb', line 44 def migration_file_dir Wukong::Deploy.root.join('db/migrate') end |
#perform_migration(*names, options) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/wukong-migrate/migrate_runner.rb', line 70 def perform_migration(*names, ) names.each do |name| migration = Wukong::Migration.retrieve(name) migration.write_attribute(:log, self.log) migration.perform() end end |
#run ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/wukong-migrate/migrate_runner.rb', line 78 def run case command when 'generate' generate_migration_file(specified_migration, settings.db) when 'perform' load_all_migration_files! perform_migration(specified_migration, ) when 'all' load_all_migration_files! perform_migration(*Wukong::Migration.all_migrations, ) else log.error "Please specify a valid command" dump_help_and_exit! end end |
#specified_migration ⇒ Object
40 41 42 |
# File 'lib/wukong-migrate/migrate_runner.rb', line 40 def specified_migration args[1] or die('Must specify a migration when using this command', 1) end |