transplant 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.9.3" > .rvmrc
9
+ environment_id="ruby-1.9.3-p125@transplant"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.11.3" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
27
+ \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
28
+ else
29
+ # If the environment file has not yet been created, use the RVM CLI to select.
30
+ rvm --create "$environment_id" || {
31
+ echo "Failed to create RVM environment '${environment_id}'."
32
+ return 1
33
+ }
34
+ fi
35
+
36
+ # If you use bundler, this might be useful to you:
37
+ # if [[ -s Gemfile ]] && {
38
+ # ! builtin command -v bundle >/dev/null ||
39
+ # builtin command -v bundle | grep $rvm_path/bin/bundle >/dev/null
40
+ # }
41
+ # then
42
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
43
+ # gem install bundler
44
+ # fi
45
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
46
+ # then
47
+ # bundle install | grep -vE '^Using|Your bundle is complete'
48
+ # fi
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in custom_rails_importer.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Matt Bridges
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Custom Rails Importer
2
+
3
+ The gem was born from a need for the organization I work for ([CollegePlus](http://collegeplus.org)). We were migrating our systems from one, single, monolithic system into a Service Oriented Architecture (SOA) using Rails and needed a way to do this as easily as possible. Thus, **Custom Rails Importer** was born.
4
+
5
+ #### Currently, you can only import data from a MySQL database. I hope to have this gem be database agnostic in the future.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'custom_rails_importer'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install custom_rails_importer
20
+
21
+ ## Usage
22
+
23
+ **Will come soon...**
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,73 @@
1
+ module Transplant
2
+ class Manager
3
+
4
+ cattr_accessor :app_name
5
+
6
+ def self.connect(app_name, mysql_credentials = {}, local_conn)
7
+ @@app_name = app_name
8
+ @@remote = Mysql2::Client.new mysql_credentials
9
+ @@local ||= local_conn
10
+ end
11
+
12
+ def self.local(sql)
13
+ @@local.execute sql
14
+ end
15
+
16
+ def self.save(klass, other = {})
17
+ nice_class_name = klass.class.to_s.tableize.humanize
18
+ if klass.valid?
19
+ klass.save!
20
+ increment
21
+ klass
22
+ else
23
+ increment_failure(klass.class.to_s)
24
+ puts "Invalid #{nice_class_name} information:"
25
+ Stats.output("Additional Info about #{nice_class_name}", other)
26
+ Stats.output("#{nice_class_name} errors", klass.errors.full_messages)
27
+ Stats.output("#{nice_class_name} attributes:", klass.attributes)
28
+ return false
29
+ end
30
+ end
31
+
32
+ def self.remote(sql)
33
+ @@remote.query(sql)
34
+ end
35
+
36
+ def self.local_tables
37
+ tables = @@local.tables
38
+ tables.delete 'schema_migrations'
39
+ tables
40
+ end
41
+
42
+ def self.local_truncate(*tables)
43
+ tables.each { |table| local "TRUNCATE TABLE #{table.to_s}" }
44
+ end
45
+
46
+ def self.local_truncate_all
47
+ local_truncate *local_tables
48
+ end
49
+
50
+ def self.increment
51
+ @@total_records ||= 0
52
+ @@total_records += 1
53
+ end
54
+
55
+ def self.increment_failure(klass_name)
56
+ @@failures ||= Hash.new
57
+ @@failures[klass_name] ||= 0
58
+ @@failures[klass_name] += 1
59
+ end
60
+
61
+ def self.failures
62
+ @@failures
63
+ rescue
64
+ @@failures ||= Hash.new
65
+ end
66
+
67
+ def self.total_records
68
+ @@total_records
69
+ rescue
70
+ @@total_records ||= Hash.new
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,67 @@
1
+ module Transplant
2
+ class Stats
3
+
4
+ def initialize(transplanter)
5
+ @transplanter = transplanter
6
+ end
7
+
8
+ def output_all_info(opts = {})
9
+ total_import_time(opts[:measurement]) if opts[:measurement].present?
10
+ total_import_records
11
+ failures
12
+ end
13
+
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)
19
+ end
20
+ end
21
+
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}"
31
+ end
32
+ end
33
+ end
34
+
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}"
44
+ end
45
+ end
46
+ end
47
+
48
+ def total_import_records
49
+ puts "\nTotal number of records imported into #{Manager.app_name}: #{@transplanter.total_records}"
50
+ end
51
+
52
+ def total_import_time(measurement)
53
+ puts "Total time taken to import everything into #{Manager.app_name}"
54
+ puts measurement
55
+ end
56
+
57
+ def failures
58
+ if @transplanter.failures.count <= 0
59
+ puts "\nNo failed record imports!!!! Time to par-tay!!!!\n"
60
+ else
61
+ failures = Hash[@transplanter.failures.map { |key, value| [key.tableize.humanize, value] }]
62
+ Stats.output("\nTotal number failed imports to #{Manager.app_name}:", failures)
63
+ end
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ module Transplant
2
+ VERSION = "0.0.1"
3
+ end
data/lib/transplant.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "transplant/version"
2
+ require "transplant/stats"
3
+ require "transplant/manager"
4
+
5
+ module Transplant
6
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/transplant/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Matt Bridges"]
6
+ gem.email = ["mbridges.91@gmail.com"]
7
+ gem.description = %q{Transplant your data from your old database into your new Rails application}
8
+ gem.summary = %q{Transplant your data from your old database into your new Rails application}
9
+ gem.homepage = "http://github.com/mattdbridges/transplant"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "transplant"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Transplant::VERSION
17
+
18
+ gem.add_dependency "activesupport", "~> 3.2.2"
19
+ gem.add_dependency "mysql2", "~> 0.3.11"
20
+ gem.add_development_dependency "rspec", "~> 2.9.0"
21
+ gem.add_development_dependency "rake", "~> 0.9.2.2"
22
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: transplant
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matt Bridges
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.2
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.2
30
+ - !ruby/object:Gem::Dependency
31
+ name: mysql2
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.3.11
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.3.11
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 2.9.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.9.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 0.9.2.2
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.9.2.2
78
+ description: Transplant your data from your old database into your new Rails application
79
+ email:
80
+ - mbridges.91@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rvmrc
87
+ - Gemfile
88
+ - LICENSE
89
+ - README.md
90
+ - Rakefile
91
+ - lib/transplant.rb
92
+ - lib/transplant/manager.rb
93
+ - lib/transplant/stats.rb
94
+ - lib/transplant/version.rb
95
+ - transplant.gemspec
96
+ homepage: http://github.com/mattdbridges/transplant
97
+ licenses: []
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ segments:
109
+ - 0
110
+ hash: -4175224259072295896
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ segments:
118
+ - 0
119
+ hash: -4175224259072295896
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 1.8.21
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: Transplant your data from your old database into your new Rails application
126
+ test_files: []