transplant 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,24 +6,28 @@ module Transplant
6
6
  def initialize(app_name, connection)
7
7
  @app_name = app_name
8
8
  @connection = connection
9
+ @queries ||= []
10
+ @results ||= {}
9
11
  end
10
12
 
11
13
  def query(sql)
12
- @connection.execute sql
14
+ return @results[sql] if @queries.include?(sql)
15
+ @queries << sql
16
+ @results[sql] = @connection.execute(sql)
13
17
  end
14
18
 
15
19
  def save(klass, other = {})
16
- klass_name = klass.class.to_s.tableize.humanize
20
+ klass_name = klass.class.to_s.tableize.humanize.singularize
17
21
  if klass.valid?
18
22
  klass.save!
19
- increment
23
+ succeed klass_name
20
24
  klass
21
25
  else
22
- increment_failure(klass.class.to_s)
26
+ fail(klass_name)
23
27
  puts "Invalid #{klass_name} information:"
24
28
  Stats.output("Additional Info about #{klass_name}", other)
25
29
  Stats.output("#{klass_name} errors", klass.errors.full_messages)
26
- Stats.output("#{klass_name} attributes:", klass.attributes)
30
+ Stats.output("#{klass_name} attributes", klass.attributes)
27
31
  return false
28
32
  end
29
33
  end
@@ -35,6 +39,10 @@ module Transplant
35
39
  @tables
36
40
  end
37
41
 
42
+ def column_names(table_name)
43
+ connection.columns(table_name).map(&:name)
44
+ end
45
+
38
46
  def truncate(*tables)
39
47
  tables.each { |table| self.query "TRUNCATE TABLE #{table.to_s}" }
40
48
  end
@@ -43,27 +51,28 @@ module Transplant
43
51
  truncate *tables
44
52
  end
45
53
 
46
- def increment
47
- @total_records ||= 0
48
- @total_records += 1
54
+ def succeed(klass_name)
55
+ @successes ||= Hash.new
56
+ @successes[klass_name] ||= 0
57
+ @successes[klass_name] += 1
49
58
  end
50
59
 
51
- def increment_failure(klass_name)
60
+ def fail(klass_name)
52
61
  @failures ||= Hash.new
53
62
  @failures[klass_name] ||= 0
54
63
  @failures[klass_name] += 1
55
64
  end
56
65
 
57
66
  def failures
58
- @failures
59
- rescue
60
67
  @failures ||= Hash.new
61
68
  end
62
69
 
63
- def total_records
64
- @total_records
65
- rescue
66
- @total_records ||= Hash.new
70
+ def successes
71
+ @successes ||= Hash.new
72
+ end
73
+
74
+ def total_successes
75
+ @successes.map { |table, count| count }.inject{ |sum,x| sum + x }
67
76
  end
68
77
  end
69
78
  end
@@ -2,64 +2,78 @@ module Transplant
2
2
  class Stats
3
3
 
4
4
  def initialize(transplanter)
5
- @transplanter = transplanter
5
+ @planter = transplanter
6
6
  end
7
7
 
8
8
  def output_all_info(opts = {})
9
9
  total_import_time(opts[:measurement]) if opts[:measurement].present?
10
10
  total_import_records
11
- @transplanter.failures
11
+ successes
12
+ failures
12
13
  end
13
14
 
14
- def self.output(header, input, depth = 0, sub_output = false)
15
- if input.is_a? Hash
16
- hash_output(header, input, depth, sub_output)
17
- elsif input.is_a? Array
18
- array_output(header, input, depth, sub_output)
15
+ class << self
16
+
17
+ def output(header, input, depth = 0, sub_output = false)
18
+ if input.is_a? Hash
19
+ hash_output(header, input, depth, sub_output)
20
+ elsif input.is_a? Array
21
+ array_output(header, input, depth, sub_output)
22
+ end
19
23
  end
20
- end
21
24
 
22
- def self.hash_output(header, hash, depth = 0, sub_hash = false)
23
- puts "#{"\t"*depth}#{header}:" if hash.any?
24
- hash.each_pair do |key, value|
25
- if value.is_a? Hash
26
- hash_output("", value, depth + 1, sub_hash = true)
27
- elsif value.is_a? Array
28
- array_output("", value, depth + 1, sub_array = true)
29
- else
30
- puts "#{"\t"*(depth + 1)}#{key}: #{value}"
25
+ def hash_output(header, hash, depth = 0, sub_hash = false)
26
+ puts tabs(depth) + "#{header}:" if hash.any? && header.present?
27
+ hash.each_pair do |key, value|
28
+ if value.is_a?(Hash) || value.is_a?(Array)
29
+ output(key, value, depth + 1, sub_hash = true)
30
+ else
31
+ puts tabs(depth + 1) + "* #{key}: #{value}"
32
+ end
31
33
  end
