如何更改 Rails 应用程序数据?

发布于 2024-07-17 06:30:38 字数 158 浏览 3 评论 0原文

我看过很多关于 ActiveRecord 迁移以及是否应该使用它们来更改应用程序中的数据的讨论,有些人说是,有些人说不。 我的问题是,如果您不使用迁移来执行此操作,那么您使用什么? 只是你写的另一个脚本?

我正在寻求有关替代方法的建议,以及为什么它们可能比仅仅使用迁移更好。

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月苍老的讽刺 2024-07-24 06:30:39

如果您使用提供的

rake db:reset

rake db:schema:load

任务,就会出现一个问题,这些任务使用 schema.rb 作为设置数据库的基础。 所以没有数据被加载并且你被卡住了。

Rails 敏捷 Web 开发,第三版,你应该得到它(如果 Ruby 书是“Pickaxe”书,这应该是“Hammock”书,顺便说一句?)如果你还没有这样做,DHH 说:

...迁移并不是真正的目的
携带种子数据。 它们太暂时了
本质上是可靠地做到这一点。
移民是为了带你离开
模式的一个版本到下一个版本,
不创建新的模式
从头开始——我们有 db/schema.rb 文件
为此。

所以,一旦你真的
开始真正的应用程序,
人们不会早跑你
当他们设置时迁移
应用。 他们将从
无论存储在什么版本
db/schema.rb 并忽略所有这些
以前的迁移。 这意味着
迁移创建的任何数据
永远不会将其放入数据库,所以
你不能依赖它。

有很多
获得更多的替代方法
永久种子数据。 最简单的是
可能只是为了创建一个新文件
db/seed.rb,其中包含那些
Product.create 调用将执行以下操作
设置。 然后可以调用该文件
rake db:schema:load 创建后
初始架构。

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.

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:

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

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.

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.

会傲 2024-07-24 06:30:39

很多时候,迁移是最合适的,并且不能用单独的脚本替代。 想象一下以下场景:应用程序已经在使用实时数据; 代码列包含“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

在这种情况下,在数据传输到姓名和邮政编码列之前无法删除代码列。

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.


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.

始终不够 2024-07-24 06:30:39

当我需要修改数据库中的某些数据时,我将创建一个 Rake 任务来运行一些库函数来完成这项工作。 这样,数据操作将是可重复的,并且如果需要,也可以从迁移中运行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文