什么是放置桌子的最佳方式?在Rails 3中删除模型? [英] What is the best way to drop a table & remove a model in Rails 3?

查看:110
本文介绍了什么是放置桌子的最佳方式?在Rails 3中删除模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型&一张我在应用程序中不再需要的表格,我可以将它们留在那里,但我想删除它们以保持整洁。



我试图弄清楚最好的方法是将我们的迁移 & db / schema.rb 文件&它可能会对我的生产环境产生任何副作用,我的应用程序位于Heroku上。我在本地机器上使用PostgreSQL, heroku。



到目前为止,我已经找到了两种方法来做到这一点,但我不确定哪种方法是最好的方法/ rails方法?

方法1



我想过要进入我的数据库&放下桌子&然后销毁该模型。$ ​​b
$ b

  rails db 
DROP TABLE table_name
\ q
rails销毁模型model_name

如果我这样做,我对这个模型/表格的迁移会发生什么?我对这个模型有两个迁移,一个是 timestamp_create_modelname &一个add_attribute_to_table名称。



此方法还会更新 db / schema.rb 文件吗?



当我将应用程序推送到Heroku时,我怀疑模型将被删除,但表格将保留原位,是否有heroku命令来删除表格。

方法二



我读到的另一种方法是生成一个新的迁移以删除表格&然后销毁该模型。

  rails生成迁移drop_tablename 

&然后更新下面的文件:

db / migrate / timestamp_drop_tablename (更新后回复Dan Wich的答案)



  class DropTablename< ActiveRecord :: Migration 
def up
drop_table:tablename
end
$ b $ def def
create_table:tablename do | t |
t.string:table_column
t.references:anothertable

t.timestamps
end
add_index:tablenames,:anothertable_id
end
end

&然后在终端:

  rake db:migrate 
rails destroy model model_name
rake db:migrate
git add。
git commit -m移除表格/模型名称
git push heroku master
heroku run rake db:migrate
heroku restart

这似乎是最好的方法,但旧迁移文件会发生什么情况?他们会保持&更新db / shema每次运行rake db时:迁移只能被db / migrate / timestamp_drop_tablename覆盖?



我很乐意尝试第二种方法,但会就像有人与经验丰富的人一样,告诉我轨道做这件事的方法。第二种方法是处理这种情况的理想方法:您的迁移文件旨在表示数据库随时间变化的方式。旧的迁移文件将保留在您的项目中(假设您想要回滚到旧版本),但是当您 rake db:migrate 因为它知道它们已经被运行(基于数据库的schema_migrations表中的数据)。



您的schema.rb将只更新一次以反映您的数据库不再包含该表。



对您的代码进行一些小调整:您的迁移文件应将表中的放在方法,最好在 down 方法中重新创建它。 up表示您的迁移会使表及时向前移动,如果迁移回滚,将运行 down 方法。


I have a model & a table which I no longer need in my App, I could leave them there but I would like to remove them to keep things tidy.

I'm trying to figure out the best way to remove them with out messing around with my migrations & db/schema.rb files & any side effect it could have on my production environment, my app is on Heroku. I'm using PostgreSQL on both my local machine & heroku.

So far I've found two ways to do this but am not sure which is the best method/ rails way?

Method 1

I thought about just going in to my database & dropping the table & then destroying the model.

rails db
DROP TABLE table_name
\q
rails destroy model model_name

If I do this what will happen to the migrations I have for this model/table? I have two migrations for this model, a timestamp_create_modelname & a add_attribute_to_table name.

Also will this method update the db/schema.rb file?

When I push the App to Heroku I suspect the Model will be removed but the table will remain in place, is there a heroku command to drop a table.

Method Two

Another way I read about was to generate a new migration to drop the table & then destroy the model.

rails generate migration drop_tablename

& then update the below file:

db/migrate/timestamp_drop_tablename (updated in responce to Dan Wich's answer below)

class DropTablename < ActiveRecord::Migration
  def up
    drop_table :tablename
  end

  def down
    create_table :tablename do |t|
      t.string :table_column
      t.references :anothertable

      t.timestamps        
    end
    add_index :tablenames, :anothertable_id
  end
end

& then in the terminal:

rake db:migrate
rails destroy model model_name
rake db:migrate
git add .
git commit -m "removed table/model_name"
git push heroku master
heroku run rake db:migrate
heroku restart

This seems to be the best method but what happens to the older migration files? Will they remain & update db/shema every time I run rake db:migrate only to be overridden by db/migrate/timestamp_drop_tablename?

I'm happy to experiment with the second method but would like someone with someone with experience to weigh in & tell me the rails ways for doing this.

解决方案

The second method is the ideal way to handle this: your migration files are meant to represent how your database has changed over time. The older migration files will remain in your project (in case, hypothetically, you wanted to roll back to an older version), but Rails will not run them when you rake db:migrate because it knows they've already been run (based on data in the database's schema_migrations table).

Your schema.rb will just be updated once to reflect that your database no longer contains that table.

One minor tweak to your code: your migration file should drop the table in the up method, and ideally recreate it in the down method. The "up" signifies that your migration drops the table to move forward in time, and if the migration is rolled back, the down method will be run.

这篇关于什么是放置桌子的最佳方式?在Rails 3中删除模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