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.



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

def initialize
  @params = Redmine::Installer::ConfigParams.new
  @params.add('database')
  @params.add('host').default('localhost')
  @params.add('username')
  @params.add('password')
  @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
# File 'lib/redmine-installer/plugins/database.rb', line 37

def self.restore_all(redmine_root, backup_dir)
  load_all(redmine_root).each do |klass|
    klass.restore(backup_dir)
  end
end

Instance Method Details

#backup(dir) ⇒ Object



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

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.



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

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



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

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



109
110
111
# File 'lib/redmine-installer/plugins/database.rb', line 109

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

#load(data) ⇒ Object

Load paramaters for connection



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

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



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

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



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

def restore(dir)
  file = file_for_backup(dir)

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

  Kernel.system(command_for_restore(file))
end