Class: Hobo::MigrationGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Generators::HoboSupport::ThorShell, Rails::Generators::Migration
Defined in:
lib/generators/hobo/migration/migration_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details



22
23
24
# File 'lib/generators/hobo/migration/migration_generator.rb', line 22

def self.banner
  "rails generate hobo:migration #{self.arguments.map(&:usage).join(' ')} [options]"
end

.next_migration_number(dirname) ⇒ Object

the Rails::Generators::Migration.next_migration_number gives a NotImplementedError in Rails 3.0.0.beta4, so we need to implement the logic of ActiveRecord. For other ORMs we will wait for the rails implementation see groups.google.com/group/rubyonrails-talk/browse_thread/thread/a507ce419076cda2



18
19
20
# File 'lib/generators/hobo/migration/migration_generator.rb', line 18

def self.next_migration_number(dirname)
  ActiveRecord::Generators::Base.next_migration_number dirname
end

Instance Method Details

#migrateObject



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
86
87
88
89
# File 'lib/generators/hobo/migration/migration_generator.rb', line 46

def migrate
  return if migrations_pending?

  generator = Generators::Hobo::Migration::Migrator.new(lambda{|c,d,k,p| extract_renames!(c,d,k,p)})
  up, down = generator.generate

  if up.blank?
    say "Database and models match -- nothing to change"
    return
  end

  say "\n---------- Up Migration ----------"
  say up
  say "----------------------------------"

  say "\n---------- Down Migration --------"
  say down
  say "----------------------------------"

  action = options[:generate] && 'g' ||
           options[:migrate] && 'm' ||
           choose("\nWhat now: [g]enerate migration, generate and [m]igrate now or [c]ancel?", /^(g|m|c)$/)

  if action != 'c'
    if name.blank? && !options[:default_name]
      final_migration_name = choose("\nMigration filename: [<enter>=#{migration_name}|<custom_name>]:", /^[a-z0-9_ ]*$/, migration_name).strip.gsub(' ', '_')
    end
    final_migration_name = migration_name if final_migration_name.blank?

    up.gsub!("\n", "\n    ")
    up.gsub!(/ +\n/, "\n")
    down.gsub!("\n", "\n    ")
    down.gsub!(/ +\n/, "\n")

    @up = up
    @down = down
    @migration_class_name = final_migration_name.camelize

    migration_template 'migration.rb.erb', "db/migrate/#{final_migration_name.underscore}.rb"
    rake('db:migrate') if action == 'm'
  end
rescue HoboFields::Model::FieldSpec::UnknownSqlTypeError => e
  say "Invalid field type: #{e}"
end