迁移后仍然没有用户表

时间:2013-03-09 04:24:21

标签: ruby-on-rails activerecord sinatra

我正在使用Ruby和Rails的面向服务的设计。在第一章中,它使用活动记录创建一个带有Sinatra应用程序的服务。我创建了活动记录迁移(001_create_users.rb,见下文),并运行rake文件进行迁移(使用命令rake db:migrate),它给了我输出,这似乎表明用户表已创建(请参阅这个帖子的底部是运行rake db:migrate之后的输出。但是,当我运行规范(rspec spec/service_spec.rb,见下文)时,它给了我一个错误,说它找不到用户表。

table_structure': Could not find table 'users' (ActiveRecord::StatementInvalid)

你能告诉我我可能做错了什么吗?本书第一章的源代码也可以在https://github.com/pauldix/service-oriented-design-with-ruby/tree/master/chapter_01

找到

规格/ service_spec.rb

RSpec.configure do |conf|
  conf.include Rack::Test::Methods
end

def app
  Sinatra::Application
end

describe "service" do
  before(:each) do
    User.delete_all
  end

  describe "GET on /api/v1/users/:id" do
    before(:each) do
      User.create(
        :name => "paul",
        :email => "paul@pauldix.net",
        :password => "strongpass",
        :bio => "rubyist")
    end

    it "should return a user by name" do
      get '/api/v1/users/paul'
      last_response.should be_ok
      attributes = JSON.parse(last_response.body)["user"]
      attributes["name"].should == "paul"
    end

/db/migrate/001_create_users.rb

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string :name
      t.string :email
      t.string :password
      t.string :bio

      t.timestamps
    end
  end

  def self.down
    drop_table :users
  end
end

Rake文件

require 'rubygems'
require 'active_record'
require 'yaml'
require 'logger'

desc "Load the environment"
task :environment do
  env = ENV["SINATRA_ENV"] || "development"
  databases = YAML.load_file("config/database.yml")
  ActiveRecord::Base.establish_connection(databases[env])
end

namespace :db do
  desc "Migrate the database"
  task(:migrate => :environment) do
    ActiveRecord::Base.logger = Logger.new(STDOUT)
    ActiveRecord::Migration.verbose = true
    ActiveRecord::Migrator.migrate("db/migrate")
  end
end

运行rake db:migrate

D, [2013-03-08T20:11:55.490937 #3824] DEBUG -- :    (0.2ms)  select sqlite_version(*)
D, [2013-03-08T20:11:55.493906 #3824] DEBUG -- :    (2.1ms)  CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
D, [2013-03-08T20:11:55.497176 #3824] DEBUG -- :    (2.6ms)  CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
D, [2013-03-08T20:11:55.544128 #3824] DEBUG -- :    (44.9ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" 
I, [2013-03-08T20:11:55.544615 #3824]  INFO -- : Migrating to CreateUsers (1)
D, [2013-03-08T20:11:55.545263 #3824] DEBUG -- :    (0.1ms)  begin transaction
==  CreateUsers: migrating ====================================================
-- create_table(:users)
D, [2013-03-08T20:11:55.570509 #3824] DEBUG -- :    (0.6ms)  CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "email" varchar(255), "password" varchar(255), "bio" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) 
   -> 0.0018s
==  CreateUsers: migrated (0.0019s) ===========================================

D, [2013-03-08T20:11:55.571113 #3824] DEBUG -- :    (0.1ms)  INSERT INTO "schema_migrations" ("version") VALUES ('1')
D, [2013-03-08T20:11:55.576187 #3824] DEBUG -- :    (4.8ms)  commit transaction
michael$ rspec spec/service_spec.rb
D, [2013-03-08T20:12:57.812685 #3854] DEBUG -- : env: test
D, [2013-03-08T20:12:57.910829 #3854] DEBUG -- : db/test.sqlite3 database connection established...

1 个答案:

答案 0 :(得分:0)

migrate任务调用environment作为先决条件,默认情况下将环境设置为“开发”,除非有一个ENV变量说明其他内容。

您的dev数据库会被迁移,但不会迁移您的测试数据库。

就像你的书的说明一样,稍后,请致电

rake db:migrate SINATRA_ENV=test

这将是您正在迁移的测试数据库。