2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2016-08-06 12:26:20 -04:00
|
|
|
require "support/schema_dumping_helper"
|
|
|
|
require "models/default"
|
|
|
|
require "models/entrant"
|
2006-02-09 13:06:29 -05:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class DefaultTest < ActiveRecord::TestCase
|
2006-11-20 03:54:42 -05:00
|
|
|
def test_nil_defaults_for_not_null_columns
|
2015-12-15 17:01:30 -05:00
|
|
|
%w(id name course_id).each do |name|
|
2006-11-20 03:54:42 -05:00
|
|
|
column = Entrant.columns_hash[name]
|
2018-05-12 22:26:10 -04:00
|
|
|
assert_not column.null, "#{name} column should be NOT NULL"
|
2015-12-15 17:01:30 -05:00
|
|
|
assert_not column.default, "#{name} column should be DEFAULT 'nil'"
|
2006-11-20 03:54:42 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-08-31 06:19:59 -04:00
|
|
|
if current_adapter?(:PostgreSQLAdapter)
|
|
|
|
def test_multiline_default_text
|
2014-12-01 10:49:13 -05:00
|
|
|
record = Default.new
|
2008-08-31 06:19:59 -04:00
|
|
|
# older postgres versions represent the default with escapes ("\\012" for a newline)
|
2014-12-01 10:49:13 -05:00
|
|
|
assert("--- []\n\n" == record.multiline_default || "--- []\\012\\012" == record.multiline_default)
|
2008-08-31 06:19:59 -04:00
|
|
|
end
|
2014-12-01 10:26:40 -05:00
|
|
|
end
|
|
|
|
end
|
2014-12-01 08:28:50 -05:00
|
|
|
|
2014-12-01 10:26:40 -05:00
|
|
|
class DefaultNumbersTest < ActiveRecord::TestCase
|
|
|
|
class DefaultNumber < ActiveRecord::Base; end
|
|
|
|
|
|
|
|
setup do
|
|
|
|
@connection = ActiveRecord::Base.connection
|
|
|
|
@connection.create_table :default_numbers do |t|
|
|
|
|
t.integer :positive_integer, default: 7
|
|
|
|
t.integer :negative_integer, default: -5
|
|
|
|
t.decimal :decimal_number, default: "2.78", precision: 5, scale: 2
|
2014-12-01 08:28:50 -05:00
|
|
|
end
|
2008-08-31 06:19:59 -04:00
|
|
|
end
|
2014-12-01 10:26:40 -05:00
|
|
|
|
|
|
|
teardown do
|
2015-01-20 07:25:33 -05:00
|
|
|
@connection.drop_table :default_numbers, if_exists: true
|
2014-12-01 10:26:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_default_positive_integer
|
|
|
|
record = DefaultNumber.new
|
|
|
|
assert_equal 7, record.positive_integer
|
|
|
|
assert_equal "7", record.positive_integer_before_type_cast
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_default_negative_integer
|
|
|
|
record = DefaultNumber.new
|
2014-12-01 10:49:13 -05:00
|
|
|
assert_equal (-5), record.negative_integer
|
2014-12-01 10:26:40 -05:00
|
|
|
assert_equal "-5", record.negative_integer_before_type_cast
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_default_decimal_number
|
|
|
|
record = DefaultNumber.new
|
Suppress `warning: BigDecimal.new is deprecated` in activerecord
`BigDecimal.new` has been deprecated in BigDecimal 1.3.3
which will be a default for Ruby 2.5.
Refer https://github.com/ruby/bigdecimal/commit/533737338db915b00dc7168c3602e4b462b23503
```
$ cd rails/activerecord/
$ git grep -l BigDecimal.new | grep \.rb | xargs sed -i -e "s/BigDecimal.new/BigDecimal/g"
```
- Changes made only to Active Record. Will apply the same change to
other module once this commit is merged.
- The following deprecation has not been addressed because it has been
reported at `ActiveRecord::Result.new`. `ActiveRecord::Result.ancestors`
did not show `BigDecimal`.
* Not addressed
```ruby
/path/to/rails/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb:34:
warning: BigDecimal.new is deprecated
```
* database_statements.rb:34
```ruby
ActiveRecord::Result.new(result.fields, result.to_a) if result
```
* ActiveRecord::Result.ancestors
```ruby
[ActiveRecord::Result,
Enumerable,
ActiveSupport::ToJsonWithActiveSupportEncoder,
Object,
Metaclass::ObjectMethods,
Mocha::ObjectMethods,
PP::ObjectMixin,
ActiveSupport::Dependencies::Loadable,
ActiveSupport::Tryable,
JSON::Ext::Generator::GeneratorMethods::Object,
Kernel,
BasicObject]
```
This commit has been tested with these Ruby and BigDecimal versions
- ruby 2.5 and bigdecimal 1.3.3
```
$ ruby -v
ruby 2.5.0dev (2017-12-14 trunk 61217) [x86_64-linux]
$ gem list |grep bigdecimal
bigdecimal (default: 1.3.3, default: 1.3.2)
```
- ruby 2.4 and bigdecimal 1.3.0
```
$ ruby -v
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux-gnu]
$ gem list |grep bigdecimal
bigdecimal (default: 1.3.0)
```
- ruby 2.3 and bigdecimal 1.2.8
```
$ ruby -v
ruby 2.3.5p376 (2017-09-14 revision 59905) [x86_64-linux]
$ gem list |grep -i bigdecimal
bigdecimal (1.2.8)
```
- ruby 2.2 and bigdecimal 1.2.6
```
$ ruby -v
ruby 2.2.8p477 (2017-09-14 revision 59906) [x86_64-linux]
$ gem list |grep bigdecimal
bigdecimal (1.2.6)
```
2017-12-13 14:46:46 -05:00
|
|
|
assert_equal BigDecimal("2.78"), record.decimal_number
|
2014-12-01 10:26:40 -05:00
|
|
|
assert_equal "2.78", record.decimal_number_before_type_cast
|
|
|
|
end
|
2008-08-31 06:19:59 -04:00
|
|
|
end
|
2008-09-14 07:06:10 -04:00
|
|
|
|
2013-06-07 18:36:37 -04:00
|
|
|
class DefaultStringsTest < ActiveRecord::TestCase
|
|
|
|
class DefaultString < ActiveRecord::Base; end
|
|
|
|
|
|
|
|
setup do
|
|
|
|
@connection = ActiveRecord::Base.connection
|
|
|
|
@connection.create_table :default_strings do |t|
|
|
|
|
t.string :string_col, default: "Smith"
|
|
|
|
t.string :string_col_with_quotes, default: "O'Connor"
|
|
|
|
end
|
|
|
|
DefaultString.reset_column_information
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_default_strings
|
|
|
|
assert_equal "Smith", DefaultString.new.string_col
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_default_strings_containing_single_quotes
|
|
|
|
assert_equal "O'Connor", DefaultString.new.string_col_with_quotes
|
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
|
|
|
@connection.drop_table :default_strings
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-03 07:49:45 -04:00
|
|
|
if current_adapter?(:PostgreSQLAdapter)
|
|
|
|
class PostgresqlDefaultExpressionTest < ActiveRecord::TestCase
|
|
|
|
include SchemaDumpingHelper
|
|
|
|
|
|
|
|
test "schema dump includes default expression" do
|
|
|
|
output = dump_table_schema("defaults")
|
2019-03-29 11:18:48 -04:00
|
|
|
if ActiveRecord::Base.connection.database_version >= 100000
|
2017-04-26 00:45:08 -04:00
|
|
|
assert_match %r/t\.date\s+"modified_date",\s+default: -> { "CURRENT_DATE" }/, output
|
|
|
|
assert_match %r/t\.datetime\s+"modified_time",\s+default: -> { "CURRENT_TIMESTAMP" }/, output
|
|
|
|
else
|
|
|
|
assert_match %r/t\.date\s+"modified_date",\s+default: -> { "\('now'::text\)::date" }/, output
|
|
|
|
assert_match %r/t\.datetime\s+"modified_time",\s+default: -> { "now\(\)" }/, output
|
|
|
|
end
|
2015-05-03 07:49:45 -04:00
|
|
|
assert_match %r/t\.date\s+"modified_date_function",\s+default: -> { "now\(\)" }/, output
|
|
|
|
assert_match %r/t\.datetime\s+"modified_time_function",\s+default: -> { "now\(\)" }/, output
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-15 17:01:30 -05:00
|
|
|
if current_adapter?(:Mysql2Adapter)
|
2015-05-04 06:01:24 -04:00
|
|
|
class MysqlDefaultExpressionTest < ActiveRecord::TestCase
|
|
|
|
include SchemaDumpingHelper
|
|
|
|
|
2018-10-24 21:11:31 -04:00
|
|
|
if supports_default_expression?
|
|
|
|
test "schema dump includes default expression" do
|
|
|
|
output = dump_table_schema("defaults")
|
|
|
|
assert_match %r/t\.binary\s+"uuid",\s+limit: 36,\s+default: -> { "\(uuid\(\)\)" }/i, output
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-08 15:27:35 -04:00
|
|
|
if subsecond_precision_supported?
|
2015-10-11 08:45:55 -04:00
|
|
|
test "schema dump datetime includes default expression" do
|
2015-05-04 06:01:24 -04:00
|
|
|
output = dump_table_schema("datetime_defaults")
|
2018-07-03 03:17:40 -04:00
|
|
|
assert_match %r/t\.datetime\s+"modified_datetime",\s+default: -> { "CURRENT_TIMESTAMP(?:\(\))?" }/i, output
|
|
|
|
end
|
|
|
|
|
|
|
|
test "schema dump datetime includes precise default expression" do
|
|
|
|
output = dump_table_schema("datetime_defaults")
|
|
|
|
assert_match %r/t\.datetime\s+"precise_datetime",.+default: -> { "CURRENT_TIMESTAMP\(6\)" }/i, output
|
2015-05-04 06:01:24 -04:00
|
|
|
end
|
2015-10-11 08:45:55 -04:00
|
|
|
|
2018-07-08 15:27:35 -04:00
|
|
|
test "schema dump timestamp includes default expression" do
|
|
|
|
output = dump_table_schema("timestamp_defaults")
|
|
|
|
assert_match %r/t\.timestamp\s+"modified_timestamp",\s+default: -> { "CURRENT_TIMESTAMP(?:\(\))?" }/i, output
|
|
|
|
end
|
2018-07-03 03:17:40 -04:00
|
|
|
|
2018-07-08 15:27:35 -04:00
|
|
|
test "schema dump timestamp includes precise default expression" do
|
|
|
|
output = dump_table_schema("timestamp_defaults")
|
|
|
|
assert_match %r/t\.timestamp\s+"precise_timestamp",.+default: -> { "CURRENT_TIMESTAMP\(6\)" }/i, output
|
|
|
|
end
|
2015-10-11 08:45:55 -04:00
|
|
|
|
2018-07-08 15:27:35 -04:00
|
|
|
test "schema dump timestamp without default expression" do
|
|
|
|
output = dump_table_schema("timestamp_defaults")
|
|
|
|
assert_match %r/t\.timestamp\s+"nullable_timestamp"$/, output
|
|
|
|
end
|
2015-10-11 08:45:55 -04:00
|
|
|
end
|
2015-05-04 06:01:24 -04:00
|
|
|
end
|
|
|
|
|
2008-08-31 06:19:59 -04:00
|
|
|
class DefaultsTestWithoutTransactionalFixtures < ActiveRecord::TestCase
|
2008-10-09 08:35:42 -04:00
|
|
|
# ActiveRecord::Base#create! (and #save and other related methods) will
|
2015-03-10 22:21:19 -04:00
|
|
|
# open a new transaction. When in transactional tests mode, this will
|
2010-06-14 17:21:53 -04:00
|
|
|
# cause Active Record to create a new savepoint. However, since MySQL doesn't
|
2008-10-09 08:35:42 -04:00
|
|
|
# support DDL transactions, creating a table will result in any created
|
|
|
|
# savepoints to be automatically released. This in turn causes the savepoint
|
|
|
|
# release code in AbstractAdapter#transaction to fail.
|
|
|
|
#
|
2015-03-10 22:21:19 -04:00
|
|
|
# We don't want that to happen, so we disable transactional tests here.
|
|
|
|
self.use_transactional_tests = false
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2012-10-19 10:08:58 -04:00
|
|
|
def using_strict(strict)
|
2012-10-26 10:51:02 -04:00
|
|
|
connection = ActiveRecord::Base.remove_connection
|
|
|
|
ActiveRecord::Base.establish_connection connection.merge(strict: strict)
|
2012-10-19 10:08:58 -04:00
|
|
|
yield
|
|
|
|
ensure
|
2012-10-26 10:51:02 -04:00
|
|
|
ActiveRecord::Base.remove_connection
|
|
|
|
ActiveRecord::Base.establish_connection connection
|
2012-10-19 10:08:58 -04:00
|
|
|
end
|
|
|
|
|
2016-08-13 17:17:20 -04:00
|
|
|
# Strict mode controls how MySQL handles invalid or missing values
|
|
|
|
# in data-change statements such as INSERT or UPDATE. A value can be
|
|
|
|
# invalid for several reasons. For example, it might have the wrong
|
|
|
|
# data type for the column, or it might be out of range. A value is
|
|
|
|
# missing when a new row to be inserted does not contain a value for
|
|
|
|
# a non-NULL column that has no explicit DEFAULT clause in its definition.
|
|
|
|
# (For a NULL column, NULL is inserted if the value is missing.)
|
2012-10-19 10:08:58 -04:00
|
|
|
#
|
2016-08-13 17:17:20 -04:00
|
|
|
# If strict mode is not in effect, MySQL inserts adjusted values for
|
|
|
|
# invalid or missing values and produces warnings. In strict mode,
|
|
|
|
# you can produce this behavior by using INSERT IGNORE or UPDATE IGNORE.
|
2012-10-19 10:08:58 -04:00
|
|
|
#
|
2019-10-03 20:22:29 -04:00
|
|
|
# https://dev.mysql.com/doc/refman/en/sql-mode.html#sql-mode-strict
|
2016-08-13 17:17:20 -04:00
|
|
|
def test_mysql_not_null_defaults_non_strict
|
2012-10-19 10:08:58 -04:00
|
|
|
using_strict(false) do
|
2016-08-13 17:17:20 -04:00
|
|
|
with_mysql_not_null_table do |klass|
|
2014-12-01 10:49:13 -05:00
|
|
|
record = klass.new
|
2016-08-13 17:17:20 -04:00
|
|
|
assert_nil record.non_null_integer
|
|
|
|
assert_nil record.non_null_string
|
|
|
|
assert_nil record.non_null_text
|
|
|
|
assert_nil record.non_null_blob
|
2012-10-19 10:08:58 -04:00
|
|
|
|
2014-12-01 10:49:13 -05:00
|
|
|
record.save!
|
|
|
|
record.reload
|
2012-10-19 10:08:58 -04:00
|
|
|
|
2016-08-13 17:17:20 -04:00
|
|
|
assert_equal 0, record.non_null_integer
|
|
|
|
assert_equal "", record.non_null_string
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "", record.non_null_text
|
|
|
|
assert_equal "", record.non_null_blob
|
2012-10-19 10:08:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-13 17:17:20 -04:00
|
|
|
def test_mysql_not_null_defaults_strict
|
2012-10-19 10:08:58 -04:00
|
|
|
using_strict(true) do
|
2016-08-13 17:17:20 -04:00
|
|
|
with_mysql_not_null_table do |klass|
|
2014-12-01 10:49:13 -05:00
|
|
|
record = klass.new
|
2016-08-13 17:17:20 -04:00
|
|
|
assert_nil record.non_null_integer
|
|
|
|
assert_nil record.non_null_string
|
2014-12-01 10:49:13 -05:00
|
|
|
assert_nil record.non_null_text
|
2016-08-13 17:17:20 -04:00
|
|
|
assert_nil record.non_null_blob
|
2012-10-19 10:08:58 -04:00
|
|
|
|
2016-06-20 11:38:48 -04:00
|
|
|
assert_raises(ActiveRecord::NotNullViolation) { klass.create }
|
2012-10-19 10:08:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-13 17:17:20 -04:00
|
|
|
def with_mysql_not_null_table
|
2008-10-09 08:35:42 -04:00
|
|
|
klass = Class.new(ActiveRecord::Base)
|
2016-08-13 17:17:20 -04:00
|
|
|
klass.table_name = "test_mysql_not_null_defaults"
|
2008-10-09 08:35:42 -04:00
|
|
|
klass.connection.create_table klass.table_name do |t|
|
2016-08-13 17:17:20 -04:00
|
|
|
t.integer :non_null_integer, null: false
|
|
|
|
t.string :non_null_string, null: false
|
|
|
|
t.text :non_null_text, null: false
|
|
|
|
t.blob :non_null_blob, null: false
|
2008-10-09 08:35:42 -04:00
|
|
|
end
|
2012-10-19 10:08:58 -04:00
|
|
|
|
|
|
|
yield klass
|
2008-10-09 08:35:42 -04:00
|
|
|
ensure
|
|
|
|
klass.connection.drop_table(klass.table_name) rescue nil
|
|
|
|
end
|
2007-01-12 03:46:01 -05:00
|
|
|
end
|
2006-02-09 13:06:29 -05:00
|
|
|
end
|