Rails数据库是正确的,但schema.rb不是

时间:2016-06-05 13:57:22

标签: ruby-on-rails postgresql sqlite database-schema database-migration

编辑:rake db的结果:migrate:reset(schema.rb仍然显示为过时):

rake db:migrate:reset
== 20160515232901 CreateUsers: migrating ======================================
-- create_table(:users)
   -> 0.0090s
== 20160515232901 CreateUsers: migrated (0.0091s) =============================

== 20160515232934 AddDeviseToUsers: migrating =================================
-- change_table(:users)
   -> 0.0428s
-- add_index(:users, :email, {:unique=>true})
   -> 0.0072s
-- add_index(:users, :reset_password_token, {:unique=>true})
   -> 0.0057s
== 20160515232934 AddDeviseToUsers: migrated (0.0559s) ========================

== 20160515235141 AddNamesToUsers: migrating ==================================
-- add_column(:users, :first_name, :string)
   -> 0.0006s
-- add_column(:users, :last_name, :string)
   -> 0.0005s
== 20160515235141 AddNamesToUsers: migrated (0.0014s) =========================

== 20160516084432 CreateGoals: migrating ======================================
-- create_table(:goals)
   -> 0.0092s
== 20160516084432 CreateGoals: migrated (0.0108s) =============================

== 20160516084610 CreateAccomplishments: migrating ============================
-- create_table(:accomplishments)
   -> 0.0111s
== 20160516084610 CreateAccomplishments: migrated (0.0114s) ===================

== 20160518210446 CreateFriendships: migrating ================================
-- create_table(:friendships)
   -> 0.0073s
== 20160518210446 CreateFriendships: migrated (0.0074s) =======================

== 20160519203358 CreateImpressions: migrating ================================
-- create_table(:impressions)
   -> 0.0096s
== 20160519203358 CreateImpressions: migrated (0.0096s) =======================

== 20160527144308 AddCompletedStatusToGoals: migrating ========================
-- add_column(:goals, :completed_status, :boolean)
   -> 0.0007s
== 20160527144308 AddCompletedStatusToGoals: migrated (0.0008s) ===============

== 20160528013005 AddDueDateTimeToGoals: migrating ============================
-- add_column(:goals, :due_date_time, :timestamp)
   -> 0.0006s
== 20160528013005 AddDueDateTimeToGoals: migrated (0.0007s) ===================

== 20160601230946 CreateClubs: migrating ======================================
-- create_table(:clubs)
   -> 0.0125s
== 20160601230946 CreateClubs: migrated (0.0127s) =============================

== 20160601231112 CreateMemberships: migrating ================================
-- create_table(:memberships)
   -> 0.0186s
-- add_foreign_key(:memberships, :clubs)
   -> 0.0029s
-- add_foreign_key(:memberships, :users)
   -> 0.0021s
== 20160601231112 CreateMemberships: migrated (0.0239s) =======================

== 20160605124945 AddGoalsToClubs: migrating ==================================
-- add_reference(:clubs, :goal, {:index=>true})
   -> 0.0084s
-- add_foreign_key(:clubs, :goals)
   -> 0.0018s
== 20160605124945 AddGoalsToClubs: migrated (0.0103s) =========================

我最近在现有的rails应用程序上使用sqlite切换到postgres。我无法在SQLite下迁移的最后一次迁移之后更新schema.rb文件。当我完成rake db:reset时,我收到此消息(建议我创建了一个club表和一个成员表,以及其他内容):

$ rake db:reset-- enable_extension("plpgsql")
   -> 0.0238s
-- create_table("accomplishments", {:force=>:cascade})
   -> 0.0258s
-- create_table("clubs", {:force=>:cascade})
   -> 0.0233s
-- add_index("clubs", ["goal_id"], {:name=>"index_clubs_on_goal_id", :using=>:btree})
   -> 0.0200s
-- create_table("friendships", {:force=>:cascade})
   -> 0.0172s
-- create_table("goals", {:force=>:cascade})
   -> 0.0219s
-- create_table("impressions", {:force=>:cascade})
   -> 0.0279s
