ActiveRecord 迁移器中的版本控制如何工作?

发布于 2024-07-16 09:27:53 字数 1088 浏览 4 评论 0原文

我正在尝试通过使用不同的包(ActiveRecord、ActiveSupport)来学习 Rails,而无需使用 Rails gem。

不过,我不知道如何创建包含三个类的数据库。 这是我的 rakefile:

require 'rubygems'
require 'activerecord'   
require 'yaml'   

task :default => :migrate   

desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x"   
task :migrate => :environment do   
  ActiveRecord::Migrator.migrate('db/migrate', ENV["VERSION"] ? ENV["VERSION"].to_i : nil )   
end   

task :environment do   
  ActiveRecord::Base.establish_connection(YAML::load(File.open('database.yml')))   
  ActiveRecord::Base.logger = Logger.new(File.open('database.log', 'a'))   
end   

我有三个文件:

001_create_appearances.rb 001_create_movies.rb 001_create_actors.rb

我过去都成功运行过这些文件,每个文件都继承自 ActiveRecord::Migration 并执行 self.up/self.down。

然而,我有一个问题,当我运行 rake 时,它​​返回“多个迁移的版本号为 1”。 我是否应该将所有迁移打包到一个文件中,例如 001_create_database.rb?

是否有手册描述这些版本如何工作?

我尝试阅读 migrator.rb 的源代码,但找不到 up_without_benchmarks/down_without_benchmarks 所在的位置。

I am trying to learn Rails by working with different packages (ActiveRecord, ActiveSupport) without rails gem.

I can't figure out how to create a database with three classes, though.
Here's my rakefile:

require 'rubygems'
require 'activerecord'   
require 'yaml'   

task :default => :migrate   

desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x"   
task :migrate => :environment do   
  ActiveRecord::Migrator.migrate('db/migrate', ENV["VERSION"] ? ENV["VERSION"].to_i : nil )   
end   

task :environment do   
  ActiveRecord::Base.establish_connection(YAML::load(File.open('database.yml')))   
  ActiveRecord::Base.logger = Logger.new(File.open('database.log', 'a'))   
end   

And I have three files:

001_create_appearances.rb 001_create_movies.rb 001_create_actors.rb

Which I have all run successfully in the past, each one just inherits from ActiveRecord::Migration and does self.up/self.down.

I have, however, problem, that when I run rake, it returns "Multiple migrations have the version number 1". Should I pack all the migrations into one file, like 001_create_database.rb?

Is there a manual somewhere that describes how these versions work?

I tried reading source of migrator.rb but couldn't find where up_without_benchmarks/down_without_benchmarks where located.

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

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

发布评论

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

评论(1

挽梦忆笙歌 2024-07-23 09:27:53

正确的。 迁移必须以唯一的编号开始,以便您希望执行它们。 默认情况下,在 Rails 2.x 中,此数字表示当前日期和时间。

这样做的原因是为了让开发人员能够轻松地运行和撤消迁移。 如果两个开发人员同时创建新的迁移(这将导致他们在被推送到版本控制系统时具有相同的编号),则使用时间戳而不是连续整数。

Right. Migrations must start with unique numbers in order that you wish for them to be executed. By default in rails 2.x this number will be a representation of the current date and time.

The reason for this is to allow developers to easily run and undo migrations. Timestamps are used instead of sequential integers in case two developers simultaneously create a new migration (Which would result in them having the same number when they are pushed to a version control system).

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