aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
Commit message (Collapse)AuthorAgeFilesLines
...
* | | option to disable scopes that `ActiveRecord.enum` generates by defaultAlfred Dominic2018-12-041-0/+9
| | |
* | | Merge pull request #34609 from kamipo/delete_all_on_collection_proxyRyuta Kamizono2018-12-042-6/+16
|\ \ \ | | | | | | | | | | | | Ensure that `delete_all` on collection proxy returns affected count
| * | | Ensure that `delete_all` on collection proxy returns affected countRyuta Kamizono2018-12-042-6/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Unlike the `Relation#delete_all`, `delete_all` on collection proxy doesn't return affected count. Since the `CollectionProxy` is a subclass of the `Relation`, this inconsistency is probably not intended, so it should return the count consistently.
* | | | Reset scope after collection deleteGannon McGibbon2018-12-042-0/+64
|/ / / | | | | | | | | | | | | Reset scope after delete on collection association to clear stale offsets of removed records.
* | | Fix NumericData.average test on ruby 2.6Abdallah Samman2018-12-031-2/+6
| | |
* | | Assigned but unused variable - birdutilum2018-12-031-1/+1
| | | | | | | | | | | | See: https://travis-ci.org/rails/rails/jobs/462233144#L1384
* | | Add ability to prevent writes to a databaseEileen Uchitelle2018-11-304-0/+206
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This PR adds the ability to prevent writes to a database even if the database user is able to write (ie the database is a primary and not a replica). This is useful for a few reasons: 1) when converting your database from a single db to a primary/replica setup - you can fix all the writes on reads early on, 2) when we implement automatic database switching or when an app is manually switching connections this feature can be used to ensure reads are reading and writes are writing. We want to make sure we raise if we ever try to write in read mode, regardless of database type and 3) for local development if you don't want to set up multiple databases but do want to support rw/ro queries. This should be used in conjunction with `connected_to` in write mode. For example: ``` ActiveRecord::Base.connected_to(role: :writing) do Dog.connection.while_preventing_writes do Dog.create! # will raise because we're preventing writes end end ActiveRecord::Base.connected_to(role: :reading) do Dog.connection.while_preventing_writes do Dog.first # will not raise because we're not writing end end ```
* | | Merge pull request #34572 from kamipo/fix_scoping_with_query_methodRyuta Kamizono2018-11-302-1/+7
|\ \ \ | | | | | | | | Fix the scoping with query methods in the scope block
| * | | Fix the scoping with query methods in the scope blockRyuta Kamizono2018-11-302-1/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Follow up #33394. #33394 only fixes the case of scoping with klass methods in the scope block which invokes `klass.all`. Query methods in the scope block also need to invoke `klass.all` to be affected by the scoping.
* | | | Allow aliased attributes in updateGannon McGibbon2018-11-291-0/+6
| | | | | | | | | | | | | | | | Allow aliased attributes to be used in `#update_columns` and `#update`.
* | | | Allow spaces in postgres table namesGannon McGibbon2018-11-281-0/+5
| |/ / |/| | | | | | | | | | | Fixes issue where "user post" is misinterpreted as "\"user\".\"post\"" when quoting table names with the postgres adapter.
* | | Merge pull request #33835 from schneems/schneems/faster_cache_versionSean Griffin2018-11-271-2/+80
|\ \ \ | | | | | | | | Use raw time string from DB to generate ActiveRecord#cache_version
| * | | Do not silently fail to generate a cache_versionschneems2018-10-171-0/+8
| | | | | | | | | | | | | | | | | | | | | | | | When an `updated_at` column exists on the model, but is not available on the instance (likely due to a select), we should raise an error rather than silently not generating a cache_version. Without this behavior it's likely that cache entries will not be able to be invalidated and this will happen without notice. This behavior was reported and described by @lsylvester in https://github.com/rails/rails/pull/34197#issuecomment-429668759.
| * | | Use raw time string from DB to generate ActiveRecord#cache_versionschneems2018-10-171-2/+72
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently, the `updated_at` field is used to generate a `cache_version`. Some database adapters return this timestamp value as a string that must then be converted to a Time value. This process requires a lot of memory and even more CPU time. In the case where this value is only being used for a cache version, we can skip the Time conversion by using the string value directly. - This PR preserves existing cache format by converting a UTC string from the database to `:usec` format. - Some databases return an already converted Time object, in those instances, we can directly use `created_at`. - The `updated_at_before_type_cast` can be a value that comes from either the database or the user. We only want to optimize the case where it is from the database. - If the format of the cache version has been changed, we cannot apply this optimization, and it is skipped. - If the format of the time in the database is not UTC, then we cannot use this optimization, and it is skipped. Some databases (notably PostgreSQL) returns a variable length nanosecond value in the time string. If the value ends in a zero, then it is truncated For instance instead of `2018-10-12 05:00:00.000000` the value `2018-10-12 05:00:00` is returned. We detect this case and pad the remaining zeros to ensure consistent cache version generation. Before: Total allocated: 743842 bytes (6626 objects) After: Total allocated: 702955 bytes (6063 objects) (743842 - 702955) / 743842.0 # => 5.4% ⚡️⚡️⚡️⚡️⚡️ Using the CodeTriage application and derailed benchmarks this PR shows between 9-11% (statistically significant) performance improvement versus the commit before it. Special thanks to @lsylvester for helping to figure out a way to preserve the usec format and for helping with many implementation details.
* | | | Merge pull request #34528 from DmitryTsepelev/fix-ignored-attributesRafael França2018-11-273-0/+26
|\ \ \ \ | |_|/ / |/| | | Additional types of ResultSet should not contain keys of #attributes_to_define_after_schema_loads
| * | | Cached columns_hash fields should be excluded from ResultSet#column_typesDmitryTsepelev2018-11-273-0/+26
| | | |
* | | | More exercise singular association queryRyuta Kamizono2018-11-284-7/+14
| | | | | | | | | | | | | | | | Follow up ba4e68f577efc76f351d30a2914e29942b97830e.
* | | | Ensure that singular association should execute limited queryRyuta Kamizono2018-11-282-5/+12
|/ / /
* | | Make implicit order column configurableTekin Suleyman2018-11-261-0/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When calling ordered finder methods such as +first+ or +last+ without an explicit order clause, ActiveRecord sorts records by primary key. This can result in unpredictable and surprising behaviour when the primary key is not an auto-incrementing integer, for example when it's a UUID. This change makes it possible to override the column used for implicit ordering such that +first+ and +last+ will return more predictable results. For Example: class Project < ActiveRecord::Base self.implicit_order_column = "created_at" end
* | | Merge pull request #34520 from yahonda/bump_pg93Rafael França2018-11-265-383/+361
|\ \ \ | | | | | | | | Bump the minimum version of PostgreSQL to 9.3
| * | | Bump the minimum version of PostgreSQL to 9.3Yasuo Honda2018-11-255-383/+361
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | https://www.postgresql.org/support/versioning/ - 9.1 EOLed on September 2016. - 9.2 EOLed on September 2017. 9.3 is also not supported since Nov 8, 2018. https://www.postgresql.org/about/news/1905/ I think it may be a little bit early to drop PostgreSQL 9.3 yet. * Deprecated `supports_ranges?` since no other databases support range data type * Add `supports_materialized_views?` to abstract adapter Materialized views itself is supported by other databases, other connection adapters may support them * Remove `with_manual_interventions` It was only necessary for PostgreSQL 9.1 or earlier * Drop CI against PostgreSQL 9.2
* | | | Merge pull request #34521 from azbshiri/mysql-exec-query-resultEileen M. Uchitelle2018-11-261-0/+5
|\ \ \ \ | | | | | | | | | | Test when using MySQL `exec_query` returns `ActiveRecord::Result` all…
| * | | | When running exec_query MySQL always returns ActiveRecord::ResultAlireza Bashiri2018-11-251-0/+5
| |/ / / | | | | | | | | | | | | | | | | When running `exec_query` with `INSERT` (or other write commands), MySQL returns `ActiveRecord::Result`.
* | | | Fix random CI failure due to non-deterministic sorting orderRyuta Kamizono2018-11-261-2/+2
| | | | | | | | | | | | | | | | https://travis-ci.org/rails/rails/jobs/459534536#L1280
* | | | SQLite 3.7.16+ returns the order of the primary key columnsYasuo Honda2018-11-251-2/+0
|/ / / | | | | | | | | | | | | | | | | | | | | | https://www.sqlite.org/releaselog/3_7_16.html > 9 Enhance the PRAGMA table_info command so that the "pk" column is an increasing integer to show the order of columns in the primary key. Rails 6 supports SQLite 3.8 then we can remove this skip condition.
* | | Raises error when attempting to modify enum valuesebyrds2018-11-231-0/+14
| | |
* | | Merge pull request #34468 from gmcgibbon/redact_sql_in_errorsRafael França2018-11-233-14/+44
|\ \ \ | | | | | | | | Redact SQL in errors
| * | | Redact SQL in errorsGannon McGibbon2018-11-223-14/+44
| | | | | | | | | | | | | | | | | | | | Move `ActiveRecord::StatementInvalid` SQL to error property. Also add bindings as an error property.
* | | | Use squiggly heredoc to strip odd indentation in the executed SQLRyuta Kamizono2018-11-2213-69/+59
|/ / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Before: ``` LOG: execute <unnamed>: SELECT t.oid, t.typname FROM pg_type as t WHERE t.typname IN ('int2', 'int4', 'int8', 'oid', 'float4', 'float8', 'bool') LOG: execute <unnamed>: SELECT t.oid, t.typname, t.typelem, t.typdelim, t.typinput, r.rngsubtype, t.typtype, t.typbasetype FROM pg_type as t LEFT JOIN pg_range as r ON oid = rngtypid WHERE t.typname IN ('int2', 'int4', 'int8', 'oid', 'float4', 'float8', 'text', 'varchar', 'char', 'name', 'bpchar', 'bool', 'bit', 'varbit', 'timestamptz', 'date', 'money', 'bytea', 'point', 'hstore', 'json', 'jsonb', 'cidr', 'inet', 'uuid', 'xml', 'tsvector', 'macaddr', 'citext', 'ltree', 'interval', 'path', 'line', 'polygon', 'circle', 'lseg', 'box', 'time', 'timestamp', 'numeric') OR t.typtype IN ('r', 'e', 'd') OR t.typinput::varchar = 'array_in' OR t.typelem != 0 LOG: statement: SHOW TIME ZONE LOG: statement: SELECT 1 LOG: execute <unnamed>: SELECT COUNT(*) FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind IN ('r','v','m') -- (r)elation/table, (v)iew, (m)aterialized view AND c.relname = 'accounts' AND n.nspname = ANY (current_schemas(false)) ``` After: ``` LOG: execute <unnamed>: SELECT t.oid, t.typname FROM pg_type as t WHERE t.typname IN ('int2', 'int4', 'int8', 'oid', 'float4', 'float8', 'bool') LOG: execute <unnamed>: SELECT t.oid, t.typname, t.typelem, t.typdelim, t.typinput, r.rngsubtype, t.typtype, t.typbasetype FROM pg_type as t LEFT JOIN pg_range as r ON oid = rngtypid WHERE t.typname IN ('int2', 'int4', 'int8', 'oid', 'float4', 'float8', 'text', 'varchar', 'char', 'name', 'bpchar', 'bool', 'bit', 'varbit', 'timestamptz', 'date', 'money', 'bytea', 'point', 'hstore', 'json', 'jsonb', 'cidr', 'inet', 'uuid', 'xml', 'tsvector', 'macaddr', 'citext', 'ltree', 'interval', 'path', 'line', 'polygon', 'circle', 'lseg', 'box', 'time', 'timestamp', 'numeric') OR t.typtype IN ('r', 'e', 'd') OR t.typinput::varchar = 'array_in' OR t.typelem != 0 LOG: statement: SHOW TIME ZONE LOG: statement: SELECT 1 LOG: execute <unnamed>: SELECT COUNT(*) FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind IN ('r','v','m') -- (r)elation/table, (v)iew, (m)aterialized view AND c.relname = 'accounts' AND n.nspname = ANY (current_schemas(false)) ```
* | | Fixing an issue when parsing an opclass by allowing indexed columnThomas Bianchini2018-11-211-0/+12
| | | | | | | | | | | | | | | | | | | | | | | | in indexdef to be wrapped up by double quotes Fixes #34493. *Thomas Bianchini*
* | | Merge pull request #34491 from rails/fix-query-cache-on-multiple-connectionsEileen M. Uchitelle2018-11-211-0/+16
|\ \ \ | | | | | | | | Fix query cache for multiple connections
| * | | Fix query cache for multiple connectionsEileen Uchitelle2018-11-201-0/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently the query cache is only aware of one handler so once we added multiple databases switching on the handler we broke query cache for those reading connections. While #34054 is the proper fix, that fix is not straight forward and I want to make sure that the query cache isn't just broken for all other connections not in the main handler.
* | | | Make connection handler per thread instead of per fiberEileen Uchitelle2018-11-201-0/+37
|/ / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The connection handler was using the RuntimeRegistry which kind of implies it's a per thread registry. But it's actually per fiber. If you have an application that uses fibers and you're using multiple databases, when you switch the connection handler to swap connections new fibers running on the same thread used to get a different connection id. This PR changes the code to actually use a thread so that we get the same connection. Fixes https://github.com/rails/rails/issues/30047 [Eileen M. Uchitelle, Aaron Patterson, & Arthur Neeves]
* | | Merge pull request #33954 from ↵Aaron Patterson2018-11-201-0/+10
|\ \ \ | | | | | | | | | | | | | | | | febeling/inconsistent-assignment-has-many-through-33942 Fix handling of duplicates for `replace` on has_many-through
| * | | Fix handling of duplicates for `replace` on has_many-throughFlorian Ebeling2018-11-061-0/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | There was a bug in the handling of duplicates when assigning (replacing) associated records, which made the result dependent on whether a given record was associated already before being assigned anew. E.g. post.people = [person, person] post.people.count # => 2 while post.people = [person] post.people = [person, person] post.people.count # => 1 This change adds a test to provoke the former incorrect behavior, and fixes it. Cause of the bug was the handling of record collections as sets, and using `-` (difference) and `&` (union) operations on them indiscriminately. This temporary conversion to sets would eliminate duplicates. The fix is to decorate record collections for these operations, and only for the `has_many :through` case. It is done by counting occurrences, and use the record together with the occurrence number as element, in order to make them work well in sets. Given a, b = *Person.all then the collection used for finding the difference or union of records would be internally changed from [a, b, a] to [[a, 1], [b, 1], [a, 2]] for these operations. So a first occurrence and a second occurrence would be distinguishable, which is all that is necessary for this task. Fixes #33942.
* | | | Merge pull request #34453 from bogdanvlviv/exercise-connected_to-and-connects_toAaron Patterson2018-11-191-0/+35
|\ \ \ \ | | | | | | | | | | Exercise `connected_to` and `connects_to` methods
| * | | | Exercise `connected_to` and `connects_to` methodsbogdanvlviv2018-11-151-0/+35
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Since both methods are public API I think it makes sense to add these tests in order to prevent any regression in the behavior of those methods after the 6.0 release. Exercise `connected_to` - Ensure that the method raises with both `database` and `role` arguments - Ensure that the method raises without `database` and `role` Exercise `connects_to` - Ensure that the method returns an array of established connections(as mentioned in the docs of the method) Related to #34052
* | | | | Arel: Implemented DB-aware NULL-safe comparison (#34451)Dmytro Shteflyuk2018-11-1510-1/+358
|/ / / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Arel: Implemented DB-aware NULL-safe comparison * Fixed where clause inversion for NULL-safe comparison * Renaming "null_safe_eq" to "is_not_distinct_from", "null_safe_not_eq" to "is_distinct_from" [Dmytro Shteflyuk + Rafael Mendonça França]
* | | | Merge pull request #34437 from kbrock/union_all_parenRafael Mendonça França2018-11-132-10/+22
|\ \ \ \ | | | | | | | | | | | | | | | Fix: Arel now emits a single pair of parens for UNION and UNION ALL
| * | | | Emit single pair of parens for UNION and UNION ALLKeenan Brock2018-11-132-10/+22
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | mysql has a great implementation to suppress multiple parens for union sql statements. This moves that functionality to the generic implementation This also introduces that functionality for UNION ALL
* | | | | Merge pull request #34436 from gmcgibbon/fix_default_max_bind_length_sqliteRafael França2018-11-131-0/+20
|\ \ \ \ \ | |/ / / / |/| | | | Adjust bind length of SQLite to default (999)
| * | | | Adjust bind length of SQLite to default (999)Gannon McGibbon2018-11-131-0/+20
| | | | | | | | | | | | | | | | | | | | | | | | | Change `#bind_params_length` in SQLite adapter to return the default maximum amount (999). See https://www.sqlite.org/limits.html
* | | | | Merge pull request #34429 from ↵Rafael França2018-11-131-3/+21
|\ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | bogdanvlviv/ensure-that-connected_to-establish_connection Ensure that `ActiveRecord::Base#connected_to` with `:database` establishes connection
| * | | | | Ensure that `ActiveRecord::Base#connected_to` with `:database` establishes ↵bogdanvlviv2018-11-121-3/+21
| |/ / / / | | | | | | | | | | | | | | | | | | | | | | | | | connection Related to #34052
* / / / / Add support for UNLOGGED Postgresql tablesJacob Evelyn2018-11-131-0/+74
|/ / / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This commit adds support for the `ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.create_unlogged_tables` setting, which turns `CREATE TABLE` SQL statements into `CREATE UNLOGGED TABLE` statements. This can improve PostgreSQL performance but at the cost of data durability, and thus it is highly recommended that you *DO NOT* enable this in a production environment.
* | | | Fix test case for money schema defaultRyuta Kamizono2018-11-121-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Follow up a741208f80dd33420a56486bd9ed2b0b9862234a. Since a741208, `Decimal#serialize` which is superclass of `Money` type is no longer no-op, so it consistently serialize/deserialize a value as a decimal even if schema default.
* | | | Ensure casting by decimal attribute when queryingRyuta Kamizono2018-11-121-2/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Related 34cc301f03aea2e579d6687a9ea9782afc1089a0. `QueryAttribute#value_for_database` calls only `type.serialize`, and `Decimal#serialize` is a no-op unlike other attribute types. Whether or not `serialize` will invoke `cast` is undefined in our test cases, but it actually does not work properly unless it does so for now.
* | | | Test prepared statement cache only if prepared statements is enabledRyuta Kamizono2018-11-091-1/+1
| | | |
* | | | PostgreSQL: Properly quote all `Infinity` and `NaN`Ryuta Kamizono2018-11-091-2/+22
| | | | | | | | | | | | | | | | Since quoted `Infinity` and `NaN` are valid data for PostgreSQL.
* | | | Add an :if_not_exists option to create_tablefatkodima2018-11-081-0/+30
| | | | | | | | | | | | | | | | [fatkodima & Stefan Kanev]