branchbot 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0fa6c1ef194f2f10f54ac1a874d0ef1971e585f2ff9b2c8aefe958178fca3d54
4
- data.tar.gz: fbfc9ffc56df62c39152ece3a5ca4ba28334bf857e9b42fdb4c19c65c08a6707
3
+ metadata.gz: df7287ee16f259b9e6a70200cc292c27fe9cdeb0993625e5aad7d9cc0faee1cf
4
+ data.tar.gz: 12c488ca1233cce3b9f9b8006df0f83acad249b9a7484bc80fa2f18fcdd48c45
5
5
  SHA512:
6
- metadata.gz: 2e3b4baf23cd8108b3d096a91d946c9e0b8f175374b83103024514416ce0cb255e3e2b991ce82d6a2fb5112efe972c9891225e6294dcf7ee3f2a61f1308fc5e5
7
- data.tar.gz: 263700bdbfa46eba2d124a21df567946bfed5209676ac1c45fa6d2612bb61782e44a76413e92ebce61446da03837e6352870f8716eea3058e8a10a41ee036ed1
6
+ metadata.gz: 8fa0b95f0774aa1aefc64c0a9bfbfe595a5391c42de155e70c4dbac71ca6ebf83d924fe0c2acf6c1520759d38e330ad2685d14a5fda7dc17cfd748ac8fbdb2d7
7
+ data.tar.gz: b819e74822ce02f1e0a79289501f53d2c00fed89aed1f03862d237f22a38e52b0d1065f2d3e3f7856d1b088538ff2e4a0289671483ac1a40d616017e8c5c98ea
data/README.md CHANGED
@@ -16,6 +16,11 @@ This allows you to make some changes to the structure of your database on a feat
16
16
 
17
17
  Note: Be sure to keep the `post-checkout` name to match Git's expectations.
18
18
 
19
+ # Options
20
+
21
+ - `--app-root` - The app root relative to the project root (default: '.')
22
+ - `--db-config-erb` - Interpret database.yml as ERB an template before parsing as YML (default: disabled)
23
+
19
24
  # Support
20
25
 
21
26
  Only PostgreSQL and MySQL databases are currently supported.
@@ -24,7 +29,7 @@ Only PostgreSQL and MySQL databases are currently supported.
24
29
 
25
30
  While this hook is geared towards Rails and depends on Ruby, it is very easy to use it in non-Ruby/Rails projects, so long as you have Ruby installed on your system.
26
31
 
27
- This script will look for a `database.yml` config file (default: `config/database.yml` in your project's root directory), and expects it to look like this:
32
+ This script will look for a `config/database.yml` file in your app's root (configurable via `--app-root`), and expects it to look like this:
28
33
 
29
34
  ```yaml
30
35
  development:
@@ -34,6 +39,4 @@ development:
34
39
  database: <database name>
35
40
  ```
36
41
 
37
- The path to this file is configurable via the `--db-config-path` option.
38
-
39
42
  We currently support two adapters: `mysql2` and `postgresql`. We don't actually rely on these gems, but instead use them to determine which database's command line tools we should use (mysqldump/mysql or pg_dump/psql). Use `mysql2` if you are using a MySQL database, or `postgresql` if you are using a PostgreSQL database.
data/exe/branchbot CHANGED
@@ -11,7 +11,8 @@ program :description, "Saves and restores the state of your local database as yo
11
11
  command :switch do |cmd|
12
12
  cmd.syntax = "branchbot switch [options]"
13
13
  cmd.option '--from FROM_REF', String, 'ref switching from'
14
- cmd.option '--db-config-path path/to/database.yml', String, 'path to database.yml relative to project root (default: config/database.yml)'
14
+ cmd.option '--app-root relative/path/to/app', String, 'path to app root relative to project root (default: .)'
15
+ cmd.option '--db-config-erb', 'Interpret the database config as ERB before parsing YML'
15
16
 
16
17
  cmd.action do |args, options|
17
18
  unless options.from
@@ -19,10 +20,12 @@ command :switch do |cmd|
19
20
  next
20
21
  end
21
22
 
22
- options.default db_config_path: 'config/database.yml'
23
+ options.default app_root: '.'
24
+ options.default db_config_erb: false
23
25
 
24
26
  Branchbot::BranchSwitcher.new(
25
- database_yml_path: options.db_config_path
27
+ app_root: options.app_root,
28
+ erb_in_database_yml: options.db_config_erb
26
29
  ).switch_from(options.from)
27
30
  end
28
31
  end
@@ -1,7 +1,10 @@
1
1
  module Branchbot
2
2
  class BranchSwitcher
3
- def initialize(database_yml_path: 'config/database.yml')
4
- @database_yml_path = database_yml_path
3
+ def initialize(app_root: '.', erb_in_database_yml: true)
4
+ @app_root = app_root
5
+ @project_root = %x[git rev-parse --show-toplevel].strip
6
+ @dump_folder = "#{@project_root}/.db_branch_dumps"
7
+ @erb_in_database_yml = erb_in_database_yml
5
8
  end
6
9
 
7
10
  def switch_from(prev_ref)
@@ -13,11 +16,8 @@ module Branchbot
13
16
  # destination branch, so we can remove that immediately.
14
17
  @source_branches = branches_from_refhead(prev_ref).reject{ |b| b == @destination_branch }
15
18
 
16
- @project_root = %x[git rev-parse --show-toplevel].strip
17
- @dump_folder = "#{@project_root}/.db_branch_dumps"
18
-
19
19
  # Load Rails DB config and grab database name
20
- @rails_db_config = YAML.load(ERB.new(File.read(File.join @project_root, @database_yml_path)).result)
20
+ @rails_db_config = YAML.load(db_config_yml)
21
21
  dev_database_name = @rails_db_config['development']['database']
22
22
 
23
23
  begin
@@ -57,24 +57,42 @@ module Branchbot
57
57
  `git show-ref --heads | grep #{ref} | awk '{print $2}'`.split("\n").map{ |b| b.sub(/^refs\/heads\//, '') }
58
58
  end
59
59
 
60
+ def app_root
61
+ File.join @project_root, @app_root
62
+ end
63
+
60
64
  def prepare_test_database
61
- if File.exists?("#{@project_root}/Rakefile")
65
+ if File.exists?("#{app_root}/Rakefile")
62
66
  print "Preparing test database..."
63
67
 
64
68
  rake_cmd = "rake db:test:prepare"
65
69
 
66
- if File.exists?("#{@project_root}/bin/rake")
70
+ if File.exists?("#{app_root}/bin/rake")
67
71
  rake_cmd = "./bin/#{rake_cmd}"
68
- elsif File.exists?("#{@project_root}/Gemfile")
72
+ elsif File.exists?("#{app_root}/Gemfile")
69
73
  rake_cmd = "bundle exec #{rake_cmd}"
70
74
  end
71
75
 
72
- system rake_cmd
76
+ system "cd #{app_root} && #{rake_cmd}"
73
77
 
74
78
  print "done!\n"
75
79
  else
76
80
  print "No Rakefile detected, skipping test database restoration\n"
77
81
  end
78
82
  end
83
+
84
+ def db_config_path
85
+ File.join app_root, 'config', 'database.yml'
86
+ end
87
+
88
+ def db_config_yml
89
+ database_yml_content = File.read(db_config_path)
90
+
91
+ if @erb_in_database_yml
92
+ ERB.new(database_yml_content).result
93
+ else
94
+ database_yml_content
95
+ end
96
+ end
79
97
  end
80
- end
98
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Branchbot
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: branchbot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Ryan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-04 00:00:00.000000000 Z
11
+ date: 2021-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: commander