rake_multi_db 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +22 -0
- data/README.md +69 -0
- data/lib/rake_multi_db.rb +6 -0
- data/lib/rake_multi_db/railtie.rb +10 -0
- data/lib/rake_multi_db/version.rb +4 -0
- data/lib/tasks/rake_multi_db.rake +108 -0
- data/lib/tasks/rake_multi_db.rb +108 -0
- metadata +65 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 36d13216a0523133d5bbb2a4adcdbe18dcb9b62a
|
4
|
+
data.tar.gz: 6590ab8777d999b067c456f0df5eb962687e1228
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8dd0d10d5f5a3a9f2abceed3a6022046a85f6de933109aa7894eb9ac9abeca8795b36ff8003e90cb437d1e0d34d6b3ba8f0b965726643cd7084ba3f0c5fbbaa8
|
7
|
+
data.tar.gz: f1073cae5f580836aaa056a8f93da5586a3cbac204926dc247b2d3330bf590bd064498a13c26d7555dcbd82cf43ca1e53243f2823bea5d2633370f6fda3eb4c4
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Andrew
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# rake_multi_db_helper
|
2
|
+
Simple interactive rake task to automatically drop/migrate/drop & migrate/reset multiple databases
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
1. Add `gem 'rake_multi_db', '~> 0.0'`to your application's Gemfile.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
Simply run this rake task as you would any other rake task, from your rails project directory, using the _multi_db:_ prefix and the appropriate tasks and parameters.
|
9
|
+
|
10
|
+
### Parameters
|
11
|
+
The following parameters are able to be invoked
|
12
|
+
|
13
|
+
| Parameter | Default Value | Comment | Example |
|
14
|
+
| ------------- |:-------------:| -----:|----------:|
|
15
|
+
| environments | test,development | A comma-seperated string of environments to work with. | ```rake multi_db:drop environments=test,development```|
|
16
|
+
| force | false | Whether to forcefully progress if an error occurs,<br /> without prompting the user. | ```rake multi_db:drop force=true``` |
|
17
|
+
|
18
|
+
The following tasks are able to be used
|
19
|
+
|
20
|
+
| Task | Purpose |
|
21
|
+
|--------:|-------------------------------------------:|
|
22
|
+
| drop | Drops the specified environment's database.|
|
23
|
+
| migrate| Migrates the specified environment's database from the relevant migration file.|
|
24
|
+
| redo | Drops and migrates the specified environment's database.|
|
25
|
+
| reset | Resets all table data for the specified environment.|
|
26
|
+
|
27
|
+
Arguments are passed as follows:
|
28
|
+
```rake multi_db:{Task} {parameter}={parameter_value}```
|
29
|
+
|
30
|
+
i.e.
|
31
|
+
|
32
|
+
To drop specified environment databases:
|
33
|
+
|
34
|
+
```rake multi_db:drop environments={environments_to_drop_comma_seperated}```
|
35
|
+
|
36
|
+
To drop test and development databases:
|
37
|
+
|
38
|
+
```rake multi_db:drop``` or ```rake multi_db:drop environments=test,development```
|
39
|
+
|
40
|
+
To migrate all Test & Development databases:
|
41
|
+
|
42
|
+
```rake multi_db:migrate``` or ```rake multi_db:migrate environments=test,development```
|
43
|
+
|
44
|
+
|
45
|
+
To drop and migrate all Test & Development databases:
|
46
|
+
|
47
|
+
```rake multi_db:redo``` or ```rake multi_db:redo environments=test,development```
|
48
|
+
|
49
|
+
To reset all Test & Development databases
|
50
|
+
|
51
|
+
```rake multi_db:reset``` or ```rake multi_db:reset environments=test,development```
|
52
|
+
|
53
|
+
|
54
|
+
## Things to keep in mind
|
55
|
+
1. The value specified by ```RAILS_ENV``` is ignored entirely.
|
56
|
+
2. When no environments are passed through to this rake task, it will automatically use both ```test``` and ```development``` environments.
|
57
|
+
i.e. running ```rake multi_db:drop``` will drop ***all*** ```test``` and ```development``` databases. If you only want to drop, say, the test databases, specify an environment, as follows:
|
58
|
+
```rake multi_db:drop environments=test```
|
59
|
+
|
60
|
+
3. By default, when an error is encountered, the user will be asked to confirm whether they would like to continue. If you want to forcefully progress, and suppress all prompts, execute the rake task using the ```force``` parameter
|
61
|
+
i.e. ```rake multi_db:drop environments=test,development force=true```
|
62
|
+
|
63
|
+
## Contributing
|
64
|
+
|
65
|
+
1. Fork it!
|
66
|
+
2. Create your feature branch: `git checkout -b my-new-feature`
|
67
|
+
3. Commit your changes: `git commit -am 'Add some feature'`
|
68
|
+
4. Push to the branch: `git push origin my-new-feature`
|
69
|
+
5. Submit a pull request :D
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'open3'
|
2
|
+
|
3
|
+
namespace :multi_db do
|
4
|
+
environments = ENV['environments'].nil? ? ['test', 'development'] : ENV['environments'].split(',')
|
5
|
+
|
6
|
+
desc "Drops all specified environment databases"
|
7
|
+
task :drop do
|
8
|
+
environments.each do |env_name|
|
9
|
+
drop env_name
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
desc "Migrates all specified environment databases"
|
14
|
+
task :migrate do
|
15
|
+
environments.each do |env_name|
|
16
|
+
migrate env_name
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
desc "Drops and then Migrates all specified environment databases"
|
21
|
+
task :redo do
|
22
|
+
environments.each do |env_name|
|
23
|
+
drop env_name
|
24
|
+
migrate env_name
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
desc "Resets all table data in the specified environment databases"
|
29
|
+
task :reset do
|
30
|
+
environments.each do |env_name|
|
31
|
+
reset env_name
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
desc "Executes the seed script to populate the specified environment databases"
|
36
|
+
task :seed do
|
37
|
+
environments.each do |env_name|
|
38
|
+
seed env_name
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
# Helper methods #
|
45
|
+
def prompt_user(message)
|
46
|
+
# Don't show a message, if the force param was passed
|
47
|
+
force = ENV['force'].nil? ? false : ENV['force']
|
48
|
+
|
49
|
+
return true if force
|
50
|
+
|
51
|
+
puts "#{message} (y|n|yes|no)"
|
52
|
+
|
53
|
+
# parse the response
|
54
|
+
resp = STDIN.gets.strip
|
55
|
+
|
56
|
+
case resp.downcase
|
57
|
+
when 'yes', 'y'
|
58
|
+
return true
|
59
|
+
when 'no', 'n'
|
60
|
+
puts 'Aborting...'
|
61
|
+
exit 0
|
62
|
+
else
|
63
|
+
# Re-prompt the user
|
64
|
+
return prompt_user message
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def drop(env_name)
|
69
|
+
puts "Dropping #{env_name.capitalize} Database"
|
70
|
+
|
71
|
+
execute_command("rake db:drop RAILS_ENV=#{env_name}")
|
72
|
+
end
|
73
|
+
|
74
|
+
def migrate(env_name)
|
75
|
+
puts "Migrating #{env_name.capitalize} Database"
|
76
|
+
|
77
|
+
execute_command("rake db:migrate RAILS_ENV=#{env_name}")
|
78
|
+
end
|
79
|
+
|
80
|
+
def reset(env_name)
|
81
|
+
puts "Resetting #{env_name.capitalize} Database"
|
82
|
+
|
83
|
+
execute_command("rake db:reset RAILS_ENV=#{env_name}")
|
84
|
+
end
|
85
|
+
|
86
|
+
def seed(env_name)
|
87
|
+
puts "Seeding #{env_name.capitalize} Database"
|
88
|
+
|
89
|
+
execute_command("rake db:seed RAILS_ENV=#{env_name}")
|
90
|
+
end
|
91
|
+
|
92
|
+
def execute_command(command)
|
93
|
+
Open3::popen3(command) do |stdin, stdout, stderr|
|
94
|
+
out_text = stdout.gets
|
95
|
+
error_text = stderr.gets
|
96
|
+
|
97
|
+
unless error_text.nil?
|
98
|
+
puts "Error: #{error_text}"
|
99
|
+
|
100
|
+
prompt_user 'An error occurred. Would you like to continue?'
|
101
|
+
end
|
102
|
+
|
103
|
+
unless out_text.nil?
|
104
|
+
puts out_text
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'open3'
|
2
|
+
|
3
|
+
namespace :multi_db do
|
4
|
+
environments = ENV['environments'].nil? ? ['test', 'development'] : ENV['environments'].split(',')
|
5
|
+
|
6
|
+
desc "Drops all specified environment databases"
|
7
|
+
task :drop do
|
8
|
+
environments.each do |env_name|
|
9
|
+
drop env_name
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
desc "Migrates all specified environment databases"
|
14
|
+
task :migrate do
|
15
|
+
environments.each do |env_name|
|
16
|
+
migrate env_name
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
desc "Drops and then Migrates all specified environment databases"
|
21
|
+
task :redo do
|
22
|
+
environments.each do |env_name|
|
23
|
+
drop env_name
|
24
|
+
migrate env_name
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
desc "Resets all table data in the specified environment databases"
|
29
|
+
task :reset do
|
30
|
+
environments.each do |env_name|
|
31
|
+
reset env_name
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
desc "Executes the seed script to populate the specified environment databases"
|
36
|
+
task :seed do
|
37
|
+
environments.each do |env_name|
|
38
|
+
seed env_name
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
# Helper methods #
|
45
|
+
def prompt_user(message)
|
46
|
+
# Don't show a message, if the force param was passed
|
47
|
+
force = ENV['force'].nil? ? false : ENV['force']
|
48
|
+
|
49
|
+
return true if force
|
50
|
+
|
51
|
+
puts "#{message} (y|n|yes|no)"
|
52
|
+
|
53
|
+
# parse the response
|
54
|
+
resp = STDIN.gets.strip
|
55
|
+
|
56
|
+
case resp.downcase
|
57
|
+
when 'yes', 'y'
|
58
|
+
return true
|
59
|
+
when 'no', 'n'
|
60
|
+
puts 'Aborting...'
|
61
|
+
exit 0
|
62
|
+
else
|
63
|
+
# Re-prompt the user
|
64
|
+
return prompt_user message
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def drop(env_name)
|
69
|
+
puts "Dropping #{env_name.capitalize} Database"
|
70
|
+
|
71
|
+
execute_command("rake db:drop RAILS_ENV=#{env_name}")
|
72
|
+
end
|
73
|
+
|
74
|
+
def migrate(env_name)
|
75
|
+
puts "Migrating #{env_name.capitalize} Database"
|
76
|
+
|
77
|
+
execute_command("rake db:migrate RAILS_ENV=#{env_name}")
|
78
|
+
end
|
79
|
+
|
80
|
+
def reset(env_name)
|
81
|
+
puts "Resetting #{env_name.capitalize} Database"
|
82
|
+
|
83
|
+
execute_command("rake db:reset RAILS_ENV=#{env_name}")
|
84
|
+
end
|
85
|
+
|
86
|
+
def seed(env_name)
|
87
|
+
puts "Seeding #{env_name.capitalize} Database"
|
88
|
+
|
89
|
+
execute_command("rake db:seed RAILS_ENV=#{env_name}")
|
90
|
+
end
|
91
|
+
|
92
|
+
def execute_command(command)
|
93
|
+
Open3::popen3(command) do |stdin, stdout, stderr|
|
94
|
+
out_text = stdout.gets
|
95
|
+
error_text = stderr.gets
|
96
|
+
|
97
|
+
unless error_text.nil?
|
98
|
+
puts "Error: #{error_text}"
|
99
|
+
|
100
|
+
prompt_user 'An error occurred. Would you like to continue?'
|
101
|
+
end
|
102
|
+
|
103
|
+
unless out_text.nil?
|
104
|
+
puts out_text
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rake_multi_db
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Walter
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-01-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3'
|
27
|
+
description:
|
28
|
+
email: andrew@xtrasimplicity.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- LICENSE
|
34
|
+
- README.md
|
35
|
+
- lib/rake_multi_db.rb
|
36
|
+
- lib/rake_multi_db/railtie.rb
|
37
|
+
- lib/rake_multi_db/version.rb
|
38
|
+
- lib/tasks/rake_multi_db.rake
|
39
|
+
- lib/tasks/rake_multi_db.rb
|
40
|
+
homepage: http://github.com/xtrasimplicity/rake_all_db_helper
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata: {}
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 2.6.13
|
61
|
+
signing_key:
|
62
|
+
specification_version: 4
|
63
|
+
summary: Simple interactive rake task to automatically drop/migrate/drop & migrate
|
64
|
+
test and development databases
|
65
|
+
test_files: []
|