Class: ActiveRecord::Importer::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord/importer/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(db_config_path = 'database.yml', db_source_key = 'source', db_dest_key = 'dest') ⇒ Runner

Returns a new instance of Runner.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/activerecord/importer/runner.rb', line 7

def initialize( db_config_path = 'database.yml', db_source_key = 'source', db_dest_key = 'dest' )

  @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
end

Instance Method Details

#debug_dump_table(sql) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/activerecord/importer/runner.rb', line 72

def debug_dump_table( sql )
  
  ActiveRecord::Base.establish_connection( @db_source )
  puts "OK - Verbindung erfolgreich aufgebaut!!!"

  recs = ActiveRecord::Base.connection.execute( sql )

  puts "OK - Found #{recs.length} Records!!!"

  puts "SQL>#{sql}<"

  recs.each do |rec|
    print '.'
    debug_dump_record( rec )
  end

  puts "OK"
end

#import(sql, model_clazz) ⇒ Object

sql - source query model_clazz - target model/table



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/activerecord/importer/runner.rb', line 25

def import( sql, model_clazz  )  

  ActiveRecord::Base.establish_connection( @db_source )
  
  recs = ActiveRecord::Base.connection.execute( sql )

  puts "SQL>#{sql}<"

  puts "#{model_clazz.name} -> Found #{recs.length} Records"

  ActiveRecord::Base.establish_connection(  @db_dest )

  recs.each_with_index do |rec,i|

    # entertain use on console .....o....o...1000.. etc.
    if ((i+1) % 1000) == 0
      print (i+1)
    elsif ((i+1) % 100) == 0
      print 'O'
    elsif ((i+1) % 10) == 0
      print 'o'
    else
      print '.'  
    end
     
    print "\r\n" if ((i+1) % 80) == 0   # add new line after 80 records

    #debug_dump_record( rec )
    model_clazz.create!( downcase_hash( rec ) )  
  end

  puts "OK"    
end

#version_stringObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/activerecord/importer/runner.rb', line 59

def version_string
  
  username     = ENV['USERNAME']     || '!!(username missing)'
  computername = ENV['COMPUTERNAME'] || '!!(computername missing)'
  
  buf = ""
  buf << "generated on #{Time.now} "
  buf << "by #{username} @ #{computername} " 
  buf << "using Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
  buf
end