From 75a2e4a8dff8c01dad913048d3676b993a8e23a2 Mon Sep 17 00:00:00 2001
From: Cody Cutrer <cody@instructure.com>
Date: Fri, 13 Dec 2013 15:20:03 -0700
Subject: support creating temporary tables from queries

also override drop_table in AbstractMySQLAdapter to properly drop
temporary tables without committing the transaction
---
 .../test/cases/adapters/mysql2/schema_test.rb      |  9 ++++++++
 activerecord/test/cases/migration_test.rb          | 26 ++++++++++++++++++++++
 2 files changed, 35 insertions(+)

(limited to 'activerecord/test')

diff --git a/activerecord/test/cases/adapters/mysql2/schema_test.rb b/activerecord/test/cases/adapters/mysql2/schema_test.rb
index 5db60ff8a0..43c9116b5a 100644
--- a/activerecord/test/cases/adapters/mysql2/schema_test.rb
+++ b/activerecord/test/cases/adapters/mysql2/schema_test.rb
@@ -65,6 +65,15 @@ module ActiveRecord
         assert_nil index_c.using
         assert_equal :fulltext, index_c.type
       end
+
+      def test_drop_temporary_table
+        @connection.transaction do
+          @connection.create_table(:temp_table, temporary: true)
+          # if it doesn't properly say DROP TEMPORARY TABLE, the transaction commit
+          # will complain that no transaction is active
+          @connection.drop_table(:temp_table, temporary: true)
+        end
+      end
     end
   end
 end
diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb
index 519045095d..ebcc778b8b 100644
--- a/activerecord/test/cases/migration_test.rb
+++ b/activerecord/test/cases/migration_test.rb
@@ -443,6 +443,32 @@ class MigrationTest < ActiveRecord::TestCase
     Person.connection.drop_table :binary_testings rescue nil
   end
 
+  def test_create_table_with_query
+    Person.connection.drop_table :table_from_query_testings rescue nil
+    Person.connection.create_table(:person, force: true)
+
+    Person.connection.create_table :table_from_query_testings, as: "SELECT id FROM person"
+
+    columns = Person.connection.columns(:table_from_query_testings)
+    assert_equal 1, columns.length
+    assert_equal "id", columns.first.name
+
+    Person.connection.drop_table :table_from_query_testings rescue nil
+  end
+
+  def test_create_table_with_query_from_relation
+    Person.connection.drop_table :table_from_query_testings rescue nil
+    Person.connection.create_table(:person, force: true)
+
+    Person.connection.create_table :table_from_query_testings, as: Person.select(:id)
+
+    columns = Person.connection.columns(:table_from_query_testings)
+    assert_equal 1, columns.length
+    assert_equal "id", columns.first.name
+
+    Person.connection.drop_table :table_from_query_testings rescue nil
+  end
+
   if current_adapter? :OracleAdapter
     def test_create_table_with_custom_sequence_name
       # table name is 29 chars, the standard sequence name will
-- 
cgit v1.2.3