Class: Worktree::DbManager
- Inherits:
-
Object
- Object
- Worktree::DbManager
- Defined in:
- lib/worktree/db_manager.rb
Instance Attribute Summary collapse
-
#spec ⇒ Object
readonly
Returns the value of attribute spec.
Instance Method Summary collapse
- #createdb!(db_name) ⇒ Object
- #db_host ⇒ Object
- #db_port ⇒ Object
- #dropdb! ⇒ Object
- #environment_spec ⇒ Object
-
#initialize(spec_file, environment = 'development') ⇒ DbManager
constructor
A new instance of DbManager.
- #multi? ⇒ Boolean
- #template ⇒ Object
Constructor Details
#initialize(spec_file, environment = 'development') ⇒ DbManager
Returns a new instance of DbManager.
9 10 11 12 |
# File 'lib/worktree/db_manager.rb', line 9 def initialize(spec_file, environment = 'development') @spec = YAML.load_file(spec_file) @environment = environment end |
Instance Attribute Details
#spec ⇒ Object (readonly)
Returns the value of attribute spec.
7 8 9 |
# File 'lib/worktree/db_manager.rb', line 7 def spec @spec end |
Instance Method Details
#createdb!(db_name) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/worktree/db_manager.rb', line 46 def createdb!(db_name) cmd = if db_port "createdb -h #{db_host} -p #{db_port} -T #{template} #{db_name}" else "createdb -h #{db_host} -T #{template} #{db_name}" end Worktree.run_command cmd end |
#db_host ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/worktree/db_manager.rb', line 30 def db_host if multi? environment_spec.dig('primary', 'host') else environment_spec['host'] end end |
#db_port ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/worktree/db_manager.rb', line 22 def db_port if multi? environment_spec.dig('primary', 'port') else environment_spec['port'] end end |
#dropdb! ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/worktree/db_manager.rb', line 55 def dropdb! cmd = if db_port "dropdb -h #{db_host} -p #{db_port} #{template}" else "dropdb -h #{db_host} #{template}" end Worktree.run_command cmd end |
#environment_spec ⇒ Object
14 15 16 |
# File 'lib/worktree/db_manager.rb', line 14 def environment_spec @spec.fetch(@environment, {}) end |
#multi? ⇒ Boolean
18 19 20 |
# File 'lib/worktree/db_manager.rb', line 18 def multi? environment_spec.key? 'primary' end |
#template ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/worktree/db_manager.rb', line 38 def template if multi? environment_spec.dig('primary', 'database') else environment_spec['database'] end end |