Class: ExternalMigration::Schemas::SchemasMigration
- Inherits:
-
Object
- Object
- ExternalMigration::Schemas::SchemasMigration
- Defined in:
- lib/external_migration/schemas.rb
Overview
Façade class make easy way to running batchs schemas migrations
File Format
name_migration:
:type: CUSTOM, SCHEMA
from: URL to from schema or Custom Param
to: URL to destinate schema or Custom Param
transformer: class will be called to data transformations
Instance Attribute Summary collapse
-
#processor ⇒ Object
Returns the value of attribute processor.
-
#schemas ⇒ Object
Returns the value of attribute schemas.
-
#transformer ⇒ Object
Returns the value of attribute transformer.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
- #eval_class(class_str) ⇒ Object
-
#initialize(file_schemas, schemas = nil) ⇒ SchemasMigration
constructor
A new instance of SchemasMigration.
- #migrate! ⇒ Object
- #migrate_custom ⇒ Object
- #migrate_schema ⇒ Object
- #require_dependence(file_name) ⇒ Object
- #run_migration_job ⇒ Object
-
#search_dependency(file_name) ⇒ Object
Array with possible location of file.
- #transformer_class=(class_str) ⇒ Object
- #transformer_from_schema ⇒ Object
Constructor Details
#initialize(file_schemas, schemas = nil) ⇒ SchemasMigration
Returns a new instance of SchemasMigration.
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/external_migration/schemas.rb', line 21 def initialize(file_schemas, schemas=nil) if schemas.nil? raise "Invalid File: should not null!" if file_schemas.nil? raise "Invalid File: should not exists!" if not File.exists?(file_schemas) @schemas = YAML::load(File.open(file_schemas)) else @schemas = schemas end @verbose = true end |
Instance Attribute Details
#processor ⇒ Object
Returns the value of attribute processor.
19 20 21 |
# File 'lib/external_migration/schemas.rb', line 19 def processor @processor end |
#schemas ⇒ Object
Returns the value of attribute schemas.
19 20 21 |
# File 'lib/external_migration/schemas.rb', line 19 def schemas @schemas end |
#transformer ⇒ Object
Returns the value of attribute transformer.
19 20 21 |
# File 'lib/external_migration/schemas.rb', line 19 def transformer @transformer end |
#verbose ⇒ Object
Returns the value of attribute verbose.
19 20 21 |
# File 'lib/external_migration/schemas.rb', line 19 def verbose @verbose end |
Instance Method Details
#eval_class(class_str) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/external_migration/schemas.rb', line 65 def eval_class(class_str) begin class_found = eval class_str raise "its %s not a class" % class_str if !class_found.is_a?(Class) rescue class_found = false end end |
#migrate! ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/external_migration/schemas.rb', line 32 def migrate! ActiveRecord::Base.transaction do @schemas.each do |key,schema| @migration_name = key @schema = schema msg = "Starting external migration: %s..." % @migration_name Rails.logger.info msg puts msg if verbose result = run_migration_job raise ActiveMigartionSchemasError.new("Failing Migrate Schemas: %s" % key) if not result msg = "Ending: %s." % @migration_name Rails.logger.info msg puts msg if verbose end end end |
#migrate_custom ⇒ Object
135 136 137 138 139 140 |
# File 'lib/external_migration/schemas.rb', line 135 def migrate_custom raise "Transformer not assigned" if @transformer.nil? raise "Invalid Custom Migration Transformer" if not @transformer.respond_to?(:migrate!) @transformer.migrate! end |
#migrate_schema ⇒ Object
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/external_migration/schemas.rb', line 124 def migrate_schema migration = ExternalMigration::Migration.new migration.name = @migration_name migration.schema_from = @schema[:from] migration.schema_to = @schema[:to] migration.transformer = @transformer if not @transformer.nil? migration.processor = @processor if not @processor.nil? #rock! migration.migrate! end |
#require_dependence(file_name) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/external_migration/schemas.rb', line 93 def require_dependence(file_name) Rails.logger.warn "Requiring file %s" % file_name search_dependency(file_name).each do |file| if File.exists? file Rails.logger.debug "Including file %s" % file require file break end end end |
#run_migration_job ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/external_migration/schemas.rb', line 54 def run_migration_job transformer_from_schema() case @schema[:type] when :SCHEMA self.migrate_schema when :CUSTOM self.migrate_custom end end |
#search_dependency(file_name) ⇒ Object
Returns Array with possible location of file.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/external_migration/schemas.rb', line 108 def search_dependency(file_name) files = [] files << Rails.root.join("db/external_migrate/" + file_name) #possibility paths unless File.exists?(files[0]) Dir[file_name, File.("**/external_migrate/**/" + file_name), "../" + file_name, "../../" + file_name, File.("**/" + file_name)].each { |f| files << f } end files end |
#transformer_class=(class_str) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/external_migration/schemas.rb', line 74 def transformer_class=(class_str) path = class_str.split("::") path.map!(&:underscore) file_name = path.join("/") + ".rb" class_found = eval_class(class_str) if class_found==false require_dependence(file_name) class_found = eval_class(class_str) raise "[%s] Invald informed Transformer: %s. Schema: %s" % [@migration_name, class_str, @schema.to_yaml] if class_found == false end @transformer = (eval class_str).new @schema end |
#transformer_from_schema ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/external_migration/schemas.rb', line 142 def transformer_from_schema if @schema.include? :transformer self.transformer_class = @schema[:transformer] else @transformer = nil end end |