Class: TaliaUtil::Configuration::MysqlDatabaseSetup

Inherits:
Object
  • Object
show all
Defined in:
lib/talia_util/configuration/mysql_database_setup.rb

Overview

This contains some methods to set mysql databases on a production system from scratch. Setting the root password will apply immediately, the SQL operations will be cached and applied in one go.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMysqlDatabaseSetup

Returns a new instance of MysqlDatabaseSetup.



11
12
13
# File 'lib/talia_util/configuration/mysql_database_setup.rb', line 11

def initialize
  @sql_statements = []
end

Instance Attribute Details

#app_nameObject

Returns the value of attribute app_name.



16
17
18
# File 'lib/talia_util/configuration/mysql_database_setup.rb', line 16

def app_name
  @app_name
end

#db_prefixObject

Prefix for having multiple databases on one install



18
19
20
# File 'lib/talia_util/configuration/mysql_database_setup.rb', line 18

def db_prefix
  @db_prefix
end

#hostObject

Returns the value of attribute host.



15
16
17
# File 'lib/talia_util/configuration/mysql_database_setup.rb', line 15

def host
  @host
end

#sockObject

Returns the value of attribute sock.



17
18
19
# File 'lib/talia_util/configuration/mysql_database_setup.rb', line 17

def sock
  @sock
end

Instance Method Details

#assign_root_pw(new_root_pw) ⇒ Object

Assign a new root password



34
35
36
37
38
# File 'lib/talia_util/configuration/mysql_database_setup.rb', line 34

def assign_root_pw(new_root_pw)
  success = mysqladmin("password #{new_root_pw}")
  @root_pw = new_root_pw if(success)
  success
end

#create_database(database) ⇒ Object

Create the given database and set the permssions on it

Raises:

  • (ArgumentError)


41
42
43
44
45
# File 'lib/talia_util/configuration/mysql_database_setup.rb', line 41

def create_database(database)
  raise(ArgumentError, "Credentials incomplete") unless(@rails_user && @rails_pw)
  @sql_statements << "CREATE DATABASE #{database};"
  @sql_statements << "GRANT ALL ON #{database}.* TO '#{@rails_user}'@'#{@host || 'localhost'}' IDENTIFIED BY '#{@rails_pw}'"
end

#create_default_databasesObject

Creates the default databases for the application. You can call back a block for each db to do something depending on the success of the operation

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
# File 'lib/talia_util/configuration/mysql_database_setup.rb', line 50

def create_default_databases
  raise(ArgumentError, "App name not set") unless(@app_name)
  %w(production test development).each do |db_suffix|
    db_name = "#{db_prefix}#{@app_name}_#{db_suffix}"
    create_database(db_name)
  end
end

#executeObject

Executes all stored statements



59
60
61
62
63
64
65
66
67
# File 'lib/talia_util/configuration/mysql_database_setup.rb', line 59

def execute
  execute_as_root do |connection|
    connection.transaction do
      @sql_statements.each do |statement|
          connection.execute(statement)
      end
    end
  end
end

#rails_credentials(rails_user, rails_pw) ⇒ Object

Set the normal user/pw



27
28
29
30
# File 'lib/talia_util/configuration/mysql_database_setup.rb', line 27

def rails_credentials(rails_user, rails_pw)
  @rails_user = rails_user
  @rails_pw = rails_pw
end

#root_credentials(root_user = 'root', root_pw = nil) ⇒ Object

Set the root user/pw



21
22
23
24
# File 'lib/talia_util/configuration/mysql_database_setup.rb', line 21

def root_credentials(root_user = 'root', root_pw = nil)
  @root_user = root_user
  @root_pw = root_pw
end