aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
Commit message (Collapse)AuthorAgeFilesLines
* Fix dirty tracking after rollback.Ryuta Kamizono2019-04-167-25/+27
| | | | | | | | | | | | | | | | | Currently the rollback only restores primary key value, `new_record?`, `destroyed?`, and `frozen?`. Since the `save` clears current dirty attribute states, retrying save after rollback will causes no change saved if partial writes is enabled (by default). This makes `remember_transaction_record_state` remembers original values then restores dirty attribute states after rollback. Fixes #15018. Fixes #30167. Fixes #33868. Fixes #33443. Closes #33444. Closes #34504.
* Don't refer `@transaction_state` directlyRyuta Kamizono2019-04-161-5/+5
| | | | | | | | | | | | | Since 8180c39, remaining transaction state is cleared in `force_clear_transaction_record_state` to less work `sync_with_transaction_state`. But it caused a race condition that `@transaction_state` would be cleared by other threads if the state is finalized. To work as before, snapshot `@transaction_state` to local variable not to refer `@transaction_state` directly. Fixes #35983.
* Merge pull request #35899 from ↵Eileen M. Uchitelle2019-04-151-10/+17
|\ | | | | | | | | eileencodes/fix-connection-when-handler-doesnt-exist Ensure a handler is set when using `connected_to`
| * Ensure a handler is set when using `connected_to`eileencodes2019-04-081-10/+17
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | After looking at #35800 there is definitely an issue in the `connected_to` method although it's generally behaving. Here are the details: 1) I added a default connection role - writing - to the connection handler lookup. I did this because otherwise if you did this: ``` connected_to(databse: :development) ``` And development wasn't a pre-established role it would create a new handler and connect using that. I don't think this is right so I've updated it to pick up the default (:writing) unless otherwise specified. To set a handler when using the database version pass a hash like you would to `connects_to`: ``` connected_to(database: { readonly_slow: :development }) ``` This will connect the `development` database to the `readonly_slow` handler/role. 2) I updated the tests to match this behavior that we expect. 3) I updated the documentation to clarify that using `connected_to` with a `database` key will establish a new connection every time. This is exactly how `establish_connection` behaves and I feel this is correct. If you want to only establish a connection once you should do that in the model with `connects_to` and then swap on the role instead of on the database hash/key. 4) In regards to #35800 this fixes the case where you pass a symbol to the db and not a hash. But it doesn't fix a case where you may pass an unknown handler to an abstract class that's not connected. This is tricky because technical AbstractFoo doesn't have any connections except for through ApplicationRecord, so in the case of the application that was shared we should only be swapping connections on ActiveRecord::Base because there are no other actual connections - AbstractFoo isn't needed since it's not establishing a new connection. If we need AbstractFoo to connect to a new handler we should establish that connection with the handler in AbstractFoo before trying to shard there.
* | Fix dirty tracking for `touch`Ryuta Kamizono2019-04-153-5/+29
| | | | | | | | | | | | | | | | | | | | Before this fix, `touch` only clears dirty tracking for touched attributes, doesn't track saved (touched) changes. This fixes that tracks saved changes and carry over remaining changes. Fixes #33429. Closes #34306.
* | Merge pull request #35970 from yskkin/reversible_commentRyuta Kamizono2019-04-155-7/+55
|\ \ | | | | | | make change_column_comment and change_table_comment invertible
| * | make change_column_comment and change_table_comment invertibleYoshiyuki Kinjo2019-04-155-7/+55
| | | | | | | | | | | | | | | | | | | | | | | | | | | We can revert migrations using `change_column_comment` or `change_table_comment` at current master. However, results are not what we expect: comments are remained in new status. This change tells previous comment to these methods in a way like `change_column_default`.
* | | Refactor `sync_with_transaction_state`Ryuta Kamizono2019-04-151-8/+14
| | | | | | | | | | | | | | | | | | | | | All transaction states (:committed, :fully_committed, :rolledback, :fully_rolledback) are mutually exclusive. And also, `force_clear_transaction_record_state` should clear `@transaction_state` as well.
* | | Remove useless `update_attributes_from_transaction_state` and ↵Ryuta Kamizono2019-04-141-14/+3
| | | | | | | | | | | | `set_transaction_state`
* | | Don't expose `add_to_transaction`Ryuta Kamizono2019-04-142-14/+8
|/ / | | | | | | | | | | | | | | | | | | | | | | | | | | `add_to_transaction` was added at da840d1, but it should not be called by except internal, since `remember_transaction_record_state` should be called only once before saving. And also, currently `add_to_transaction` doesn't always add the record to transaction since da8de91, that is the reason hard to use that even in internal. Even if `add_to_transaction` ensure to add the record to transaction, that is an internal concern, people don't need to explicitly call `add_to_transaction`.
* | Merge pull request #35958 from yskkin/bulk_change_tableRyuta Kamizono2019-04-143-41/+26
|\ \ | | | | | | use PostgreSQL's bulk_alter_table implementation
| * | use PostgreSQL's bulk_alter_table implementationYoshiyuki Kinjo2019-04-133-41/+26
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Running this migration on mysql at current master fails because `add_references_for_alter` is missing. ``` change_table :users, bulk: true do |t| t.references :article end ``` This is also true for postgresql adapter, but its `bulk_alter_table` implementation can fallback in such case. postgresql's implementation is desirable to prevent unknown failure like this.
* | | Improve wording of commentsChris Salzberg2019-04-133-3/+3
|/ / | | | | | | | | | | Most of the time, these methods are called from actual methods defined from columns in the schema, not from method_missing, so the current wording is misleading.
* | Merge pull request #35918 from ↵Ryuta Kamizono2019-04-122-2/+2
|\ \ | | | | | | | | | | | | kamipo/lazy_sync_with_transaction_state_on_destroy Lazy sync with transaction state on destroy
| * | Lazy sync with transaction state on destroyRyuta Kamizono2019-04-102-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This reverts commit 58410b3d566e6b93c7b71c0eec0fc11ec906b68e. If we have any implicit commit/rollback callbacks, it is necessary to add record to transaction explicitly like `:touch_deferred_attributes`. https://github.com/rails/rails/blob/5f261d04d6f857d49c75124df809adfbd6cd5b5e/activerecord/lib/active_record/touch_later.rb#L9 https://github.com/rails/rails/blob/5f261d04d6f857d49c75124df809adfbd6cd5b5e/activerecord/lib/active_record/touch_later.rb#L25 But I can't find any other implicit commit/rollback callbacks in our code base at least now. I think the `self.class.connection.add_transaction_record(self)` line doesn't cover any behavior.
* | | Merge pull request #35920 from ↵Ryuta Kamizono2019-04-121-1/+11
|\ \ \ | | | | | | | | | | | | | | | | kamipo/dont_call_commit_callbacks_for_invalid_record Don't call after_commit callbacks despite a record isn't saved
| * | | Don't call after_commit callbacks despite a record isn't savedRyuta Kamizono2019-04-121-1/+11
| |/ / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Regardless of a record isn't saved (e.g. validation is failed), `after_commit` / `after_rollback` callbacks are invoked for now. To fix the issue, this adds a record to the current transaction only when a record is actually saved. Fixes #29747. Closes #29833.
* | | Merge pull request #28830 from kamipo/dont_regard_extension_block_as_scopeRyuta Kamizono2019-04-122-30/+10
|\ \ \ | | | | | | | | Fix `automatic_inverse_of` not to be disabled if extension block is given
| * | | Fix `automatic_inverse_of` not to be disabled if extension block is givenRyuta Kamizono2019-04-122-30/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | If an association has a scope, `automatic_inverse_of` is to be disabled. But extension block is obviously not a scope. It should not be regarded as a scope. Fixes #28806.
* | | | Refactor around sql_type metadata and columnRyuta Kamizono2019-04-124-33/+35
|/ / / | | | | | | | | | | | | | | | * remove useless `@type_metadata` and `@array` * move the compatibility code (for array) into column * etc.
* | | Merge pull request #35921 from Shopify/deduplicate-activerecord-stringsRafael França2019-04-111-2/+2
|\ \ \ | | | | | | | | Deduplicate Active Record reflection names
| * | | Deduplicate Active Record reflection namesJean Boussier2019-04-101-2/+2
| | | |
* | | | Merge pull request #35922 from ↵Rafael França2019-04-112-80/+80
|\ \ \ \ | | | | | | | | | | | | | | | | | | | | michaelglass/move-sqlite-3-database-statements-into-database-statements make SQLite3 `last_inserted_id` private and organize DatabaseStatement methods
| * | | | moves sqlite3 methods that mirror Abstract::DatabaseStatements into ↵Michael Glass2019-04-102-80/+80
| | |/ / | |/| | | | | | | | | | Sqlite3::DatabaseStatements and make those that are private in Abstract::DatabaseStatements private for sqlite3
* | | | Merge pull request #35933 from kamipo/refactor_dirty_trackingRyuta Kamizono2019-04-121-8/+6
|\ \ \ \ | | | | | | | | | | PERF: 2x ~ 30x faster dirty tracking
| * | | | PERF: 2x ~ 30x faster dirty trackingRyuta Kamizono2019-04-111-8/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently, although using both dirty tracking (ivar backed and attributes backed) on one model is not supported (doesn't fully work at least), both dirty tracking are being performed, that is very slow. As long as attributes backed dirty tracking is used, ivar backed dirty tracking should not need to be performed. I've refactored to extract new `ForcedMutationTracker` which only tracks `force_change` to be performed for ivar backed dirty tracking, that makes dirty tracking on Active Record 2x ~ 30x faster. https://gist.github.com/kamipo/971dfe0891f0fe1ec7db8ab31f016435 Before: ``` Warming up -------------------------------------- changed? 4.467k i/100ms changed 5.134k i/100ms changes 3.023k i/100ms changed_attributes 4.358k i/100ms title_change 3.185k i/100ms title_was 3.381k i/100ms Calculating ------------------------------------- changed? 42.197k (±28.5%) i/s - 187.614k in 5.050446s changed 50.481k (±16.0%) i/s - 246.432k in 5.045759s changes 30.799k (± 7.2%) i/s - 154.173k in 5.030765s changed_attributes 51.530k (±14.2%) i/s - 252.764k in 5.041106s title_change 44.667k (± 9.0%) i/s - 222.950k in 5.040646s title_was 44.635k (±16.6%) i/s - 216.384k in 5.051098s ``` After: ``` Warming up -------------------------------------- changed? 24.130k i/100ms changed 13.503k i/100ms changes 6.511k i/100ms changed_attributes 9.226k i/100ms title_change 48.221k i/100ms title_was 96.060k i/100ms Calculating ------------------------------------- changed? 245.478k (±16.1%) i/s - 1.182M in 5.015837s changed 157.641k (± 4.9%) i/s - 796.677k in 5.066734s changes 70.633k (± 5.7%) i/s - 358.105k in 5.086553s changed_attributes 95.155k (±13.6%) i/s - 470.526k in 5.082841s title_change 566.481k (± 3.5%) i/s - 2.845M in 5.028852s title_was 1.487M (± 3.9%) i/s - 7.493M in 5.046774s ```
* | | | | adjust styletakakuda2019-04-111-17/+17
|/ / / /
* / / / Adding type option example to the documentation [ci skip] (#35917)Roberto Miranda2019-04-101-0/+1
|/ / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Adding type option example to the documentation [ci skip] It was hard for me looking https://api.rubyonrails.org/ to find that there was a type option. Adding this to the doc would be helpful especially for application with old tables where the references are still an integer not bigint * Update activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb Co-Authored-By: robertomiranda <rjmaltamar@gmail.com>
* | | Merge pull request #28155 from lcreid/belongs_toRyuta Kamizono2019-04-101-2/+6
|\ \ \ | | | | | | | | | | | | Fix "autosave: true" on belongs_to of join model causes invalid records to be saved
| * | | Fix circular `autosave: true`Larry Reid2018-07-231-2/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Use a variable local to the `save_collection_association` method in `activerecord/lib/active_record/autosave_association.rb`, instead of an instance variable. Prior to this PR, when there was a circular series of `autosave: true` associations, the callback for a `has_many` association was run while another instance of the same callback on the same association hadn't finished running. When control returned to the first instance of the callback, the instance variable had changed, and subsequent associated records weren't saved correctly. Specifically, the ID field for the `belongs_to` corresponding to the `has_many` was `nil`. Remove unnecessary test and comments. Fixes #28080.
* | | | Accidentally lost `comment` in `Column#==` and `Column#hash`Ryuta Kamizono2019-04-101-2/+4
| | | | | | | | | | | | | | | | Refer #35875.
* | | | Remove unused `sequence_name` in `sql_for_insert`Ryuta Kamizono2019-04-102-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | All adapters (sqlite3, mysql2, postgresql, oracle-enhanced, sqlserver) doesn't use `sequence_name` in `sql_for_insert`. https://github.com/rsim/oracle-enhanced/blob/4e0db270a93859c9713fd079dbb315b9fe550e57/lib/active_record/connection_adapters/oracle_enhanced/database_statements.rb#L79-L85 https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/959fe8f49744460b876bc205c73259f8d4f37629/lib/active_record/connection_adapters/sqlserver/database_statements.rb#L226-L249 It can be handled in `exec_insert` like postgresql adapter if we want.
* | | | There is no need to create `QueryAttribute` to just type cast a valueRyuta Kamizono2019-04-102-4/+2
| | | |
* | | | Merge pull request #35875 from Shopify/alloc-free-comparisonsRafael França2019-04-094-32/+38
|\ \ \ \ | | | | | | | | | | Improve == and hash methods on various schema cache structs to be allocation free.
| * | | | Improve == and hash methods on various schema cache structs to be allocation ↵Jean Boussier2019-04-094-32/+38
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | free. The previous implementation would allocate 2 arrays per comparisons. I tried relying on Struct, but they do allocate one Hash inside `Struct#hash`.
* | | | | Clarify exists check in logsDan Fitch2019-04-091-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The default log messages for Model.exists?, when called from .save on an object which uses scoped uniqueness validation like: class Example < ApplicationRecord validates :field, uniqueness: {scope: parent_id} end can result in slightly misleading logs. An example case: ↳ app/controllers/example_controller.rb:23 (0.2ms) begin transaction ↳ app/controllers/example_controller.rb:39 Example Exists (0.2ms) SELECT 1 AS one FROM "examples" WHERE "examples"."field" IS NULL AND "examples"."parent_id" = ? LIMIT ? [["parent_id", 123], ["LIMIT", 1]] ↳ app/controllers/example_controller.rb:39 (0.1ms) rollback transaction To me, a Rails newbie, this parsed as the following: - started the transaction to create a thing - found that your object exists already! - so we rolled back the transaction (even though the actual cause of the transaction is something that happens after the Exists check.) All this does is add a question mark to the message, to make it clear in the log that this is a check, not a confirmation. This may be kind of silly, but it may save some future goofs by newbs like me.
* | | | | Merge pull request #35909 from simi/alias-postgresql-adapterRyuta Kamizono2019-04-101-0/+1
|\ \ \ \ \ | | | | | | | | | | | | Bring back postgresql_version as an alias.
| * | | | | Bring back postgresql_version as an alias.Josef Šimánek2019-04-091-0/+1
| |/ / / /
* | | | | Remove duplicated attribute alias resolution in `_select!`Ryuta Kamizono2019-04-091-6/+3
| | | | | | | | | | | | | | | | | | | | This is also resolved in `arel_column`.
* | | | | `get_database_version` is not public API [ci skip]Ryuta Kamizono2019-04-091-1/+1
|/ / / /
* | | | Fix upsert method commentRyo Hashimoto2019-04-091-1/+1
| | | | | | | | | | | | | | | | | | | | Because this method only updates or inserts a single record like `insert` method.
* | | | Merge pull request #34800 from mqchau/mysqlCountDeleteRowInLockMatthew Draper2019-04-091-1/+3
|\ \ \ \ | | | | | | | | | | Wrap Mysql count of deleted rows in lock block to avoid conflict in test
| * | | | Wrap Mysql count of deleted rows in lock block to avoid conflict in testQuan Chau2019-04-081-1/+3
| | | | |
* | | | | Merge pull request #35887 from kamipo/argument_errorRyuta Kamizono2019-04-093-6/+6
|\ \ \ \ \ | | | | | | | | | | | | Raise `ArgumentError` for invalid `:limit` and `:precision` like as other options
| * | | | | Raise `ArgumentError` for invalid `:limit` and `:precision` like as other ↵Ryuta Kamizono2019-04-073-6/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | options When I've added new `:size` option in #35071, I've found that invalid `:limit` and `:precision` raises `ActiveRecordError` unlike other invalid options. I think that is hard to distinguish argument errors and statement invalid errors since the `StatementInvalid` is a subclass of the `ActiveRecordError`. https://github.com/rails/rails/blob/c9e4c848eeeb8999b778fa1ae52185ca5537fffe/activerecord/lib/active_record/errors.rb#L103 ```ruby begin # execute any migration rescue ActiveRecord::StatementInvalid # statement invalid rescue ActiveRecord::ActiveRecordError, ArgumentError # `ActiveRecordError` except `StatementInvalid` is maybe an argument error end ``` I'd say this is the inconsistency worth fixing. Before: ```ruby add_column :items, :attr1, :binary, size: 10 # => ArgumentError add_column :items, :attr2, :decimal, scale: 10 # => ArgumentError add_column :items, :attr3, :integer, limit: 10 # => ActiveRecordError add_column :items, :attr4, :datetime, precision: 10 # => ActiveRecordError ``` After: ```ruby add_column :items, :attr1, :binary, size: 10 # => ArgumentError add_column :items, :attr2, :decimal, scale: 10 # => ArgumentError add_column :items, :attr3, :integer, limit: 10 # => ArgumentError add_column :items, :attr4, :datetime, precision: 10 # => ArgumentError ```
* | | | | | Merge pull request #35890 from kamipo/except_table_name_from_columnRyuta Kamizono2019-04-097-49/+48
|\ \ \ \ \ \ | |_|_|_|_|/ |/| | | | | Except `table_name` from column objects
| * | | | | Except `table_name` from column objectsRyuta Kamizono2019-04-087-49/+48
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The `table_name` was added at #23677 to detect whether serial column or not correctly. We can do that detection before initialize column object, it makes column object size smaller, and it probably helps column object de-duplication.
* | | | | | Merge pull request #35892 from ryohashimoto/bulk_insert_logsEileen M. Uchitelle2019-04-081-1/+4
|\ \ \ \ \ \ | | | | | | | | | | | | | | Improve log messages for #insert_all` / `#upsert_all` etc. methods
| * | | | | | Improve log messages for #insert_all` / `#upsert_all` / `#insert` / `#upsert ↵Ryo Hashimoto2019-04-081-1/+4
| | |/ / / / | |/| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | etc. methods In #35077, `#insert_all` / `#upsert_all` / `#insert` / `#upsert` etc. methods are added. But Active Record logs only “Bulk Insert” log messages when they are invoked. This commit improves the log messages to use collect words for how invoked them.
* | | | | | When skipping duplicates in bulk insert on MySQL, avoid assigning id when ↵Bob Lail2019-04-082-4/+4
| |/ / / / |/| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | not specified If `id` is an `AUTONUMBER` column, then my former strategy here of assigning `no_op_column` to an arbitrary column would fail in this specific scenario: 1. `model.columns.first` is an AUTONUMBER column 2. `model.columns.first` is not assigned in the insert attributes I added three tests: the first test covers the actual error; the second test documents that this _isn't_ a problem when a value is given for the AUTONUMBER column and the third test ensures that this no-op strategy isn't secretly doing an UPSERT.