Commit Graph

20036 Commits

Author SHA1 Message Date
Marcelo Lauxen
5fdc7d385f Fix read_attribute_before_type_cast to consider attribute aliases
Numericality validations for aliased attributes are not able
to get the value of the attribute before type cast because
activerecord was trying to get the value of the attribute based
on attribute alias name and not the original attribute name.

Example of validation which would pass even if a invalid value
would be provided

    class MyModel < ActiveRecord::Base
      validates :aliased_balance, numericality: { greater_than_or_equal_to: 0 }
    end

If we instantiate MyModel like bellow it will be valid because
when numericality validation runs it will not be able to get the
value before type cast, so it uses the type casted value
which will be `0.0` and the validation will match.

    subject = MyModel.new(aliased_balance: "abcd")
    subject.valid?

But if we declare MyModel like this

    class MyModel < ActiveRecord::Base
      validates :balance, numericality: { greater_than_or_equal_to: 0 }
    end

and assign "abcd" value to `balance` when the validations
run the model will be invalid because activerecord will be able
to get the value before type cast.

With this change `read_attribute_before_type_cast` will be able to
get the value before type cast even when the attr_name is an
attribute_alias.
2020-10-18 20:32:47 -03:00
Gannon McGibbon
bd90ed1630
Merge pull request #40379 from dmitry/40378-adds-same-record-on-build
Prevent adding the same record twice on build when using source record in attribute when using has_many_inversing  #40378
2020-10-16 18:10:02 -04:00
John Hawthorn
64b1c81532
Merge pull request #40387 from jhawthorn/exists_on_contradiction
Avoid query from exists? on contradictory relation
2020-10-15 11:06:54 -07:00
John Hawthorn
3b622bc0d6 Avoid query from exists? on contradictory relation
Co-authored-by: Adam Hess <HParker@github.com>
2020-10-14 12:30:10 -07:00
Jean Boussier
d6929e5a09 Handle binary strings in Active Record serialized columns
Serialized attributes stored in BLOB columns will be loaded
with the `ASCII-8BIT` (AKA BINARY) encoding.

So unless the serialized payload is pure ASCII, they need
to have the same internal encoding to be properly compared.

Since the serializer have no way to know how the string will
be stored, it's up to the column type to properly set the
encoding.
2020-10-14 11:57:34 +02:00
Eugene Kenny
55badbba3b Skip PostgreSQL interval tests on other adapters
Also remove an unnecessary magic encoding comment.
2020-10-13 21:08:40 +01:00
dmitry
50e4aaaf25
Prevent adding the same record twice on build when using source record in attribute when using has_many_inversing #40378
Record should be replaced via add_to_target, not added as a separate record to an array.
2020-10-13 19:24:02 +03:00
Eileen M. Uchitelle
2e8ca04906
Merge pull request #40366 from tgxworld/fix_flaky_advisory_lock_test
Fix flaky advisory lock test.
2020-10-13 09:09:22 -04:00
Eileen M. Uchitelle
9660f2a986
Merge pull request #40372 from alexrs/patch-1
Stable sorting for DatabaseConfigurations#find_db_config
2020-10-13 08:51:11 -04:00
Alejandro Rodríguez Salamanca
fae2df5f54 Update database_configurations.rb 2020-10-13 09:43:12 +01:00
Guo Xiang Tan
d2215580d8
Fix flaky advisory lock test.
Random failures due to active connection checking within the
assert_no_changes block.

Failure:
MigrationTest#test_with_advisory_lock_closes_connection [/rails/activerecord/test/cases/migration_test.rb:947]:
--- expected
+++ actual
@@ -1 +1 @@
-["SELECT 1", "SELECT 1"]
+["SELECT 1", "SELECT 1", "SELECT 1"]

This commit scopes the query down further to ensure that the advisory
unlock query is not left in pg_stat_activity which is an indication that
the connection pool was not disconnected.
2020-10-13 09:02:09 +08:00
Rafael França
2b1a400656
Merge pull request #39784 from marcrohloff/remove-magic-parameter-from-where
Remove the magic `:chain` parameter from `Relation#where`
2020-10-09 17:50:58 -04:00
Rafael Mendonça França
345de17caf
Handle Relations with limit in include? 2020-10-06 22:04:23 +00:00
Adrianna Chang
5aff638e2d
Fix ActiveRecord::Relation#include? in case where offset is provided 2020-10-06 15:30:16 -04:00
Eliot Sykes
ab20be81e3
Support passing record to uniqueness conditions 2020-10-05 14:12:42 +01:00
Eugene Kenny
ec4fe0a34a
Merge pull request #40285 from tgxworld/fix_flaky_test
Improve query used in `test_with_advisory_lock_closes_connection`.
2020-10-03 22:10:06 +01:00
Eugene Kenny
80e73ad7c1 Return rows affected from batched update_all and delete_all
The previous return value of nil was undocumented and inconsistent with
the non-batched versions of these methods.

Also lean on `each` to create the batches, and add API documentation for
`update_all`, `delete_all`, and `destroy_all` on `BatchEnumerator`.
2020-10-03 18:27:26 +01:00
George Claghorn
166b63eb67
Optimize ActiveRecord::Relation#include? on an unloaded relation
Perform an efficient existence query through #exists? instead of loading the entire relation into memory and searching it.

Before:

    > Person.where(name: "David").include?(david)
    SELECT `people`.* FROM `people` WHERE `people`.`name` = 'David'
    => true

