tb_branch_db 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ /.idea
6
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in tb_branch_db.gemspec
4
+ gemspec
@@ -0,0 +1,82 @@
1
+ = TbBranchDb
2
+
3
+ Rake tasks to easy-peasy branch a database in your Rails project.
4
+
5
+ The tasks basically implement the commands describe here:
6
+
7
+ http://mislav.uniqpath.com/rails/branching-the-database-along-with-your-code/
8
+
9
+ See the above link for further information about the branching problem and the way it works.
10
+
11
+ == Requirements
12
+
13
+ * Git as version control system
14
+ * MySQL as database
15
+ * Rails (this gem works only inside a Rails project)
16
+
17
+ == Installation
18
+
19
+ Add the gem to your bundler Gemfile:
20
+
21
+ # Gemfile
22
+ ...
23
+ group :development do
24
+ ...
25
+ gem 'tb_branch_db'
26
+ end
27
+
28
+ And run:
29
+
30
+ $ bundle install
31
+
32
+ == Usage
33
+
34
+ First update your config/database.yml to allow branching:
35
+
36
+ $ rake db:branch:setup
37
+
38
+ This will update database.yml like so:
39
+
40
+ # config/database.yml
41
+ <%
42
+ # http://mislav.uniqpath.com/rails/branching-the-database-along-with-your-code/
43
+ branch = `git symbolic-ref HEAD 2>/dev/null`.chomp.sub('refs/heads/', '')
44
+ suffix = `git config --bool branch.#{branch}.database`.chomp == 'true' ? "_#{branch}" : ""
45
+ %>
46
+ development:
47
+ ...
48
+ database: onappto_development<%= suffix %>
49
+ ...
50
+
51
+ A backup of the original database.yml is saved to config/database.yml.orig.
52
+
53
+ Then create a branch (a clone) of your master database:
54
+
55
+ $ rake db:branch
56
+
57
+ This will first create an entry in the .git/config and then clones the master database.
58
+
59
+ # example output
60
+ git config --bool branch.BRANCH.database true
61
+ rake db:create
62
+ mysqldump -u root foo_development | mysql -u root foo_development_BRANCH
63
+
64
+ If you want to cleanup the branch run:
65
+
66
+ $ rake db:branch:cleanup
67
+
68
+ # example output
69
+ git config --bool branch.BRANCH.database false
70
+ mysql -u root -e 'drop database onappto_development_BRANCH'
71
+
72
+ == Alternatives
73
+
74
+ * See https://github.com/sevenwire/db_branch
75
+
76
+ == Credits
77
+
78
+ * Mislav Marohnić (http://mislav.uniqpath.com/)
79
+
80
+
81
+
82
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,63 @@
1
+ require 'yaml'
2
+ require 'rails'
3
+ require 'fileutils'
4
+ require "tb_branch_db/rake_helper"
5
+
6
+ include TbBranchDb::RakeHelper
7
+
8
+ namespace :db do
9
+
10
+ namespace :branch do
11
+
12
+ desc "setup database.yml for branching"
13
+ task :setup do
14
+ HEAD=%q{
15
+ <%
16
+ # http://mislav.uniqpath.com/rails/branching-the-database-along-with-your-code/
17
+ branch = `git symbolic-ref HEAD 2>/dev/null`.chomp.sub('refs/heads/', '')
18
+ suffix = `git config --bool branch.#{branch}.database`.chomp == 'true' ? "_#{branch}" : ""
19
+ %>
20
+ }
21
+ FileUtils.cp "#{Rails.root}/config/database.yml", "#{Rails.root}/config/database.yml.orig"
22
+ puts "backup database.yml to database.yml.orig"
23
+
24
+ config = load_database_config
25
+ dbname = config[Rails.env]['database']
26
+ config[Rails.env]['database'] = "#{dbname}<%= suffix %>"
27
+
28
+ content = YAML.dump(config)
29
+ content.sub!('---', HEAD)
30
+
31
+ File.open("#{Rails.root}/config/database.yml", "w") do |file|
32
+ file.write(content)
33
+ end
34
+
35
+ puts "update database.yml for branching"
36
+ puts "please run 'rake db:branch' to branch db"
37
+ end
38
+
39
+ desc "cleanup db branch (drop database)"
40
+ task :cleanup do
41
+ puts self
42
+
43
+ branch = current_branch
44
+ database = master_database
45
+
46
+ run "git config --bool branch.#{branch}.database false"
47
+ run "mysql -u root -e 'drop database #{database}_#{branch}'"
48
+ end
49
+
50
+ end
51
+
52
+ desc "branch db"
53
+ task :branch do
54
+ branch = current_branch
55
+ database = master_database
56
+
57
+ run "git config --bool branch.#{branch}.database true"
58
+ run "rake db:create"
59
+ run "mysqldump -u root #{database} | mysql -u root #{database}_#{branch}"
60
+ end
61
+
62
+
63
+ end
@@ -0,0 +1,14 @@
1
+ require "tb_branch_db/version"
2
+ require "tb_branch_db/rake_helper"
3
+ require "rails"
4
+
5
+ module TbBranchDb
6
+ class BranchDBTask < Rails::Railtie
7
+ railtie_name :tb_branch_db
8
+
9
+ rake_tasks do
10
+ Dir[File.join(File.dirname(__FILE__),'tasks/*.rake')].each { |f| load f }
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,26 @@
1
+ module TbBranchDb
2
+ module RakeHelper
3
+
4
+ def load_database_config
5
+ config = File.read("config/database.yml")
6
+ config = ERB.new(config).result
7
+ YAML.load(config)
8
+ end
9
+
10
+ def current_branch
11
+ `git symbolic-ref HEAD 2>/dev/null`.chomp.sub('refs/heads/', '')
12
+ end
13
+
14
+ def master_database
15
+ branch = current_branch
16
+ database = load_database_config[Rails.env]['database']
17
+ database.sub!("_#{branch}", '')
18
+ database
19
+ end
20
+
21
+ def run(cmd)
22
+ sh cmd
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module TbBranchDb
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "tb_branch_db/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "tb_branch_db"
7
+ s.version = TbBranchDb::VERSION
8
+ s.authors = ["Thomas Baustert"]
9
+ s.email = ["business@thomasbaustert.de"]
10
+ s.homepage = ""
11
+ s.summary = %q{Rake tasks to branch a database}
12
+ s.description = %q{Rake tasks to branch a database}
13
+
14
+ s.rubyforge_project = "tb_branch_db"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tb_branch_db
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Thomas Baustert
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-03-04 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Rake tasks to branch a database
22
+ email:
23
+ - business@thomasbaustert.de
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - README.rdoc
34
+ - Rakefile
35
+ - lib/tasks/tb_branch_db.rake
36
+ - lib/tb_branch_db.rb
37
+ - lib/tb_branch_db/rake_helper.rb
38
+ - lib/tb_branch_db/version.rb
39
+ - tb_branch_db.gemspec
40
+ homepage: ""
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project: tb_branch_db
69
+ rubygems_version: 1.8.17
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Rake tasks to branch a database
73
+ test_files: []
74
+