Commit Graph

18574 Commits

Author SHA1 Message Date
Eileen M. Uchitelle b1fc1319df
Merge pull request #35795 from alimi/cache-database-version
Cache database version in schema cache
2019-04-03 11:23:47 -04:00
Ali Ibrahim 1c6e508ade Cache database version in schema cache
* The database version will get cached in the schema cache file during the
    schema cache dump. When the database version check happens, the version will
    be pulled from the schema cache and thus avoid querying the database for
    the version.
  * If the schema cache file doesn't exist, we'll query the database for the
    version and cache it on the schema cache object.
  * To facilitate this change, all connection adapters now implement
    #get_database_version and #database_version. #database_version returns the
    value from the schema cache.
  * To take advantage of the cached database version, the database version check
    will now happen after the schema cache is set on the connection in the
    connection pool.
2019-04-03 11:10:14 -04:00
Kasper Timm Hansen 9864f5e3d6
[ci skip] Touch up `db:prepare` changelog entry 2019-04-03 12:04:33 +02:00
Roberto Miranda eca847f4ba Add rake db:prepare entry to the CHANGELOG.md 2019-04-03 10:17:23 +01:00
Ryuta Kamizono 1799d91035 `bytes_margin` is only needed when `previous_packet` exists 2019-04-03 17:06:55 +09:00
Ryuta Kamizono 834716b2ee Don't assign to `@changed_attributes` in `becomes`
`@changed_attributes` is no longer used since #30985.
2019-04-03 14:49:11 +09:00
Ryuta Kamizono 96f0114e08 Use official database name [ci skip]
* s/Postgres/PostgreSQL/
* s/MYSQL/MySQL/, s/Mysql/MySQL/
* s/Sqlite/SQLite/

