activerecord-importer 0.2.0 → 0.3.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.
- data/Rakefile +1 -3
- data/lib/activerecord/importer/runner.rb +71 -34
- data/lib/activerecord/importer/version.rb +1 -1
- metadata +6 -6
data/Rakefile
CHANGED
@@ -13,8 +13,6 @@ Hoe.spec 'activerecord-importer' do
|
|
13
13
|
self.author = 'Gerald Bauer'
|
14
14
|
self.email = 'example@googlegroups.com'
|
15
15
|
|
16
|
-
|
17
|
-
self.readme_file = 'README.md'
|
18
|
-
self.history_file = 'History.md'
|
16
|
+
self.licenses = ['Public Domain']
|
19
17
|
|
20
18
|
end
|
@@ -21,8 +21,8 @@ module ActiveRecord::Importer
|
|
21
21
|
pp @db_dest
|
22
22
|
|
23
23
|
# lets use a props model for tracking versions/imports
|
24
|
-
@
|
25
|
-
@
|
24
|
+
@prop_klass = Class.new( ActiveRecord::Base ) # same as class Anoynymous < ActiveRecord::Base ; end
|
25
|
+
@prop_klass.table_name = db_props_table_name # same as self.table_name = 'new_table_name'
|
26
26
|
end
|
27
27
|
|
28
28
|
## todo: use connect_old! w/ !!! - check ar for convetion
|
@@ -41,71 +41,91 @@ module ActiveRecord::Importer
|
|
41
41
|
|
42
42
|
def import( items )
|
43
43
|
|
44
|
+
## todo: track start/stop time
|
45
|
+
|
44
46
|
items.each_with_index do |item,index|
|
45
|
-
model_clazz = item[0]
|
46
|
-
sql = item[1]
|
47
47
|
puts "Importing step #{index+1}/#{items.size}..."
|
48
|
-
|
48
|
+
|
49
|
+
model_klass = item[0]
|
50
|
+
|
51
|
+
if item.size > 2 # assume source is array (cols ary,recs ary)
|
52
|
+
cols = item[1]
|
53
|
+
recs = item[2]
|
54
|
+
import_table_from_ary( model_klass, cols, recs )
|
55
|
+
else # assume source is sql (string)
|
56
|
+
sql = item[1]
|
57
|
+
import_table_from_sql( model_klass, sql )
|
58
|
+
end
|
49
59
|
end
|
50
60
|
|
51
61
|
version = version_string()
|
52
62
|
|
53
|
-
|
54
|
-
@
|
55
|
-
puts
|
63
|
+
print "Adding version string >#{version}<..."
|
64
|
+
@prop_klass.create!( :key => 'db.version.import', :value => version )
|
65
|
+
puts 'OK'
|
56
66
|
end
|
57
67
|
|
58
68
|
|
59
|
-
|
60
|
-
|
69
|
+
def import_table_from_ary( model_klass, cols, recs )
|
70
|
+
|
71
|
+
print "Found #{recs.size} records; "
|
72
|
+
|
73
|
+
connect_new()
|
61
74
|
|
62
|
-
|
75
|
+
print "adding to table >#{model_klass.name}<"
|
76
|
+
|
77
|
+
recs.each_with_index do |rec,i|
|
78
|
+
obj = model_klass.new
|
79
|
+
cols.each_with_index do |col,index|
|
80
|
+
## nb: use setters; lets us use obj.id = 42 e.g. mass assignment ignores/protects ids
|
81
|
+
obj.send( "#{col}=", rec[index] )
|
82
|
+
end
|
83
|
+
obj.save!
|
84
|
+
print_progress(i) # keep user entertained ...o....O...
|
85
|
+
end
|
86
|
+
puts 'OK'
|
87
|
+
end # method import_table_from_ary
|
88
|
+
|
89
|
+
|
90
|
+
def import_table_from_sql( model_klass, sql )
|
63
91
|
|
64
92
|
connect_old()
|
65
93
|
|
66
|
-
|
94
|
+
print "Fetching records with query >#{sql}<..."
|
67
95
|
|
68
96
|
recs = ActiveRecord::Base.connection.execute( sql )
|
69
97
|
|
70
|
-
puts
|
98
|
+
puts 'OK'
|
99
|
+
print "Found #{recs.length} records; "
|
71
100
|
|
72
101
|
connect_new()
|
73
102
|
|
74
|
-
|
103
|
+
print "adding records to table >#{model_klass.name}<"
|
75
104
|
|
76
105
|
recs.each_with_index do |rec,i|
|
77
|
-
|
78
|
-
# entertain use on console .....o....o...1000.. etc.
|
79
|
-
if ((i+1) % 1000) == 0
|
80
|
-
print (i+1)
|
81
|
-
elsif ((i+1) % 100) == 0
|
82
|
-
print 'O'
|
83
|
-
elsif ((i+1) % 10) == 0
|
84
|
-
print 'o'
|
85
|
-
else
|
86
|
-
print '.'
|
87
|
-
end
|
88
|
-
|
89
|
-
print "\r\n" if ((i+1) % 80) == 0 # add new line after 80 records
|
90
|
-
|
91
106
|
#debug_dump_record( rec )
|
92
|
-
|
107
|
+
model_klass.create!( downcase_hash( rec ) )
|
108
|
+
|
109
|
+
print_progress(i) # keep user entertained ...o....O...
|
93
110
|
end
|
94
111
|
|
95
|
-
puts
|
112
|
+
puts 'OK'
|
96
113
|
end
|
97
114
|
|
98
115
|
|
99
116
|
def debug_dump_table( sql )
|
100
117
|
|
118
|
+
print "Connecting..."
|
101
119
|
connect_old()
|
102
|
-
puts
|
120
|
+
puts 'OK'
|
121
|
+
puts 'Connection successfully established.'
|
103
122
|
|
104
|
-
|
123
|
+
print "Fetch records with query >#{sql}<..."
|
105
124
|
|
106
125
|
recs = ActiveRecord::Base.connection.execute( sql )
|
107
126
|
|
108
|
-
puts
|
127
|
+
puts 'OK'
|
128
|
+
puts "Found #{recs.length} records."
|
109
129
|
|
110
130
|
|
111
131
|
recs.each do |rec|
|
@@ -113,12 +133,29 @@ def debug_dump_table( sql )
|
|
113
133
|
debug_dump_record( rec )
|
114
134
|
end
|
115
135
|
|
116
|
-
puts
|
136
|
+
puts 'OK'
|
117
137
|
end
|
118
138
|
|
119
139
|
|
120
140
|
private
|
121
141
|
|
142
|
+
|
143
|
+
def print_progress( i )
|
144
|
+
# entertain use on console .....o....o...1000.. etc.
|
145
|
+
if ((i+1) % 1000) == 0
|
146
|
+
print (i+1)
|
147
|
+
elsif ((i+1) % 100) == 0
|
148
|
+
print 'O'
|
149
|
+
elsif ((i+1) % 10) == 0
|
150
|
+
print 'o'
|
151
|
+
else
|
152
|
+
print '.'
|
153
|
+
end
|
154
|
+
|
155
|
+
print "\r\n" if ((i+1) % 80) == 0 # add new line after 80 records
|
156
|
+
end
|
157
|
+
|
158
|
+
|
122
159
|
def version_string
|
123
160
|
|
124
161
|
username = ENV['USERNAME'] || '!!(username missing)'
|
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: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 3
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.3.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-12-
|
18
|
+
date: 2012-12-04 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rdoc
|
@@ -64,8 +64,8 @@ files:
|
|
64
64
|
- lib/activerecord/importer/runner.rb
|
65
65
|
- lib/activerecord/importer/version.rb
|
66
66
|
homepage: https://github.com/geraldb/activerecord-importer
|
67
|
-
licenses:
|
68
|
-
|
67
|
+
licenses:
|
68
|
+
- Public Domain
|
69
69
|
post_install_message:
|
70
70
|
rdoc_options:
|
71
71
|
- --main
|