ar_dbcopy 0.1.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/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +22 -0
- data/README.rdoc +39 -0
- data/Rakefile +60 -0
- data/VERSION +1 -0
- data/ar_dbcopy.gemspec +60 -0
- data/bin/ar_dbcopy +37 -0
- data/database.yml.example +13 -0
- data/lib/ar_dbcopy.rb +90 -0
- data/test/ar_dbcopy_test.rb +7 -0
- data/test/test_helper.rb +10 -0
- metadata +87 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2009 Michael Siebert <siebertm85@googlemail.com>
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
22
|
+
|
data/README.rdoc
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
= AR_DBCopy
|
2
|
+
|
3
|
+
Simple script to copy data from one database to another.
|
4
|
+
This script can be used to migrate from one Database system
|
5
|
+
to another one, e.q. when the SQL dumps are not compatible.
|
6
|
+
Database agnosticism is achieved by using ActiveRecord.
|
7
|
+
|
8
|
+
The script just defines 2 connections, looks at the tables,
|
9
|
+
creates ActiveRecord model classes on-the-fly and copies over
|
10
|
+
the data.
|
11
|
+
|
12
|
+
Configure the databases using the database.yml.example as a
|
13
|
+
template (the format is the same as Rails's database.yml)
|
14
|
+
|
15
|
+
Then, run the script. The script only copies data, not structure,
|
16
|
+
so make sure that you created the schema and all tables in
|
17
|
+
the destination database already (use rake db:schema:load)
|
18
|
+
|
19
|
+
== Example
|
20
|
+
|
21
|
+
ARDBCopy.new("database.yml", :copy_schema => true).run!
|
22
|
+
|
23
|
+
This software was written in less than an hour. So it may
|
24
|
+
contain bugs!
|
25
|
+
|
26
|
+
== Note on Patches/Pull Requests
|
27
|
+
|
28
|
+
* Fork the project.
|
29
|
+
* Make your feature addition or bug fix.
|
30
|
+
* Add tests for it. This is important so I don't break it in a
|
31
|
+
future version unintentionally.
|
32
|
+
* Commit, do not mess with rakefile, version, or history.
|
33
|
+
(if you want to have your own version, that is fine but
|
34
|
+
bump version in a commit by itself I can ignore when I pull)
|
35
|
+
* Send me a pull request. Bonus points for topic branches.
|
36
|
+
|
37
|
+
== Copyright
|
38
|
+
|
39
|
+
Author: Copyright (c) 2009 Michael Siebert <siebertm85@googlemail.com>
|
data/Rakefile
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "ar_dbcopy"
|
8
|
+
gem.summary = %Q{copy data from one dbms to another via active_record}
|
9
|
+
gem.description = %Q{copy data from one dbms to another via active_record}
|
10
|
+
gem.email = "siebertm85@googlemail.com"
|
11
|
+
gem.homepage = "http://github.com/siebertm/ar_dbcopy"
|
12
|
+
gem.authors = ["Michael Siebert"]
|
13
|
+
gem.add_development_dependency "thoughtbot-shoulda"
|
14
|
+
gem.add_dependency "activerecord"
|
15
|
+
gem.require_paths = ['lib']
|
16
|
+
gem.executables = ["ar_dbcopy"]
|
17
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
18
|
+
end
|
19
|
+
Jeweler::GemcutterTasks.new
|
20
|
+
rescue LoadError
|
21
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
Rake::TestTask.new(:test) do |test|
|
26
|
+
test.libs << 'lib' << 'test'
|
27
|
+
test.pattern = 'test/**/*_test.rb'
|
28
|
+
test.verbose = true
|
29
|
+
end
|
30
|
+
|
31
|
+
begin
|
32
|
+
require 'rcov/rcovtask'
|
33
|
+
Rcov::RcovTask.new do |test|
|
34
|
+
test.libs << 'test'
|
35
|
+
test.pattern = 'test/**/*_test.rb'
|
36
|
+
test.verbose = true
|
37
|
+
end
|
38
|
+
rescue LoadError
|
39
|
+
task :rcov do
|
40
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
task :test => :check_dependencies
|
45
|
+
|
46
|
+
task :default => :test
|
47
|
+
|
48
|
+
require 'rake/rdoctask'
|
49
|
+
Rake::RDocTask.new do |rdoc|
|
50
|
+
if File.exist?('VERSION')
|
51
|
+
version = File.read('VERSION')
|
52
|
+
else
|
53
|
+
version = ""
|
54
|
+
end
|
55
|
+
|
56
|
+
rdoc.rdoc_dir = 'rdoc'
|
57
|
+
rdoc.title = "ar_dbcopy #{version}"
|
58
|
+
rdoc.rdoc_files.include('README*')
|
59
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
60
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/ar_dbcopy.gemspec
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{ar_dbcopy}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Michael Siebert"]
|
12
|
+
s.date = %q{2009-10-13}
|
13
|
+
s.default_executable = %q{ar_dbcopy}
|
14
|
+
s.description = %q{copy data from one dbms to another via active_record}
|
15
|
+
s.email = %q{siebertm85@googlemail.com}
|
16
|
+
s.executables = ["ar_dbcopy"]
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE",
|
19
|
+
"README.rdoc"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
".document",
|
23
|
+
".gitignore",
|
24
|
+
"LICENSE",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"ar_dbcopy.gemspec",
|
29
|
+
"bin/ar_dbcopy",
|
30
|
+
"database.yml.example",
|
31
|
+
"lib/ar_dbcopy.rb",
|
32
|
+
"test/ar_dbcopy_test.rb",
|
33
|
+
"test/test_helper.rb"
|
34
|
+
]
|
35
|
+
s.homepage = %q{http://github.com/siebertm/ar_dbcopy}
|
36
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = %q{1.3.5}
|
39
|
+
s.summary = %q{copy data from one dbms to another via active_record}
|
40
|
+
s.test_files = [
|
41
|
+
"test/ar_dbcopy_test.rb",
|
42
|
+
"test/test_helper.rb"
|
43
|
+
]
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
|
+
s.specification_version = 3
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
50
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
51
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 0"])
|
52
|
+
else
|
53
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
54
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
58
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
59
|
+
end
|
60
|
+
end
|
data/bin/ar_dbcopy
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
require 'ar_dbcopy'
|
5
|
+
|
6
|
+
class ARDBCopy::Application
|
7
|
+
def self.run!
|
8
|
+
@options = {}
|
9
|
+
|
10
|
+
@opts = OptionParser.new do |opts|
|
11
|
+
opts.banner = "Usage: #{$0} [options] database.yml"
|
12
|
+
|
13
|
+
opts.separator "copy data between two databases defined in the given database_yaml"
|
14
|
+
opts.separator "uses the databases 'source' and 'target'"
|
15
|
+
|
16
|
+
opts.on "-c", "--with-schema", "also copy the schema to the target database" do |arg|
|
17
|
+
@options[:copy_schema] = arg
|
18
|
+
end
|
19
|
+
|
20
|
+
opts.on( '-h', '--help', 'Display this screen' ) do
|
21
|
+
puts opts
|
22
|
+
exit
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
@opts.parse!
|
27
|
+
|
28
|
+
unless ARGV[0]
|
29
|
+
puts @opts
|
30
|
+
exit 1
|
31
|
+
end
|
32
|
+
|
33
|
+
ARDBCopy.new(ARGV[0], @options).run!
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
ARDBCopy::Application.run!
|
data/lib/ar_dbcopy.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# Simple script to copy data from one database to another.
|
4
|
+
# This script can be used to migrate from one Database system
|
5
|
+
# to another one, e.q. when the SQL dumps are not compatible.
|
6
|
+
# Database agnosticism is achieved by using ActiveRecord.
|
7
|
+
#
|
8
|
+
# The script just defines 2 connections, looks at the tables,
|
9
|
+
# creates ActiveRecord model classes on-the-fly and copies over
|
10
|
+
# the data.
|
11
|
+
#
|
12
|
+
# Configure the databases using the database.yml.example as a
|
13
|
+
# template (the format is the same as Rails's database.yml)
|
14
|
+
#
|
15
|
+
# Then, run the script. The script only copies data, not structure,
|
16
|
+
# so make sure that you created the schema and all tables in
|
17
|
+
# the destination database already (use rake db:schema:load)
|
18
|
+
#
|
19
|
+
require 'active_record'
|
20
|
+
|
21
|
+
class ARDBCopy
|
22
|
+
class SourceDB < ActiveRecord::Base; end
|
23
|
+
class TargetDB < ActiveRecord::Base; end
|
24
|
+
|
25
|
+
def initialize(config_file, opts={})
|
26
|
+
@copy_schema = opts[:copy_schema]
|
27
|
+
|
28
|
+
config = YAML.load_file(config_file)
|
29
|
+
|
30
|
+
ActiveRecord::Base.logger ||= Logger.new(nil)
|
31
|
+
|
32
|
+
SourceDB.establish_connection(config["source"])
|
33
|
+
ActiveRecord::Base.establish_connection(config["target"])
|
34
|
+
|
35
|
+
@tables = SourceDB.connection.tables.reject { |m| m == "schema_migrations" }
|
36
|
+
end
|
37
|
+
|
38
|
+
def run!
|
39
|
+
copy_schema if @copy_schema
|
40
|
+
copy_data
|
41
|
+
end
|
42
|
+
|
43
|
+
def copy_schema
|
44
|
+
io = StringIO.new
|
45
|
+
|
46
|
+
ActiveRecord::SchemaDumper.dump(SourceDB.connection, io) # dump the schema from the source database
|
47
|
+
io.rewind
|
48
|
+
|
49
|
+
eval(io.read)
|
50
|
+
end
|
51
|
+
|
52
|
+
def copy_data
|
53
|
+
@tables.each do |table_name|
|
54
|
+
source_model = Class.new(SourceDB) do
|
55
|
+
set_inheritance_column(:not_sti)
|
56
|
+
set_table_name table_name
|
57
|
+
end
|
58
|
+
|
59
|
+
dest_model = Class.new(TargetDB) do
|
60
|
+
set_inheritance_column(:not_sti)
|
61
|
+
set_table_name table_name
|
62
|
+
end
|
63
|
+
|
64
|
+
dest_model.delete_all
|
65
|
+
|
66
|
+
puts "Copying #{table_name} (#{source_model.count} lines)..."
|
67
|
+
|
68
|
+
i = 0
|
69
|
+
source_model.find_in_batches(:batch_size => 10_000) do |src_batch|
|
70
|
+
dest_model.transaction do
|
71
|
+
src_batch.each do |src_inst|
|
72
|
+
dst_inst = dest_model.new(src_inst.attributes)
|
73
|
+
dst_inst.id = src_inst.id
|
74
|
+
dst_inst.save!
|
75
|
+
i += 1
|
76
|
+
end
|
77
|
+
|
78
|
+
puts i
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
if __FILE__ == $0
|
86
|
+
|
87
|
+
copy = ARDBcopy.new(ARGV[0])
|
88
|
+
copy.copy_schema
|
89
|
+
copy.copy_data
|
90
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ar_dbcopy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Siebert
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-13 00:00:00 +02:00
|
13
|
+
default_executable: ar_dbcopy
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: thoughtbot-shoulda
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activerecord
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description: copy data from one dbms to another via active_record
|
36
|
+
email: siebertm85@googlemail.com
|
37
|
+
executables:
|
38
|
+
- ar_dbcopy
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- ar_dbcopy.gemspec
|
52
|
+
- bin/ar_dbcopy
|
53
|
+
- database.yml.example
|
54
|
+
- lib/ar_dbcopy.rb
|
55
|
+
- test/ar_dbcopy_test.rb
|
56
|
+
- test/test_helper.rb
|
57
|
+
has_rdoc: true
|
58
|
+
homepage: http://github.com/siebertm/ar_dbcopy
|
59
|
+
licenses: []
|
60
|
+
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options:
|
63
|
+
- --charset=UTF-8
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
version:
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: "0"
|
77
|
+
version:
|
78
|
+
requirements: []
|
79
|
+
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 1.3.5
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: copy data from one dbms to another via active_record
|
85
|
+
test_files:
|
86
|
+
- test/ar_dbcopy_test.rb
|
87
|
+
- test/test_helper.rb
|