Replaced all newly added them after 6089b31.
2019-04-03 13:08:54 +09:00
Ryuta Kamizono bffacccc6a Fix the markup for `insert_all` and `upsert_all` docs [ci skip] 2019-04-03 12:46:53 +09:00
Ryuta Kamizono 2c44e22942 Remove duplicated CHANGELOG entry [ci skip] 2019-04-03 10:55:33 +09:00
David Heinemeier Hansson 59bed68d5e
Add `after_save_commit` callback as shortcut for `after_commit :hook, on: [ :create, :update ]`. (#35804) 2019-04-02 16:36:07 -07:00
Rafael França 6c6c579a9b
Merge pull request #35805 from excid3/attachments-field-generators
Add attachment and attachments field generators
2019-04-02 19:13:25 -04:00
Eileen M. Uchitelle 2c4dab11d1
Merge pull request #35768 from robertomiranda/r/rake-db-prepare
Add db:prepare rake task.
2019-04-02 15:33:57 -04:00
Roberto Miranda 900e566a6b 💇 2019-04-02 17:04:32 +01:00
Roberto Miranda 098e4d25f1 Add test cases for rake db:prepare 2019-04-02 16:25:48 +01:00
Sharang Dashputre 771973c13d url -> URL where apt except inside actionpack/ 2019-04-01 22:56:35 +05:30
Ryuta Kamizono 6e43a207c6 Revert unused code and re-using query annotation for `update_all` and `delete_all`
This partly reverts #35617.

#35617 includes unused code (for `InsertStatement`) and re-using query
annotation for `update_all` and `delete_all`, which has not been
discussed yet.

If a relation has any annotation, I think it is mostly for SELECT query,
so re-using annotation by default is not always desired behavior for me.

We should discuss about desired behavior before publishing the
implementation.
2019-04-01 15:04:11 +09:00
Ryuta Kamizono 15501148a2 Merge pull request #35727 from zinosama/zzz/update-doc-for-includes
Update API doc for #includes on unnecessary #references

[ci skip]
2019-04-01 09:49:18 +09:00
Kasper Timm Hansen d2542882b2
Extract insert test case from #35686 2019-03-31 20:41:18 +02:00
Kasper Timm Hansen 6bc02140a3
Use accessors internally; remove needless validation 2019-03-31 20:17:57 +02:00
Kasper Timm Hansen 2ea8bfd040
Mark InsertAll as private API. Easier to add later. 2019-03-31 19:44:14 +02:00
Kasper Timm Hansen 3974bd6a5a
Capture some join calls. 2019-03-31 19:44:14 +02:00
Kasper Timm Hansen ba4e74e1b7
Merge pull request #19333 from palkan/dirty-store
Add dirty methods for store accessors
2019-03-31 19:15:03 +02:00
Ryuta Kamizono 51ab5cb043 Follow up tweaks b89a3e7e63 [ci skip]
* use backticks instead of `+`
* and more (e.g. missed replacing `Array#excluding` and
`Enumerable#excluding` in b89a3e7e63)
2019-03-31 09:52:02 +09:00
Ryuta Kamizono aae8fd0e07 Merge pull request #35799 from leboshi/master
Fix callbacks on has_many :through associations
2019-03-31 09:11:25 +09:00
Chris Oliver ecdcf06cb2 Add attachment and attachments field generators 2019-03-30 12:15:17 -05:00
Ryan Kerr 2e3bba3e3a Fix callbacks on has_many :through associations (#33249)
When adding a child record via a has_many :through association,
build_through_record would previously build the join record, and then
assign the child record and source_type option to it.  Because the
before_add and after_add callbacks are called as part of build, however,
this caused the callbacks to receive incomplete records, specifically
without the other end of the has_many :through association.  Collecting
all attributes before building the join record ensures the callbacks
receive the fully constructed record.
2019-03-30 00:37:08 -04:00
David Heinemeier Hansson 4e076b03b6
Add `ActiveRecord::Relation#extract_associated` for extracting associated record (#35784)
* Add `ActiveRecord::Relation#extract_associated` for extracting associated records from a relation
2019-03-29 16:01:45 -07:00
Ryuta Kamizono 406d3a926c
Merge pull request #35794 from kamipo/type_cast_symbol_false
Type cast falsy boolean symbols on boolean attribute as false
2019-03-30 05:07:07 +09:00
Ryuta Kamizono 2d12f800f1 Type cast falsy boolean symbols on boolean attribute as false
Before 34cc301, type casting by boolean attribute when querying is a
no-op, so finding by truthy boolean string (i.e.
`where(value: "true") # => value = 'true'`) didn't work as expected
(matches it to FALSE in MySQL #32624). By type casting is ensured, a
value on boolean attribute is always serialized to TRUE or FALSE.

In PostgreSQL, `where(value: :false) # => value = 'false'` was a valid
SQL, so 34cc301 is a regresson for PostgreSQL since all symbol values
are serialized as TRUE.

I'd say using `:false` is mostly a developer's mistake (user's input
basically comes as a string), but `:false` on boolean attribute is
serialized as TRUE is not a desirable behavior for anybody.

This allows falsy boolean symbols as false, i.e.
`klass.create(value: :false).value? # => false` and
`where(value: :false) # => value = FALSE`.

Fixes #35676.
2019-03-30 04:18:25 +09:00
Rafael França f1688bf6b0
Merge pull request #35781 from excid3/rich_text-field-generator
Add rich_text field to model generators
2019-03-29 13:48:54 -04:00
Chris Oliver 19a29c6556 Add rich_text field to model generators 2019-03-28 23:58:33 -05:00
Ryuta Kamizono 3a0929901f
Merge pull request #35496 from bogdan/right-preloading
Fix preloader to never reset associations in case they are already loaded
2019-03-28 04:42:38 +09:00
Roberto Miranda 8375b8ee6d Move db:migrate call to else block 2019-03-27 17:10:44 +00:00
Roberto Miranda c6734789ad Improve db:prepare description 2019-03-27 15:25:30 +00:00
Roberto Miranda af9b2af719 Add rake db:prepare rake task.
It Creates the database, loads the schema, run the migrations and initializes with the seed data
(use db:reset to also drop the database first). This rake task runs in an idempotent way

ref https://github.com/rails/rails/pull/33139#discussion_r195930751
2019-03-27 15:16:00 +00:00
Ryuta Kamizono 3c2f301078 Don't change `collation_connection` in the current connection
We have a test case for `collation_connection` session variable, so it
should not be changed in any other test.

Fixes #35458.
2019-03-27 15:26:29 +09:00
Ryuta Kamizono b2b559c75c Fix CI failure due to remaining tagging records
`TRUNCATE TABLE posts` also resets `AUTO_INCREMENT`. If newly created a
post, it is wrongly associated with remaining tagging records.
To un-associate remaining tagging record, use `post.create_tagging!`
instead.

Fixes #35751.
2019-03-26 12:59:16 +09:00
Yasuo Honda 4dd3b2bd9a Use `assert_queries(0)` instead of `assert_no_queries` to ignore metadata queries
Fix #35665

```ruby
$ ARCONN=mysql2 bin/test test/cases/scoping/named_scoping_test.rb test/cases/tasks/database_tasks_test.rb test/cases/associations/cascaded_eager_loading_test.rb test/cases/associations/eager_singularization_test.rb -n "/^(?:NamedScopingTest#(?:test_many_should_not_fire_query_if_scope_loaded)|ActiveRecord::DatabaseTasksDumpSchemaCacheTest#(?:test_dump_schema_cache)|CascadedEagerLoadingTest#(?:test_eager_association_loading_with_has_many_sti_and_subclasses)|EagerSingularizationTest#(?:test_eager_no_extra_singularization_has_many_through_belongs_to))$/" --seed 16818
Using mysql2
Run options: -n "/^(?:NamedScopingTest#(?:test_many_should_not_fire_query_if_scope_loaded)|ActiveRecord::DatabaseTasksDumpSchemaCacheTest#(?:test_dump_schema_cache)|CascadedEagerLoadingTest#(?:test_eager_association_loading_with_has_many_sti_and_subclasses)|EagerSingularizationTest#(?:test_eager_no_extra_singularization_has_many_through_belongs_to))$/" --seed 16818

...F

Failure:
CascadedEagerLoadingTest#test_eager_association_loading_with_has_many_sti_and_subclasses [/home/yahonda/git/rails/activerecord/test/cases/associations/cascaded_eager_loading_test.rb:124]:
1 instead of 0 queries were executed.
Queries:
SHOW FULL FIELDS FROM `topics`.
Expected: 0
  Actual: 1

bin/test test/cases/associations/cascaded_eager_loading_test.rb:119

Finished in 6.894609s, 0.5802 runs/s, 1.0153 assertions/s.
4 runs, 7 assertions, 1 failures, 0 errors, 0 skips
$
```
2019-03-26 02:51:03 +00:00
Vladimir Dementyev b574d283e5
Add saved changes helpers for store accessors 2019-03-25 18:53:07 -04:00
palkan 61a39ffcc6
Add dirty methods for store accessors 2019-03-25 18:49:44 -04:00
Chirag Shah 20a8d2974d
[ci skip] Fixed typo 2019-03-25 12:49:58 +05:30
Sharang Dashputre 2b205c8fa7 Format 'RETURNING' text in the docs [ci skip] 2019-03-25 12:37:28 +05:30
zino 0f8db2126d [ci skip] Update doc for unnecessary #references
Update doc for #includes to clarify that #references is unnecessary when conditions are passed into #includes as a hash.
2019-03-23 13:46:10 -07:00
Ryuta Kamizono 430b841951
Merge pull request #35683 from Kukunin/master
Bugfix: Fix false autosave for has_one :through association
2019-03-23 18:54:20 +09:00
Sergiy Kukunin 3da0024db4 Fix unintended autosave on has_one through association
Fixes #35680

The problem occurred, when a `has one through` association contains
a foreign key (it belongs to the intermediate association).

For example, Comment belongs to Post, Post belongs to Author, and Author
`has_one :first_comment, through: :first_post`.

In this case, the value of the foreign key is comparing with the original
record, and since they are likely different, the association is marked
as changed. So it updates every time when the origin record updates.
2019-03-22 19:54:27 +02:00
Matt Yoho f41825809c Add Relation#annotate for SQL commenting
This patch has two main portions:

1. Add SQL comment support to Arel via Arel::Nodes::Comment.
2. Implement a Relation#annotate method on top of that.

== Adding SQL comment support

Adds a new Arel::Nodes::Comment node that represents an optional SQL
comment and teachers the relevant visitors how to handle it.

Comment nodes may be added to the basic CRUD statement nodes and set
through any of the four (Select|Insert|Update|Delete)Manager objects.

For example:

    manager = Arel::UpdateManager.new
    manager.table table
    manager.comment("annotation")
    manager.to_sql # UPDATE "users" /* annotation */

This new node type will be used by ActiveRecord::Relation to enable
query annotation via SQL comments.

== Implementing the Relation#annotate method

Implements `ActiveRecord::Relation#annotate`, which accepts a comment
string that will be appeneded to any queries generated by the relation.

Some examples:

    relation = Post.where(id: 123).annotate("metadata string")
    relation.first
    # SELECT "posts".* FROM "posts" WHERE "posts"."id" = 123
    # LIMIT 1 /* metadata string */

    class Tag < ActiveRecord::Base
      scope :foo_annotated, -> { annotate("foo") }
    end
    Tag.foo_annotated.annotate("bar").first
    # SELECT "tags".* FROM "tags" LIMIT 1 /* foo */ /* bar */

Also wires up the plumbing so this works with `#update_all` and
`#delete_all` as well.

This feature is useful for instrumentation and general analysis of
queries generated at runtime.
2019-03-21 20:30:56 -07:00
yuuji.yaginuma 382a9563a3 Fix document formatting of Database configuration classes [ci skip]
* Show options as list.
* Fix incorrect quoting.
2019-03-21 14:02:34 +09:00
Ryuta Kamizono d2f9b8e800 Address rubocop offences 2019-03-21 11:19:47 +09:00
Gannon McGibbon e04af4eb18
Merge pull request #35685 from XrXr/attribute-forwarding
Document option forwarding in ActiveRecord::Base.attribute
2019-03-20 15:38:47 -04:00
Alan Wu 15b11ce505 Document option forwarding in ActiveRecord::Base.attribute
This has been supported for a while but we didn't have documentation
for it.
2019-03-20 15:05:31 -04:00