base-branch 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8e2264402672cf79639cafc4a71581fe5ed1a064
4
+ data.tar.gz: cf31201887d4aae2b7603e5256baa513641591b3
5
+ SHA512:
6
+ metadata.gz: 3b4be4f7bb367833a7f6840ea5791f656b38d2f447eab6d1659f0dd9aa03e6fb842686753c4722d6c4a84a032862adeb1a8b049825a89b5480b6a8ed95842fa8
7
+ data.tar.gz: 1362ccf87e9fa92f96dd26c019805991023cb2287862830eaec57249d5c46ca4f99d113a7ca4836d4a31f456faf4f3c52d2fcb647782c538ef01aa5f595e991b
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea
11
+
12
+ /test/tmp/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 TODO: Write your name
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # BaseBranch
2
+
3
+ A simple gem to which automatically manages unique databases across code repository branches for Rails projects.
4
+
5
+ ## Description
6
+
7
+ This gem is designed for people who actively work across multiple branches for Rails projects. Quite often, each of these branches come with a set of database migrations that are not finalized.
8
+ Base Branch attempts to solve this issue by creating unique databases namespaced by the feature branch names and cloning the data in from the default database.
9
+
10
+ Using BaseBranch, it is assured that
11
+
12
+ 1. Feature Branches have a clean environment to work from.
13
+ 2. The default database tied to master branch will not suffer from migration artifacts from unmerged feature branches.
14
+ 3. Developers can drop and recreate databases used for feature branches with a peace of mind :)
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'base-branch', group: :development
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ $ bundle
27
+
28
+ Or install it yourself as:
29
+
30
+ $ gem install base-branch
31
+
32
+ After installation, run:
33
+
34
+ $ rails g base_branch:install
35
+
36
+ to complete the configuration process.
37
+
38
+ ## Usage
39
+
40
+ ### When creating branches:
41
+
42
+ Run:
43
+
44
+ $ rake base_branch:create_branch
45
+
46
+ to create a branch specific database. If branch specific database is not present, the default database will be used when the app loads.
47
+
48
+ You may have to run a `spring stop` afterwards if you are using the `spring` gem in order to load the correct database.
49
+
50
+ ## To-dos
51
+
52
+ 1. Create a task to check against merged feature branches and clean up the corresponding databases
53
+
54
+ ## Development
55
+
56
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
57
+
58
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
59
+
60
+ ## Contributing
61
+
62
+ Bug reports and pull requests are welcome on GitHub at https://github.com/KernCheh/base-branch. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
63
+
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
68
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :spec
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'base_branch/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'base-branch'
8
+ spec.version = BaseBranch::VERSION
9
+ spec.authors = ['Sim Kern Cheh']
10
+ spec.email = ['kern.cheh@gmail.com']
11
+
12
+ spec.summary = %q{Create and clone databases easily for your feature branches in Rails projects.}
13
+ spec.description = %q{Create and clone databases easily for your feature branches in Rails projects.}
14
+ spec.homepage = 'https://github.com/KernCheh/base-branch'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'rails', '>= 4.2'
26
+ spec.add_development_dependency 'bundler', '~> 1.11'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'minitest', '~> 5.0'
29
+
30
+ spec.add_development_dependency 'rr', '~> 1.1'
31
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'base-branch'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,14 @@
1
+ require 'base_branch/version'
2
+
3
+ module BaseBranch
4
+ end
5
+
6
+ require 'base_branch/git_branch'
7
+ require 'base_branch/base_branch_exceptions/default_database_unset'
8
+ require 'base_branch/base_branch_exceptions/uninitialized_repository'
9
+ require 'base_branch/database/active_database'
10
+ require 'base_branch/database/adapter/base'
11
+ require 'base_branch/database/adapter/postgresql'
12
+
13
+ # Railtie
14
+ require 'base_branch/railtie' if defined?(Rails)
@@ -0,0 +1,4 @@
1
+ module BaseBranch::BaseBranchExceptions
2
+ class DefaultDatabaseUnset < StandardError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module BaseBranch::BaseBranchExceptions
2
+ class UninitializedRepository < StandardError
3
+ end
4
+ end
@@ -0,0 +1,80 @@
1
+ require 'yaml'
2
+ require 'active_support/inflector'
3
+
4
+ module BaseBranch::Database
5
+ class ActiveDatabase
6
+ class << self
7
+ def database_name
8
+ branch_specific_db || default_db_name
9
+ end
10
+
11
+ def default_db_name
12
+ @_default_db_name ||= begin
13
+ YAML::load_file(File.join(Rails.root, 'config', 'base_branch.yml'))['default_db']
14
+ rescue Errno::ENOENT => e
15
+ warn 'Please run `rails g base_branch:install`'
16
+ nil
17
+ end
18
+ end
19
+
20
+ def branch_specific_db
21
+ db_name = branch_specific_db_name
22
+ branch_db_present?(db_name) ? db_name : nil
23
+ end
24
+
25
+ def branch_specific_db_name
26
+ current_branch = BaseBranch::GitBranch.current_branch
27
+
28
+ unless current_branch == 'master'
29
+ "#{default_db_name}-#{current_branch.gsub('/', '_')}"
30
+ end
31
+ end
32
+
33
+ def create_branch_db
34
+ unless branch_specific_db
35
+ new_db_name = branch_specific_db_name
36
+ adapter.clone_db new_db_name, default_db_name, db_user
37
+ end
38
+
39
+ new_db_name
40
+ end
41
+
42
+ def branch_config
43
+ @_branch_config ||= begin
44
+ if File.exists?(File.join(Rails.root, '.base_branch.yml'))
45
+ YAML::load_file(File.join(Rails.root, '.base_branch.yml'))
46
+ else
47
+ {
48
+ 'branches' => []
49
+ }
50
+ end
51
+ end
52
+ end
53
+
54
+ def branch_db_present?(db_name)
55
+ return nil unless db_name
56
+
57
+ branch_config['branches'].include?(db_name)
58
+ end
59
+
60
+ def current
61
+ rails_db_config['development']['database']
62
+ end
63
+
64
+ def adapter
65
+ "BaseBranch::Database::Adapter::#{rails_db_config['development']['adapter'].classify}"
66
+ .constantize
67
+ end
68
+
69
+ def db_user
70
+ rails_db_config['development']['username']
71
+ end
72
+
73
+ def rails_db_config
74
+ Rails.configuration.database_configuration
75
+ end
76
+ end
77
+
78
+ private_class_method :branch_db_present?, :current, :adapter, :db_user, :rails_db_config
79
+ end
80
+ end
@@ -0,0 +1,19 @@
1
+ module BaseBranch::Database
2
+ module Adapter
3
+ class Base
4
+ class << self
5
+ def orm_execute(stmt)
6
+ ActiveRecord::Base.connection.execute stmt
7
+ end
8
+
9
+ def orm_sanitize(field)
10
+ ActiveRecord::Base.sanitize field
11
+ end
12
+
13
+ def orm_successfully_executed?(result)
14
+ result.result_status == 1
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,29 @@
1
+ module BaseBranch::Database::Adapter
2
+ class Postgresql < Base
3
+ class << self
4
+ def clone_db(db_name, from_db, db_user)
5
+ result = if db_user
6
+ orm_execute <<-SQL
7
+ CREATE DATABASE "#{db_name}" WITH TEMPLATE "#{from_db}" OWNER #{db_user};
8
+ SQL
9
+ else
10
+ orm_execute <<-SQL
11
+ CREATE DATABASE "#{db_name}" WITH TEMPLATE "#{from_db}";
12
+ SQL
13
+ end
14
+
15
+
16
+ orm_successfully_executed? result
17
+ end
18
+
19
+ def db_present?(db_name)
20
+ field_name = orm_sanitize db_name
21
+ result = orm_execute <<-SQL
22
+ SELECT 1 from pg_database WHERE datname=#{field_name};
23
+ SQL
24
+
25
+ result.count > 0
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,22 @@
1
+ class BaseBranch::GitBranch
2
+ class << self
3
+ def current_branch
4
+ raise(
5
+ BaseBranch::BaseBranchExceptions::UninitializedRepository,
6
+ 'Please initialize a git repository and have at least one branch.'
7
+ ) if (branchez = branches).empty?
8
+
9
+ (branchez.detect { |br| br[0, 2] == '* ' } || '').gsub('* ', '').chomp
10
+ end
11
+
12
+ def master?
13
+ current_branch == 'master'
14
+ end
15
+
16
+ def branches
17
+ `git branch -a`.split("\n")
18
+ end
19
+ end
20
+
21
+ private_class_method :branches
22
+ end
@@ -0,0 +1,19 @@
1
+ module BaseBranch
2
+ class Railtie < Rails::Railtie
3
+ railtie_name :base_branch
4
+
5
+ rake_tasks do
6
+ load 'tasks/create_branch.rake'
7
+ end
8
+
9
+ initializer 'Base Branch initializer' do
10
+ ActiveSupport.on_load(:action_controller) do
11
+ include BaseBranch
12
+ end
13
+ end
14
+
15
+ generators do
16
+ require_relative '../generators/base_branch/install_generator'
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module BaseBranch
2
+ VERSION = '0.1.1'
3
+ end
@@ -0,0 +1,45 @@
1
+ require 'rails/generators/base'
2
+
3
+ module BaseBranch
4
+ class InstallGenerator < Rails::Generators::Base
5
+ class_option :default_db
6
+
7
+ desc 'This generator creates an configuration file at config/base_branch.yml'
8
+ def create_initializer_file
9
+ db_name = options[:default_db]
10
+
11
+ if !db_name || db_name.empty?
12
+ say('What is the name of your default development database? (Leave blank if unchanged from database.yml)')
13
+ db_name = ask('Database Name: ')
14
+ end
15
+
16
+ db_name = Rails.configuration.database_configuration['development']['database'] if db_name.empty?
17
+
18
+ create_file 'config/base_branch.yml', <<-YAML
19
+ # This file is automatically generated from `rails g base_branch:initializer`
20
+
21
+ default_db: '#{db_name}'
22
+ YAML
23
+
24
+ database_yml = File.join(Rails.root, 'config', 'database.yml')
25
+ database_config = YAML::load_file(database_yml)
26
+ dev_database_name = database_config['development']['database']
27
+
28
+ database_yml_lines = IO.readlines(database_yml)
29
+ database_yml_lines = database_yml_lines
30
+ .map{|s| s.gsub(dev_database_name, "<%= defined?(BaseBranch) ? BaseBranch::Database::ActiveDatabase.database_name : '#{dev_database_name}' %>")}
31
+
32
+ File.open(database_yml, 'w') do |file|
33
+ file.puts database_yml_lines
34
+ file.close
35
+ end
36
+
37
+ append_to_file '.gitignore', <<-FILE
38
+
39
+ # Do not check in base-branch files into SCM
40
+ config/base_branch.yml
41
+ .base_branch.yml
42
+ FILE
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,17 @@
1
+ namespace :base_branch do
2
+ desc 'Branch DB'
3
+ task create_branch: :environment do
4
+ puts 'Cloning DB for current branch'
5
+
6
+ config = BaseBranch::Database::ActiveDatabase.branch_config
7
+
8
+ new_branch = BaseBranch::Database::ActiveDatabase.create_branch_db
9
+ config['branches'] << new_branch
10
+
11
+ File.open(File.join(Rails.root, '.base_branch.yml'), 'w') do |f|
12
+ f.write config.to_yaml
13
+ end
14
+
15
+ puts "New database: #{new_branch}"
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: base-branch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Sim Kern Cheh
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-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: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rr
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.1'
83
+ description: Create and clone databases easily for your feature branches in Rails
84
+ projects.
85
+ email:
86
+ - kern.cheh@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - base-branch.gemspec
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/base-branch.rb
100
+ - lib/base_branch/base_branch_exceptions/default_database_unset.rb
101
+ - lib/base_branch/base_branch_exceptions/uninitialized_repository.rb
102
+ - lib/base_branch/database/active_database.rb
103
+ - lib/base_branch/database/adapter/base.rb
104
+ - lib/base_branch/database/adapter/postgresql.rb
105
+ - lib/base_branch/git_branch.rb
106
+ - lib/base_branch/railtie.rb
107
+ - lib/base_branch/version.rb
108
+ - lib/generators/base_branch/install_generator.rb
109
+ - lib/tasks/create_branch.rake
110
+ homepage: https://github.com/KernCheh/base-branch
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.4.5.1
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Create and clone databases easily for your feature branches in Rails projects.
134
+ test_files: []