Method: Bj::API#generate_migration

Defined in:
lib/bj/api.rb

#generate_migration(options = {}) ⇒ Object

generate_migration, suprisingly, generates the single migration needed for bj. you’ll notice the the migration is very short as the migration classes themselves are inner classes of the respective bj table class. see lib/bj/table.rb for details.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/bj/api.rb', line 119

def generate_migration options = {}
  options.to_options!
  options[:verbose] = true
  chroot do
    before = Dir.glob "./db/migrate/*"
    n = Dir.glob("./db/migrate/*_bj_*").size
    classname = "BjMigration#{ n }"
    util.spawn "#{ Bj.ruby } ./script/generate migration #{ classname }", options rescue nil
    after = Dir.glob "./db/migrate/*"
    candidates = after - before
    case candidates.size
      when 0
        false
      when 1
        generated = candidates.first
        open(generated, "w"){|fd| fd.puts Bj.table.migration_code(classname)}
        Bj.logger.info{ "generated <#{ generated }>" }
        generated
      else
        raise "ambiguous migration <#{ candidates.inspect }>"
    end
  end
end