db_sync 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +22 -0
- data/README.md +36 -0
- data/Rakefile +1 -0
- data/config.ru +7 -0
- data/db_sync.gemspec +23 -0
- data/lib/db_sync/railtie.rb +8 -0
- data/lib/db_sync/version.rb +3 -0
- data/lib/db_sync.rb +47 -0
- data/lib/tasks/db_sync_tasks.rake +14 -0
- data/spec/internal/config/database.yml +3 -0
- data/spec/internal/config/routes.rb +3 -0
- data/spec/internal/db/combustion_test.sqlite +0 -0
- data/spec/internal/db/data/articles.yml +5 -0
- data/spec/internal/db/schema.rb +6 -0
- data/spec/internal/log/.gitignore +1 -0
- data/spec/internal/public/favicon.ico +0 -0
- data/spec/spec_helper.rb +31 -0
- data/spec/sync_spec.rb +27 -0
- data/spec/test_spec.rb +7 -0
- metadata +101 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Scott Schulthess
|
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,36 @@
|
|
1
|
+
# DbSync
|
2
|
+
This gem is for exporting and importing data from different databases.
|
3
|
+
|
4
|
+
Recommended use is for syncing certain tables from production into development for testing.
|
5
|
+
|
6
|
+
It exports specified tables into yaml stored in db/data.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'db_sync'
|
13
|
+
|
14
|
+
|
15
|
+
And in config/initializers/db_sync.rb
|
16
|
+
|
17
|
+
DbSync.configure do |config|
|
18
|
+
config.sync_tables = ["TABLE_NAME"]
|
19
|
+
end
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Run this to dump the tables you specified in the initializer to db/data
|
24
|
+
bundle exec rake db_sync:dump_data
|
25
|
+
|
26
|
+
Run this to load the tables back in.
|
27
|
+
WARNING: this overwrites the contents of this existing table.
|
28
|
+
bundle exec rake db_sync:load_data
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
|
32
|
+
1. Fork it
|
33
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
34
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
35
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
36
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/config.ru
ADDED
data/db_sync.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'db_sync/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "db_sync"
|
8
|
+
spec.version = DbSync::VERSION
|
9
|
+
spec.authors = ["Scott Schulthess"]
|
10
|
+
spec.email = ["scottschulthess@gmail.com"]
|
11
|
+
spec.description = %q{Rails gem for syncing database tables}
|
12
|
+
spec.summary = %q{This Rails gem provides rake tasks for exporting certain tables from say production and loading them into development}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
data/lib/db_sync.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require "db_sync/version"
|
2
|
+
require 'db_sync/railtie' if defined?(Rails)
|
3
|
+
module DbSync
|
4
|
+
|
5
|
+
class Configuration
|
6
|
+
attr_accessor :sync_tables
|
7
|
+
|
8
|
+
def initializers
|
9
|
+
sync_tables = []
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class << self
|
14
|
+
attr_accessor :configuration
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.configure
|
18
|
+
self.configuration ||= Configuration.new
|
19
|
+
yield(configuration) if block_given?
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.dump_data
|
23
|
+
DbSync.configuration.sync_tables.each do |table_name|
|
24
|
+
location = FileUtils.mkdir_p "#{Rails.root}/db/data"
|
25
|
+
i = "000"
|
26
|
+
sql = "SELECT * FROM %s"
|
27
|
+
File.open("#{location.first}/#{table_name}.yml", 'w') do |file|
|
28
|
+
data = ActiveRecord::Base.connection.select_all(sql % table_name.upcase)
|
29
|
+
file.write data.inject({}) { |hash, record|
|
30
|
+
hash["#{table_name}_#{i.succ!}"] = record
|
31
|
+
hash
|
32
|
+
}.to_yaml
|
33
|
+
puts "wrote #{file.path} /"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.load_data
|
39
|
+
require 'active_record/fixtures'
|
40
|
+
base_dir = "#{Rails.root}/db/data"
|
41
|
+
fixtures_dir = File.join [base_dir, ENV['FIXTURES_DIR']].compact
|
42
|
+
fixture_files = (ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir["#{fixtures_dir}/**/*.{yml,csv}"].map {|f| f[(fixtures_dir.size + 1)..-5] })
|
43
|
+
fixture_files.each do |fixture_file|
|
44
|
+
ActiveRecord::Fixtures.create_fixtures(fixtures_dir, fixture_file)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
namespace :db_sync do
|
2
|
+
desc "Dump specific tables from configuration into db/data"
|
3
|
+
task :dump_data => :environment do
|
4
|
+
DbSync.dump_data
|
5
|
+
puts 'Dump succeeded'
|
6
|
+
end
|
7
|
+
|
8
|
+
desc "Load data from db/data, overwriting the existing tables"
|
9
|
+
task :load_data => :environment do
|
10
|
+
DbSync.load_data
|
11
|
+
puts "Loaded data"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
Binary file
|
@@ -0,0 +1 @@
|
|
1
|
+
*.log
|
File without changes
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'rubygems'
|
20
|
+
require 'bundler/setup'
|
21
|
+
|
22
|
+
require 'combustion'
|
23
|
+
|
24
|
+
Combustion.initialize! :active_record
|
25
|
+
|
26
|
+
require 'rspec/rails'
|
27
|
+
|
28
|
+
RSpec.configure do |config|
|
29
|
+
config.use_transactional_fixtures = true
|
30
|
+
end
|
31
|
+
|
data/spec/sync_spec.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Article < ActiveRecord::Base
|
4
|
+
end
|
5
|
+
|
6
|
+
|
7
|
+
describe "db_sync" do
|
8
|
+
before do
|
9
|
+
DbSync.configure do |config|
|
10
|
+
config.sync_tables = [:articles]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
let!(:article) do
|
15
|
+
Article.create(title: "test")
|
16
|
+
end
|
17
|
+
|
18
|
+
it "dumps" do
|
19
|
+
DbSync.dump_data
|
20
|
+
end
|
21
|
+
|
22
|
+
it "loads" do
|
23
|
+
DbSync.load_data
|
24
|
+
Article.find_by_title(article.title).should_not be_nil
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/spec/test_spec.rb
ADDED
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: db_sync
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Scott Schulthess
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-06-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: &70230661976540 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70230661976540
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rake
|
27
|
+
requirement: &70230661976120 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70230661976120
|
36
|
+
description: Rails gem for syncing database tables
|
37
|
+
email:
|
38
|
+
- scottschulthess@gmail.com
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- .gitignore
|
44
|
+
- .rspec
|
45
|
+
- Gemfile
|
46
|
+
- LICENSE.txt
|
47
|
+
- README.md
|
48
|
+
- Rakefile
|
49
|
+
- config.ru
|
50
|
+
- db_sync.gemspec
|
51
|
+
- lib/db_sync.rb
|
52
|
+
- lib/db_sync/railtie.rb
|
53
|
+
- lib/db_sync/version.rb
|
54
|
+
- lib/tasks/db_sync_tasks.rake
|
55
|
+
- spec/internal/config/database.yml
|
56
|
+
- spec/internal/config/routes.rb
|
57
|
+
- spec/internal/db/combustion_test.sqlite
|
58
|
+
- spec/internal/db/data/articles.yml
|
59
|
+
- spec/internal/db/schema.rb
|
60
|
+
- spec/internal/log/.gitignore
|
61
|
+
- spec/internal/public/favicon.ico
|
62
|
+
- spec/spec_helper.rb
|
63
|
+
- spec/sync_spec.rb
|
64
|
+
- spec/test_spec.rb
|
65
|
+
homepage: ''
|
66
|
+
licenses:
|
67
|
+
- MIT
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.8.10
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: This Rails gem provides rake tasks for exporting certain tables from say
|
90
|
+
production and loading them into development
|
91
|
+
test_files:
|
92
|
+
- spec/internal/config/database.yml
|
93
|
+
- spec/internal/config/routes.rb
|
94
|
+
- spec/internal/db/combustion_test.sqlite
|
95
|
+
- spec/internal/db/data/articles.yml
|
96
|
+
- spec/internal/db/schema.rb
|
97
|
+
- spec/internal/log/.gitignore
|
98
|
+
- spec/internal/public/favicon.ico
|
99
|
+
- spec/spec_helper.rb
|
100
|
+
- spec/sync_spec.rb
|
101
|
+
- spec/test_spec.rb
|