From 5f61d2819b87ea73d5d92f6cc1d28e6455683c7f Mon Sep 17 00:00:00 2001 From: pivotal Date: Tue, 6 May 2008 18:04:02 -0700 Subject: [PATCH] nw/ch - MySQL connection adapter methods --- .../connection_adapters/mysql_adapter.rb | 19 ++++++++++++++- .../test/cases/active_schema_test_mysql.rb | 23 ++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index e742d60..919f9c1 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -255,6 +255,15 @@ module ActiveRecord end end + def add_foreign_key_constraint(table, constraint_name, foreign_key_col, references_table, references_col = "id", cascade_delete = :restrict) + execute "ALTER TABLE `#{table}` ADD CONSTRAINT `#{constraint_name}` FOREIGN KEY (#{foreign_key_col}) REFERENCES `#{references_table}`(#{references_col})" + + " ON DELETE #{cascade_delete.to_s.upcase}" + end + + def drop_foreign_key_constraint(table, constraint_name) + execute "ALTER TABLE `#{table}` DROP FOREIGN KEY `#{constraint_name}`" + end + # CONNECTION MANAGEMENT ==================================== def active? @@ -333,7 +342,6 @@ module ActiveRecord # Transactions aren't supported end - def add_limit_offset!(sql, options) #:nodoc: if limit = options[:limit] unless offset = options[:offset] @@ -344,7 +352,6 @@ module ActiveRecord end end - # SCHEMA STATEMENTS ======================================== def structure_dump #:nodoc: @@ -380,6 +387,10 @@ module ActiveRecord end end + def alter_charset(name, charset = 'utf8') + execute "ALTER DATABASE `#{name}` DEFAULT CHARACTER SET `#{charset}`" + end + def drop_database(name) #:nodoc: execute "DROP DATABASE IF EXISTS `#{name}`" end @@ -387,6 +398,10 @@ module ActiveRecord def current_database select_value 'SELECT DATABASE() as db' end + + def grant_access(database_name, user, password, host='localhost', permissions='ALL') + execute "GRANT #{permissions} ON #{database_name}.* TO '#{user}'@'#{host}' IDENTIFIED BY '#{password}'" + end # Returns the database character set. def charset diff --git a/activerecord/test/cases/active_schema_test_mysql.rb b/activerecord/test/cases/active_schema_test_mysql.rb index ddf3e82..8cc4d02 100644 --- a/activerecord/test/cases/active_schema_test_mysql.rb +++ b/activerecord/test/cases/active_schema_test_mysql.rb @@ -25,6 +25,29 @@ class ActiveSchemaTest < ActiveRecord::TestCase 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 + + def test_alter_charset + assert_equal "ALTER DATABASE `ghozer` DEFAULT CHARACTER SET `utf8`", alter_charset("ghozer") + assert_equal "ALTER DATABASE `ghozer` DEFAULT CHARACTER SET `utf8`", alter_charset(:ghozer) + assert_equal "ALTER DATABASE `ghozer` DEFAULT CHARACTER SET `latin1`", alter_charset("ghozer", 'latin1') + end + + def test_grant_access + assert_equal "GRANT ALL ON ghozer.* TO 'zuul'@'localhost' IDENTIFIED BY 'thereisonlyzuul'", grant_access('ghozer', 'zuul', 'thereisonlyzuul') + assert_equal "GRANT ALL ON ghozer.* TO 'zuul'@'localhost' IDENTIFIED BY 'thereisonlyzuul'", grant_access(:ghozer, :zuul, :thereisonlyzuul) + end + + def test_add_foreign_key_constraint + assert_equal "ALTER TABLE `ghostbusters` ADD CONSTRAINT `ghost_fk` FOREIGN KEY (ghost_id) REFERENCES `ghosts`(id) ON DELETE RESTRICT", + add_foreign_key_constraint("ghostbusters", "ghost_fk", "ghost_id", "ghosts") + assert_equal "ALTER TABLE `ghostbusters` ADD CONSTRAINT `ghost_fk` FOREIGN KEY (ghost_id) REFERENCES `ghosts`(id) ON DELETE RESTRICT", + add_foreign_key_constraint(:ghostbusters, :ghost_fk, :ghost_id, :ghosts) + end + + def test_drop_foreign_key_constraint + assert_equal "ALTER TABLE `ghostbusters` DROP FOREIGN KEY `ghost_fk`", drop_foreign_key_constraint("ghostbusters", "ghost_fk") + assert_equal "ALTER TABLE `ghostbusters` DROP FOREIGN KEY `ghost_fk`", drop_foreign_key_constraint(:ghostbusters, :ghost_fk) + end end def test_add_column -- 1.5.4.5