database_fork 0.0.1
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 +7 -0
- data/.gitignore +24 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/database_fork.gemspec +22 -0
- data/lib/database_fork.rb +173 -0
- metadata +79 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c2481e3d19c6e65d915b9d101e2f278f6142ca4d
|
4
|
+
data.tar.gz: bc6f79626eeb8d59ddff470d97bcdb77e3e788bd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9cd6ba6d808ec148e4e8ad151c590cb4d2b283c85e35df54d13e3184c3c646eaf512d005cec453fd0a79fa2679157c22355428759080fbc0aefdf9842312fdce
|
7
|
+
data.tar.gz: f1115ca6776bc93c8fb322e318cd8daa20a72612e6e4a65f6de85c64cfaab95e3d9199d96172bfa451e9eb018fbb8c2d079f6ee4829572b0f4af6e9fbc373df1
|
data/.gitignore
ADDED
@@ -0,0 +1,24 @@
|
|
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
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
23
|
+
.idea
|
24
|
+
pkg
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 the-architect
|
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,29 @@
|
|
1
|
+
# DatabaseFork
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'database_fork'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install database_fork
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( https://github.com/[my-github-username]/database_fork/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "database_fork"
|
7
|
+
spec.version = '0.0.1'
|
8
|
+
spec.authors = ["the-architect"]
|
9
|
+
spec.email = ["marcel.scherf@epicteams.com"]
|
10
|
+
spec.summary = %q{Fork your database}
|
11
|
+
spec.description = %q{Fork your database}
|
12
|
+
spec.homepage = "http://github.com/"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
end
|
@@ -0,0 +1,173 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'erb'
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
class DatabaseFork
|
6
|
+
|
7
|
+
class << self
|
8
|
+
# call this at the end of your application.rb file
|
9
|
+
def setup_env(env, root_dir)
|
10
|
+
db_fork_var = "DATABASE_FORK_#{env}".upcase
|
11
|
+
db_fork_file = File.join(root_dir, 'tmp', db_fork_var)
|
12
|
+
if File.exists?(db_fork_file)
|
13
|
+
ENV[db_fork_var] = open(db_fork_file).read.strip
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# use DatabaseFork.new.run in your post-checkout hook
|
19
|
+
def initialize(config_file = '.db_forks.yml', logger = Logger.new(STDOUT))
|
20
|
+
@config_file = config_file
|
21
|
+
@logger = logger
|
22
|
+
end
|
23
|
+
|
24
|
+
def run
|
25
|
+
if config['ignore'].include?(current_branch)
|
26
|
+
log_info 'This branch name is ignored in .db_fork.yml config. Skipping along.'
|
27
|
+
reset_env
|
28
|
+
elsif Regexp.new(config['check_branch_name_regex']).match(current_branch)
|
29
|
+
log_info 'branch qualified for database forking'
|
30
|
+
|
31
|
+
if fork_exists?
|
32
|
+
log_info 'database fork already exists'
|
33
|
+
export_env
|
34
|
+
else
|
35
|
+
log_info "Create a database fork '#{fork_db_name}'? [y|n|enter=ignore]"
|
36
|
+
|
37
|
+
# trick to read user input:
|
38
|
+
decision = IO.new(IO.sysopen('/dev/tty'), 'r').gets.chomp
|
39
|
+
|
40
|
+
case decision
|
41
|
+
when 'y'
|
42
|
+
create_database_fork!
|
43
|
+
export_env
|
44
|
+
when 'n'
|
45
|
+
# do nothing
|
46
|
+
reset_env
|
47
|
+
else
|
48
|
+
config['ignore'] << current_branch
|
49
|
+
reset_env
|
50
|
+
end
|
51
|
+
end
|
52
|
+
else
|
53
|
+
log_info 'not a feature branch. not creating database fork.'
|
54
|
+
reset_env
|
55
|
+
end
|
56
|
+
|
57
|
+
save_config
|
58
|
+
end
|
59
|
+
|
60
|
+
def create_database_fork!
|
61
|
+
config['environments'].each do |env|
|
62
|
+
log_info "creating database fork '#{fork_db_name(env)}' from #{source_db(env)}"
|
63
|
+
|
64
|
+
create_dump(env)
|
65
|
+
create_database(env)
|
66
|
+
import_dump(env)
|
67
|
+
delete_dump_file(env)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def create_dump(env = 'development')
|
72
|
+
run_command %Q{mysqldump #{connection_params(env)} --routines --triggers -C #{source_db(env)} > #{dump_file_path(env)}}, "dumping #{source_db(env)}"
|
73
|
+
end
|
74
|
+
|
75
|
+
def create_database(env = 'development')
|
76
|
+
run_command %Q{mysql #{connection_params(env)} -e "CREATE DATABASE IF NOT EXISTS #{fork_db_name(env)} CHARACTER SET '#{character_set}' COLLATE '#{collation}';"}, "create database #{fork_db_name(env)}"
|
77
|
+
end
|
78
|
+
|
79
|
+
def import_dump(env = 'development')
|
80
|
+
run_command %Q{mysql #{connection_params(env)} -C -A -D#{fork_db_name(env)} < #{dump_file_path(env)}}, 'importing dump'
|
81
|
+
end
|
82
|
+
|
83
|
+
def delete_dump_file(env = 'development')
|
84
|
+
run_command "rm #{dump_file_path(env)}", 'cleanup'
|
85
|
+
end
|
86
|
+
|
87
|
+
def reset_env
|
88
|
+
run_command "rm ./tmp/DATABASE_FORK_DEVELOPMENT", 'rm DATABASE_FORK_DEVELOPMENT'
|
89
|
+
run_command "rm ./tmp/DATABASE_FORK_TEST", 'rm DATABASE_FORK_TEST'
|
90
|
+
end
|
91
|
+
|
92
|
+
def export_env
|
93
|
+
run_command "echo #{fork_db_name('development')} > ./tmp/DATABASE_FORK_DEVELOPMENT", 'setting DATABASE_FORK_DEVELOPMENT'
|
94
|
+
run_command "echo #{fork_db_name('test')} > ./tmp/DATABASE_FORK_TEST", 'setting DATABASE_FORK_TEST'
|
95
|
+
end
|
96
|
+
|
97
|
+
def run_command(command, message)
|
98
|
+
log_info message
|
99
|
+
log_debug command
|
100
|
+
`#{command}`
|
101
|
+
end
|
102
|
+
|
103
|
+
def dump_file_path(env = 'development')
|
104
|
+
"./tmp/dump_#{env}.sql"
|
105
|
+
end
|
106
|
+
|
107
|
+
# could be queried from source_db:
|
108
|
+
def character_set
|
109
|
+
config['character_set'] || 'utf8'
|
110
|
+
end
|
111
|
+
|
112
|
+
# could be queried from source_db:
|
113
|
+
def collation
|
114
|
+
config['collation'] || 'utf8_unicode_ci'
|
115
|
+
end
|
116
|
+
|
117
|
+
def log_info(message)
|
118
|
+
@logger.info message
|
119
|
+
end
|
120
|
+
|
121
|
+
def log_debug(message)
|
122
|
+
@logger.debug message
|
123
|
+
end
|
124
|
+
|
125
|
+
def fork_exists?(env = 'development')
|
126
|
+
command = %Q{mysql #{connection_params[env]} -s -N -e "SHOW DATABASES LIKE '#{fork_db_name(env)}';" }
|
127
|
+
!`#{command}`.empty?
|
128
|
+
end
|
129
|
+
|
130
|
+
# simplify
|
131
|
+
# make framework agnostic
|
132
|
+
def connection_params(env = 'development')
|
133
|
+
@connection_params ||= if ENV['USER'] == 'vagrant'
|
134
|
+
%Q{--user=#{app_connection[env]['username']} --password=#{app_connection[env]['password']} --socket=#{app_connection[env]['socket']}}
|
135
|
+
else
|
136
|
+
%Q{--user=#{app_connection[env]['username']} --password=#{app_connection[env]['password']} --host=#{app_connection[env]['host']} --port=#{app_connection[env]['port']}}
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def fork_db_name(env = 'development')
|
141
|
+
"#{source_db(env)}_#{current_branch}".strip
|
142
|
+
end
|
143
|
+
|
144
|
+
def source_db(env= 'development')
|
145
|
+
app_connection[env]['database']
|
146
|
+
end
|
147
|
+
|
148
|
+
def app_connection
|
149
|
+
@app_connection ||= YAML.load(ERB.new(open(File.join(File.dirname(__FILE__), '..', '..', 'config', 'database.yml')).read).result)
|
150
|
+
end
|
151
|
+
|
152
|
+
def current_branch
|
153
|
+
@current_branch ||= `git rev-parse --abbrev-ref HEAD`.strip.gsub('/', '_')
|
154
|
+
end
|
155
|
+
|
156
|
+
def config
|
157
|
+
@config ||= begin
|
158
|
+
config = {
|
159
|
+
'check_branch_name_regex' => '^feature_',
|
160
|
+
'ignore' => [],
|
161
|
+
'environments' => %w(development test)
|
162
|
+
}
|
163
|
+
config.merge! YAML.load(open(@config_file).read) if File.exists?(@config_file)
|
164
|
+
config
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
def save_config
|
169
|
+
File.open(@config_file, 'w') do |f|
|
170
|
+
f.puts @config.to_yaml
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: database_fork
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- the-architect
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-16 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: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
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
|
+
description: Fork your database
|
42
|
+
email:
|
43
|
+
- marcel.scherf@epicteams.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- database_fork.gemspec
|
54
|
+
- lib/database_fork.rb
|
55
|
+
homepage: http://github.com/
|
56
|
+
licenses:
|
57
|
+
- MIT
|
58
|
+
metadata: {}
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 2.2.2
|
76
|
+
signing_key:
|
77
|
+
specification_version: 4
|
78
|
+
summary: Fork your database
|
79
|
+
test_files: []
|