forked from rails/rails
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make enum column definitions db-agnostic
This is done by introducing a values option to specify the values of an enum for MySQL adapters. Using the values option in PostgreSQL would allow implicit creation of enum types without create_enum. If no enum_type was specified, the name of the enum will default to the column name. Enums in SQLite are represented as strings. Loading a schema with enum columns are now supported with any adapter. def up create_table :cats do |t| t.enum :current_mood, enum_type: mood, values: [happy, sad], default: happy, null: false end end
- Loading branch information
Showing
18 changed files
with
358 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 47 additions & 28 deletions
75
activerecord/test/cases/adapters/abstract_mysql_adapter/mysql_enum_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,66 @@ | ||
# frozen_string_literal: true | ||
|
||
require "cases/helper" | ||
require "support/schema_dumping_helper" | ||
require "cases/enum_shared_test_cases" | ||
|
||
class MySQLEnumTest < ActiveRecord::AbstractMysqlTestCase | ||
self.use_transactional_tests = false | ||
|
||
include SchemaDumpingHelper | ||
|
||
class EnumTest < ActiveRecord::Base | ||
attribute :state, :integer | ||
|
||
enum :state, { | ||
start: 0, | ||
middle: 1, | ||
finish: 2 | ||
} | ||
end | ||
|
||
def setup | ||
EnumTest.lease_connection.create_table :enum_tests, id: false, force: true do |t| | ||
t.column :enum_column, "enum('text','blob','tiny','medium','long','unsigned','bigint')" | ||
t.column :state, "TINYINT(1)" | ||
end | ||
end | ||
module MySQLSharedEnumTestCases | ||
include SharedEnumTestCases | ||
|
||
def test_should_not_be_unsigned | ||
column = EnumTest.columns_hash["enum_column"] | ||
column = EnumTest.columns_hash["current_mood"] | ||
assert_not_predicate column, :unsigned? | ||
end | ||
|
||
def test_should_not_be_bigint | ||
column = EnumTest.columns_hash["enum_column"] | ||
column = EnumTest.columns_hash["current_mood"] | ||
assert_not_predicate column, :bigint? | ||
end | ||
|
||
def test_schema_dumping | ||
schema = dump_table_schema "enum_tests" | ||
assert_match %r{t\.column "enum_column", "enum\('text','blob','tiny','medium','long','unsigned','bigint'\)"$}, schema | ||
assert_match %r{t\.enum "current_mood", default: "sad", values: \["sad", "ok", "happy"\]}, schema | ||
end | ||
end | ||
|
||
class MySQLEnumTest < ActiveRecord::AbstractMysqlTestCase | ||
include MySQLSharedEnumTestCases | ||
|
||
self.use_transactional_tests = false | ||
|
||
def test_enum_with_attribute | ||
enum_test = EnumTest.create!(state: :middle) | ||
assert_equal "middle", enum_test.state | ||
def setup | ||
@connection = ActiveRecord::Base.lease_connection | ||
@connection.create_table :enum_tests, force: true do |t| | ||
t.column :current_mood, 'enum("sad","ok","happy")', default: "sad" | ||
end | ||
end | ||
|
||
def test_schema_load | ||
original, $stdout = $stdout, StringIO.new | ||
|
||
ActiveRecord::Schema.define do | ||
create_enum :color, ["blue", "green"] | ||
|
||
change_table :enum_tests do |t| | ||
t.enum :best_color, enum_type: "color", values: ["blue", "green"], default: "blue", null: false | ||
end | ||
end | ||
|
||
assert @connection.column_exists?(:enum_tests, :best_color, "string", values: ["blue", "green"], default: "blue", null: false) | ||
ensure | ||
$stdout = original | ||
end | ||
|
||
def test_enum_column_without_values_raises_error | ||
error = assert_raises(ArgumentError) do | ||
@connection.add_column :enum_tests, :best_color, :enum, null: false | ||
end | ||
|
||
assert_equal "values are required for enums", error.message | ||
end | ||
end | ||
|
||
class MySQLEnumWithValuesTest < ActiveRecord::AbstractMysqlTestCase | ||
include MySQLSharedEnumTestCases | ||
|
||
self.use_transactional_tests = false | ||
end |
Oops, something went wrong.