seed-snapshot 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e364fa35f42da8d2117d456964ec7981b6733bec
4
+ data.tar.gz: 6f675c704dcafe46cfac09faa3f850a075c39519
5
+ SHA512:
6
+ metadata.gz: e292f7ce0c52cdfbe323239f955b589fd21faa20bd9eeb495bddd88322634de1864d4b68adb2aed1242a436eaa7ea8dc5b02dda53301ad39061ea003fd435741
7
+ data.tar.gz: 35634c999b8d4bd412bb5bb08af9c9a6c98b823b48d310add17e25b848b7671b5b6fe650180d45a7707acb7ed8ba84a9a048c0ecefb92c0873abe7826bf1cca5
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ .DS_Store
12
+ /vendor
13
+
14
+ debug.log
15
+ test/config.yml
data/.travis.yml ADDED
@@ -0,0 +1,17 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.4
4
+ - 2.4.1
5
+ - ruby-head
6
+ env:
7
+ - "AR_VERSION=4.2.8"
8
+ - "AR_VERSION=4.2.9"
9
+ - "AR_VERSION=5.0.4"
10
+ - "AR_VERSION=5.1.3"
11
+ sudo: false
12
+ cache: bundler
13
+ branches:
14
+ only:
15
+ - master
16
+ before_install:
17
+ - mysql -e "create database activerecord_unittest;"
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "activerecord", "~> #{ENV['AR_VERSION']}" if ENV['AR_VERSION']
4
+
5
+ # Specify your gem's dependencies in seed-snapshot.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 yo_waka
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.
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # SeedSnapshot
2
+
3
+ [![Build Status](https://travis-ci.org/waka/seed-snapshot.png?branch=master)](https://travis-ci.org/waka/seed-snapshot)
4
+
5
+ This library manage of dump/restore data by database.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ group :test do
13
+ gem 'seed-snapshot'
14
+ end
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install seed-snapshot
24
+
25
+ ## Usage
26
+
27
+ Configuration in RSpec with DatabaseCleaner (optional).
28
+
29
+ ```
30
+ require 'seed-snapshot'
31
+
32
+ RSpec.configure do |config|
33
+ tables = [User, Item]
34
+
35
+ config.before(:suite) do
36
+ DatabaseCleaner.strategy = :transaction
37
+
38
+ if SeedSnapshot.exists?
39
+ SeedSnapshot.restore(tables)
40
+ else
41
+ # load seed data normally
42
+ end
43
+ end
44
+
45
+ config.before(:each) do
46
+ DatabaseCleaner.start
47
+ end
48
+
49
+ config.after(:each) do
50
+ DatabaseCleaner.clean
51
+ end
52
+
53
+ config.after(:suite) do
54
+ unless SeedSnapshot.exists?
55
+ SeedSnapshot.dump(tables)
56
+ end
57
+ end
58
+ end
59
+ ```
60
+
61
+ ## Development
62
+
63
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
64
+
65
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
66
+
67
+ ## Contributing
68
+
69
+ Bug reports and pull requests are welcome on GitHub at https://github.com/waka/seed-snapshot.
70
+
71
+
72
+ ## License
73
+
74
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
75
+
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "lib" << "test"
6
+ t.test_files = Dir.glob("test/**/*_test.rb")
7
+ end
8
+
9
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "seed-snapshot"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,34 @@
1
+ require 'seed/configuration'
2
+ require 'seed/mysql'
3
+ require 'seed/snapshot'
4
+
5
+ module SeedSnapshot
6
+ # @param Array[ActiveRecord::Base] classes
7
+ # @param Array[ActiveRecord::Base] ignore_classes
8
+ # @param Boolean force
9
+ def self.dump(classes: [], ignore_classes: [], force: false)
10
+ snapshot = Seed::Snapshot.new(self.configuration)
11
+ snapshot.dump(classes, ignore_classes, force)
12
+ end
13
+
14
+ def self.restore
15
+ snapshot = Seed::Snapshot.new(self.configuration)
16
+ snapshot.restore
17
+ end
18
+
19
+ # @return Boolean
20
+ def self.exists?
21
+ snapshot = Seed::Snapshot.new(self.configuration)
22
+ snapshot.exist_path?
23
+ end
24
+
25
+ def self.clean
26
+ snapshot = Seed::Snapshot.new(self.configuration)
27
+ snapshot.clean
28
+ end
29
+
30
+ # @return Seed::Configuration
31
+ def self.configuration
32
+ Seed::Configuration.new
33
+ end
34
+ end
@@ -0,0 +1,36 @@
1
+ require 'active_record'
2
+
3
+ module Seed
4
+ class Configuration
5
+ def initialize
6
+ # error if adapter is not mysql
7
+ raise 'seed-snapshot support only MySQL' unless adapter_name == 'Mysql2'
8
+ end
9
+
10
+ def adapter_name
11
+ @adapter_name ||= ActiveRecord::Base.connection.adapter_name
12
+ end
13
+
14
+ def schema_version
15
+ @schema_version ||= ActiveRecord::Migrator.current_version
16
+ end
17
+
18
+ def database_options
19
+ @options ||= ActiveRecord::Base.connection.raw_connection.query_options
20
+ end
21
+
22
+ # ${Rails.root}/tmp/dump
23
+ def base_path
24
+ Pathname.new(Dir.pwd).join('tmp').join('dump')
25
+ end
26
+
27
+ # ${Rails.root}/tmp/dump/123456789.sql'
28
+ def current_version_path
29
+ base_path.join(schema_version.to_s + '.sql')
30
+ end
31
+
32
+ def make_tmp_dir
33
+ FileUtils.mkdir_p(base_path) unless File.exist?(base_path)
34
+ end
35
+ end
36
+ end
data/lib/seed/mysql.rb ADDED
@@ -0,0 +1,23 @@
1
+ module Seed
2
+ class Mysql
3
+ def self.dump(file_path, username:, password:, host:, port:, database:, tables:, ignore_tables:)
4
+ host = 'localhost' unless host
5
+ cmd = "MYSQL_PWD=#{password} mysqldump -u #{username} -h #{host} #{database} -t #{allow_tables_option(tables)} #{ignore_tables_option(ignore_tables)} > #{file_path}"
6
+ system cmd
7
+ end
8
+
9
+ def self.restore(file_path, username:, password:, host:, port:, database:)
10
+ host = 'localhost' unless host
11
+ cmd = "MYSQL_PWD=#{password} mysql -u #{username} -h #{host} #{database} < #{file_path}"
12
+ system cmd
13
+ end
14
+
15
+ def self.allow_tables_option(tables)
16
+ tables.join(' ')
17
+ end
18
+
19
+ def self.ignore_tables_option(tables)
20
+ tables.map { |table| "--ignore-table=#{table}" }.join(' ')
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,75 @@
1
+ module Seed
2
+ class Snapshot
3
+ def initialize(configuration)
4
+ @configuration = configuration
5
+ end
6
+
7
+ def dump(classes = [], ignore_classes = [], force_dump = false)
8
+ @configuration.make_tmp_dir
9
+
10
+ if exist_path? && !force_dump
11
+ puts 'Dump file already exists for current schema.'
12
+ return
13
+ end
14
+
15
+ Mysql.dump(
16
+ @configuration.current_version_path,
17
+ options.merge({
18
+ tables: tables(classes),
19
+ ignore_tables: ignore_tables(ignore_classes)
20
+ })
21
+ )
22
+ end
23
+
24
+ def restore
25
+ @configuration.make_tmp_dir
26
+
27
+ unless exist_path?
28
+ puts "Dump file does not exist for current schema."
29
+ return
30
+ end
31
+
32
+ Mysql.restore(
33
+ @configuration.current_version_path,
34
+ options
35
+ )
36
+ end
37
+
38
+ def clean
39
+ unless exist_path?
40
+ puts "Dump file does not exist for current schema."
41
+ return
42
+ end
43
+
44
+ version_path = @configuration.current_version_path
45
+ File.delete(version_path)
46
+ end
47
+
48
+ def exist_path?
49
+ version_path = @configuration.current_version_path
50
+ File.exist?(version_path)
51
+ end
52
+
53
+ def options
54
+ db = @configuration.database_options
55
+ {
56
+ username: db[:username],
57
+ password: db[:password],
58
+ host: db[:host],
59
+ port: db[:port],
60
+ database: db[:database]
61
+ }
62
+ end
63
+
64
+ def tables(classes)
65
+ classes.map do |cls|
66
+ cls.table_name
67
+ end
68
+ end
69
+
70
+ def ignore_tables(classes)
71
+ db = @configuration.database_options[:database]
72
+ tables(classes).push("#{db}.schema_migrations")
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,3 @@
1
+ module SeedSnapshot
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,3 @@
1
+ module SeedSnapshot
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'seed_snapshot/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'seed-snapshot'
8
+ spec.version = SeedSnapshot::VERSION
9
+ spec.authors = ['yo_waka']
10
+ spec.email = ['y.wakahara@gmail.com']
11
+
12
+ spec.summary = %q{Easy dump/restore tool for ActiveRecord.}
13
+ spec.description = %q{Easy dump/restore tool for ActiveRecord.}
14
+ spec.homepage = 'https://github.com/waka/seed-snapshot'
15
+ spec.license = 'MIT'
16
+
17
+ spec.require_paths = ['lib']
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+
20
+ spec.add_development_dependency 'bundler'
21
+ spec.add_development_dependency 'rake'
22
+ spec.add_development_dependency 'pry'
23
+ spec.add_development_dependency 'minitest'
24
+ spec.add_development_dependency 'erubis'
25
+ spec.add_development_dependency 'mysql2', '>= 0.3', '< 0.4'
26
+ spec.add_runtime_dependency 'activerecord', '>= 4.2'
27
+ end
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seed-snapshot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - yo_waka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: erubis
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: mysql2
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0.3'
90
+ - - "<"
91
+ - !ruby/object:Gem::Version
92
+ version: '0.4'
93
+ type: :development
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0.3'
100
+ - - "<"
101
+ - !ruby/object:Gem::Version
102
+ version: '0.4'
103
+ - !ruby/object:Gem::Dependency
104
+ name: activerecord
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '4.2'
110
+ type: :runtime
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '4.2'
117
+ description: Easy dump/restore tool for ActiveRecord.
118
+ email:
119
+ - y.wakahara@gmail.com
120
+ executables: []
121
+ extensions: []
122
+ extra_rdoc_files: []
123
+ files:
124
+ - ".gitignore"
125
+ - ".travis.yml"
126
+ - Gemfile
127
+ - LICENSE.txt
128
+ - README.md
129
+ - Rakefile
130
+ - bin/console
131
+ - bin/setup
132
+ - lib/seed-snapshot.rb
133
+ - lib/seed/configuration.rb
134
+ - lib/seed/mysql.rb
135
+ - lib/seed/snapshot.rb
136
+ - lib/seed/version.rb
137
+ - lib/seed_snapshot/version.rb
138
+ - seed-snapshot.gemspec
139
+ homepage: https://github.com/waka/seed-snapshot
140
+ licenses:
141
+ - MIT
142
+ metadata: {}
143
+ post_install_message:
144
+ rdoc_options: []
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 2.5.2
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: Easy dump/restore tool for ActiveRecord.
163
+ test_files: []