32
34
  end
33
- end
34
35
 
35
- def self.array_output(header, array, depth = 0, sub_array = false)
36
- puts "#{"\t"*depth}#{header}:" if array.any?
37
- array.each do |item|
38
- if item.is_a? Array
39
- array_output("", item, depth + 1, sub_array = true)
40
- elsif item.is_a? Hash
41
- hash_output("", item, depth + 1, sub_hash = true)
42
- else
43
- puts "#{"\t"*(depth + 1)}* #{item}"
36
+ def array_output(header, array, depth = 0, sub_array = false)
37
+ puts tabs(depth) + "#{header}:" if array.any? && header.present?
38
+ array.each do |item|
39
+ if item.is_a?(Hash) || item.is_a?(Array)
40
+ output("", item, depth + 1, sub_hash = true)
41
+ else
42
+ puts tabs(depth + 1) + "* #{item}"
43
+ end
44
44
  end
45
45
  end
46
+
47
+ def tabs(count)
48
+ "\t"*count
49
+ end
50
+ private :tabs
51
+
46
52
  end
47
53
 
48
54
  def total_import_records
49
- puts "\nTotal number of records imported into #{@transplanter.app_name}: #{@transplanter.total_records}"
55
+ puts "Estimated number of records imported into #{@planter.app_name}: #{@planter.total_successes}"
56
+ end
57
+
58
+ def total_import_time(m)
59
+ puts "Total time taken to import everything into #{@planter.app_name}: #{(m.real/60).round(2)} minutes"
50
60
  end
51
61
 
52
- def total_import_time(measurement)
53
- puts "Total time taken to import everything into #{@transplanter.app_name}"
54
- puts measurement
62
+ def successes
63
+ if @planter.successes.count <= 0
64
+ puts "\nNo records were imported! Boo!!!!\n"
65
+ else
66
+ count = Hash[@planter.successes.map { |key, value| [key.tableize.humanize, value] }]
67
+ self.class.output("\nEstimated number of successful imports to #{@planter.app_name}", count)
68
+ end
55
69
  end
56
70
 
57
71
  def failures
58
- if @transplanter.failures.count <= 0
72
+ if @planter.failures.count <= 0
59
73
  puts "\nNo failed record imports!!!! Time to par-tay!!!!\n"
60
74
  else
61
- failures = Hash[@transplanter.failures.map { |key, value| [key.tableize.humanize, value] }]
62
- self.class.output("\nTotal number failed imports to #{@transplanter.app_name}:", failures)
75
+ count = Hash[@planter.failures.map { |key, value| [key.tableize.humanize, value] }]
76
+ self.class.output("\nEstimated number failed imports to #{@planter.app_name}", count)
63
77
  end
64
78
  end
65
79
 
@@ -1,3 +1,3 @@
1
1
  module Transplant
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/transplant.gemspec CHANGED
@@ -16,7 +16,7 @@ Gem::Specification.new do |gem|
16
16
  gem.version = Transplant::VERSION
17
17
 
18
18
  gem.add_dependency "activesupport", "~> 3.2.2"
19
- gem.add_dependency "mysql2", "~> 0.3.11"
19
+ gem.add_dependency "activerecord", "~> 3.2.2"
20
20
  gem.add_development_dependency "rspec", "~> 2.9.0"
21
21
  gem.add_development_dependency "rake", "~> 0.9.2.2"
22
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: transplant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-02 00:00:00.000000000 Z
12
+ date: 2012-04-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -28,13 +28,13 @@ dependencies:
28
28
  - !ruby/object:Gem::Version
29
29
  version: 3.2.2
30
30
  - !ruby/object:Gem::Dependency
31
- name: mysql2
31
+ name: activerecord
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
35
  - - ~>
36
36
  - !ruby/object:Gem::Version
37
- version: 0.3.11
37
+ version: 3.2.2
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,7 +42,7 @@ dependencies:
42
42
  requirements:
43
43
  - - ~>
44
44
  - !ruby/object:Gem::Version
45
- version: 0.3.11
45
+ version: 3.2.2
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: rspec
48
48
  requirement: !ruby/object:Gem::Requirement
@@ -107,7 +107,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
107
107
  version: '0'
108
108
  segments:
109
109
  - 0
110
- hash: 2211955661356025144
110
+ hash: 3664032782828776263
111
111
  required_rubygems_version: !ruby/object:Gem::Requirement
112
112
  none: false
113
113
  requirements:
@@ -116,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  version: '0'
117
117
  segments:
118
118
  - 0
119
- hash: 2211955661356025144
119
+ hash: 3664032782828776263
120
120
  requirements: []
121
121
  rubyforge_project:
122
122
  rubygems_version: 1.8.21