Commit Graph

17540 Commits

Author SHA1 Message Date
Jonathan Hefner
9f399a6b6b Rely on AM::ForbiddenAttributesProtection autoload
`ActiveModel::ForbiddenAttributesProtection` is autoloaded, so this
`require` is not necessary.
2023-03-15 13:23:00 -05:00
Jonathan Hefner
8986050c41
Merge pull request #47683 from yskkin/patch-3
Remove redundant require: follow #10776
2023-03-15 13:16:20 -05:00
Matthew Draper
0dc919b544
Merge pull request #47684 from rails/revert-47493-quote_binary_strings
Revert "Quote binary strings in Arel"
2023-03-16 01:40:13 +10:30
Eileen M. Uchitelle
3b7e4134e5
Merge pull request #47664 from Shopify/support-composite-identifier-in-find
Extend `ActiveRecord::FinderMethods#find` with support for composite pk values
2023-03-15 10:57:34 -04:00
Nikita Vasilevsky
7375ff2494 Introduce ActiveRecord::PrimaryKey#composite_primary_key? abstraction
Initial implementation falls back to `primary_key.is_a?(Array)` so
it can be immediately used in place of direct `is_a?` checks.
Though implementation may be changed to rely on a pre-initialized
`@composite_primary_key` ivar in the future.
2023-03-15 13:45:23 +00:00
Nikita Vasilevsky
b05321d31a Extend ActiveRecord::FinderMethods#find with support for composite pk values
`ActiveRecord::FinderMethods#find` now supports passing sets of
composite primary key values like:

```ruby
Cpk::Book.find([1, 1])
Cpk::Book.find([[1, 1]])
Cpk::Book.find([1, 1], [1, 2])
Cpk::Book.find([[1, 1], [1, 2]])
```

