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

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

问题描述

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

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.

我正在尝试找出删除它们的最佳方法,同时又不搞乱我的迁移 &db/schema.rb 文件和它可能对我的生产环境产生任何副作用,我的应用程序在 Heroku 上.我在本地机器上使用 PostgreSQLHeroku.

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.

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

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

我想直接进入我的数据库 &放下桌子&然后销毁模型.

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

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

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.

这个方法也会更新db/schema.rb文件吗?

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

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

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.

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

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 (根据 Dan Wich 的回答更新)

db/migrate/timestamp_drop_tablename (updated in response 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

这似乎是最好的方法,但是旧的迁移文件会怎样?他们会留下吗?每次运行 rake db:migrate 时更新 db/schema 只会被 db/migrate/timestamp_drop_tablename 覆盖?

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

我很高兴尝试第二种方法,但希望有经验的人来权衡一下&告诉我执行此操作的 Rails 方法.

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.

推荐答案

第二种方法是处理此问题的理想方法:您的迁移文件旨在表示您的数据库随时间的变化情况.旧的迁移文件将保留在您的项目中(假设您想回滚到旧版本),但是当您 rake db:migrate 时 Rails 不会运行它们,因为它知道它们已经运行(基于数据库的 schema_migrations 表中的数据).

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).

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

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

对您的代码进行一个小调整:您的迁移文件应该在 up 方法中删除表,并且最好在 down 方法中重新创建它.up"表示你的迁移丢表及时向前移动,如果迁移回滚,将运行down方法.

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天全站免登陆