After:

    > Person.where(name: "David").include?(david)
    SELECT 1 AS one FROM `people` WHERE `people`.`name` = 'David' AND `people`.`id` = 1 LIMIT 1
    => true
2020-10-02 21:52:07 -04:00
Ryuta Kamizono
ab9cb60076 Remove duplicated code in type_to_sql
The `interval` with precision handling already exists in the codebase.

c5d9aab37b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb (L1237-L1242)
2020-10-03 02:14:52 +09:00
Jonathan Hefner
efeafd39e9 Fix Mysql2ActiveSchemaTest test
Follow-up to 0e4473ca935f7b0f7c95abf2bbe41ee2c61abcaa.

`row_format_dynamic_by_default?` is a private method.
2020-10-02 11:44:30 -05:00
Akira Matsuda
6515fdbbda Attribute reader / writer methods are defined as public methods 2020-10-02 22:23:38 +09:00
Akira Matsuda
1a237904c4 to_json and from_json are public methods 2020-10-02 22:22:10 +09:00
Akira Matsuda
eec73f77a4 value_methods on Relation are public methods 2020-10-02 22:22:10 +09:00
Akira Matsuda
68583dc23e Association#class_name and table_name are public methods 2020-10-02 22:22:10 +09:00
Akira Matsuda
f30f9453d8 Methods for locking_column are supposed to be public methods 2020-10-02 22:22:10 +09:00
Akira Matsuda
3da4f2b46b Timestamp column methods are public methods 2020-10-02 22:22:10 +09:00
Akira Matsuda
c93b38c0b1 ConnectionPool#disconnect and clear_reloadable_connections are public methods 2020-10-02 22:22:10 +09:00
Akira Matsuda
7acee4d7c0 Association setter methods are defined as public methods 2020-10-02 22:22:10 +09:00
Akira Matsuda
e57d9202a7 Methods with association name are defined as public methods 2020-10-02 22:22:10 +09:00
Akira Matsuda
632cb80c1b includes, preload, joins, and eager_load are public methods 2020-10-02 21:57:42 +09:00
Akira Matsuda
0e4473ca93 All these tested schema methods are public 2020-10-02 21:55:37 +09:00
Akira Matsuda
4e525a2a2b Every foreign_key column is supposed to be a public method 2020-10-02 21:53:56 +09:00
Akira Matsuda
b0f7b5fba6 Every primary_key column is supposed to be a public method
In fact it's already been called via public_send
https://github.com/rails/rails/blob/702add64/activerecord/lib/active_record/associations/collection_association.rb#L63
2020-10-02 21:51:10 +09:00
Akira Matsuda
fd9d82a238 count, avarage, maximum, minimum, and sum are defined as public methods in Arel::Expressions 2020-10-02 21:45:58 +09:00
Akira Matsuda
954b8c9600 Methods given via :dependent are supposed to be public 2020-10-02 21:37:23 +09:00
Akira Matsuda
e3da5546ed Relation methods are public 2020-10-02 21:36:35 +09:00
Akira Matsuda
7dc034f269 Arel::Node#asc and :desc are public methods 2020-10-02 20:58:45 +09:00
Akira Matsuda
3fc64f53a0 Association#klass is a public method 2020-10-02 20:56:50 +09:00
Akira Matsuda
08cf764318 Association#destroy_all, reset, and delete_all are public methods 2020-10-02 20:56:09 +09:00
Akira Matsuda
63c20507b3 Relation#delete_all, update_all, and destroy_all are public methods 2020-10-02 20:54:27 +09:00
Akira Matsuda
28c27cdf0b AR adapter_method is supposed to be defined as a public method 2020-10-02 20:53:35 +09:00
Akira Matsuda
ffd03692b1 touch and touch_later are both defined as public methods 2020-10-02 20:50:02 +09:00
Akira Matsuda
b719bb064d String#rjust and ljust are public methods 2020-10-02 20:42:31 +09:00
Akira Matsuda
9c0a6642d4 up and down are supposed to be public methods 2020-10-02 20:41:56 +09:00
Akira Matsuda
fe786824d9 Time.utc and Time.local are public methods 2020-10-02 20:38:54 +09:00
Akira Matsuda
5d33ff4532 Module#define_method is a public method 2020-10-02 20:37:15 +09:00
Andrey Novikov
e5a5cc4835 Add support for PostgreSQL interval datatype.
Add support for PostgreSQL `interval` data type with conversion to
`ActiveSupport::Duration` when loading records from database and
serialization to ISO 8601 formatted duration string on save.
Add support to define a column in migrations and get it in a schema dump.
Optional column precision is supported.

To use this in 6.1, you need to place the next string to your model file:

    attribute :duration, :interval

To keep old behavior until 6.2 is released:

    attribute :duration, :string
2020-09-30 14:54:55 -07:00
Guo Xiang Tan
7bc6ccaf73
Improve query used in test_with_advisory_lock_closes_connection.
The test is flaky and I can't reproduce it locally. Using the count does
not provide any useful information on failure so we select the queries
instead.
2020-09-30 09:32:54 +08:00
Rafael França
57bd5f0df8
Merge pull request #39700 from cheshire137/master
Replace space/hyphen in enum scope names
2020-09-28 15:19:09 -04:00
Gannon McGibbon
11bd634ae4 Revert "Refactor uncastable through reflection test to detect join key overrides" 2020-09-25 17:49:52 -04:00