Skip to content

Instantly share code, notes, and snippets.

@AquaGeek
Created May 14, 2011 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AquaGeek/971651 to your computer and use it in GitHub Desktop.
Save AquaGeek/971651 to your computer and use it in GitHub Desktop.
Rails Lighthouse ticket #3523
From 0c61aa1b6be696b4b1b8a9b96128b201b2be8393 Mon Sep 17 00:00:00 2001
From: Gavin Stark <g.stark@realdigitalmedia.com>
Date: Sat, 19 Mar 2011 10:51:49 -0400
Subject: [PATCH] MySQL schema dumps no longer create VIEWs as if they were TABLEs
---
.../connection_adapters/mysql_adapter.rb | 14 ++++++--------
activerecord/test/cases/schema_dumper_test.rb | 5 +++++
activerecord/test/schema/mysql_specific_schema.rb | 4 ++++
3 files changed, 15 insertions(+), 8 deletions(-)
diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
index 368c5b2..5e08b1e 100644
--- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -453,13 +453,7 @@ module ActiveRecord
# SCHEMA STATEMENTS ========================================
def structure_dump #:nodoc:
- if supports_views?
- sql = "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'"
- else
- sql = "SHOW TABLES"
- end
-
- select_all(sql).map do |table|
+ select_all(show_tables_sql).map do |table|
table.delete('Table_type')
sql = "SHOW CREATE TABLE #{quote_table_name(table.to_a.first.last)}"
exec_without_stmt(sql).first['Create Table'] + ";\n\n"
@@ -506,7 +500,7 @@ module ActiveRecord
def tables(name = nil) #:nodoc:
tables = []
- result = execute("SHOW TABLES", name)
+ result = execute(show_tables_sql, name)
result.each { |field| tables << field[0] }
result.free
tables
@@ -789,6 +783,10 @@ module ActiveRecord
end
column
end
+
+ def show_tables_sql
+ supports_views? ? "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'" : "SHOW TABLES"
+ end
end
end
end
diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb
index 9b2c7c0..433179a 100644
--- a/activerecord/test/cases/schema_dumper_test.rb
+++ b/activerecord/test/cases/schema_dumper_test.rb
@@ -162,6 +162,11 @@ class SchemaDumperTest < ActiveRecord::TestCase
assert_equal 'add_index "companies", ["firm_id", "type", "rating", "ruby_type"], :name => "company_index"', index_definition
end
+ def test_schema_dump_should_not_include_views
+ output = standard_dump
+ assert_no_match %r{view_binary_fields}, output
+ end
+
def test_schema_dump_should_honor_nonstandard_primary_keys
output = standard_dump
match = output.match(%r{create_table "movies"(.*)do})
diff --git a/activerecord/test/schema/mysql_specific_schema.rb b/activerecord/test/schema/mysql_specific_schema.rb
index 30e1c5a..3e66a90 100644
--- a/activerecord/test/schema/mysql_specific_schema.rb
+++ b/activerecord/test/schema/mysql_specific_schema.rb
@@ -11,6 +11,10 @@ ActiveRecord::Schema.define do
end
ActiveRecord::Base.connection.execute <<-SQL
+CREATE OR REPLACE VIEW view_binary_fields AS SELECT * FROM binary_fields
+SQL
+
+ ActiveRecord::Base.connection.execute <<-SQL
DROP PROCEDURE IF EXISTS ten;
SQL
--
1.7.3.4
From 806d136cd415e3fbb3cec63111a13b249711a5c7 Mon Sep 17 00:00:00 2001
From: Gavin Stark <g.stark@realdigitalmedia.com>
Date: Sun, 17 Jan 2010 16:57:23 -0500
Subject: [PATCH] mysql schema dump should not dump views as if they were tables
---
.../connection_adapters/mysql_adapter.rb | 14 ++++++--------
activerecord/test/cases/schema_dumper_test.rb | 5 +++++
activerecord/test/schema/mysql_specific_schema.rb | 4 ++++
3 files changed, 15 insertions(+), 8 deletions(-)
diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
index 8c0bf63..5a4a797 100644
--- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -374,13 +374,7 @@ module ActiveRecord
# SCHEMA STATEMENTS ========================================
def structure_dump #:nodoc:
- if supports_views?
- sql = "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'"
- else
- sql = "SHOW TABLES"
- end
-
- select_all(sql).inject("") do |structure, table|
+ select_all(show_tables_sql).inject("") do |structure, table|
table.delete('Table_type')
structure += select_one("SHOW CREATE TABLE #{quote_table_name(table.to_a.first.last)}")["Create Table"] + ";\n\n"
end
@@ -426,7 +420,7 @@ module ActiveRecord
def tables(name = nil) #:nodoc:
tables = []
- result = execute("SHOW TABLES", name)
+ result = execute(show_tables_sql, name)
result.each { |field| tables << field[0] }
result.free
tables
@@ -645,6 +639,10 @@ module ActiveRecord
end
column
end
+
+ def show_tables_sql
+ supports_views? ? "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'" : "SHOW TABLES"
+ end
end
end
end
diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb
index 1c43e3c..2959ae5 100644
--- a/activerecord/test/cases/schema_dumper_test.rb
+++ b/activerecord/test/cases/schema_dumper_test.rb
@@ -162,6 +162,11 @@ class SchemaDumperTest < ActiveRecord::TestCase
assert_equal 'add_index "companies", ["firm_id", "type", "rating", "ruby_type"], :name => "company_index"', index_definition
end
+ def test_schema_dump_should_not_include_views
+ output = standard_dump
+ assert_no_match %r{view_binary_fields}, output
+ end
+
def test_schema_dump_should_honor_nonstandard_primary_keys
output = standard_dump
match = output.match(%r{create_table "movies"(.*)do})
diff --git a/activerecord/test/schema/mysql_specific_schema.rb b/activerecord/test/schema/mysql_specific_schema.rb
index c78d99f..fb71d73 100644
--- a/activerecord/test/schema/mysql_specific_schema.rb
+++ b/activerecord/test/schema/mysql_specific_schema.rb
@@ -11,6 +11,10 @@ ActiveRecord::Schema.define do
end
ActiveRecord::Base.connection.execute <<-SQL
+CREATE OR REPLACE VIEW view_binary_fields AS SELECT * FROM binary_fields
+SQL
+
+ ActiveRecord::Base.connection.execute <<-SQL
DROP PROCEDURE IF EXISTS ten;
SQL
--
1.6.5.5
From 1788158757372d39cb1cb8badde8c84ffe04e4f4 Mon Sep 17 00:00:00 2001
From: Gavin Stark <g.stark@realdigitalmedia.com>
Date: Mon, 30 Nov 2009 14:03:12 -0500
Subject: [PATCH] Adding support for MySQL schema dumper not outputing views as if they were tables.
---
.../connection_adapters/mysql_adapter.rb | 14 ++++++--------
activerecord/test/cases/schema_dumper_test.rb | 5 +++++
activerecord/test/schema/mysql_specific_schema.rb | 4 ++++
3 files changed, 15 insertions(+), 8 deletions(-)
diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
index 19345d3..a7a2525 100644
--- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -384,13 +384,7 @@ module ActiveRecord
# SCHEMA STATEMENTS ========================================
def structure_dump #:nodoc:
- if supports_views?
- sql = "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'"
- else
- sql = "SHOW TABLES"
- end
-
- select_all(sql).inject("") do |structure, table|
+ select_all(show_tables_sql).inject("") do |structure, table|
table.delete('Table_type')
structure += select_one("SHOW CREATE TABLE #{quote_table_name(table.to_a.first.last)}")["Create Table"] + ";\n\n"
end
@@ -436,7 +430,7 @@ module ActiveRecord
def tables(name = nil) #:nodoc:
tables = []
- result = execute("SHOW TABLES", name)
+ result = execute(show_tables_sql, name)
result.each { |field| tables << field[0] }
result.free
tables
@@ -625,6 +619,10 @@ module ActiveRecord
end
column
end
+
+ def show_tables_sql
+ supports_views? ? "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'" : "SHOW TABLES"
+ end
end
end
end
diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb
index ba714a9..86905f9 100644
--- a/activerecord/test/cases/schema_dumper_test.rb
+++ b/activerecord/test/cases/schema_dumper_test.rb
@@ -165,6 +165,11 @@ class SchemaDumperTest < ActiveRecord::TestCase
end
if current_adapter?(:MysqlAdapter)
+ def test_schema_dump_should_not_include_views
+ output = standard_dump
+ assert_no_match %r{view_binary_fields}, output
+ end
+
def test_schema_dump_should_not_add_default_value_for_mysql_text_field
output = standard_dump
assert_match %r{t.text\s+"body",\s+:null => false$}, output
diff --git a/activerecord/test/schema/mysql_specific_schema.rb b/activerecord/test/schema/mysql_specific_schema.rb
index c78d99f..fb71d73 100644
--- a/activerecord/test/schema/mysql_specific_schema.rb
+++ b/activerecord/test/schema/mysql_specific_schema.rb
@@ -11,6 +11,10 @@ ActiveRecord::Schema.define do
end
ActiveRecord::Base.connection.execute <<-SQL
+CREATE OR REPLACE VIEW view_binary_fields AS SELECT * FROM binary_fields
+SQL
+
+ ActiveRecord::Base.connection.execute <<-SQL
DROP PROCEDURE IF EXISTS ten;
SQL
--
1.6.4.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment