Sha256: 460500cb73ad1165c91735ff389ea98e14f4b5524327843b356346a119c8f818

Contents?: true

Size: 1.84 KB

Versions: 6

Compression:

Stored size: 1.84 KB

Contents

module Padrino
  module Generators 
    module SqlHelpers
      def self.create_db(adapter, user, password, host, database, charset, collation)
        case adapter
          when 'postgres'
            arguments = []
            arguments << "--encoding=#{charset}" if charset
            arguments << "--host=#{host}" if host
            arguments << "--username=#{user}" if user
            arguments << database
            system("createdb", *arguments)
          when 'mysql'
            arguments = ["--user=#{user}"]
            arguments << "--password=#{password}" unless password.blank?
            
            unless %w[127.0.0.1 localhost].include?(host)
              arguments << "--host=#{host}"
            end

            arguments << '-e'
            arguments << "CREATE DATABASE #{database} DEFAULT CHARACTER SET #{charset} DEFAULT COLLATE #{collation}"

            system('mysql',*arguments)
          else
            raise "Adapter #{adapter} not supported for creating databases yet."
        end
      end

      def self.drop_db(adapter, user, password, host, database)
        case adapter
          when 'postgres'
            arguments = []
            arguments << "--host=#{host}" if host
            arguments << "--username=#{user}" if user
            arguments << database
            system("dropdb", *arguments)
          when 'mysql'
            arguments = ["--user=#{user}"]
            arguments << "--password=#{password}" unless password.blank?

            unless %w[127.0.0.1 localhost].include?(host)
              arguments << "--host=#{host}"
            end

            arguments << '-e'
            arguments << "DROP DATABASE IF EXISTS #{database}"

            system('mysql',*arguments)
          else
            raise "Adapter #{adapter} not supported for dropping databases yet."
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
padrino-gen-0.12.0 lib/padrino-gen/padrino-tasks/sql-helpers.rb
padrino-gen-0.12.0.rc3 lib/padrino-gen/padrino-tasks/sql-helpers.rb
padrino-gen-0.12.0.rc2 lib/padrino-gen/padrino-tasks/sql-helpers.rb
padrino-gen-0.12.0.rc1 lib/padrino-gen/padrino-tasks/sql-helpers.rb
padrino-gen-0.11.4 lib/padrino-gen/padrino-tasks/sql-helpers.rb
padrino-gen-0.11.3 lib/padrino-gen/padrino-tasks/sql-helpers.rb