类似 Rails 的数据库迁移? [英] Rails-like Database Migrations?

查看:21
本文介绍了类似 Rails 的数据库迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何易于安装/使用(在 unix 上)的数据库迁移工具,例如 Rails Migrations?我真的很喜欢这个想法,但是纯粹为了管理我的数据库迁移而安装 ruby​​/rails 似乎有点过头了.

Is there any easy to install/use (on unix) database migration tools like Rails Migrations? I really like the idea, but installing ruby/rails purely to manage my database migrations seems overkill.

推荐答案

只需使用 ActiveRecord 和一个简单的 Rakefile.例如,如果您将迁移放在 db/migrate 目录中,并且有一个包含 db 配置的 database.yml 文件,那么这个简单的 Rakefile 应该可以工作:

Just use ActiveRecord and a simple Rakefile. For example, if you put your migrations in a db/migrate directory and have a database.yml file that has your db config, this simple Rakefile should work:

Rakefile:

require 'active_record'
require 'yaml'

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(STDOUT)
end

database.yml:

adapter: mysql
encoding: utf8
database: test_database
username: root
password:
host: localhost

之后,您将能够运行 rake migrate 并在没有周围的 Rails 应用程序的情况下获得所有迁移优势.

Afterwards, you'll be able to run rake migrate and have all the migration goodness without a surrounding rails app.

另外,我有一组 bash 脚本,它们执行与 ActiveRecord 迁移非常相似的功能,但它们仅适用于 Oracle.在切换到 Ruby 和 Rails 之前,我曾经使用过它们.它们有些复杂,我不为它们提供任何支持,但如果您有兴趣,请随时与我联系.

Alternatively, I have a set of bash scripts that perform a very similar function to ActiveRecord migrations, but they only work with Oracle. I used to use them before switching to Ruby and Rails. They are somewhat complicated and I provide no support for them, but if you are interested, feel free to contact me.

这篇关于类似 Rails 的数据库迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