1

started a new rails 3 app. I have aded a couple of migrations which were generated through Scaffold. Nothing is output to the console when I run "rake db:migrate", even when I run "rake --verbose db:migrate"

here is a sample migration

class CreateBicycles < ActiveRecord::Migration
  def self.up
    create_table :bicycles do |t|
      t.string :title
      t.text :Note
      t.string :Manufacturer
      t.string :Model
      t.date :year_manufactured
      t.integer :view_count
      t.timestamps
    end
  end

  def self.down
    drop_table :bicycles
  end
end
Jimmy
  • 35,686
  • 13
  • 80
  • 98
rornoob
  • 41
  • 1
  • 3
  • Can you post your migrations? – Alex Nov 20 '10 at 01:57
  • I mean in the body of your question. :) – Alex Nov 20 '10 at 02:05
  • Where are you running your rake tasks from? Make sure you are running in the root of your rails directory and that you even have a RakeFile. – Alex Nov 20 '10 at 02:15
  • I'm running the command from the rood dir of the project and I have a Rakefile! – rornoob Nov 20 '10 at 02:17
  • 2
    Have you configured your database.yml correctly? – Alex Nov 20 '10 at 02:19
  • Yep, I ran the rake cmd with trace, and rpeatedly I get the same output:** Invoke db:migrate (first_time) ** Invoke environment (first_time) ** Execute environment ** Execute db:migrate ** Invoke db:schema:dump (first_time) ** Invoke environment ** Execute db:schema:dump – rornoob Nov 20 '10 at 02:23
  • What are the contents of the rakefile? – Zabba Dec 18 '10 at 17:55

2 Answers2

1

I've met the problem like you.Here is my problem description.

try rake db:version,it returns 0.

try rake db:migrate,nothing happened and no table created,even there are migration files in db/migrate. try rake db:migrate VERSION=YourAnyMigrationFileTimestamp, an exception raised,blames no such migration.

if your problem like me,may be you got or install a wrong rake task which change the default root when running migration task.

to check it,you need debug the third task,(http://stackoverflow.com/questions/2663912/rails-debugging-rails-tasks can tell you how to debug rake task).

debug line 548 in activerecord-3.0.1/lib/active_record/migration.rb,run Dir.pwd,if the result is not your app root,there must be some other task change your default task dir.

The following is what i've debugged

(rdb:1) p self.class

ActiveRecord::Migrator

(rdb:1) n

/home/raykin/.rvm/gems/ruby-1.9.2-p0@r3local/gems/activerecord- 3.0.1/lib/active_record/migration.rb:550

files = Dir["#{@migrations_path}/[0-9]_.rb"]

(rdb:1) n

/home/raykin/.rvm/gems/ruby-1.9.2-p0@r3local/gems/activerecord- 3.0.1/lib/active_record/migration.rb:552

migrations = files.inject([]) do |klasses, file|

(rdb:1) p files

[]

(rdb:1) p @migrations_path

"db/migrate/"

(rdb:1) p Dir['db/migrate/[0-9]_.rb']

[]

(rdb:1) l

* output flushed *

(rdb:1) Dir.pwd

* Unknown command: "Dir.pwd". Try "help".

(rdb:1) p Dir.pwd

"/home/raykin/campus/:/code/campus/db/data_export"

see,the current dir has been changed,cause rails would load all task before running migration(there seems a method to skip the all load),and some bad task(just i wrote it yesterday...) change the current dir.

This is exactly not a problem of Rails,but it is really a confused result for user. hopefully can help you.

raykin
  • 11
  • 2
1

I have an absolutely bizarre answer that I have confirmed to be true. We were building docker containers with VERSION=<our application version> and found that none of our migrations would run inside the container, but ran fine outside the container.

Long story short: verify that you have not specified any environment variable named VERSION, otherwise the migration will use this as the target_version and if it is anything but nil, it won't run all migrations.

Relevant bit of code from lib/activerecord/tasks/database_tasks.rb:

  def migrate
    raise "Empty VERSION provided" if ENV["VERSION"] && ENV["VERSION"].empty?

    verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
    version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
    scope = ENV["SCOPE"]
    verbose_was, Migration.verbose = Migration.verbose, verbose
    Migrator.migrate(migrations_paths, version) do |migration|
      scope.blank? || scope == migration.scope
    end
    ActiveRecord::Base.clear_cache!
  ensure
    Migration.verbose = verbose_was
  end
kross
  • 3,627
  • 2
  • 32
  • 60