Rails-like Database Migrations?

前端 未结 6 1769
长情又很酷
长情又很酷 2020-12-23 22:30

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 mig

6条回答
  •  不知归路
    2020-12-23 23:22

    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.

    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.

提交回复
热议问题