你如何改變你的Rails應用程序數據?

[英]How do you change your Rails App Data?


I have seen a lot of talk regarding ActiveRecord Migrations and whether or not they should be used to change data within your application, some people saying yes some saying no. My question is if you are not using Migrations to do this then what are you using? Just another script that you write?

我已經看到很多關於ActiveRecord遷移的討論,以及它們是否應該用於更改應用程序中的數據,有些人說是,有些人說不。我的問題是,如果您沒有使用遷移來執行此操作,那么您使用的是什么?你寫的另一個腳本?

I am after suggestitions on alternative ways and why they might be a better idea than just using migrations.

我之后提出了關於替代方法的建議以及為什么它們可能比僅僅使用遷移更好。

3 个解决方案

#1


14  

One problem comes if you use the provided

如果您使用提供的,會出現一個問題

rake db:reset

and

rake db:schema:load

tasks, which use schema.rb as the basis for setting up your database. So no data gets loaded and you're stuck.

任務,使用schema.rb作為設置數據庫的基礎。因此,沒有數據加載,你就被卡住了。

In Agile Web Development with Rails, Third Edition, which you should get (if the Ruby book is the "Pickaxe" book, should this be the "Hammock" book, btw?) if you haven't done so already, DHH says:

在你應該得到的Rails,第三版的敏捷Web開發中(如果Ruby書是“Pickaxe”一書,這本書應該是“Hammock”,順便說一句?)如果你還沒有這樣做,DHH說:

...migrations aren’t really meant to carry seed data. They’re too temporal in nature to do that reliably. Migrations are here to bring you from one version of the schema to the next, not to create a fresh schema from scratch—we have the db/schema.rb file for that.

......遷移並不意味着攜帶種子數據。它們在性質上太過時間,無法可靠地做到這一點。遷移是為了讓您從一個版本的架構到下一個版本,而不是從頭開始創建一個新的架構 - 我們有db / schema.rb文件。

So, as soon as you actually get going with a real application, people won’t be running your early migrations when they set up the application. They’ll start from whatever version is stored in db/schema.rb and ignore all those previous migrations. This means that any data created by the migrations never make it into the database, so you can’t rely on it.

因此,只要您真正開始使用真正的應用程序,人們就不會在設置應用程序時運行早期遷移。它們將從db / schema.rb中存儲的任何版本開始,並忽略所有先前的遷移。這意味着遷移創建的任何數據都不會進入數據庫,因此您無法依賴它。

There are many alternative ways to have more permanent seed data. The easiest is probably just to create a new file in db/seed.rb, which contains those Product.create calls that’ll do the setup. This file can then be called after rake db:schema:load creates the initial schema.

有許多替代方法可以獲得更永久的種子數據。最簡單的可能只是在db / seed.rb中創建一個新文件,其中包含那些將進行設置的Product.create調用。然后可以在rake db:schema之后調用此文件:load創建初始模式。

#2


4  

a lot of times, migrations are the best fit and cannot be replaced with a separate script. Imagine the following scenario: the application is already in use with live data; the code column contains a code in the form "name-zip_code" (yeah I know it's ugly, but it happens), and you want to split that into two columns, 'name' and 'zip_code', while getting rid of the 'code' column.

很多時候,遷移是最合適的,不能用單獨的腳本替換。想象一下以下場景:應用程序已經在使用實時數據;代碼列包含“name-zip_code”形式的代碼(是的,我知道它很丑,但它發生了),你想把它分成兩列,'name'和'zip_code',同時擺脫'代碼列。


def self.up
  add_column :companies, :zip_code, :integer
  add_column :companies, :name, :string
  Company.reset_column_information
  Company.find(:all).each do |company|
    name, zip_code = company.code.split('-')
    company.update_attributes(:name => name, :zip_code => zip_code)  
  end
  remove_column :companies, :code
end

in this case, the code column cannot be removed before the data is transfered to the name and zip code columns.

在這種情況下,在將數據傳輸到名稱和郵政編碼列之前,無法刪除代碼列。

#3


1  

When I need to modify some data in the databse, I will create a Rake task that runs some library function to do the work. This way, the data manipulation will be repeatable and if required, can be run from a migration as well.

當我需要修改數據庫中的一些數據時,我將創建一個Rake任務來運行一些庫函數來完成工作。這樣,數據操作將是可重復的,並且如果需要,也可以從遷移中運行。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/04/15/72fb788e8ff7713b6d4c5fa840231fc6.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com