Class: Specify::Configuration::DBConfig

Inherits:
Config
  • Object
show all
Defined in:
lib/specify/configuration/db_config.rb

Overview

DBConfigs are Specify:Database configurations.

Instance Attribute Summary collapse

Attributes inherited from Config

#dir_names, #hosts

Instance Method Summary collapse

Methods inherited from Config

#add_database, #add_host, empty, #saved?, #touch

Constructor Details

#initialize(host, database, file = nil) ⇒ DBConfig

Returns a new DBConfig for database on host

file: the YAML file (path) containg the configuration.



27
28
29
30
31
32
33
34
35
36
# File 'lib/specify/configuration/db_config.rb', line 27

def initialize(host, database, file = nil)
  super(file)
  @host = host
  @database = database
  @port = hosts.dig @host, :port
  @user_name = params&.dig :db_user, :name
  @user_password = params&.dig :db_user, :password
  @session_user = params&.fetch :sp_user, nil
  @saved = known? ? true : false
end

Instance Attribute Details

#databaseObject

The name of the Specify database.



8
9
10
# File 'lib/specify/configuration/db_config.rb', line 8

def database
  @database
end

#hostObject

The name of the MySQL/MariaDB host for the Specify database.



11
12
13
# File 'lib/specify/configuration/db_config.rb', line 11

def host
  @host
end

#portObject

The port for the MySQL/MariaDB server for the Specify database.



14
15
16
# File 'lib/specify/configuration/db_config.rb', line 14

def port
  @port
end

#session_userObject

An existing Specify::Model::User#name; the name of the Specify::Model::User that is logged in to #collection during a Session.



22
23
24
# File 'lib/specify/configuration/db_config.rb', line 22

def session_user
  @session_user
end

#user_nameObject

The MySQL/MariaDB user for the database. This is typically the Specify master user.



18
19
20
# File 'lib/specify/configuration/db_config.rb', line 18

def user_name
  @user_name
end

Instance Method Details

#changed_password?Boolean

Returns true if the user_password attribute differs from the :password in the #params of the YAML file.

Returns:

  • (Boolean)


55
56
57
# File 'lib/specify/configuration/db_config.rb', line 55

def changed_password?
  params[:db_user][:password] != @user_password
end

#changed_port?Boolean

Returns true if #port differs from the :port in the #params of the YAML file.

Returns:

  • (Boolean)


61
62
63
# File 'lib/specify/configuration/db_config.rb', line 61

def changed_port?
  hosts[host][:port] != port
end

#changed_session_user?Boolean

Returns true if the #session_user differs from the :so_user in the #params of the YAML file.

Returns:

  • (Boolean)


67
68
69
# File 'lib/specify/configuration/db_config.rb', line 67

def changed_session_user?
  params[:sp_user] != session_user
end

#changed_user?Boolean

Returns true if #user_name differs from the user :name in the #params of the YAML file.

Returns:

  • (Boolean)


49
50
51
# File 'lib/specify/configuration/db_config.rb', line 49

def changed_user?
  params[:db_user][:name] != user_name
end

#connectionObject

Returns the connection paramaters for the database as a Hash.



39
40
41
42
43
44
45
# File 'lib/specify/configuration/db_config.rb', line 39

def connection
  raise "#{database} on #{host} not configured" unless known?
  { host: host,
    port: port || 3306,
    user: user_name,
    password: @user_password }
end

#db_userObject

Returns a Hash with the MySQL/MariaDB user name and password.



78
79
80
# File 'lib/specify/configuration/db_config.rb', line 78

def db_user
  { name: @user_name, password: @user_password }
end

#host?Boolean

Returns true if #host is known (has been configured), false otherwise.

Returns:

  • (Boolean)


90
91
92
# File 'lib/specify/configuration/db_config.rb', line 90

def host?
  hosts[host]
end

#known?Boolean

Returns true if #database is known (has been configured), false otherwise.

Returns:

  • (Boolean)


96
97
98
# File 'lib/specify/configuration/db_config.rb', line 96

def known?
  params ? true : false
end

#paramsObject

Returns a Hash with the parameters for the #host #database from the configuration YAML file.



102
103
104
# File 'lib/specify/configuration/db_config.rb', line 102

def params
  super.dig:hosts, @host, :databases, @database
end

#saveObject

Saves the current state to the YAML file.



114
115
116
117
118
# File 'lib/specify/configuration/db_config.rb', line 114

def save
  return true if saved?
  host? ? update_host : save_new_host
  super
end

#user_password=(password) ⇒ Object

Sets the MySQL/MariaDB user_password.



133
134
135
136
# File 'lib/specify/configuration/db_config.rb', line 133

def user_password=(password)
  @user_password = password
  touch
end