Creating migration outside of Rails

I am using ActiveRecord outside of Rails. I would like the program to create a migration skeleton (as well as a system for collecting and supporting them).

Can anyone make an offer?

+8
source share
4 answers

Also take a look at the new active_record_migrations

+4
source

There is a gem for using Rails Database Migrations in projects without Rails. His name is "standalone_migrations"

Here is the link

https://github.com/thuss/standalone-migrations

+3
source

If you do not like to use rake, but still get the ActiveRecord :: Migration system part, you can use the following to handle the ups and downs of a simple ruby ​​(without any rails):

require 'active_record' require 'benchmark' # Migration method, which does not uses files in db/migrate but in-memory migrations # Based on ActiveRecord::Migrator::migrate def migrate(migrations, target_version = nil) direction = case when target_version.nil? :up when (ActiveRecord::Migrator::current_version == target_version) return # do nothing when ActiveRecord::Migrator::current_version > target_version :down else :up end ActiveRecord::Migrator.new(direction, migrations, target_version).migrate puts "Current version: #{ActiveRecord::Migrator::current_version}" end # MigrationProxy deals with loading Migrations from files, we reuse it # to create instances of the migration classes we provide class MigrationClassProxy < ActiveRecord::MigrationProxy def initialize(migrationClass, version) super(migrationClass.name, version, nil, nil) @migrationClass = migrationClass end def mtime 0 end def load_migration @migrationClass.new(name, version) end end # Hash of all our migrations migrations = { 2016_08_09_2013_00 => class CreateSolutionTable < ActiveRecord::Migration[5.0] def change create_table :solution_submissions do |t| t.string :problem_hash, index: true t.string :solution_hash, index: true t.float :resemblance t.timestamps end end self # Necessary to get the class instance into the hash! end, 2016_08_09_2014_16 => class CreateProductFields < ActiveRecord::Migration[5.0] # ... self end }.map { |key,value| MigrationClassProxy.new(value, key) } ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => 'XXX.db' ) # Play all migrations (rake db:migrate) migrate(migrations, migrations.last.version) # ... or undo them (rake db:migrate VERSION=0) migrate(migrations, 0) class ApplicationRecord < ActiveRecord::Base self.abstract_class = true end class SolutionSubmission < ApplicationRecord end 
+2
source

There is another stone called otr-activerecord . This gemstone provides the following tasks:

  • rake db:create
  • rake db:create_migration[name]
  • rake db:drop
  • rake db:environment:set
  • rake db:fixtures:load
  • rake db:migrate
  • rake db:migrate:status
  • rake db:rollback
  • rake db:schema:cache:clear
  • rake db:schema:cache:dump
  • rake db:schema:dump
  • rake db:schema:load
  • rake db:seed
  • rake db:setup
  • rake db:structure:dump
  • rake db:structure:load
  • rake db:version

All you have to do is install it and add a Rakefile with content

 load 'tasks/otr-activerecord.rake' OTR::ActiveRecord.configure_from_file! 'config/database.yml' 

I prefer this gemstone over active_record_migrations or Standalone Migration, because these two gemstones depend on railties , i.e. almost all Rails. For example, Nokogiri takes a long time to compile and takes up a lot of space.

0
source

All Articles