and treats values as values of the composite primary key columns but
only for models with the `primary_key` being an `Array`.
2023-03-15 13:44:54 +00:00
Eileen M. Uchitelle
9527e5e307
Merge pull request #47668 from Shopify/pm/destroy-async-composite-keys
Destroying associations asynchronously respect query constraints
2023-03-15 09:18:42 -04:00
Rafael Mendonça França
74554cde8b
Merge PR #47675 2023-03-15 13:05:44 +00:00
Matthew Draper
bb7f3be138
Revert "Quote binary strings in Arel" 2023-03-15 19:50:50 +10:30
Yoshiyuki Kinjo
db944ce3b2
Remove redundant require: follow #10776
active_model/forbidden_attributes_protection is not used since #10776
2023-03-15 18:16:51 +09:00
Yasuo Honda
126d2169d0
Merge pull request #47655 from alpaca-tc/deferrable_exclusion_constraint
Adds support for deferrable exclude constraints in PostgreSQL.
2023-03-15 08:27:01 +09:00
Paarth Madan
a270108bf6 Destroy associations async respect query constraints
The codepaths related to destroying associations asynchronously now
consider when query constraints are present. In most cases, this means
interpreting the primary key as an array of columns, and identifying
associated records by a tuple of these columns, where previously this
would've been a single ID. In each of the callsites, we use branching.
This is done to maintain backwards compatibility and ensure the
signature of the destroy job remains stable: it has consumers outside of
Rails.
2023-03-14 19:09:09 -04:00
Andrew Novoselac
6902cbce1b Introducs TestFixtures#fixture_paths.
Multiple fixture paths can now be specified using the `#fixture_paths` accessor.
2023-03-14 19:02:56 -04:00
Hormoz Kheradmand
34d8a65969
add nodoc to valid_*_options methods 2023-03-14 12:32:58 -07:00
eileencodes
0603d512dc
Pass self directly to connection_class
Returning `self` and `db_config` from `resolve_config_for_connection` is
hold over from legacy behavior when we returned `self.name` or
`Base.name` rather than just `self`. We can simplify this by passing
`self` directly` to the handlers `establish_connection`.
2023-03-14 11:26:11 -04:00
alpaca-tc
cbc7b59749 Adds support for deferrable exclude constraints in PostgreSQL.
By default, exclude constraints in PostgreSQL are checked after each statement.
This works for most use cases, but becomes a major limitation when replacing
records with overlapping ranges by using multiple statements.

```ruby
exclusion_constraint :users, "daterange(valid_from, valid_to) WITH &&", deferrable: :immediate
```

Passing `deferrable: :immediate` checks constraint after each statement,
but allows manually deferring the check using `SET CONSTRAINTS ALL DEFERRED`
within a transaction. This will cause the excludes to be checked after the transaction.

It's also possible to change the default behavior from an immediate check
(after the statement), to a deferred check (after the transaction):

```ruby
exclusion_constraint :users, "daterange(valid_from, valid_to) WITH &&", deferrable: :deferred
```

*Hiroyuki Ishii*
2023-03-14 23:29:35 +09:00
Jason Karns
1f61fd65ae
Delegated Type supports customizeable foreign_type column 2023-03-14 09:19:30 -04:00
Alexander Ryazantsev
8381a503c1
Typo fix on create_table description 2023-03-13 23:22:28 +03:00
Eileen M. Uchitelle
deeb50f3dd
Merge pull request #47616 from szymonlipka/fix-drop-table-invert
Fix 47614 issue with inverting create_table/drop_table migration
2023-03-13 10:46:41 -04:00
Eileen M. Uchitelle
3c0569ec17
Merge pull request #47635 from Shopify/generate-composite-pk-in-fixtures
Fix fixtures id generation for composite primary keys
2023-03-13 10:26:06 -04:00
Szymon Lipka
d8b564f7e7 Fix drop table invert when if_exists option is present 2023-03-13 15:07:37 +01:00
Yasuo Honda
3025eaa091
Merge pull request #46192 from alpaca-tc/support_unique_constraints
Add support for unique constraints (PostgreSQL-only).
2023-03-12 18:31:38 +09:00
Matthew Draper
511ffed752
Merge pull request #47438 from benedikt/arel-documentation
Adds documentation for Arel::Nodes::Node
2023-03-12 03:34:06 +10:30
Nikita Vasilevsky
059627cbbd Fix fixtures id generation for composite primary keys 2023-03-10 21:27:39 +00:00
Nikita Vasilevsky
8b43c13e53 Use composite primary_key value as a fallback for query_constraints_list
Given a model with a composite primary key, the `query_constraints_list`
should equal the `primary_key` value to enable capabilities managed by
`query_constraints_list` such as `destroy`, `update`, `delete` and others.
2023-03-10 18:31:02 +00:00
Eileen M. Uchitelle
62e36149c5
Merge pull request #47625 from Shopify/add-model-with-a-composite-primary-key
Define `ActiveRecord::Base#id` API for composite primary key models
2023-03-10 10:48:44 -05:00
Nikita Vasilevsky
fb127c0d42 Define ActiveRecord::Base#id API for composite primary key models
Given a model with a composite primary key:
```ruby
class Order < ActiveRecord::Base
  self.primary_key = [:shop_id, :id]
end
```

`ActiveRecord::Base#id` method will return an array of values for every
column of the primary key.

```ruby
order = Order.create!(shop_id: 1, id: 2)
order.id # => [1, 2]
```

The `id` column is accessible through the `read_attribute` method:

```ruby
order.read_attribute(:id) # => 2
```
2023-03-10 14:42:57 +00:00
alpaca-tc
d849ee04c6 Add support for unique constraints (PostgreSQL-only).
```ruby
add_unique_key :sections, [:position], deferrable: :deferred, name: "unique_section_position"
remove_unique_key :sections, name: "unique_section_position"
```

See PostgreSQL's [Unique Constraints](https://www.postgresql.org/docs/current/ddl-constraints.html#DDL-CONSTRAINTS-UNIQUE-CONSTRAINTS) documentation for more on unique constraints.

By default, unique constraints in PostgreSQL are checked after each statement.
This works for most use cases, but becomes a major limitation when replacing
records with unique column by using multiple statements.

An example of swapping unique columns between records.

```ruby
old_item = Item.create!(position: 1)
new_item = Item.create!(position: 2)

Item.transaction do
  old_item.update!(position: 2)
  new_item.update!(position: 1)
end
```

Using the default behavior, the transaction would fail when executing the
first `UPDATE` statement.

