database_fork 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/database_fork.gemspec +1 -1
- data/lib/database_fork.rb +28 -22
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fd8885a83edb2df3957725d5e5640cbb203fb355
|
4
|
+
data.tar.gz: 74c890ff847f6f13fc720eeeeb64e906de7d99dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f427ee23fbf2c4734074a7bf8f20d796ce899745758c5d90e3cbab70b40edd3cf44fdf0b599af1efbf3ac42bc8a4bcf12cdffb4e16025a1a4258267e832c0f9b
|
7
|
+
data.tar.gz: d8d326fdcdf9d7f6cb4928b6aeb460c6cd3716fa53525eb890ae404dc2f47c5cc40b48691f757fb2fd98f800be723bb81c35dd5bb22d48c3cc73fd80f97bcfc9
|
data/database_fork.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "database_fork"
|
7
|
-
spec.version = '0.0.
|
7
|
+
spec.version = '0.0.2'
|
8
8
|
spec.authors = ["the-architect"]
|
9
9
|
spec.email = ["marcel.scherf@epicteams.com"]
|
10
10
|
spec.summary = %q{Fork your database}
|
data/lib/database_fork.rb
CHANGED
@@ -16,11 +16,14 @@ class DatabaseFork
|
|
16
16
|
end
|
17
17
|
|
18
18
|
# use DatabaseFork.new.run in your post-checkout hook
|
19
|
-
def initialize(
|
20
|
-
@
|
19
|
+
def initialize(root_dir, logger = Logger.new(STDOUT))
|
20
|
+
@root_dir = root_dir
|
21
|
+
@config_file = File.join(@root_dir, '.db_forks.yml')
|
21
22
|
@logger = logger
|
23
|
+
@commands = []
|
22
24
|
end
|
23
25
|
|
26
|
+
# TODO: simplify this somehow
|
24
27
|
def run
|
25
28
|
if config['ignore'].include?(current_branch)
|
26
29
|
log_info 'This branch name is ignored in .db_fork.yml config. Skipping along.'
|
@@ -32,12 +35,7 @@ class DatabaseFork
|
|
32
35
|
log_info 'database fork already exists'
|
33
36
|
export_env
|
34
37
|
else
|
35
|
-
|
36
|
-
|
37
|
-
# trick to read user input:
|
38
|
-
decision = IO.new(IO.sysopen('/dev/tty'), 'r').gets.chomp
|
39
|
-
|
40
|
-
case decision
|
38
|
+
case ask_user("Create database: '#{fork_db_name}'? (y(es), n(no), enter=ignore)")
|
41
39
|
when 'y'
|
42
40
|
create_database_fork!
|
43
41
|
export_env
|
@@ -50,13 +48,18 @@ class DatabaseFork
|
|
50
48
|
end
|
51
49
|
end
|
52
50
|
else
|
53
|
-
log_info 'not a feature branch. not creating database fork.'
|
54
51
|
reset_env
|
55
52
|
end
|
56
53
|
|
57
54
|
save_config
|
58
55
|
end
|
59
56
|
|
57
|
+
|
58
|
+
def ask_user(question)
|
59
|
+
log_info question
|
60
|
+
IO.new(IO.sysopen('/dev/tty'), 'r').gets.chomp
|
61
|
+
end
|
62
|
+
|
60
63
|
def create_database_fork!
|
61
64
|
config['environments'].each do |env|
|
62
65
|
log_info "creating database fork '#{fork_db_name(env)}' from #{source_db(env)}"
|
@@ -68,14 +71,17 @@ class DatabaseFork
|
|
68
71
|
end
|
69
72
|
end
|
70
73
|
|
74
|
+
# TODO: refactor to adapter
|
71
75
|
def create_dump(env = 'development')
|
72
76
|
run_command %Q{mysqldump #{connection_params(env)} --routines --triggers -C #{source_db(env)} > #{dump_file_path(env)}}, "dumping #{source_db(env)}"
|
73
77
|
end
|
74
78
|
|
79
|
+
# TODO: refactor to adapter
|
75
80
|
def create_database(env = 'development')
|
76
81
|
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
82
|
end
|
78
83
|
|
84
|
+
# TODO: refactor to adapter
|
79
85
|
def import_dump(env = 'development')
|
80
86
|
run_command %Q{mysql #{connection_params(env)} -C -A -D#{fork_db_name(env)} < #{dump_file_path(env)}}, 'importing dump'
|
81
87
|
end
|
@@ -85,6 +91,7 @@ class DatabaseFork
|
|
85
91
|
end
|
86
92
|
|
87
93
|
def reset_env
|
94
|
+
log_info 'Resetting fork information'
|
88
95
|
run_command "rm ./tmp/DATABASE_FORK_DEVELOPMENT", 'rm DATABASE_FORK_DEVELOPMENT'
|
89
96
|
run_command "rm ./tmp/DATABASE_FORK_TEST", 'rm DATABASE_FORK_TEST'
|
90
97
|
end
|
@@ -94,14 +101,15 @@ class DatabaseFork
|
|
94
101
|
run_command "echo #{fork_db_name('test')} > ./tmp/DATABASE_FORK_TEST", 'setting DATABASE_FORK_TEST'
|
95
102
|
end
|
96
103
|
|
97
|
-
def run_command(command, message)
|
104
|
+
def run_command(command, message, dry_run = false)
|
98
105
|
log_info message
|
99
106
|
log_debug command
|
100
|
-
|
107
|
+
@commands << [command, message]
|
108
|
+
`#{command}` unless dry_run
|
101
109
|
end
|
102
110
|
|
103
111
|
def dump_file_path(env = 'development')
|
104
|
-
"
|
112
|
+
File.join(@root_dir, 'tmp', "dump_#{env}.sql")
|
105
113
|
end
|
106
114
|
|
107
115
|
# could be queried from source_db:
|
@@ -146,23 +154,21 @@ class DatabaseFork
|
|
146
154
|
end
|
147
155
|
|
148
156
|
def app_connection
|
149
|
-
@app_connection ||= YAML.load(ERB.new(open(File.join(
|
157
|
+
@app_connection ||= YAML.load(ERB.new(open(File.join(@root_dir, '..', '..', 'config', 'database.yml')).read).result)
|
150
158
|
end
|
151
159
|
|
152
160
|
def current_branch
|
153
161
|
@current_branch ||= `git rev-parse --abbrev-ref HEAD`.strip.gsub('/', '_')
|
154
162
|
end
|
155
163
|
|
164
|
+
DEFAULTS = {
|
165
|
+
'check_branch_name_regex' => '^feature_',
|
166
|
+
'ignore' => [],
|
167
|
+
'environments' => %w(development test)
|
168
|
+
}
|
169
|
+
|
156
170
|
def config
|
157
|
-
@config ||=
|
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
|
171
|
+
@config ||= DEFAULTS.merge(YAML.load(open(@config_file).read)) rescue DEFAULTS
|
166
172
|
end
|
167
173
|
|
168
174
|
def save_config
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: database_fork
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- the-architect
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|