Class: RedmineInstaller::Database::Base

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

Direct Known Subclasses

MySQL, PostgreSQL

Constant Summary

Constants included from Utils

Utils::PROGRESSBAR_FORMAT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#class_name, #create_dir, #env_user, #error, #logger, #ok, #pastel, #print_title, #prompt, #run_command

Constructor Details

#initialize(redmine) ⇒ Base

Returns a new instance of Base.



50
51
52
# File 'lib/redmine-installer/database.rb', line 50

def initialize(redmine)
  @redmine = redmine
end

Instance Attribute Details

#backupObject (readonly)

Returns the value of attribute backup.



48
49
50
# File 'lib/redmine-installer/database.rb', line 48

def backup
  @backup
end

Instance Method Details

#backuped?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/redmine-installer/database.rb', line 54

def backuped?
  @backup && File.exist?(@backup)
end

#buildObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/redmine-installer/database.rb', line 100

def build
  data = {}
  data['adapter'] = adapter_name
  data['database'] = @database
  data['username'] = @username if @username.present?
  data['password'] = @password if @password.present?
  data['encoding'] = @encoding
  data['host'] = @host
  data['port'] = @port

  {
    'production' => data,
    'development' => data
  }
end

#do_restore(file) ⇒ Object

Recreate database should be done in 2 commands because of postgre’s ‘–command’ options which can do only 1 operations. Otherwise result is unpredictable.



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

def do_restore(file)
  puts 'Database cleaning'
  Kernel.system drop_database_command
  Kernel.system create_database_command

  puts 'Database restoring'
  Kernel.system restore_command(file)
end

#get_parametersObject



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

def get_parameters
  @database = prompt.ask('Database:', required: true)
  @host = prompt.ask('Host:', default: 'localhost', required: true)
  @username = prompt.ask('Username:', default: '')
  @password = prompt.mask('Password:', default: '')
  @encoding = prompt.ask('Encoding:', default: 'utf8mb4', required: true)
  @port = prompt.ask('Port:', default: default_port, convert: lambda(&:to_i), required: true)
end

#make_backup(dir) ⇒ Object



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

def make_backup(dir)
  puts 'Database backuping'
  @backup = File.join(dir, "#{@database}.sql")
  Kernel.system backup_command(@backup)
end

#make_configObject



76
77
78
79
80
# File 'lib/redmine-installer/database.rb', line 76

def make_config
  File.open(@redmine.database_yml_path, 'w') do |f|
    f.puts(YAML.dump(build))
  end
end

#set_paramaters(definition) ⇒ Object



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

def set_paramaters(definition)
  @database = definition['database']
  @username = definition['username']
  @password = definition['password']
  @encoding = definition['encoding']
  @host = definition['host']
  @port = definition['port']
end

#to_sObject



116
117
118
# File 'lib/redmine-installer/database.rb', line 116

def to_s
  "<#{class_name} #{@username}@#{@host}:#{@port} (#{@encoding})>"
end