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_port ⇒ Object
- #dropdb! ⇒ Object
- #environment_spec ⇒ Object
-
#initialize(config_file, environment = 'development') ⇒ DbManager
constructor
A new instance of DbManager.
- #multi? ⇒ Boolean
- #template ⇒ Object
Constructor Details
#initialize(config_file, environment = 'development') ⇒ DbManager
Returns a new instance of DbManager.
9 10 11 12 |
# File 'lib/worktree/db_manager.rb', line 9 def initialize(config_file, environment = 'development') @spec = YAML.load_file(config_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
38 39 40 41 42 43 44 45 |
# File 'lib/worktree/db_manager.rb', line 38 def createdb!(db_name) cmd = if db_port "createdb -h localhost -p #{db_port} -T #{template} #{db_name}" else "createdb -h localhost -T #{template} #{db_name}" end Worktree.run_command cmd 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
47 48 49 50 51 52 53 54 |
# File 'lib/worktree/db_manager.rb', line 47 def dropdb! cmd = if db_port "dropdb -h localhost -p #{db_port} #{template}" else "dropdb -h localhost #{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
30 31 32 33 34 35 36 |
# File 'lib/worktree/db_manager.rb', line 30 def template if multi? environment_spec.dig('primary', 'database') else environment_spec['database'] end end |