2012-08-09 6 views
7

के बाहर माइग्रेशन जेनरेट करें मैं रेल के बाहर ActiveRecord का उपयोग कर रहा हूं। मैं एक कार्यक्रम चाहता हूं कि एक माइग्रेशन के कंकाल उत्पन्न करें (साथ ही साथ उन्हें इकट्ठा करने और बनाए रखने के लिए एक प्रणाली)।रेल

क्या कोई सुझाव दे सकता है?

उत्तर

3

गैर रेल परियोजनाओं में रेल डेटाबेस डेटाबेस माइग्रेशन का उपयोग करने के लिए एक मणि है। इसका नाम "standalone_migrations"

यहाँ एक लिंक

आप रेक उपयोग करने के लिए पसंद नहीं है https://github.com/thuss/standalone-migrations

+3

यह लिंक सवाल का जवाब दे सकते हैं, यहां उत्तर के आवश्यक हिस्सों को शामिल करना बेहतर है और संदर्भ के लिए लिंक प्रदान करना बेहतर है। लिंक किए गए पृष्ठ में परिवर्तन होने पर लिंक-केवल उत्तर अमान्य हो सकते हैं। –

1

पर एक नज़र डालें, लेकिन अभी भी की प्रणाली हिस्सा मिलता है ActiveRecord :: माइग्रेशन, फिर आप सादे रूबी (बिना किसी रेल के) से अप और डाउन को संभालने के लिए निम्न का उपयोग कर सकते हैं:

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