Class: DeclareSchema::MigrationGenerator

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

Constant Summary

Constants included from Support::ThorShell

Support::ThorShell::PREFIX

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details



26
27
28
# File 'lib/generators/declare_schema/migration/migration_generator.rb', line 26

def banner
  "rails generate declare_schema:migration #{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



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

def 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
90
91
92
# File 'lib/generators/declare_schema/migration/migration_generator.rb', line 46

def migrate
  return if migrations_pending?

  generator = Generators::DeclareSchema::Migration::Migrator.new do |to_create, to_drop, kind_str, name_prefix|
    extract_renames!(to_create, to_drop, kind_str, name_prefix)
  end

  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 "----------------------------------"

  final_migration_name =
    name.presence ||
    if !options[:default_name]
      choose("Migration filename (spaces will be converted to _) [#{default_migration_name}]:", /^[a-z0-9_ ]*$/,
             default_migration_name).strip.gsub(' ', '_').presence
    end ||
    default_migration_name

  @up = indent(up.strip, 4)
  @down = indent(down.strip, 4)
  @migration_class_name = final_migration_name.camelize

  migration_template('migration.rb.erb', "db/migrate/#{final_migration_name.underscore}.rb")

  db_migrate_command = ::DeclareSchema.db_migrate_command
  if options[:migrate]
    say db_migrate_command
    bare_rails_command = db_migrate_command.sub(/\Abundle exec +/, '').sub(/\Arake +|rails +/, '')
    rails_command(bare_rails_command)
  else
    say "\nNot running migration since --migrate not given. When you are ready, run:\n\n   #{db_migrate_command}\n\n"
  end
rescue ::DeclareSchema::UnknownTypeError => ex
  say "Invalid field type: #{ex}"
end