Class: DbVcs::Manager

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter_name) ⇒ Manager

Returns a new instance of Manager.

Parameters:

  • adapter_name (String)


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

#adapterObject (readonly)

Returns the value of attribute adapter.



44
45
46
# File 'lib/db_vcs/manager.rb', line 44

def adapter
  @adapter
end

#adapter_nameObject (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

Parameters:

  • :target (String)

    target branch name

  • :source (String)

    a branch name to copy the db from



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

Parameters:

  • branch_name (String)


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>

Parameters:

  • adapter_name (String)

Returns:

Raises:

  • (NotImplementedError)

    in case if no adapter is found



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.

Parameters:

  • :target (String)

    new database name

  • :source (String)

    database name to create a new db from



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.

Parameters:

  • db_name (String)


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.

Parameters:

  • db_name (String)


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.

Parameters:

  • text (String, nil)

Returns:

  • void



100
101
102
# File 'lib/db_vcs/manager.rb', line 100

def failure(text)
  puts "\e[1m\e[31m#{message(text)}\e[0m\e[0m"
end

#message(text) ⇒ Object

Returns void.

Parameters:

  • text (String, nil)

Returns:

  • void



88
89
90
# File 'lib/db_vcs/manager.rb', line 88

def message(text)
  "#{adapter_name.capitalize} :: #{text}"
end

#regular(text) ⇒ Object

Returns void.

Parameters:

  • text (String, nil)

Returns:

  • void



106
107
108
# File 'lib/db_vcs/manager.rb', line 106

def regular(text)
  puts message(text)
end

#success(text) ⇒ Object

Returns void.

Parameters:

  • text (String, nil)

Returns:

  • void



94
95
96
# File 'lib/db_vcs/manager.rb', line 94

def success(text)
  puts "\e[1m\e[32m#{message(text)}\e[0m\e[0m"
end