activerecord-importer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ ### 0.1.0 / 2012-11-29
2
+
3
+ * Everything is new. First release
@@ -0,0 +1,7 @@
1
+ History.md
2
+ Manifest.txt
3
+ README.md
4
+ Rakefile
5
+ lib/activerecord/importer.rb
6
+ lib/activerecord/importer/runner.rb
7
+ lib/activerecord/importer/version.rb
@@ -0,0 +1,7 @@
1
+ # activerecord-importer
2
+
3
+ A simple data importer.
4
+
5
+ ## License
6
+
7
+ The scripts are dedicated to the public domain. Use it as you please with no restrictions whatsoever.
@@ -0,0 +1,20 @@
1
+ require 'hoe'
2
+ require './lib/activerecord/importer/version.rb'
3
+
4
+ Hoe.spec 'activerecord-importer' do
5
+
6
+ self.version = ActiveRecord::Importer::VERSION
7
+
8
+ self.summary = 'activerecord-importer - Another Data Importer'
9
+ self.description = self.summary
10
+
11
+ self.urls = ['https://github.com/geraldb/activerecord-importer']
12
+
13
+ self.author = 'Gerald Bauer'
14
+ self.email = 'example@googlegroups.com'
15
+
16
+ # switch extension to .markdown for gihub formatting
17
+ self.readme_file = 'README.md'
18
+ self.history_file = 'History.md'
19
+
20
+ end
@@ -0,0 +1,19 @@
1
+
2
+ require 'pp'
3
+ require 'yaml'
4
+
5
+
6
+ require 'activerecord/importer/version'
7
+ require 'activerecord/importer/runner'
8
+
9
+ module ActiveRecord::Importer
10
+
11
+ def self.banner
12
+ "activerecord-importer #{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
13
+ end
14
+
15
+ end
16
+
17
+
18
+ # say hello on load
19
+ puts ActiveRecord::Importer.banner
@@ -0,0 +1,117 @@
1
+
2
+
3
+ module ActiveRecord::Importer
4
+
5
+ class Runner
6
+
7
+ def initialize( db_config_path = 'database.yml', db_source_key = 'source', db_dest_key = 'dest' )
8
+
9
+ @db_config = YAML.load_file( db_config_path )
10
+
11
+ @db_source = @db_config[ db_source_key ]
12
+ @db_dest = @db_config[ db_dest_key ]
13
+
14
+ puts "datasource old:"
15
+ pp @db_source
16
+
17
+ puts "datasource new:"
18
+ pp @db_dest
19
+ end
20
+
21
+
22
+ ### sql - source query
23
+ ### model_clazz - target model/table
24
+
25
+ def import( sql, model_clazz )
26
+
27
+ ActiveRecord::Base.establish_connection( @db_source )
28
+
29
+ recs = ActiveRecord::Base.connection.execute( sql )
30
+
31
+ puts "SQL>#{sql}<"
32
+
33
+ puts "#{model_clazz.name} -> Found #{recs.length} Records"
34
+
35
+ ActiveRecord::Base.establish_connection( @db_dest )
36
+
37
+ recs.each_with_index do |rec,i|
38
+
39
+ # entertain use on console .....o....o...1000.. etc.
40
+ if ((i+1) % 1000) == 0
41
+ print (i+1)
42
+ elsif ((i+1) % 100) == 0
43
+ print 'O'
44
+ elsif ((i+1) % 10) == 0
45
+ print 'o'
46
+ else
47
+ print '.'
48
+ end
49
+
50
+ print "\r\n" if ((i+1) % 80) == 0 # add new line after 80 records
51
+
52
+ #debug_dump_record( rec )
53
+ model_clazz.create!( downcase_hash( rec ) )
54
+ end
55
+
56
+ puts "OK"
57
+ end
58
+
59
+ def version_string
60
+
61
+ username = ENV['USERNAME'] || '!!(username missing)'
62
+ computername = ENV['COMPUTERNAME'] || '!!(computername missing)'
63
+
64
+ buf = ""
65
+ buf << "generated on #{Time.now} "
66
+ buf << "by #{username} @ #{computername} "
67
+ buf << "using Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
68
+ buf
69
+ end
70
+
71
+
72
+ def debug_dump_table( sql )
73
+
74
+ ActiveRecord::Base.establish_connection( @db_source )
75
+ puts "OK - Verbindung erfolgreich aufgebaut!!!"
76
+
77
+ recs = ActiveRecord::Base.connection.execute( sql )
78
+
79
+ puts "OK - Found #{recs.length} Records!!!"
80
+
81
+ puts "SQL>#{sql}<"
82
+
83
+ recs.each do |rec|
84
+ print '.'
85
+ debug_dump_record( rec )
86
+ end
87
+
88
+ puts "OK"
89
+ end
90
+
91
+
92
+
93
+ private
94
+ def downcase_hash( hash )
95
+ new_hash = {}
96
+ hash.each do |k,v|
97
+ # hack: remove rownum key (only internal use; used only for paging)
98
+ next if k.downcase.to_s == 'rownum'
99
+
100
+ if v.class == String
101
+ new_hash[ k.downcase ] = v.rstrip # remove trailing spaces (added by cobol in db2!!!)
102
+ else
103
+ new_hash[ k.downcase ] = v
104
+ end
105
+ end
106
+ new_hash
107
+ end
108
+
109
+ def debug_dump_record( hash )
110
+ hash.each do |k,v|
111
+ puts "#{k} => >#{v}< : #{v.class}"
112
+ end
113
+ end
114
+
115
+ end # class Runner
116
+
117
+ end # module ActiveRecord::Importer
@@ -0,0 +1,7 @@
1
+
2
+ module ActiveRecord ; end # forward reference
3
+
4
+ module ActiveRecord::Importer
5
+ VERSION = "0.1.0"
6
+ end
7
+
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-importer
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Gerald Bauer
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-11-30 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rdoc
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 19
29
+ segments:
30
+ - 3
31
+ - 10
32
+ version: "3.10"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: hoe
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 7
44
+ segments:
45
+ - 3
46
+ - 0
47
+ version: "3.0"
48
+ type: :development
49
+ version_requirements: *id002
50
+ description: activerecord-importer - Another Data Importer
51
+ email: example@googlegroups.com
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files:
57
+ - Manifest.txt
58
+ files:
59
+ - History.md
60
+ - Manifest.txt
61
+ - README.md
62
+ - Rakefile
63
+ - lib/activerecord/importer.rb
64
+ - lib/activerecord/importer/runner.rb
65
+ - lib/activerecord/importer/version.rb
66
+ homepage: https://github.com/geraldb/activerecord-importer
67
+ licenses: []
68
+
69
+ post_install_message:
70
+ rdoc_options:
71
+ - --main
72
+ - README.md
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ requirements: []
94
+
95
+ rubyforge_project: activerecord-importer
96
+ rubygems_version: 1.8.24
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: activerecord-importer - Another Data Importer
100
+ test_files: []
101
+