flipp 0.0.1.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/flipp.gemspec +2 -2
- data/lib/flipp.rb +1 -1
- data/lib/flipp/flipp.rb +41 -39
- data/lib/flipp/helpers/git_helper.rb +37 -0
- data/lib/flipp/railtie.rb +17 -0
- data/{tasks/flipp.rb → lib/flipp/tasks.rb} +3 -3
- data/spec/lib/flipp/flipp_spec.rb +3 -2
- data/spec/lib/{helpers → flipp/helpers}/git_helper_spec.rb +9 -9
- data/spec/spec_helper.rb +1 -1
- metadata +11 -11
- data/lib/flipp/railties/reestablish_connection.rb +0 -11
- data/lib/helpers/git_helper.rb +0 -33
data/Rakefile
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
$:.unshift File.expand_path('..', __FILE__)
|
2
|
-
require '
|
2
|
+
require 'lib/flipp/tasks'
|
data/flipp.gemspec
CHANGED
@@ -5,7 +5,7 @@ require 'bundler'
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "flipp"
|
8
|
-
s.version = '0.0.
|
8
|
+
s.version = '0.0.2'
|
9
9
|
s.platform = Gem::Platform::RUBY
|
10
10
|
s.authors = ["Mike Pack"]
|
11
11
|
s.email = ["mikepackdev@gmail.com"]
|
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.add_runtime_dependency 'activerecord'
|
19
19
|
s.add_runtime_dependency 'database_exporter'
|
20
20
|
|
21
|
-
s.files = %w( README.md Rakefile flipp.gemspec
|
21
|
+
s.files = %w( README.md Rakefile flipp.gemspec ) + Dir["lib/**/*.rb"]
|
22
22
|
s.test_files = Dir["spec/**/*.rb"]
|
23
23
|
s.require_paths = ["lib"]
|
24
24
|
end
|
data/lib/flipp.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
require 'flipp/flipp'
|
2
|
-
require 'flipp/
|
2
|
+
require 'flipp/railtie'
|
data/lib/flipp/flipp.rb
CHANGED
@@ -1,56 +1,58 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'active_record'
|
3
3
|
require 'database_exporter'
|
4
|
-
require File.join(File.dirname(__FILE__), '
|
4
|
+
require File.join(File.dirname(__FILE__), 'helpers/git_helper')
|
5
5
|
|
6
6
|
# The Flipp class orchestrates the actual switching of databaseses
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
module Flipp
|
8
|
+
class Flipp
|
9
|
+
def initialize(branch = Flipp::GitHelper.current_branch)
|
10
|
+
@branch = branch
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
def switch_databases
|
14
|
+
current_config = ActiveRecord::Base.connection_pool.spec.config
|
15
|
+
new_config = current_config.merge!({:database => new_database_name(current_config)})
|
15
16
|
|
16
|
-
|
17
|
+
puts "Switching databases (#{new_config[:database]})..."
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
# Try connecting to the new database
|
20
|
+
begin
|
21
|
+
ActiveRecord::Base.establish_connection new_config
|
22
|
+
rescue
|
23
|
+
create_new_db_or_fallback current_config, new_config
|
24
|
+
end
|
23
25
|
end
|
24
|
-
end
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
27
|
+
def create_new_db_or_fallback(current_config, new_config)
|
28
|
+
# Try creating then connecting to the new database
|
29
|
+
begin
|
30
|
+
create_and_copy_data! current_config, new_config
|
31
|
+
rescue => exception
|
32
|
+
# Fallback and use the original config
|
33
|
+
puts "Could not use the new database, falling back to your normal development database..."
|
34
|
+
ActiveRecord::Base.establish_connection current_config
|
35
|
+
end
|
34
36
|
end
|
35
|
-
end
|
36
37
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
38
|
+
def new_database_name(config)
|
39
|
+
# The new name for the database
|
40
|
+
# eg. if the original dev database is dev_db and the branch was "new_feature",
|
41
|
+
# the new database would be db_db_new_feature
|
42
|
+
config[:database] + '_' + @branch
|
43
|
+
end
|
43
44
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
45
|
+
def create_and_copy_data!(current_config, new_config)
|
46
|
+
# Copy the current development database's data to the new database
|
47
|
+
puts "Copying data to the new database (this could take a minute but will only run once)..."
|
48
|
+
copy_data current_config, new_config
|
48
49
|
|
49
|
-
|
50
|
-
|
51
|
-
|
50
|
+
# Try out the new database
|
51
|
+
ActiveRecord::Base.establish_connection new_config
|
52
|
+
end
|
52
53
|
|
53
|
-
|
54
|
-
|
54
|
+
def copy_data(current_config, new_config)
|
55
|
+
DatabaseExporter.new(current_config).copy(new_config)
|
56
|
+
end
|
55
57
|
end
|
56
58
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
module Flipp
|
4
|
+
module Helpers
|
5
|
+
module GitHelper
|
6
|
+
def self.hook_installed?
|
7
|
+
File.exists? '.git/hooks/post-checkout'
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.install_hook
|
11
|
+
# Check for the .git directory
|
12
|
+
if File.directory? '.git'
|
13
|
+
unless File.directory? '.git/hooks'
|
14
|
+
# Create hooks directory
|
15
|
+
end
|
16
|
+
|
17
|
+
# Copy the file to the git hooks
|
18
|
+
puts 'Moving git hook into place...'
|
19
|
+
FileUtils.cp 'lib/flipp/git_hooks/post-checkout', File.expand_path('.git/hooks/post-checkout')
|
20
|
+
# And give it execute permissions
|
21
|
+
FileUtils.chmod 0755, '.git/hooks/post-checkout'
|
22
|
+
else
|
23
|
+
puts "Can't find your .git directory. Are you at the root of your project?"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.uninstall_hook
|
28
|
+
puts 'Removing git hook...'
|
29
|
+
FileUtils.rm File.expand_path('.git/hooks/post-checkout')
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.current_branch
|
33
|
+
`git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* /\1/'`.chomp[1..-1] # Remove control character
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'flipp'
|
2
|
+
|
3
|
+
module Flipp
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
initializer "restablish_connection.configure_rails_initialization" do
|
6
|
+
if Rails.env.development? and Flipp::GitHelper.hook_installed?
|
7
|
+
# Use the connection determined by flipp
|
8
|
+
flipp = Flipp::Flipp.new
|
9
|
+
flipp.switch_databases
|
10
|
+
end
|
11
|
+
|
12
|
+
rake_tasks do
|
13
|
+
load 'flipp/tasks.rb'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -1,15 +1,15 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), '
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', 'lib/flipp/helpers/git_helper')
|
2
2
|
|
3
3
|
namespace :flipp do
|
4
4
|
desc 'Install flipp'
|
5
5
|
task :install do
|
6
6
|
# Move git hook into place
|
7
|
-
GitHelper.install_hook
|
7
|
+
Flipp::GitHelper.install_hook
|
8
8
|
end
|
9
9
|
|
10
10
|
desc 'Uninstall flipp'
|
11
11
|
task :uninstall do
|
12
12
|
# Remove git hook
|
13
|
-
GitHelper.uninstall_hook
|
13
|
+
Flipp::GitHelper.uninstall_hook
|
14
14
|
end
|
15
15
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Flipp do
|
4
|
-
let(:flipp) { Flipp.new('new_feature_branch') }
|
3
|
+
describe Flipp::Flipp do
|
4
|
+
let(:flipp) { Flipp::Flipp.new('new_feature_branch') }
|
5
5
|
let(:old_db) { {:adapter => 'mysql2', :host => 'localhost', :username => 'root', :database => 'old_db'} }
|
6
6
|
let(:new_db) { {:adapter => 'mysql2', :host => 'localhost', :username => 'root', :database => 'new_db'} }
|
7
7
|
|
@@ -40,6 +40,7 @@ describe Flipp do
|
|
40
40
|
|
41
41
|
describe '#create_and_copy_data!' do
|
42
42
|
it 'copies the database' do
|
43
|
+
ActiveRecord::Base.stub(:establish_connection)
|
43
44
|
flipp.should_receive(:copy_data).with(old_db, new_db)
|
44
45
|
flipp.create_and_copy_data!(old_db, new_db)
|
45
46
|
end
|
@@ -1,38 +1,38 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe GitHelper do
|
3
|
+
describe Flipp::Helpers::GitHelper do
|
4
4
|
describe '#hook_installed?' do
|
5
5
|
it 'returns true when the hook exists' do
|
6
6
|
FileUtils.touch '.git/hooks/post-checkout'
|
7
|
-
GitHelper.hook_installed?.should be_true
|
7
|
+
Flipp::Helpers::GitHelper.hook_installed?.should be_true
|
8
8
|
end
|
9
9
|
|
10
10
|
it 'returns false when the hook does not exists' do
|
11
11
|
FileUtils.rm '.git/hooks/post-checkout'
|
12
|
-
GitHelper.hook_installed?.should be_false
|
12
|
+
Flipp::Helpers::GitHelper.hook_installed?.should be_false
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
16
|
describe '#install_hook' do
|
17
17
|
it 'moves the git hook into place' do
|
18
|
-
GitHelper.install_hook
|
19
|
-
FileUtils.compare_file('.git/hooks/post-checkout', File.expand_path('
|
18
|
+
Flipp::Helpers::GitHelper.install_hook
|
19
|
+
FileUtils.compare_file('.git/hooks/post-checkout', File.expand_path('../../../../../lib/flipp/git_hooks/post-checkout', __FILE__))
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
23
|
describe '#uninstall_hook' do
|
24
|
-
before { GitHelper.install_hook }
|
24
|
+
before { Flipp::Helpers::GitHelper.install_hook }
|
25
25
|
|
26
26
|
it 'remove the git hook' do
|
27
|
-
GitHelper.uninstall_hook
|
28
|
-
GitHelper.hook_installed?.should be_false
|
27
|
+
Flipp::Helpers::GitHelper.uninstall_hook
|
28
|
+
Flipp::Helpers::GitHelper.hook_installed?.should be_false
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
32
|
describe '#current_branch' do
|
33
33
|
it 'returns the name of the current git branch' do
|
34
34
|
`git checkout -b new_and_fun_branch`
|
35
|
-
GitHelper.current_branch.should == 'new_and_fun_branch'
|
35
|
+
Flipp::Helpers::GitHelper.current_branch.should == 'new_and_fun_branch'
|
36
36
|
|
37
37
|
# Cleanup
|
38
38
|
`git checkout master && git branch -d new_and_fun_branch`
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: flipp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-03-
|
12
|
+
date: 2012-03-13 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
|
-
requirement: &
|
16
|
+
requirement: &70236750364920 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70236750364920
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: database_exporter
|
27
|
-
requirement: &
|
27
|
+
requirement: &70236750364320 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70236750364320
|
36
36
|
description: To alleviate issues around highly divergent development databases on
|
37
37
|
feature/refactor branches, flipp helps switch databases upon new branch checkouts.
|
38
38
|
email:
|
@@ -44,13 +44,13 @@ files:
|
|
44
44
|
- README.md
|
45
45
|
- Rakefile
|
46
46
|
- flipp.gemspec
|
47
|
-
- tasks/flipp.rb
|
48
47
|
- lib/flipp/flipp.rb
|
49
|
-
- lib/flipp/
|
48
|
+
- lib/flipp/helpers/git_helper.rb
|
49
|
+
- lib/flipp/railtie.rb
|
50
|
+
- lib/flipp/tasks.rb
|
50
51
|
- lib/flipp.rb
|
51
|
-
- lib/helpers/git_helper.rb
|
52
52
|
- spec/lib/flipp/flipp_spec.rb
|
53
|
-
- spec/lib/helpers/git_helper_spec.rb
|
53
|
+
- spec/lib/flipp/helpers/git_helper_spec.rb
|
54
54
|
- spec/spec_helper.rb
|
55
55
|
homepage: http://www.mikepackdev.com
|
56
56
|
licenses: []
|
@@ -78,5 +78,5 @@ specification_version: 3
|
|
78
78
|
summary: Switch development databases upon branch checkouts.
|
79
79
|
test_files:
|
80
80
|
- spec/lib/flipp/flipp_spec.rb
|
81
|
-
- spec/lib/helpers/git_helper_spec.rb
|
81
|
+
- spec/lib/flipp/helpers/git_helper_spec.rb
|
82
82
|
- spec/spec_helper.rb
|
@@ -1,11 +0,0 @@
|
|
1
|
-
require 'flipp'
|
2
|
-
|
3
|
-
class ReestablishConnection < Rails::Railtie
|
4
|
-
initializer "restablish_connection.configure_rails_initialization" do
|
5
|
-
if Rails.env.development? and GitHelper.hook_installed?
|
6
|
-
# Use the connection determined by flipp
|
7
|
-
flipp = Flipp.new
|
8
|
-
flipp.switch_databases
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
data/lib/helpers/git_helper.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'fileutils'
|
2
|
-
|
3
|
-
module GitHelper
|
4
|
-
def self.hook_installed?
|
5
|
-
File.exists? '.git/hooks/post-checkout'
|
6
|
-
end
|
7
|
-
|
8
|
-
def self.install_hook
|
9
|
-
# Check for the .git directory
|
10
|
-
if File.directory? '.git'
|
11
|
-
unless File.directory? '.git/hooks'
|
12
|
-
# Create hooks directory
|
13
|
-
end
|
14
|
-
|
15
|
-
# Copy the file to the git hooks
|
16
|
-
puts 'Moving git hook into place...'
|
17
|
-
FileUtils.cp 'lib/git_hooks/post-checkout', File.expand_path('.git/hooks/post-checkout')
|
18
|
-
# And give it execute permissions
|
19
|
-
FileUtils.chmod 0755, '.git/hooks/post-checkout'
|
20
|
-
else
|
21
|
-
puts "Can't find your .git directory. Are you at the root of your project?"
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def self.uninstall_hook
|
26
|
-
puts 'Removing git hook...'
|
27
|
-
FileUtils.rm File.expand_path('.git/hooks/post-checkout')
|
28
|
-
end
|
29
|
-
|
30
|
-
def self.current_branch
|
31
|
-
`git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* /\1/'`.chomp[1..-1] # Remove control character
|
32
|
-
end
|
33
|
-
end
|