问题描述
有没有容易安装/使用(在unix上)数据库迁移工具,如Rails迁移?我真的很喜欢这个想法,但是安装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
目录中,并且有一个 database.yml
文件, db config,这个简单的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
之后,您可以运行
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.
这篇关于像数据库迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!