By passing the `:deferrable` option to the `add_unique_key` statement in
migrations, it's possible to defer this check.

```ruby
add_unique_key :items, [:position], deferrable: :immediate
```

Passing `deferrable: :immediate` does not change the behaviour of the previous example,
but allows manually deferring the check using `SET CONSTRAINTS ALL DEFERRED` within a transaction.
This will cause the unique constraints to be checked after the transaction.

It's also possible to adjust the default behavior from an immediate
check (after the statement), to a deferred check (after the transaction):

```ruby
add_unique_key :items, [:position], deferrable: :deferred
```

PostgreSQL allows users to create a unique constraints on top of the unique
index that cannot be deferred. In this case, even if users creates deferrable
unique constraint, the existing unique index does not allow users to violate uniqueness
within the transaction. If you want to change existing unique index to deferrable,
you need execute `remove_index` before creating deferrable unique constraints.

*Hiroyuki Ishii*
2023-03-10 16:09:32 +09:00
Jonathan Hefner
db8f664b50
Merge pull request #47490 from lazaronixon/has_secure_password_salt
Enhance has_secure_password to also generate a password_salt method

Co-authored-by: Guillermo Iguaran <guilleiguaran@gmail.com>
2023-03-09 14:53:10 -06:00
Nixon
ebe9e575b7 Enhance has_secure_password to also generate a password_salt method 2023-03-09 16:44:10 -03:00
Eileen M. Uchitelle
bde7821fbd
Merge pull request #47609 from Shopify/factor-out-column-def
Factor out the `valid_column_definition_options` method to AbstractAdapter class
2023-03-09 11:17:08 -05:00
Matthew Draper
7048f30991
Merge pull request #47493 from olefriis/quote_binary_strings
Quote binary strings in Arel
2023-03-09 21:21:33 +10:30
Ole Friis Østergaard
f4242739aa Move SQLite3 blob encoding to ActiveModel 2023-03-09 09:23:00 +00:00
Hormoz Kheradmand
cdb895774d
Factor out valid_column_definition_options
Shopify is implementing a custom ActiveRecord adapter to integrate
with [Vitess](https://vitess.io/), and we would like to overload the
`valid_{column,table}_definition_options` methods and add
additional valid options for schema migrations.

For example:

```ruby
module ActiveRecord
  module ConnectionAdapters
    class VitessMysql2Adapter < Mysql2Adapter
      ...

      def valid_table_definition_options
        super + [:skip_vschema_migrations, :sharding_keys, :auto_increment]
      end

      def valid_column_definition_options
        super + [:skip_vschema_migrations, :sharding_keys, :auto_increment]
      end
    end
  end
end
```

This is the simplest possible change and factors out the various `valid_{table,column,primary_key}_definition_options` to be a public
method on an adapter instance.
2023-03-08 13:40:20 -08:00
eileencodes
3bfd559b6a
Make some database tasks methods private
These were never supposed to be public and they aren't very useful
outside of database tasks so make them private.
2023-03-08 12:06:05 -05:00
fatkodima
e3640557ef Add missing openssl require to activerecord/encryption/config.rb file 2023-03-07 17:13:36 +02:00
Ole Friis Østergaard
d624a8e260 Ensure BigDecimals are not binary-encoded
In Ruby 2.7, `BigDecimal#to_s` generates ASCII-8BIT-encoded strings.
We don't want those to get hex-encoded in the generated SQL.
2023-03-07 09:38:21 +00:00
Ole Friis Østergaard
20c3eae1e5 Quote binary strings for SQLite3 and MySQL
We should properly hex-encode binary strings for SQLite3 and MySQL.
For e.g. MySQL, _not_ doing that _may_ work, but it will also
produce database warnings.
2023-03-07 09:38:21 +00:00
sampatbadhe
0bf7afccd1 Use cached instance of o.index 2023-03-07 07:21:36 +05:30
Yasuo Honda
d0d9e8e576
Merge pull request #47552 from lsylvester/where-include-index
Allow both include and where on PostgreSQL add_index
2023-03-06 18:33:01 +09:00
Rafael Mendonça França
9f60cd8dc7
Merge PR #45463 2023-03-03 22:58:01 +00:00
Rafael Mendonça França
35d574dbfd
Merge pull request #47569 from p8/activemodel/add-model-name-to-missing-attribute-error
Add class name to ActiveModel::MissingAttributeError error message
2023-03-03 17:50:26 -05:00
Eileen M. Uchitelle
3771f6865c
Merge pull request #47566 from Shopify/do-not-use-query-constraints-if-association-doesnt-explicitly-specifies-it
Do not use `query_constraints` if association doesn't explicitly specifies it
2023-03-03 09:14:50 -05:00
Eileen M. Uchitelle
8200a3d678
Merge pull request #47565 from Shopify/fix-nullifying-association-with-composite-query-constraints
Fix nullifying association with composite query constraints
2023-03-03 09:13:44 -05:00
Eileen M. Uchitelle
d90d3894cd
Merge pull request #45642 from kbrock/quoted_columns
[ActiveRecord] ToSql#HomogeneousIn uses visitor pattern to produce Attribute sql
2023-03-03 09:09:27 -05:00
Petrik
661c995f3b Add class name to ActiveModel::MissingAttributeError error message.
When an attribute is missing the current message is unclear about which
class is missing the attribute, especially when there are multiple
classes that could miss the attribute.

By adding the classs name to the error message it is easier to debug:

```ruby
user = User.first
user.pets.select(:id).first.user_id
=> ActiveModel::MissingAttributeError: missing attribute 'user_id' for Pet
```

This also makes the error message more inline with the
UnknownAttributeError message:

```ruby
=> ActiveModel::UnknownAttributeError: unknown attribute 'name' for Person
```

Co-authored-by: Yasuo Honda <yasuo.honda@gmail.com
2023-03-03 14:31:22 +01:00
Rafael Mendonça França
049dfd4ccf
Remove deprecated Tasks::DatabaseTasks.schema_file_type 2023-03-03 00:38:27 +00:00
Rafael Mendonça França
3ec629784c
Remove deprecated override of Enumerable#sum 2023-03-03 00:38:26 +00:00
Rafael Mendonça França
96b9fd6f14
Remove deprecated config.active_record.partial_writes 2023-03-03 00:38:25 +00:00
Rafael Mendonça França
96c9db1b48
Remove deprecated ActiveRecord::Base config accessors 2023-03-03 00:38:24 +00:00
Nikita Vasilevsky
f45663a30b Do not use query_constraints if association doesn't explicitly specifies it 2023-03-03 00:19:39 +00:00
Nikita Vasilevsky
2faba7433c Fix nullifying association with composite query constraints
Given a models setup like:
```ruby
class BlogPost < ApplicationRecord
  query_constraints :blog_id, :id

  has_many :comments, query_constraints: [:blog_id, :blog_post_id]
end

class Comment < ApplicationRecord
  query_constraints :blog_id, :blog_post_id

  belongs_to :blog_post, query_constraints: [:blog_id, :blog_post_id]
end
```

It should be possible to nullify both `blog_post.comments = []` and
`comment.blog_post = nil` association which should result in
nullification of all parts of the composite query constraints,
meaning `blog_id` and `blog_post_id` being nil on the affected comments.

Co-authored-by: Paarth Madan <paarth.madan@shopify.com>
2023-03-02 23:01:50 +00:00
Lachlan Sylvester
fd1a3b46b5 support using both include and where at the same time on a PostgreSQL index 2023-03-02 17:03:52 +11:00
Nikita Vasilevsky
2801ea64c3 Fix has_many through association assginments with custom scope and custom joining association name
`where_values_hash` method signature accepts `table_name` which is not
always the same as the association name.
So passing `through_association.reflection.name.to_s` as `table_name`
in `through_scope_attributes` wasn't accurate for every case.
This commit fixes the issue by passing the `table_name` taken from
`through_association.reflection.klass.table_name` instead.
2023-03-01 17:56:03 +00:00
Eileen M. Uchitelle
5cbf61502c
Merge pull request #47541 from Shopify/has-many-through-assign-with-query-constraints
Support assignments for composite key has_many through associations
2023-03-01 12:51:53 -05:00
Eileen M. Uchitelle
7d7ed37cfc
Merge pull request #47534 from Shopify/fix-polymorphic-has-many-through-assignments
Fix assigning through records when using polymorphic has many through association
2023-03-01 12:50:50 -05:00
Nikita Vasilevsky
bd93237d78 Support assignments for composite key has_many through associations
Given a has_many through association with a composite foreign key, like:
```ruby
class BlogPost < ApplicationRecord
  query_constraints :blog_id, :id

  has_many :blog_post_tags, query_constraints: [:blog_id, :blog_post_id]
  has_many :tags, through: :blog_post_tags
end

class BlogPostTag < ApplicationRecord
  belongs_to :blog_post, query_constraints: [:blog_id, :blog_post_id]
  belongs_to :tag, query_constraints: [:blog_id, :tag_id]
end

class Tag < ApplicationRecord
  query_constraints :blog_id, :id

  has_many :blog_post_tags, query_constraints: [:blog_id, :tag_id]
  has_many :blog_posts, through: :blog_post_tags
end
```

It should be possible to assign tags to a blog post:
```ruby
blog_post = BlogPost.create!(title: "Hello world")
blog_post.tags << Tag.new(name: "ruby")
```
2023-02-28 23:10:57 +00:00
Matt McMurray
e39b859b87
Replace call to BINARY with CAST(_ AS BINARY) (#47523)
* Replace call to BINARY with CAST(_ AS BINARY)

* Remove space b/w function and quote

Co-authored-by: Rafael Mendonça França <rafael@franca.dev>

* Update unit test to assert for new CAST syntax

---------

Co-authored-by: Rafael Mendonça França <rafael@franca.dev>
2023-02-28 16:15:44 -05:00
Nikita Vasilevsky
c12a77da40 Fix assigning through records when using polymorphic has many through association 2023-02-28 17:56:56 +00:00
eileencodes
232aed8504
Allow configs_for to accept a custom hash key
Now that we support a way to register custom configurations we need to
allow applications to find those configurations. This change adds a
`config_key` option to `configs_for` to find db configs where the
configuration_hash contains a particular key.

I have also removed the deprecation for `include_replicas` while I was
in here to make the method signature cleaner. I've updated the upgrade
guide with the removal.
2023-02-28 12:12:08 -05:00
eileencodes
3d004435b4
Allow applications to register custom database configurations
Previously, applications could only have two types of database
configuration objects, `HashConfig` and `UrlConfig`. This meant that if
you wanted your config to implement custom methods you had to monkey
patch `DatabaseConfigurations` to take a custom class into account. This
PR allows applications to register a custom db_config handler so that
custom configs can respond to needed methods. This is especially useful
for tools like Vitess where we may want to indicate it's sharded, but
not give Rails direct access to that knowledge.

Using the following database.yml as an example:

```yaml
development:
  primary:
    database: my_db
  animals:
    database: my_animals_db
    vitess:
      sharded: 1
```

We can register a custom handler that will generate `VitessConfig`
objects instead of a `HashConfig` object in an initializer:

```ruby
ActiveRecord::DatabaseConfigurations.register_db_config_handler do |env_name, name, url, config|
  next unless config.key?(:vitess)

  VitessConfig.new(env_name, name, config)
end
```

and create the `VitessConfig` class:

```ruby
class VitessConfig < ActiveRecord::DatabaseConfigurations::UrlConfig
  def sharded?
    vitess_config.fetch("sharded", false)
  end

  private
    def vitess_config
      configuration_hash.fetch(:vitess_config)
    end
end
```

Now when the application is booted, the config with the `vitess` key
will generate a `VitessConfig` object where all others will generate a
`HashConfig`.

Things to keep in mind:

1) It is recommended but not required that these custom configs inherit
from Rails so you don't need to reimplement all the existing methods.
2) Applications must implement the configuration in which their config
should be used, otherwise first config wins (so all their configs
will be the custom one.)
3) The procs must support 4 arguments to accommodate `UrlConfig`. I am
thinking of deprecating this and forcing the URL parsing to happen in
the `UrlConfig` directly.
4) There is one tiny behavior change where when we have a nil url key in
the config hash we no longer merge it back into the configuration hash.
We also end up with a `HashConfig` instead of a `UrlConfig`. I think
this is fine because a `nil` URL is...useless.

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
2023-02-28 10:12:28 -05:00
Matthew Draper
31f5cb18fa
Merge pull request #47495 from olefriis/use-same-binding-grammar-for-arel-as-for-active-record
Use same rules for bindings in BoundSqlLiteral as in ActiveRecord
2023-03-01 00:38:13 +10:30
Matthew Draper
e0f1fc0c28
Merge pull request #47481 from olefriis/support-arel-nodes-as-bindings-in-bounds-sql-literal
Support Arel nodes as bindings in BoundSqlLiteral
2023-03-01 00:37:38 +10:30
Ole Friis Østergaard
4dabfd7f5e Respect permitted_classes for default yaml column serializer 2023-02-28 11:49:31 +01:00
Ole Friis Østergaard
009461f320 Revert to previous heuristic for determining type/coder 2023-02-28 11:48:39 +01:00
Keenan Brock
07d2407bee
Use visitor pattern in ToSql#HomogeneousIn to produce Attribute sql
In `ToSql`, operators that generate the sql for an `Attribute` use `visit(o.left, collector)`
This can be seen in `Equality` (and friends), `Case`, `SelectStatement`, and `In`.

