gpt4 book ai didi

ruby-on-rails - 将遗留数据迁移到 Rails 中的新模型

转载 作者:数据小太阳 更新时间:2023-10-29 07:51:53 27 4
gpt4 key购买 nike

我又要开始使用 Rails 了,现在遇到了一个令我感到恐惧的难题。在使用数据库方面,我有点菜鸟,所以如果这是相当基础的,请原谅我。

我有一个旧的 Rails 应用程序,其中包含我不想再遵循的数据模型。该模型应该被弃用,取而代之的是更轻、更简单的模型。

较旧的应用程序也非常单一,因此我试图将其分解为更小的服务组件。

所以这引出了我的问题,因为通常不赞成从一个模型使用多个数据库……将存储在旧模型中的数据转换到我的新模型(一次一个服务)的最佳方法是什么?

例如,让我们假设我在旧版和新版中都有一个用户模型。在旧模型中,用户有很多列,并非所有列都应适用于新模型。

这方面的一个例子可能是,从旧模型中用户仅限于单个地址到能够分配一对多关系的变化,其中地址在他们自己的模型中被拆分并简单地使用外键引用什么的。

编辑 1:

最终目标是尽可能轻松地将旧模型数据库中的数据虹吸到新模型数据库中,一次一个数据集。

编辑 2:

最初是在我的手机上发布的。以下是一些可能有助于提出建议的示例。

旧模型

  create_table "brands", force: :cascade do |t|
t.string "name"
t.string "url"
t.string "logo"
t.boolean "verified"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "hidden", default: false
t.string "facebook_url"
t.string "twitter_handle"
t.string "pinterest_handle"
t.string "google_plus_url"
t.string "address_street1"
t.string "address_street2"
t.string "address_street3"
t.string "address_city"
t.string "address_state"
t.string "address_zip"
t.string "address_country"
t.string "email"
t.string "phone"
t.string "story_title"
t.text "story_text"
t.string "story_photo"
end

新模式

  create_table "companies", force: :cascade do |t|
t.string "companyName", null: false
t.string "companyURL", null: false
t.boolean "companyIsActive", default: true, null: false
t.boolean "companyDataIsVerified", default: false, null: false
t.string "companyLogoFileURL"
t.datetime "companyFoundedOnDate"
t.integer "companyHQLocationID"
t.integer "companyParentCompanyID"
t.integer "companyFirstSuggestedByID"
t.string "companyFacebookURL"
t.string "companyGooglePlusURL"
t.string "companyInstagramURL"
t.string "companyPinterestURL"
t.string "companyTwitterURL"
t.string "companyStoryTitle"
t.text "companyStoryContent"
t.string "companyStoryImageFileURL"
t.boolean "companyIsHiddenFromIndex", default: false, null: false
t.integer "companyDataScraperID"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

所以,基本上...我希望能够从旧模型中获取数据,例如品牌“名称”列并将其相关值虹吸到新模型中,以便该值最终出现在公司的“companyName”列中一个完全不同的 postgresql 实例。

最佳答案

经过多次这样做,我可以告诉您,最简单的事情是创建一个简单的 rake 任务,它迭代第一个集合并在新集合中创建项目。

不需要使用DataMapper之类的东西。您已经有了 ActiveRecord,可以简单地定义每个模型要使用的数据库连接。

在你的config/database.yml中:

brand_database:
adapter: postgresql
host: brand_host
username: brand_user
password: brand_pass
database: brand_db

company_database:
adapter: postgresql
host: company_host
username: company_user
password: company_pass
database: company_db

在你的模型中:

class Brand < ActiveRecord::Base
establish_connection :brand_database
end

class Company < ActiveRecord::Base
establish_connection :company_database
end

在你的新 rake 任务中(lib/tasks/db.rake):

# lib/tasks/db.rake
namespace :db do
desc "Migrate brand records to company records"
task :migrate_brands_to_companies, [] => :environment do
Brand.find_each do |brand|
Company.find_or_initialize_by(companyName: brand.name) do |company|
puts "\n\tCreating Company record for #{brand.name}"
company.companyURL = brand.url
company.companyLogoFileURL = brand.logo
company.companyTwitterURL = "https://twitter.com/#{brand.twitter_handle}"
company.companyIsHiddenFromIndex = brand.hidden
company.created_at = brand.created_at
company.updated_at = brand.updated_at
company.save!
end
end
end
end

最后,运行 rake 任务:

$ rake db:migrate_brands_to_companies

我需要这样说:Rails 是使用可靠的约定构建的。不遵守该约定每次都会导致问题和额外费用。我已经看过很多次了。每次我看到有人背离该惯例时,他们都会遇到比他们预想的要多得多的麻烦。他们打破了很多“Rails 魔法”。

关于ruby-on-rails - 将遗留数据迁移到 Rails 中的新模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40315344/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com