activerecord-importer 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,10 @@ module ActiveRecord::Importer
|
|
4
4
|
|
5
5
|
class Runner
|
6
6
|
|
7
|
-
def initialize( db_config_path = 'database.yml',
|
7
|
+
def initialize( db_config_path = 'database.yml',
|
8
|
+
db_source_key = 'source',
|
9
|
+
db_dest_key = 'dest',
|
10
|
+
db_props_table_name = 'props' )
|
8
11
|
|
9
12
|
@db_config = YAML.load_file( db_config_path )
|
10
13
|
|
@@ -16,23 +19,59 @@ module ActiveRecord::Importer
|
|
16
19
|
|
17
20
|
puts "datasource new:"
|
18
21
|
pp @db_dest
|
22
|
+
|
23
|
+
# lets use a props model for tracking versions/imports
|
24
|
+
@prop_clazz = Class.new( ActiveRecord::Base ) # same as class Anoynymous < ActiveRecord::Base ; end
|
25
|
+
@prop_clazz.table_name = db_props_table_name # same as self.table_name = 'new_table_name'
|
26
|
+
end
|
27
|
+
|
28
|
+
## todo: use connect_old! w/ !!! - check ar for convetion
|
29
|
+
def connect_old
|
30
|
+
ActiveRecord::Base.establish_connection( @db_source )
|
31
|
+
end
|
32
|
+
|
33
|
+
def connect_new
|
34
|
+
ActiveRecord::Base.establish_connection( @db_dest )
|
35
|
+
end
|
36
|
+
|
37
|
+
def connect( key )
|
38
|
+
ActiveRecord::Base.establish_connection( @db_config[ key ] )
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
def import( items )
|
43
|
+
|
44
|
+
items.each_with_index do |item,index|
|
45
|
+
model_clazz = item[0]
|
46
|
+
sql = item[1]
|
47
|
+
puts "Importing step #{index+1}/#{items.size}..."
|
48
|
+
import_table( model_clazz, sql )
|
49
|
+
end
|
50
|
+
|
51
|
+
version = version_string()
|
52
|
+
|
53
|
+
puts "Adding version string >#{version}<..."
|
54
|
+
@prop_clazz.create!( :key => 'db.version.import', :value => version )
|
55
|
+
puts "OK"
|
19
56
|
end
|
20
|
-
|
21
|
-
|
57
|
+
|
58
|
+
|
22
59
|
### sql - source query
|
23
60
|
### model_clazz - target model/table
|
24
|
-
|
25
|
-
def import( sql, model_clazz )
|
26
61
|
|
27
|
-
|
62
|
+
def import_table( model_clazz, sql )
|
63
|
+
|
64
|
+
connect_old()
|
65
|
+
|
66
|
+
puts "Fetching records with query >#{sql}<..."
|
28
67
|
|
29
68
|
recs = ActiveRecord::Base.connection.execute( sql )
|
30
69
|
|
31
|
-
puts "
|
70
|
+
puts "OK - Found #{recs.length} records."
|
32
71
|
|
33
|
-
|
72
|
+
connect_new()
|
34
73
|
|
35
|
-
|
74
|
+
puts "Adding records to table >#{model_clazz.name}<..."
|
36
75
|
|
37
76
|
recs.each_with_index do |rec,i|
|
38
77
|
|
@@ -50,35 +89,24 @@ module ActiveRecord::Importer
|
|
50
89
|
print "\r\n" if ((i+1) % 80) == 0 # add new line after 80 records
|
51
90
|
|
52
91
|
#debug_dump_record( rec )
|
53
|
-
model_clazz.create!( downcase_hash( rec ) )
|
92
|
+
model_clazz.create!( downcase_hash( rec ) )
|
54
93
|
end
|
55
94
|
|
56
|
-
puts "OK"
|
95
|
+
puts "OK"
|
57
96
|
end
|
58
97
|
|
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
98
|
|
72
99
|
def debug_dump_table( sql )
|
73
100
|
|
74
|
-
|
75
|
-
puts "OK -
|
101
|
+
connect_old()
|
102
|
+
puts "OK - Connection successfully established."
|
103
|
+
|
104
|
+
puts "SQL>#{sql}<"
|
76
105
|
|
77
106
|
recs = ActiveRecord::Base.connection.execute( sql )
|
78
107
|
|
79
|
-
puts "OK - Found #{recs.length}
|
108
|
+
puts "OK - Found #{recs.length} records."
|
80
109
|
|
81
|
-
puts "SQL>#{sql}<"
|
82
110
|
|
83
111
|
recs.each do |rec|
|
84
112
|
print '.'
|
@@ -88,9 +116,21 @@ def debug_dump_table( sql )
|
|
88
116
|
puts "OK"
|
89
117
|
end
|
90
118
|
|
91
|
-
|
92
|
-
|
119
|
+
|
93
120
|
private
|
121
|
+
|
122
|
+
def version_string
|
123
|
+
|
124
|
+
username = ENV['USERNAME'] || '!!(username missing)'
|
125
|
+
computername = ENV['COMPUTERNAME'] || '!!(computername missing)'
|
126
|
+
|
127
|
+
buf = ""
|
128
|
+
buf << "generated on #{Time.now} "
|
129
|
+
buf << "by #{username} @ #{computername} "
|
130
|
+
buf << "using Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
|
131
|
+
buf
|
132
|
+
end
|
133
|
+
|
94
134
|
def downcase_hash( hash )
|
95
135
|
new_hash = {}
|
96
136
|
hash.each do |k,v|
|
@@ -104,7 +144,7 @@ def downcase_hash( hash )
|
|
104
144
|
end
|
105
145
|
end
|
106
146
|
new_hash
|
107
|
-
end
|
147
|
+
end
|
108
148
|
|
109
149
|
def debug_dump_record( hash )
|
110
150
|
hash.each do |k,v|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-importer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Gerald Bauer
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-12-03 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rdoc
|
@@ -40,11 +40,11 @@ dependencies:
|
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
42
42
|
- !ruby/object:Gem::Version
|
43
|
-
hash:
|
43
|
+
hash: 1
|
44
44
|
segments:
|
45
45
|
- 3
|
46
|
-
-
|
47
|
-
version: "3.
|
46
|
+
- 3
|
47
|
+
version: "3.3"
|
48
48
|
type: :development
|
49
49
|
version_requirements: *id002
|
50
50
|
description: activerecord-importer - Another Data Importer
|