`HomogeneousIn` manually produces the sql for an `Attribute`, introduced in 72fd0bae
This change makes `HomogeneousIn` follow the pattern.
2023-02-27 17:11:14 -05:00
Jean Boussier
c15093c856
Merge pull request #47518 from adrianna-chang-shopify/ac-fix-pg-sql-warnings
Avoid setting notice receiver on PG connection when ignoring SQL warnings
2023-02-27 17:32:43 +01:00
Eileen M. Uchitelle
08d75b4140
Merge pull request #47425 from adrianna-chang-shopify/ac-fix-no-autoincrement-sqlite
Ensure `AUTOINCREMENT` declaration is preserved when altering SQLite tables
2023-02-27 11:11:07 -05:00
Adrianna Chang
099711c037
Avoid setting notice receiver on PG connection when ignoring SQL warnings 2023-02-27 10:23:10 -05:00
Eileen M. Uchitelle
65ae7d1755
Merge pull request #47476 from sj26/disable-joins-uuids
Disable Joins UUID support
2023-02-27 09:18:41 -05:00
Jorge Manrubia
5d7b6d823f
Add option to configure digest algorithm used by Active Record Encryption (#44873)
Before, it was using the configured by Rails. Having a mechanism to configure it
for Active Record encryption makes sense to prevent problems with encrypted content
when the default in Rails changes.

Additionally, there was a bug making AR encryption use the older SHA1 before
`ActiveSupport.hash_digest_class` got initialized to SHA256. This bug was exposed
by https://github.com/rails/rails/pull/44540. We will now set SHA256 as the standard
for 7.1+, and SHA1 for previous versions.
2023-02-27 10:16:41 +01:00
Samuel Cochran
18ecbe987e
Avoid casting to an integer
Not all ids are integers. Existing tests still pass without the cast,
and removing the cast causes uuid joins to work.
2023-02-27 10:49:09 +11:00
Yasuo Honda
357b1cfb1f Use squiggly heredoc to remove extra spaces after DEPRECATION WARNING:
- Without this commit

```ruby
DEPRECATION WARNING:                 Passing the coder as positional argument is deprecated and will be remove in Rails 7.2.

                Please pass the coder as a keyword argument:

                  serialize :foo, coder: JSON
 (called from <class:User> at rep.rb:20)
DEPRECATION WARNING:                 Passing the class as positional argument is deprecated and will be remove in Rails 7.2.

                Please pass the class as a keyword argument:

                  serialize :bar, type: Hash
 (called from <class:User> at rep.rb:21)

```

- With this commit

```ruby
DEPRECATION WARNING: Passing the coder as positional argument is deprecated and will be remove in Rails 7.2.

Please pass the coder as a keyword argument:

  serialize :foo, coder: JSON
 (called from <class:User> at rep.rb:20)
DEPRECATION WARNING: Passing the class as positional argument is deprecated and will be remove in Rails 7.2.

Please pass the class as a keyword argument:

  serialize :bar, type: Hash
 (called from <class:User> at rep.rb:21)
```
2023-02-26 23:42:32 +09:00
Jon Dufresne
da82e587f2 Improve typography of user facing validation messages
With the universal adoption of UTF-8 in browsers, user facing text can
use more optimal Unicode typography. In digital and print design, using
RIGHT SINGLE QUOTATION MARK (U+2019) is normally preferred over
APOSTROPHE (U+0027) in contractions.

For details, see the Unicode Standard Section 6.2:
https://www.unicode.org/versions/Unicode13.0.0/ch06.pdf

> Punctuation Apostrophe. U+2019 right single quotation mark is
> preferred where the character is to represent a punctuation mark, as
> for contractions: “We’ve been here before.” In this latter case,
> U+2019 is also referred to as a punctuation apostrophe.
2023-02-25 08:21:19 -08:00
fatkodima
2a6adc9efe Speedup activerecord tests 2023-02-25 02:04:10 +02:00
Ole Friis Østergaard
03f2a2c331 Use same rules for bindings in BoundSqlLiteral as in ActiveRecord 2023-02-24 13:28:00 +00:00
Ole Friis Østergaard
584ad87572 Support Arel nodes as bindings in BoundSqlLiteral 2023-02-24 08:49:37 +00:00
Jean Boussier
cee15555c9 Stop serializing columns as YAML by default
YAML is great for configuration and such, but for serializing arbitrary
data it has many problem, both in term of performance and efficiency
but also in term of security.

As such, we shouldn't let it be the default.

The open question is wether we should provide another default, or
just let users chose what they want based on their own tradeoffs.

Many people would probably suggest JSON as the new default, unfortunately
I don't think it's a good fit either because the parsers available in
Ruby have some wonky behaviors:

```ruby
>> ActiveSupport::JSON.decode(ActiveSupport::JSON.encode(Object.new))
=> {}
>> JSON.load(JSON.dump(Object.new))
=> "#<Object:0x000000012b61a068>"
```

If we were to select another default, I beleive it would need several
properties:

  - It should only serialized a safe list of primitive types.
  - It should explictly raise if attempting to serialize complex types.
2023-02-24 08:55:04 +01:00
Jean Boussier
b0dd7c7ae2
Merge pull request #47482 from Shopify/serialization-default
Serialized attribute should be able to be defined in abstract classes
2023-02-23 17:45:23 +01:00
Jean Boussier
c4c0b799e4 Serialized attribute should be able to be defined in abstract classes
Ref: https://github.com/rails/rails/pull/47191

This feature recently regressed when https://github.com/rails/rails/pull/47191
was merged to handle defaults.

The `attribute` definition callback is set on the parent class and
is triggered when the child class schema is loaded.

I don't know if this is fixeable.
2023-02-23 17:08:02 +01:00
Lorin Thwaits
093cc00ded Avoid "comparison of Gem::Version..." error in Ruby < 3.1 2023-02-23 11:25:45 +00:00
zzak
d2af670dba
Remove Copyright years (#47467)
* Remove Copyright years

* Basecamp is now 37signals... again

Co-authored-by: David Heinemeier Hansson <dhh@hey.com>

---------

Co-authored-by: David Heinemeier Hansson <dhh@hey.com>
2023-02-23 11:38:16 +01:00
Benedikt Deicke
e0edad409e Adds documentation for Arel::Nodes::Node 2023-02-23 07:16:57 +00:00
Carlos Antonio da Silva
701311e9cd Improve wording on eager_load and preload AR docs [ci skip] 2023-02-22 20:44:13 -03:00
Carlos Antonio da Silva
c4b050984a Fix a couple typos and revise sentence on new column serializer docs
[ci skip]
2023-02-22 20:05:05 -03:00
Jean Boussier
185f2d718d Allow to define the default column serializer
YAML has quite a bit of footguns, as such it's desirable
to be able to substitute it for something else or even
simply to force users to define a serializer explictly for
every serialized columns.
2023-02-22 19:32:28 +01:00
Eileen M. Uchitelle
3886edc676
Merge pull request #47452 from Shopify/has-many-through-with-query-constraints
Support has_many through associations with composite query_constraints
2023-02-22 09:36:50 -05:00
Jean Boussier
2c97993201
Merge pull request #47191 from serg-kovalev/fix/issue-46351-serialize-default
Respect Column default in Type::Serialized
2023-02-22 10:30:04 +01:00
Siarhei Kavaliou
695db7b7a5
Respect Column default in Type::Serialized #46351 2023-02-22 10:03:53 +03:00
Nikita Vasilevsky
5e915f3879 Support has_many through associations with composite query_constraints
Given an association like:
```ruby
BlogPost.has_many :blog_post_tags,
  query_constraints: [:blog_id, :blog_post_id]
BlogPost.has_many :tags, through: :blog_post_tags
```

The `tags` association records can be queried
and the resulting JOIN query will include both `blog_id` and
`blog_post_id` in the ON clause.
2023-02-22 00:01:22 +00:00
Yasuo Honda
9621ae961c
Merge pull request #44803 from steve-abrams/activerecord-index-include-option
Add support for `:include` index option
2023-02-22 08:36:28 +09:00
sabrams86
e42d9f7066 Add support for include index option
Add support for including non-key columns in
btree indexes for PostgreSQL with the INCLUDE
parameter.

Example:

    def change
      add_index :users, :email, include: [:id, :created_at]
    end

Will result in:

    CREATE INDEX index_users_on_email USING btree (email) INCLUDE (id,
    created_at)

The INCLUDE parameter is described in the PostgreSQL docs:
  https://www.postgresql.org/docs/current/sql-createindex.html
2023-02-21 15:52:50 -07:00
Jean Boussier
136066fe80
Merge pull request #46579 from joshuay03/fix-through-association-incorrect-preloading
Fix: Incorrectly preloading through association records when middle association has been loaded
2023-02-21 12:44:20 +01:00
fatkodima
a11bef88da Optimize SchemaDumpers check for ignored tables 2023-02-18 03:22:28 +02:00
Joshua Young
0178e1de86 [Fix: #45822] Incorrectly preloading through association records when middle association has been loaded 2023-02-18 08:40:28 +11:00
Eileen M. Uchitelle
e6087bd616
Merge pull request #47424 from eileencodes/support-string-or-symbol-for-owner_name
Support string or symbol for owner_name
2023-02-17 11:07:00 -05:00
Adrianna Chang
bc0996562f
Ensure auto_increment declaration is preserved when copying SQLite tables
Fixes #47400.
Previously, when copying a table as a result of an ALTER TABLE
statement, the AUTOINCREMENT declaration on the PK column was dictated
by the absence of a `:default` option on that column. However,
we were always passing `default` when copying the columns to the new table,
regardless of whether the column originally had a default. This led to
the AUTOINCREMENT declaration being lost.

This commit fixes the issue by tracking a column's auto_increment value,
and only passing the `default` option in `#copy_table` when the column
does not have the `auto_increment` option set.

Co-authored-by: fatkodima <fatkodima123@gmail.com>
2023-02-17 10:53:01 -05:00
eileencodes
5f39b8e6cf
Support string or symbol for owner_name
In legacy systems the `owner_name` didn't have to be a class so it's
possible that connections are using a string as an identifier. I think
this should be allowed since we turn it into a class that defines the
necessary methods that are called on the connection class object (like
`primary_class?` and `preventing_writes?`.

Before, `owner_name` had to be a class so any application wanting to use
a special identifier would be forced to use `ActiveRecord::Base` or
ensure their `config` was a `Symbol`. With this change, you can pass a
`db_config` object and a string to use for the owner OR you can pass a
symbol and still define a custom owner. In some cases you might not want
the name to default to the symbol version of your config name.
2023-02-17 10:45:57 -05:00
fatkodima
73fcc1833f Fix retrieving foreign keys referencing tables named like keywords in PostgreSQL and MySQL 2023-02-17 13:05:11 +02:00
Eileen M. Uchitelle
6cea2589a4
Merge pull request #47410 from Shopify/composite-foreign-key-assoc-preload
`Associations::Preloader` supports preloading associations with composite keys
2023-02-16 17:46:52 -05:00