2010-05-05 42 views

回答

1

你可以安裝phpMyAdmin並手動刪除表,或者如果你想從Rails框架內做到這一點,讓你在同步遷移,爲什麼不創建滴上自己的表的新移民.up並在self.down上創建表。

class DropOldTablesMigration < ActiveRecord::Migration 
    def self.up 
    drop_table :prefix_table1 
    drop_table :prefix_table2 
    end 

    def self.down 
    create_table :prefix_table1 do |t| 
     t.column :name, :string 
    end 
    create_table :prefix_table2 do |t| 
     t.column :name, :string 
    end 
    end 
end 

編輯: 只需跟進,如果問題是,有很多表,你不希望他們鍵入的所有,你可以做這樣的事情:

class DropOldTablesMigration < ActiveRecord::Migration 
    def self.up 
    ActiveRecord::Base.connection.tables.each do |t| 
     unless t.index('yourprefix_') == nil 
     drop_table t 
     end 
    end 
    end 
end 

當你向下遷移時,你將無法重新創建表格,但取決於應用中發生的情況,這可能不是問題。


編輯迴應您的評論:

要創建一個新的遷移,從您的應用程序運行以下命令的根:

script/generate migration YourMigrationName 

遷移文件將會爲您創建在db/migrate中。添加你想運行的代碼並保存。要運行新文件,請在命令行輸入以下內容:

rake db:migrate 
+0

對不起,我是Rails的新手。我試圖在/ public中安裝Wordpress,所以我只需要放棄我的(破碎的)表格 - Wordpress將處理重新安裝。 因此 - 我創建了例如db/migrate/001_drop_old_tables.rb,插入你的代碼,減去'self.down'位,然後運行這個? 我如何在部署新文件後運行它? – strangerpixel 2010-05-05 16:49:08

+3

我想你應該只安裝phpmyadmin並手動刪除表。它看起來並不真正與你的Rails應用程序相關。 – 2010-05-05 17:10:55

+0

在這種情況下,我傾向於同意Tomas。 – 2010-05-05 17:18:16