Module: Padrino::Generators::SqlHelpers
- Defined in:
- lib/padrino-gen/padrino-tasks/sql-helpers.rb
Class Method Summary collapse
- .create_db(adapter, user, password, host, database, charset, collation) ⇒ Object
- .drop_db(adapter, user, password, host, database) ⇒ Object
Class Method Details
.create_db(adapter, user, password, host, database, charset, collation) ⇒ Object
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 |
# File 'lib/padrino-gen/padrino-tasks/sql-helpers.rb', line 6 def self.create_db(adapter, user, password, host, database, charset, collation) case adapter when 'postgres' environment = {} environment['PGPASSWORD'] = password unless password.nil? || password.empty? arguments = [] arguments << "--encoding=#{charset}" if charset arguments << "--host=#{host}" if host arguments << "--username=#{user}" if user arguments << database Process.wait Process.spawn(environment, 'createdb', *arguments) when 'mysql', 'mysql2' environment = {} environment['MYSQL_PWD'] = password unless password.nil? || password.empty? arguments = [] arguments << "--user=#{user}" if user arguments << "--host=#{host}" unless %w[127.0.0.1 localhost].include?(host) arguments << '-e' arguments << "CREATE DATABASE #{database} DEFAULT CHARACTER SET #{charset} DEFAULT COLLATE #{collation}" Process.wait Process.spawn(environment, 'mysql', *arguments) when 'sqlite', 'sqlite3' raise "Database #{database} already exists" if File.file?(database) FileUtils.mkdir_p(File.dirname(database)) File.open(database, 'a') {} else raise "Adapter #{adapter} not supported for creating databases yet." end end |
.drop_db(adapter, user, password, host, database) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/padrino-gen/padrino-tasks/sql-helpers.rb', line 40 def self.drop_db(adapter, user, password, host, database) case adapter when 'postgres' environment = {} environment['PGPASSWORD'] = password unless password.nil? || password.empty? arguments = [] arguments << "--host=#{host}" if host arguments << "--username=#{user}" if user arguments << database Process.wait Process.spawn(environment, 'dropdb', *arguments) when 'mysql', 'mysql2' environment = {} environment['MYSQL_PWD'] = password unless password.nil? || password.empty? arguments = [] arguments << "--user=#{user}" if user arguments << "--host=#{host}" unless %w[127.0.0.1 localhost].include?(host) arguments << '-e' arguments << "DROP DATABASE IF EXISTS #{database}" Process.wait Process.spawn(environment, 'mysql', *arguments) when 'sqlite', 'sqlite3' File.delete(database) if File.file?(database) else raise "Adapter #{adapter} not supported for dropping databases yet." end end |