Class: ActiveRecord::Importer::Runner
- Inherits:
-
Object
- Object
- ActiveRecord::Importer::Runner
- Defined in:
- lib/activerecord/importer/runner.rb
Instance Method Summary collapse
- #connect(key) ⇒ Object
- #connect_new ⇒ Object
-
#connect_old ⇒ Object
todo: use connect_old! w/ !!! - check ar for convetion.
- #debug_dump_table(sql) ⇒ Object
- #import(items) ⇒ Object
- #import_table_from_ary(model_klass, cols, recs) ⇒ Object
- #import_table_from_sql(model_klass, sql) ⇒ Object
-
#initialize(db_config_path = 'database.yml', db_source_key = 'source', db_dest_key = 'dest', db_props_table_name = 'props') ⇒ Runner
constructor
A new instance of Runner.
Constructor Details
#initialize(db_config_path = 'database.yml', db_source_key = 'source', db_dest_key = 'dest', db_props_table_name = 'props') ⇒ Runner
Returns a new instance of Runner.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/activerecord/importer/runner.rb', line 7 def initialize( db_config_path = 'database.yml', db_source_key = 'source', db_dest_key = 'dest', db_props_table_name = 'props' ) @db_config = YAML.load_file( db_config_path ) @db_source = @db_config[ db_source_key ] @db_dest = @db_config[ db_dest_key ] puts "datasource old:" pp @db_source puts "datasource new:" pp @db_dest # lets use a props model for tracking versions/imports @prop_klass = Class.new( ActiveRecord::Base ) # same as class Anoynymous < ActiveRecord::Base ; end @prop_klass.table_name = db_props_table_name # same as self.table_name = 'new_table_name' end |
Instance Method Details
#connect(key) ⇒ Object
37 38 39 |
# File 'lib/activerecord/importer/runner.rb', line 37 def connect( key ) ActiveRecord::Base.establish_connection( @db_config[ key ] ) end |
#connect_new ⇒ Object
33 34 35 |
# File 'lib/activerecord/importer/runner.rb', line 33 def connect_new ActiveRecord::Base.establish_connection( @db_dest ) end |
#connect_old ⇒ Object
todo: use connect_old! w/ !!! - check ar for convetion
29 30 31 |
# File 'lib/activerecord/importer/runner.rb', line 29 def connect_old ActiveRecord::Base.establish_connection( @db_source ) end |
#debug_dump_table(sql) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/activerecord/importer/runner.rb', line 129 def debug_dump_table( sql ) print "Connecting..." connect_old() puts 'OK' puts 'Connection successfully established.' print "Fetch records with query >#{sql}<..." recs = ActiveRecord::Base.connection.execute( sql ) puts 'OK' puts "Found #{recs.length} records." recs.each do |rec| print '.' debug_dump_record( rec ) end puts 'OK' end |
#import(items) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/activerecord/importer/runner.rb', line 42 def import( items ) ## todo: track start/stop time items.each_with_index do |item,index| puts "Importing step #{index+1}/#{items.size}..." model_klass = item[0] if item.size > 2 # assume source is array (cols ary,recs ary) cols = item[1] recs = item[2] import_table_from_ary( model_klass, cols, recs ) else # assume source is sql (string) sql = item[1] import_table_from_sql( model_klass, sql ) end end version = version_string() print "Adding version string >#{version}<..." @prop_klass.create!( :key => 'db.version.import', :value => version ) puts 'OK' end |
#import_table_from_ary(model_klass, cols, recs) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/activerecord/importer/runner.rb', line 69 def import_table_from_ary( model_klass, cols, recs ) print "Found #{recs.size} records; " connect_new() print "adding to table >#{model_klass.name}<" objs = [] recs.each_with_index do |rec,i| obj = model_klass.new cols.each_with_index do |col,index| ## nb: use setters; lets us use obj.id = 42 e.g. mass assignment ignores/protects ids obj.send( "#{col}=", rec[index] ) end objs << obj ## obj.save! print_progress(i) # keep user entertained ...o....O... end ## lets use batch inserts thanks to activerecord-importer gem (see https://github.com/zdennis/activerecord-import) model_klass.import( objs ) puts 'OK' end |
#import_table_from_sql(model_klass, sql) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/activerecord/importer/runner.rb', line 98 def import_table_from_sql( model_klass, sql ) connect_old() print "Fetching records with query >#{sql}<..." recs = ActiveRecord::Base.connection.execute( sql ) puts 'OK' print "Found #{recs.length} records; " connect_new() print "adding records to table >#{model_klass.name}<" objs = [] recs.each_with_index do |rec,i| #debug_dump_record( rec ) objs << model_klass.new( downcase_hash( rec ) ) print_progress(i) # keep user entertained ...o....O... end ## lets use batch inserts thanks to activerecord-importer gem (see https://github.com/zdennis/activerecord-import) model_klass.import( objs ) puts 'OK' end |