MySQL: create_database takes :charset and :collation options. Charset defaults to utf8. References #8448.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6848 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper 2007-05-26 00:20:37 +00:00
parent 79212def83
commit ed2a84f99b
4 changed files with 66 additions and 20 deletions

View File

@ -1,5 +1,7 @@
*SVN*
* MySQL: create_database takes :charset and :collation options. Charset defaults to utf8. #8448 [matt]
* Find with a list of ids supports limit/offset. #8437 [hrudududu]
* Optimistic locking: revert the lock version when an update fails. #7840 [plang]

View File

@ -316,8 +316,19 @@ module ActiveRecord
create_database(name)
end
def create_database(name) #:nodoc:
execute "CREATE DATABASE `#{name}`"
# Create a new MySQL database with optional :charset and :collation.
# Charset defaults to utf8.
#
# Example:
# create_database 'charset_test', :charset => 'latin1', :collation => 'latin1_bin'
# create_database 'matt_development'
# create_database 'matt_development', :charset => :big5
def create_database(name, options = {})
if options[:collation]
execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}` COLLATE `#{options[:collation]}`"
else
execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}`"
end
end
def drop_database(name) #:nodoc:
@ -325,7 +336,17 @@ module ActiveRecord
end
def current_database
select_one("SELECT DATABASE() as db")["db"]
select_value 'SELECT DATABASE() as db'
end
# Returns the database character set.
def charset
show_variable 'character_set_database'
end
# Returns the database collation strategy.
def collation
show_variable 'collation_database'
end
def tables(name = nil) #:nodoc:
@ -386,6 +407,11 @@ module ActiveRecord
end
# SHOW VARIABLES LIKE 'name'
def show_variable(name)
select_value "SHOW VARIABLES LIKE '#{name}'"
end
private
def connect
encoding = @config[:encoding]

View File

@ -16,6 +16,14 @@ class ActiveSchemaTest < Test::Unit::TestCase
assert_equal "DROP TABLE people", drop_table(:people)
end
if current_adapter?(:MysqlAdapter)
def test_create_mysql_database_with_encoding
assert_equal "CREATE DATABASE `matt` DEFAULT CHARACTER SET `utf8`", create_database(:matt)
assert_equal "CREATE DATABASE `aimonetti` DEFAULT CHARACTER SET `latin1`", create_database(:aimonetti, {:charset => 'latin1'})
assert_equal "CREATE DATABASE `matt_aimonetti` DEFAULT CHARACTER SET `big5` COLLATE `big5_chinese_ci`", create_database(:matt_aimonetti, {:charset => :big5, :collation => :big5_chinese_ci})
end
end
def test_add_column
assert_equal "ALTER TABLE people ADD `last_name` varchar(255)", add_column(:people, :last_name, :string)
end

View File

@ -46,6 +46,16 @@ class AdapterTest < Test::Unit::TestCase
end
end
if current_adapter?(:MysqlAdapter)
def test_charset
assert @connection.charset
end
def test_collation
assert @connection.collation
end
end
def test_table_alias
def @connection.test_table_alias_length() 10; end
class << @connection