From ed2a84f99b75ed0c2dec4a0145720ee84ab9e114 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 26 May 2007 00:20:37 +0000 Subject: 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 --- activerecord/CHANGELOG | 2 + .../connection_adapters/mysql_adapter.rb | 44 +++++++++++++++++----- activerecord/test/active_schema_test_mysql.rb | 20 +++++++--- activerecord/test/adapter_test.rb | 20 +++++++--- 4 files changed, 66 insertions(+), 20 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index d8d4c2988d..3eb790f10d 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -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] diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 8617191ac5..a3d211c85c 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -209,7 +209,7 @@ module ActiveRecord def quoted_true "1" end - + def quoted_false "0" end @@ -238,7 +238,7 @@ module ActiveRecord disconnect! connect end - + def disconnect! @connection.close rescue nil end @@ -304,7 +304,7 @@ module ActiveRecord else sql = "SHOW TABLES" end - + select_all(sql).inject("") do |structure, table| table.delete('Table_type') structure += select_one("SHOW CREATE TABLE #{table.to_a.first.last}")["Create Table"] + ";\n\n" @@ -316,16 +316,37 @@ 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: execute "DROP DATABASE IF EXISTS `#{name}`" 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: @@ -359,10 +380,10 @@ module ActiveRecord def create_table(name, options = {}) #:nodoc: super(name, {:options => "ENGINE=InnoDB"}.merge(options)) end - + def rename_table(name, new_name) execute "RENAME TABLE #{name} TO #{new_name}" - end + end def change_column_default(table_name, column_name, default) #:nodoc: current_type = select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Type"] @@ -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] diff --git a/activerecord/test/active_schema_test_mysql.rb b/activerecord/test/active_schema_test_mysql.rb index b72223526a..d7a20f9338 100644 --- a/activerecord/test/active_schema_test_mysql.rb +++ b/activerecord/test/active_schema_test_mysql.rb @@ -5,9 +5,9 @@ class ActiveSchemaTest < Test::Unit::TestCase ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do alias_method :real_execute, :execute def execute(sql, name = nil) return sql end - end + end end - + def teardown ActiveRecord::ConnectionAdapters::MysqlAdapter.send(:alias_method, :execute, :real_execute) end @@ -15,17 +15,25 @@ class ActiveSchemaTest < Test::Unit::TestCase def test_drop_table 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 - + def test_add_column_with_limit assert_equal "ALTER TABLE people ADD `key` varchar(32)", add_column(:people, :key, :string, :limit => 32) end - + private def method_missing(method_symbol, *arguments) ActiveRecord::Base.connection.send(method_symbol, *arguments) end -end \ No newline at end of file +end diff --git a/activerecord/test/adapter_test.rb b/activerecord/test/adapter_test.rb index 0c12c4d37e..6d986d5dba 100644 --- a/activerecord/test/adapter_test.rb +++ b/activerecord/test/adapter_test.rb @@ -19,7 +19,7 @@ class AdapterTest < Test::Unit::TestCase def test_indexes idx_name = "accounts_idx" - + if @connection.respond_to?(:indexes) indexes = @connection.indexes("accounts") assert indexes.empty? @@ -39,24 +39,34 @@ class AdapterTest < Test::Unit::TestCase ensure @connection.remove_index(:accounts, :name => idx_name) rescue nil end - + def test_current_database if @connection.respond_to?(:current_database) assert_equal ENV['ARUNIT_DB_NAME'] || "activerecord_unittest", @connection.current_database 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 alias_method :old_table_alias_length, :table_alias_length alias_method :table_alias_length, :test_table_alias_length end - + assert_equal 'posts', @connection.table_alias_for('posts') assert_equal 'posts_comm', @connection.table_alias_for('posts_comments') assert_equal 'dbo_posts', @connection.table_alias_for('dbo.posts') - + class << @connection alias_method :table_alias_length, :old_table_alias_length end @@ -66,7 +76,7 @@ class AdapterTest < Test::Unit::TestCase if ActiveRecord::Base.connection.respond_to?(:reset_pk_sequence!) require 'fixtures/movie' require 'fixtures/subscriber' - + def test_reset_empty_table_with_custom_pk Movie.delete_all Movie.connection.reset_pk_sequence! 'movies' -- cgit v1.2.3