2013-02-12 51 views
8

每次我運行一個rake db:migrate時,ra​​ils決定更改我的schema.rb文件。在某些情況下,這是完全合理的,但在其他一些情況下,似乎是無緣由的。我感到困惑的是,當我從git中提取新遷移和新版本的schema.rb時,然後運行rake db:migrate。由於此遷移附帶了新版本的schema.rb文件,因此我不應該更新schema.rb。但是,每次都會改變它。發生這種情況時我發現令人難以置信的愚蠢的變化,如:rails無故更改schema.rb

add_index "my_table", ["column1", "column2"], :name => "index_on_some_columns" 

add_index "my_table", ["column2", "column1"], :name => "index_on_some_columns" 

發生這種情況時我只需運行git checkout db/schema.rb和我的生活下去,但它irkes我沒有盡頭。是否有這樣做的原因,我怎樣才能阻止它做到這一點?

編輯:下面是摘錄從差異

@@ -165,12 +165,11 @@ ActiveRecord::Schema.define(:version => 20130206001907) do 
    t.column "updated_at", :datetime 
- t.column "coordinates", :point, :srid => 4326 
@@ -200,15 +199,16 @@ ActiveRecord::Schema.define(:version => 20130206001907) do 
    t.column "something", :boolean 
+ t.column "coordinates", :point, :srid => 4326 
+ t.column "random_string", :string 
    t.column "remote", :string 
- t.column "random_string", :string 
    end 

- add_index "my_table", ["id", "foreign_id"], :name => "index_active_my_table_on_foreign_and_id" 
- add_index "my_table", ["id", "active"], :name => "index_my_table_on_active_and_id" 
- add_index "my_table", ["id", "content_modified_at"], :name => "index_my_table_on_content_modified_at_and_id" 
+ add_index "my_table", ["foreign_id", "id"], :name => "index_active_my_table_on_foreign_and_id" 
+ add_index "my_table", ["active", "id"], :name => "index_my_table_on_active_and_id" 
+ add_index "my_table", ["content_modified_at", "id"], :name => "index_my_table_on_content_modified_at_and_id" 
+0

如何在遷移文件中定義此索引? – Novae 2013-02-12 21:58:31

+0

發表您的差異 – cbrulak 2013-02-12 22:14:16

+0

在多種情況下,遷移說'add_index:my_table,[「column2」,「column1」]',但通過git提供的schema.rb有相反的順序:'add_index「my_table」,[「column1」, 「列2」]'。這似乎是一致的,但現在我想知道列的相反順序是如何進入代碼的。這可能是天真的,但這可能與使用linux/mac有關嗎? – wesdotcool 2013-02-12 22:16:10

回答

6

由於schema.rb文件的新版本中附帶此遷移,我不應該更新schema.rb。

這是不準確的。

每次Rails運行遷移時,它都會使用數據庫作爲源更新schema.rb文件。它不查看現有的schema.rb文件,它只是使用來自數據庫的信息並覆蓋它。

看來真正的問題是,在生成schema.rb文件時,在兩個不同的環境(Ruby,Rails,MySQL和操作系統的不同組合)中運行相同的遷移可能會產生不同的結果。

解決方案是確保在代碼中檢查的每個人都使用相同的軟件版本,並儘可能保證。如果這是不可能的(因爲這是Windows與Linux與Mac的區別,你不想改變你的操作系統),你只需要處理不便。

+0

謝謝你爲我澄清。我將不得不看看我能否共同破解一些能夠繞過文件更改的文件,如果文件沒有真正改變的話! – wesdotcool 2013-02-15 19:55:38

0

對我來說,解決方案是首先製作rake db:schema:load。並且比rake db:migrate停止更改我的schema.rb無故。

注意:rake db:schema:load將刪除所有現有數據,並就現有schema.rb的重新創建數據庫。