Class: MysqlRakeTasks::Tasks

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql_rake_tasks/tasks.rb

Class Method Summary collapse

Class Method Details

.create_user_sql(config) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mysql_rake_tasks/tasks.rb', line 63

def self.create_user_sql(config)
  if config.nil? then
    return ""
  end

  if config['username'].nil? then
    puts 'Error code: missing username entry'
  end

  sql  = <<-SQL
    GRANT
    ALL PRIVILEGES
    ON #{config['database']}.*
    TO #{config['username']}@localhost
    IDENTIFIED BY '#{config['password']}';
  SQL
end

.create_users(args) ⇒ Object

creates user permissions for mysql database for localhost only



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mysql_rake_tasks/tasks.rb', line 35

def self.create_users(args)
  args = self.get_input(args)
  @root_user = args[:root_user]
  @pass = args[:pass]

  # create a mysql user for each listing in database.yml file
  Rails::configuration.database_configuration.each do |listing|
    begin
 @config = listing[1]
      db = Mysql2::Client.new(
           :host => 'localhost',
           :username => @root_user,
           :password => @pass,
           :socket => @config['socket'])

      sql = self.create_user_sql(@config)
      db.query sql
	  $stdout.puts "Created #{@config['username']} on #{@config['database']}\n"
    rescue Mysql2::Error => e
      $stdout.puts "Error code: #{e.errno}"
      $stdout.puts "Error message: #{e.error}"
      $stdout.puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
    ensure # disconnect from server
      db.close if db
    end
  end
end

.get_input(*args) ⇒ Object

Parses input args for username and password, if not given it will prompt the user



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mysql_rake_tasks/tasks.rb', line 15

def self.get_input(*args)
  if !args[0].nil? then
    root_user = args[0][:root_user]
    root_pass = args[0][:pass]
  end

  if root_user.nil? or root_pass.nil? then
    $stdout.puts 'mysql user:'
    root_user = $stdin.gets.chomp

	$stdout.puts 'mysql password:'
    system 'stty -echo'
    root_pass = $stdin.gets.chomp
    system 'stty echo'
  end

  {:root_user => root_user, :pass => root_pass}
end


120
121
122
123
124
125
# File 'lib/mysql_rake_tasks/tasks.rb', line 120

def self.print_header
  print_separator
  printf "| %30s | %13s | %9s | %8s | %8s |\n",
    "Table Name".ljust(30), "Rows", "Data Size", "IDX Size", "Total Size"
   print_separator
end


116
117
118
# File 'lib/mysql_rake_tasks/tasks.rb', line 116

def self.print_separator
  puts   "+--------------------------------+---------------+-----------+----------+------------+"
end

.statsObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/mysql_rake_tasks/tasks.rb', line 81

def self.stats
  config = Rails::configuration.database_configuration[Rails.env]

  begin
    dbh = Mysql2::Client.new( :host => config['host'], :username => config['username'], :password => config['password'])
    sql = stats_query(config['database'])
    result = dbh.query sql

    print_header
    db_total = 0
    result.each  do |row|
      printf "| %30s | %13s | %9s | %8s | %10s |\n",
        row["table_name"].ljust(30),
        number_to_human(row["rows"]).rjust(13),
        number_to_human_size(row["data"]),
        number_to_human_size(row["idx"]),
        number_to_human_size(row["total_size"])

	  db_total += row["total_size"].to_i
    end

    print_separator
    printf "|%70s | %10s |\n",'',  number_to_human_size(db_total)
    print_separator
    puts "Database: #{config['database']}  MySQL Server Version: #{dbh.info[:version]}\n"
    puts " "
  rescue Mysql2::Error => e
    puts "Error code: #{e.errno}"
    puts "Error message: #{e.error}"
    puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
  ensure
    dbh.close if dbh
  end
end

.stats_query(db_name) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/mysql_rake_tasks/tasks.rb', line 127

def self.stats_query(db_name)
  sql = <<-SQL
      SELECT table_name,
      concat(table_rows) rows,
      concat(data_length) data,
      concat(index_length) idx,
      concat(data_length+index_length) total_size
      FROM information_schema.TABLES
      WHERE table_schema LIKE '#{db_name}'
      ORDER BY table_name;
  SQL
end