-- create_table("memberships", {:force=>:cascade})
   -> 0.0200s
-- add_index("memberships", ["club_id"], {:name=>"index_memberships_on_club_id", :using=>:btree})
   -> 0.0200s
-- add_index("memberships", ["user_id"], {:name=>"index_memberships_on_user_id", :using=>:btree})
   -> 0.0127s
-- create_table("users", {:force=>:cascade})
   -> 0.0253s
-- add_index("users", ["email"], {:name=>"index_users_on_email", :unique=>true, :using=>:btree})
   -> 0.0156s
-- add_index("users", ["reset_password_token"], {:name=>"index_users_on_reset_password_token", :unique=>true, :using=>:btree})
   -> 0.0092s
-- add_foreign_key("clubs", "goals")
   -> 0.0060s
-- add_foreign_key("memberships", "clubs")
   -> 0.0054s
-- add_foreign_key("memberships", "users")
   -> 0.0063s
-- initialize_schema_migrations_table()
   -> 0.0359s
-- enable_extension("plpgsql")
   -> 0.0220s
-- create_table("accomplishments", {:force=>:cascade})
   -> 0.0216s
-- create_table("clubs", {:force=>:cascade})
   -> 0.0153s
-- add_index("clubs", ["goal_id"], {:name=>"index_clubs_on_goal_id", :using=>:btree})
   -> 0.0086s
-- create_table("friendships", {:force=>:cascade})
   -> 0.0108s
-- create_table("goals", {:force=>:cascade})
   -> 0.0167s
-- create_table("impressions", {:force=>:cascade})
   -> 0.0125s
-- create_table("memberships", {:force=>:cascade})
   -> 0.0119s
-- add_index("memberships", ["club_id"], {:name=>"index_memberships_on_club_id", :using=>:btree})
   -> 0.0115s
-- add_index("memberships", ["user_id"], {:name=>"index_memberships_on_user_id", :using=>:btree})
   -> 0.0082s
-- create_table("users", {:force=>:cascade})
   -> 0.0154s
-- add_index("users", ["email"], {:name=>"index_users_on_email", :unique=>true, :using=>:btree})
   -> 0.0111s
-- add_index("users", ["reset_password_token"], {:name=>"index_users_on_reset_password_token", :unique=>true, :using=>:btree})
   -> 0.0175s
-- add_foreign_key("clubs", "goals")
   -> 0.0050s
-- add_foreign_key("memberships", "clubs")
   -> 0.0048s
-- add_foreign_key("memberships", "users")
   -> 0.0041s
-- initialize_schema_migrations_table()
   -> 0.0173s

但是我的schema.rb文件显示了一个过时的时间戳,并且不包含更新的更改(包括在db:migrate命令之后):

ActiveRecord::Schema.define(version: 20160516084731) do

  create_table "accomplishments", force: :cascade do |t|
    t.integer  "goal_id"
    t.string   "title"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "friends", force: :cascade do |t|
    t.integer  "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "goals", force: :cascade do |t|
    t.integer  "user_id"
    t.string   "title"
    t.text     "description"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  create_table "users", force: :cascade do |t|
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.string   "first_name"
    t.string   "last_name"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

我正在使用pg gem并删除了sqlite gem。

database.yml中:

development:
  adapter: postgresql
  encoding: unicode
  database: PA_development
  template: template0
  pool: 5
  username: postgres
  password: password

test:
  adapter: postgresql
  encoding: unicode
  database: PA_test
  template: template0
  pool: 5
  username: postgres
  password: password

如何更新架构?

EDIT: Image of migration path

1 个答案:

答案 0 :(得分:0)

[ROLLBACK]救援!

在您的情况下,要更新schema文件,您需要先运行rollback,然后再运行migrate

rake db:rollback
rake db:migrate

rake db:reset任务将删除数据库并再次进行设置。这在功能上等同于rake db:drop db:setup。这与运行所有迁移不同。它仅使用当前db/schema.rbdb/structure.sql文件的内容。如果无法回滚迁移,rake db:reset可能无法帮助您。

请参阅此链接:http://guides.rubyonrails.org/active_record_migrations.html#resetting-the-database