Module: Padrino::Generators::SqlHelpers

### Home Classes Methods

### In Files

- lib/padrino-gen/padrino-tasks/sql-helpers.rb

### Methods

- ::create_db
- ::drop_db

### Files

- README.rdoc
- README.rdoc

### Class/Module Index

Quicksearch

- Padrino
- Padrino::Generators
- Padrino::Generators::Actions
- Padrino::Generators::Actions::ClassMethods
- Padrino::Generators::App
- Padrino::Generators::AppRootNotFound
- Padrino::Generators::Cli
- Padrino::Generators::Component
- Padrino::Generators::Components
- Padrino::Generators::Components::Actions
- Padrino::Generators::Controller
- Padrino::Generators::Helper
- Padrino::Generators::Mailer
- Padrino::Generators::Migration
- Padrino::Generators::Model
- Padrino::Generators::Plugin
- Padrino::Generators::Project
- Padrino::Generators::Runner
- Padrino::Generators::SqlHelpers
- Padrino::Generators::Task
- ActiveRecord
- ActiveRecord::Schema
- Object

No matching classes.

# Padrino::Generators::SqlHelpers

### Public Class Methods

create_db(adapter, user, password, host, database, charset, collation) click to toggle source

    # 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.blank?

          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.blank?

          arguments = []
          arguments << "--user=#{user}" if user
          arguments << "--host=#{host}" unless ]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'
          fail "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) click to toggle source

    # 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.blank?

          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.blank?

          arguments = []
          arguments << "--user=#{user}" if user
          arguments << "--host=#{host}" unless ]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

[Validate]

Generated with the Darkfish Rdoc Generator 2.