Class: CreateRailsDevDB::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/create-rails-dev-db.rb

Class Method Summary collapse

Class Method Details

.run!(*arguments) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
62
# File 'lib/create-rails-dev-db.rb', line 4

def run!(*arguments)
  current_app_name = Dir.pwd.split("/").last

  # Ask for database name, defaulting to current directory name (presuming it is being run within the rails app folder)
  puts "Database name [#{current_app_name[0..64]}_development]: "
  dbname_entry = gets.chomp
  while dbname_entry.size > (64-12)
    puts "*Name of db is too long (#{dbname_entry.size}. The limit is 64.)"
    dbname_entry = gets.chomp
  end
  
  if dbname_entry.nil? || dbname_entry.size == 0
    dbname = current_app_name[0..64]
    dbname = "#{dbname}_development" 
  else
    dbname = dbname_entry
  end
  
  # Name dbs of other environment
  dbname_prod = "#{dbname}_production"
  dbname_test = "#{dbname}_test"


  # Ask for username for db to be created, defaulting to app name plus _dev
  puts "Username [#{current_app_name}_dev]: "
  username = gets.chomp
  username = "#{current_app_name}_dev" if username.nil? || username.size == 0

  # Ask for password for db to be created, defaulting to username
  puts "Password [#{username}]: "
  pass = gets.chomp
  pass = username if pass.nil? || pass.size == 0

  contents = ""

  source_file = File.expand_path(File.dirname(__FILE__)) + "/../config/database.yml"

  File.open(source_file, 'r') do |f1|  
     while line = f1.gets
       if line.include?("$")
         line.gsub!(/\$dbname/, dbname) if !line.nil? && !dbname.nil?
         line.gsub!(/\$username/, username) if !line.nil? && !username.nil?
         line.gsub!(/\$password/, pass) if !line.nil? && !pass.nil?
         line.gsub!(/\$dbname_prod/, dbname_prod) if !line.nil? && !pass.nil?
         line.gsub!(/\$dbname_test/, dbname_test) if !line.nil? && !pass.nil?
       end
       contents += line  
     end  
  end

  File.open('config/database.yml', 'w') do |f2|   
   f2.puts contents
  end


  mysql_command = "mysql -u root -p -e \"create database #{dbname}; grant all on #{dbname}.* to #{username}@'localhost' identified by '#{pass}'\";"
  puts "Executing #{mysql_command}"
  system(mysql_command)
end