Commit Graph

14399 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
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
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 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 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
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
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
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
Kasper Timm Hansen 60c29e1cc7
[ci skip] Documentation pass for insert_all etc. 2019-03-20 13:28:18 +01:00
Kasper Timm Hansen 2ea5c24085
Bulk Insert: Reuse indexes for unique_by
I found `:unique_by` with `:columns` and `:where` inside it tough to
grasp. The documentation only mentioned indexes and partial indexes.
So why duplicate a model's indexes in an insert_all/upsert_all call
when we can just look it up?

This has the added benefit of raising if no index is found, such that
people can't insert thousands of records without relying on an index
of some form.
2019-03-20 13:28:18 +01:00
yuuji.yaginuma e5d7b98fbd Make aliases of `database` option in generators work
Thor automatically adds `-` if aliases do not start with `-`.
0879c1773d/lib/thor/parser/options.rb (L53)

But Thor follows a convention of one-dash-one-letter options.
So, even if `-` is added to `db`, it does not work.
0879c1773d/lib/thor/parser/options.rb (L4)

Follow up #34021.
2019-03-20 14:52:01 +09:00
Rafael França 3dee96e92b
Merge pull request #35595 from shioyama/generated_attribute_methods_namespaced_constant
Give GeneratedAttributeMethods module a name
2019-03-19 21:17:02 -04:00
Rafael França fbc2e670f1
Merge pull request #35664 from jeremyevans/remove-roflscaling
Remove roflscaling
2019-03-19 16:36:29 -04:00
Ryuta Kamizono 7180fa7ba8
Merge pull request #35660 from kamipo/sanitize_as_sql_comment
Extract `sanitize_as_sql_comment` from SQL visitor into connection
2019-03-20 01:23:15 +09:00
Jeremy Evans 6fe624f50d Remove roflscaling constants 2019-03-19 08:48:13 -07:00
Orhan Toy e633560db1 update_at/on note for ActiveRecord::Relation.update_all (#35461)
* update_at/on note for ActiveRecord::Relation.update_all

* Verbatim updated_at/on
2019-03-19 09:27:31 -04:00
Eileen M. Uchitelle 6e94127dd2
Merge pull request #35663 from eileencodes/fix-database-configuration-when-not-actually-a-three-tier
Fix database configuration when adding another config level
2019-03-18 19:58:42 -04:00
Jeremy Evans 22d82b667f Remove roflscaling
roflscaling (using frozen string constants instead of literal
strings) was added in 2012, before frozen string literals were
added in Ruby 2.3.  Now that Rails no longer supports Ruby <2.3,
and all of these files use frozen string literals, there is
no reason to keep the roflscaling.

This does not delete or deprecate the related constants. Such
a change can be made in a later commit.
2019-03-18 14:55:06 -07:00
Kasper Timm Hansen ded9a15424
Simplify values_list with more composition
This also prevents insert_all from leaking its attributes checks.
2019-03-18 22:39:25 +01:00