Class: Redmine::Installer::Plugin::Database

Inherits:
Base
  • Object
show all
Defined in:
lib/redmine-installer/plugins/database.rb

Direct Known Subclasses

MySQL, PostgreSQL

Defined Under Namespace

Classes: MySQL, PostgreSQL

Constant Summary collapse

DATABASE_YML_PATH =
'config/database.yml'
DATABASE_BACKUP_DIR =
'__database'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

all, inherited, title

Methods included from Utils

included

Constructor Details

#initializeDatabase

Returns a new instance of Database.



45
46
47
48
49
50
51
52
# File 'lib/redmine-installer/plugins/database.rb', line 45

def initialize
  @params = Redmine::Installer::ConfigParams.new
  @params.add('database')
  @params.add('host').default('localhost')
  @params.add('username')
  @params.add('password').hide(true)
  @params.add('encoding').default('utf8')
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



9
10
11
# File 'lib/redmine-installer/plugins/database.rb', line 9

def params
  @params
end

Class Method Details

.backup_all(redmine_root, backup_dir) ⇒ Object



31
32
33
34
35
# File 'lib/redmine-installer/plugins/database.rb', line 31

def self.backup_all(redmine_root, backup_dir)
  load_all(redmine_root).each do |klass|
    klass.backup(backup_dir)
  end
end

.load_all(redmine_root) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/redmine-installer/plugins/database.rb', line 11

def self.load_all(redmine_root)
  database_file = File.join(redmine_root, DATABASE_YML_PATH)
  return [] unless File.exist?(database_file)

  to_return = []
  definitions = YAML.load_file(database_file)
  definitions.each do |name, data|

    klass = all.detect{|klass| klass.adapter_name == data['adapter']}

    next if klass.nil?

    klass = klass.new
    klass.load(data)

    to_return << klass
  end
  to_return
end

.restore_all(redmine_root, backup_dir) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/redmine-installer/plugins/database.rb', line 37

def self.restore_all(redmine_root, backup_dir)
  databases = load_all(redmine_root)
  databases.uniq! {|d| d.params['database'].value}
  databases.each do |klass|
    klass.restore(backup_dir)
  end
end

Instance Method Details

#backup(dir) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/redmine-installer/plugins/database.rb', line 92

def backup(dir)
  file = file_for_backup(dir)

  # More enviroments can use the same database
  return if File.exist?(file)

  Kernel.system(command_for_backup(file))
end

#buildObject

Transform ConfigParams into rails database.yml structure. Method creates production and developemtn environemnt with the same parameters.



57
58
59
60
61
62
63
64
65
# File 'lib/redmine-installer/plugins/database.rb', line 57

def build
  data = Hash[@params.map{|p| [p.name, p.value]}]
  data['adapter'] = self.class.adapter_name
  data = {
    'production' => data,
    'development' => data,
  }
  data
end

#file_for_backup(dir) ⇒ Object



87
88
89
90
# File 'lib/redmine-installer/plugins/database.rb', line 87

def file_for_backup(dir)
  FileUtils.mkdir_p(File.join(dir, DATABASE_BACKUP_DIR))
  File.join(dir, DATABASE_BACKUP_DIR, "#{self.class.adapter_name}.#{params['database'].value}.dump")
end

#get(name) ⇒ Object

Get valu from param



112
113
114
# File 'lib/redmine-installer/plugins/database.rb', line 112

def get(name)
  params[name].value
end

#load(data) ⇒ Object

Load paramaters for connection



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/redmine-installer/plugins/database.rb', line 68

def load(data)
  data.each do |name, value|
    # Get param
    param = @params[name]

    # Unsupported key or unnecessary parameter
    next if param.nil?

    # Save value
    param.value = value
  end
end

#make_config(redmine_root) ⇒ Object



81
82
83
84
85
# File 'lib/redmine-installer/plugins/database.rb', line 81

def make_config(redmine_root)
  File.open(File.join(redmine_root, DATABASE_YML_PATH), 'w') do |f|
    f.puts(YAML.dump(build))
  end
end

#restore(dir) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/redmine-installer/plugins/database.rb', line 101

def restore(dir)
  file = file_for_backup(dir)

  # More enviroments can use the same database
  return unless File.exist?(file)

  Kernel.system(command_for_empty)
  Kernel.system(command_for_restore(file))
end