Class: DbVcs::Manager
- Inherits:
-
Object
- Object
- DbVcs::Manager
- Defined in:
- lib/db_vcs/manager.rb
Constant Summary collapse
- ADAPTERS =
{ "postgres" => DbVcs::Adapters::Postgres, "mongo" => DbVcs::Adapters::Mongo, "mysql" => DbVcs::Adapters::Mysql }.freeze
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#adapter_name ⇒ Object
readonly
Returns the value of attribute adapter_name.
Class Method Summary collapse
-
.copy_for_all_envs(target:, source: DbVcs.config.main_branch) ⇒ void
Creates databases for target branch from source branch for all setup environments for all setup databases.
-
.drop_for_all_envs(branch_name) ⇒ Object
Drops databases of given branch name for all environments for all setup databases.
- .get_adapter_by_name(adapter_name) ⇒ Class<DbVcs::Adapters::Postgres>, Class<DbVcs::Adapters::Mongo>
Instance Method Summary collapse
- #copy(target:, source:) ⇒ void
- #create(db_name) ⇒ void
- #drop(db_name) ⇒ void
-
#failure(text) ⇒ Object
Void.
-
#initialize(adapter_name) ⇒ Manager
constructor
A new instance of Manager.
-
#message(text) ⇒ Object
Void.
-
#regular(text) ⇒ Object
Void.
-
#success(text) ⇒ Object
Void.
Constructor Details
#initialize(adapter_name) ⇒ Manager
Returns a new instance of Manager.
47 48 49 50 |
# File 'lib/db_vcs/manager.rb', line 47 def initialize(adapter_name) @adapter_name = adapter_name @adapter = self.class.get_adapter_by_name(adapter_name) end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
44 45 46 |
# File 'lib/db_vcs/manager.rb', line 44 def adapter @adapter end |
#adapter_name ⇒ Object (readonly)
Returns the value of attribute adapter_name.
44 45 46 |
# File 'lib/db_vcs/manager.rb', line 44 def adapter_name @adapter_name end |
Class Method Details
.copy_for_all_envs(target:, source: DbVcs.config.main_branch) ⇒ void
This method returns an undefined value.
Creates databases for target branch from source branch for all setup environments for all setup databases
23 24 25 26 27 28 29 30 |
# File 'lib/db_vcs/manager.rb', line 23 def copy_for_all_envs(target:, source: DbVcs.config.main_branch) DbVcs.config.dbs_in_use.each do |adapter_name| inst = new(adapter_name) DbVcs.config.environments.each do |env| inst.copy(target: DbVcs::Utils.db_name(env, target), source: DbVcs::Utils.db_name(env, source)) end end end |
.drop_for_all_envs(branch_name) ⇒ Object
Drops databases of given branch name for all environments for all setup databases
34 35 36 37 38 39 40 41 |
# File 'lib/db_vcs/manager.rb', line 34 def drop_for_all_envs(branch_name) DbVcs.config.dbs_in_use.each do |adapter_name| inst = new(adapter_name) DbVcs.config.environments.each do |env| inst.drop(DbVcs::Utils.db_name(env, branch_name)) end end end |
.get_adapter_by_name(adapter_name) ⇒ Class<DbVcs::Adapters::Postgres>, Class<DbVcs::Adapters::Mongo>
15 16 17 |
# File 'lib/db_vcs/manager.rb', line 15 def get_adapter_by_name(adapter_name) ADAPTERS[adapter_name] || raise(NotImplementedError, "No adapter for `#{adapter_name}' is implemented.") end |
Instance Method Details
#copy(target:, source:) ⇒ void
This method returns an undefined value.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/db_vcs/manager.rb', line 55 def copy(target:, source:) unless adapter.db_exists?(source) return failure "#{source}' doesn't exist" end if adapter.db_exists?(target) return failure "#{target} already exists" end success "Copying #{source} -> #{target}" adapter.copy_database(target, source) end |
#create(db_name) ⇒ void
This method returns an undefined value.
78 79 80 81 82 83 84 |
# File 'lib/db_vcs/manager.rb', line 78 def create(db_name) if adapter.db_exists?(db_name) return failure "#{db_name}' already exist" end adapter.create_database(db_name) success "Database #{db_name} created successfully" end |
#drop(db_name) ⇒ void
This method returns an undefined value.
68 69 70 71 72 73 74 |
# File 'lib/db_vcs/manager.rb', line 68 def drop(db_name) unless adapter.db_exists?(db_name) return failure "#{db_name}' doesn't exist" end adapter.drop_by_dbname(db_name) success "Database #{db_name} was dropped successfully" end |
#failure(text) ⇒ Object
Returns void.
100 101 102 |
# File 'lib/db_vcs/manager.rb', line 100 def failure(text) puts "\e[1m\e[31m#{(text)}\e[0m\e[0m" end |
#message(text) ⇒ Object
Returns void.
88 89 90 |
# File 'lib/db_vcs/manager.rb', line 88 def (text) "#{adapter_name.capitalize} :: #{text}" end |
#regular(text) ⇒ Object
Returns void.
106 107 108 |
# File 'lib/db_vcs/manager.rb', line 106 def regular(text) puts (text) end |
#success(text) ⇒ Object
Returns void.
94 95 96 |
# File 'lib/db_vcs/manager.rb', line 94 def success(text) puts "\e[1m\e[32m#{(text)}\e[0m\e[0m" end |