Commit Graph

21015 Commits

Author SHA1 Message Date
Jorge Manrubia
34ac8fc1f1 Make active record encryption work with store attributes
Fix: https://github.com/rails/rails/issues/43012
2021-08-14 11:06:51 +02:00
Jean Boussier
02d989e72c
Merge pull request #43009 from basecamp/encryption-lenght-validation
Move length-validation for encrypted columns after schema is loaded
2021-08-13 10:17:23 +02:00
Kasper Timm Hansen
0965b67bf7
Merge pull request #42967 from p8/main
Clean up missing dots in changelogs
2021-08-13 02:24:40 +02:00
Jorge Manrubia
f0fe547e20 Move length-validation for encrypted columns after schema is loaded
Doing it before meant it required a database connection at class loading
time, which was a source a trouble.

See https://github.com/rails/rails/pull/42991#discussion_r687659296
2021-08-12 22:52:50 +02:00
Jean Boussier
6fce5576ea Set record_timestamps = true in AR::InternalMetadata
Fix: https://github.com/rails/rails/issues/42983

The usefulness of these timestamps is a bit questionable,
but I suppose they could be useful for debugging, and
removing them would require to ship a migration etc.

Since `record_timestamps` is now a `class_attribute` we
can simply force it to `true` in just that class.
2021-08-11 10:05:07 +02:00
Jeremiah
b7560426b5 Simplify pluck(:value).first to pick(:value) 2021-08-10 10:12:09 -05:00
Keeran Raj Hawoldar
2408615154 Add QueryLogTags to Rails
Co-authored-by: Eileen M. Uchitelle <eileencodes@users.noreply.github.com>
Co-authored-by: Kasper Timm Hansen <kaspth@gmail.com>
2021-08-09 15:21:08 +01:00
Eileen M. Uchitelle
843c0a3fcd
Merge pull request #42938 from Modern-Treasury/rh/multidb-tasks
Add database specific setup and reset tasks for multidb configurations
2021-08-09 08:21:40 -04:00
Xavier Noria
fc7c029467 Deletes classic test related to STI
Without classic, this test would be testing that Zeitwerk works. That
belongs to the test suite of Zeitwerk.

Active Record has the class available, whether it was already loaded or
pending autoloading, is not AR's concern.
2021-08-09 13:40:03 +02:00
Xavier Noria
36e716cdc1 Implement support for before_remove_const in zeitwek mode
This needs Zeitwerk 2.5 for the on_unload hook.
2021-08-09 13:15:10 +02:00
Ryan Hall
0a296be9e1 Add database specific setup and reset tasks 2021-08-08 13:52:47 -07:00
Petrik de Heus
6828334555
Add missing dots to changelogs 2021-08-07 18:53:10 +02:00
Ryuta Kamizono
9576a4c77f
Merge pull request #42963 from jordannb/fix-unknown-attr-ref-err-doc
API Docs: Use Arel.sql examples that require Arel.sql
2021-08-07 13:42:45 +09:00
Ryuta Kamizono
4261e4dd85 nodoc field_ordered_value [ci skip] 2021-08-07 13:27:43 +09:00
Jordan Bostrom
18c0c568da docs: use Arel.sql examples that require Arel.sql 2021-08-06 16:58:38 -05:00
Ryuta Kamizono
f9468ad191 Avoid class_eval 2021-08-06 18:03:33 +09:00
Rafael Mendonça França
549ef603ad
Fix indentation 2021-08-06 03:41:59 +00:00
Rafael França
e2e74cf93a
Merge pull request #39723 from hahmed/db/friendly-error-when-no-db
Create database via UI when ActiveRecord::NoDatabaseError
2021-08-05 23:41:17 -04:00
Rafael França
0c93edd244
Merge pull request #42940 from SkipKayhil/engine-migration-newline
Move comment after newline in installed migrations
2021-08-05 23:30:06 -04:00
Rafael Mendonça França
c91a8135c7
Allow using a different socket to test mysql 2021-08-05 14:27:30 -04:00
Ryuta Kamizono
ac45e1d396
Merge pull request #42948 from yahonda/diag79936
Address "NameError: Rails couldn't find a valid model for Club association"
2021-08-05 13:31:01 +09:00
Yasuo Honda
945aa87466 Address "NameError: Rails couldn't find a valid model for Club association"
This commit addresses CI failure of Active Record isolation tests.
https://buildkite.com/rails/rails/builds/79936

Follow up #41392

* Steps to reproduce
```ruby
$ cd activerecord
$ bin/test test/cases/associations/belongs_to_associations_test.rb test/cases/associations/has_many_associations_test.rb test/cases/associations/has_many_through_disable_joins_associations_test.rb test/cases/associations/has_one_associations_test.rb test/cases/connection_adapters/schema_cache_test.rb test/cases/inheritance_test.rb test/cases/migration_test.rb test/cases/store_test.rb test/cases/strict_loading_test.rb
```

Since the entire output is too long, here is the minimum case fixed by
this commit.

```ruby
$ cd activerecord
$ bin/test test/cases/associations/belongs_to_associations_test.rb:38
Using sqlite3
Run options: --seed 34180

E

Error:
BelongsToAssociationsTest#test_belongs_to:
NameError: Rails couldn't find a valid model for Club association. Please provide the :class_name option on the association declaration. If :class_name is already provided, make sure it's an ActiveRecord::Base subclass.
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/reflection.rb:431:in `rescue in compute_class'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/reflection.rb:422:in `compute_class'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/reflection.rb:372:in `klass'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/reflection.rb:722:in `association_primary_key'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/reflection.rb:727:in `join_primary_key'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixture_set/table_row.rb:150:in `block in resolve_sti_reflections'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixture_set/table_row.rb:138:in `each_value'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixture_set/table_row.rb:138:in `resolve_sti_reflections'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixture_set/table_row.rb:92:in `fill_row_model_attributes'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixture_set/table_row.rb:70:in `initialize'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixture_set/table_rows.rb:36:in `new'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixture_set/table_rows.rb:36:in `block in build_table_rows_from'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixture_set/table_rows.rb:35:in `each'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixture_set/table_rows.rb:35:in `map'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixture_set/table_rows.rb:35:in `build_table_rows_from'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixture_set/table_rows.rb:18:in `initialize'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:692:in `new'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:692:in `table_rows'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:633:in `block (2 levels) in insert'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:632:in `each'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:632:in `block in insert'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:629:in `each'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:629:in `insert'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:615:in `read_and_insert'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:567:in `create_fixtures'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/test_fixtures.rb:268:in `load_fixtures'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/test_fixtures.rb:122:in `setup_fixtures'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/test_fixtures.rb:10:in `before_setup'

bin/test test/cases/associations/belongs_to_associations_test.rb:38

Finished in 0.083668s, 11.9520 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
$
```
2021-08-05 12:40:19 +09:00
Kasper Timm Hansen
845280a8f8
Merge branch 'main' into structurally-compatible 2021-08-04 17:23:07 +02:00
Kasper Timm Hansen
cd469fdbb3
Merge pull request #41392 from kddnewton/raise-error-belongs-to-custom-primary-key
Raise error on invalid fixture primary key
2021-08-04 17:20:29 +02:00
Kasper Timm Hansen
14540a50a0
Merge pull request #42061 from kddnewton/ar-in-order-of
Add `ActiveRecord::QueryMethods#in_order_of`.
2021-08-04 17:17:07 +02:00
Hartley McGuire
43f756778f Move comment after newline in installed migrations
When an engine's migration is installed in a rails application, a
comment is inserted after any magic comments indicating the migration's
source. However, the current implementation does not take into
account whether there is an empty line after magic comments, and the
generated migration will not pass rubocop's
Layout/EmptyLineAfterMagicComment even if the engine's migration did.
This commit changes the implementation to insert the new comment after
a newline occuring after magic comments, if it exists.

Example Engine Migration:
```ruby
# frozen_string_literal: true
# coding: ISO-8859-15

class CurrenciesHaveSymbols < ActiveRecord::Migration::Current
end
```

Before change:
```ruby
# frozen_string_literal: true
# coding: ISO-8859-15
# This migration comes from bukkits (originally 1)

class CurrenciesHaveSymbols < ActiveRecord::Migration::Current
end
```

After change:
```ruby
# frozen_string_literal: true
# coding: ISO-8859-15

# This migration comes from bukkits (originally 1)
class CurrenciesHaveSymbols < ActiveRecord::Migration::Current
end
```
2021-08-04 00:52:53 -04:00
Washington Botelho
7ab3390dfb
Better description for ensuring_owner_was prop
Co-authored-by: Kasper Timm Hansen <kaspth@gmail.com>
2021-08-02 22:15:40 -03:00
Kevin Newton
459ca2258c
Add ActiveRecord::QueryMethods#in_order_of.
This allows you to specify an explicit order that you'd like records
returned in based on a SQL expression. By default, this will be accomplished
using a case statement, as in:

```ruby
Post.in_order_of(:id, [3, 5, 1])
```

will generate the SQL:

```sql
SELECT "posts".* FROM "posts" ORDER BY CASE "posts"."id" WHEN 3 THEN 1 WHEN 5 THEN 2 WHEN 1 THEN 3 ELSE 4 END ASC
```

However, because this functionality is built into MySQL in the form of the
`FIELD` function, that connection adapter will generate the following SQL
instead:

```sql
SELECT "posts".* FROM "posts" ORDER BY FIELD("posts"."id", 1, 5, 3) DESC
```

*Kevin Newton*
2021-08-02 16:11:36 -04:00
Kevin Newton
d31071a5a2
Add ActiveRecord::Relation#structurally_compatible?. 2021-08-02 16:10:40 -04:00
Washington Botelho
668fbc62fb
ensuring_owner_was enabled only with dependent 2021-08-02 10:19:33 -03:00
Jacopo
d5b6e5dabe Fix eager_loading? when ordering with Symbol
`eager_loading?` is triggered correctly when using `order` with symbols.

```ruby
scope = Post.includes(:comments).order(:"comments.label")
=> true
```
2021-07-30 10:19:07 +02:00
Rafael Mendonça França
18707ab17f
Standardize nodoc comments 2021-07-29 21:18:07 +00:00
eileencodes
b31c2fd017
Fix typo in docs
It's marshal not marchal.
2021-07-29 12:10:43 -04:00
Jacopo
d7c0b4542c Remove offensive wording from active record
Remove "This shit is nasty" offensive wording from active record.
2021-07-28 21:25:50 +02:00
Rafael França
4ae0390c10
Merge pull request #42894 from jonathanhefner/fix-incomplete-nodoc
Fix incomplete :nodoc: directives [ci-skip]
2021-07-28 13:39:46 -04:00
Jonathan Hefner
d766c672ec Apply :nodoc: to all DatabaseStatements overrides [ci-skip] 2021-07-28 12:30:51 -05:00
Jonathan Hefner
11c2c0ef2f Fix incomplete :nodoc: directives [ci-skip] 2021-07-28 10:30:06 -05:00
eileencodes
311d0babfb
Remove "stupid" from active record
The language in the docs is clearer without saying MySQL is stupid. The
test doesn't need stupid in the value to be used - an incorrect value is
incorrect.
2021-07-28 11:09:10 -04:00
Jean Boussier
796aba1678
Merge pull request #40822 from fatkodima/fix-mysql-column-default
Fix retrieving default value for text column for MariaDB
2021-07-27 23:30:26 +02:00
George Claghorn
3fa7dce079 Remove outdated comment in ActiveRecord::Associations::AssociationScope#add_constraints [ci skip]
This comment was correct when it was added in 65843e1. In that commit, ActiveRecord::AssociationScope#scope applies the nearest association's scope, and #add_constraints skips it.

c8d8899 made the comment incorrect. It modified #scope not to apply the nearest association's scope and #add_constraints to apply scopes for the entire association chain.
2021-07-24 17:55:01 -04:00
Ryuta Kamizono
5d78c54a46
Merge pull request #42829 from yahonda/diag79471
Address FixturesWithForeignKeyViolationsTest#test_does_not_raise_if_no_fk_violations error
2021-07-21 18:05:10 +09:00
Yasuo Honda
d481d08449 Address FixturesWithForeignKeyViolationsTest#test_does_not_raise_if_no_fk_violations
Managed to reproduce CI failure at https://buildkite.com/rails/rails/builds/79496#0c03f856-9be1-4ca0-88c9-e1df21ae0a07

This commitaddresses the following errors by loading :author_addresses fixture because
`:authors` has a foreign key to `:author_addresses`.

* sqlite3 adapter
```ruby
$ bin/test test/cases/adapters/sqlite3/explain_test.rb test/cases/fixtures_test.rb -n "/^(?:SQLite3ExplainTest#(?:test_explain_with_eager_loading)|FixturesWithForeignKeyViolationsTest#(?:test_does_not_raise_if_no_fk_violations))$/" --seed 2529
Using sqlite3
Run options: -n "/^(?:SQLite3ExplainTest#(?:test_explain_with_eager_loading)|FixturesWithForeignKeyViolationsTest#(?:test_does_not_raise_if_no_fk_violations))$/" --seed 2529

.E

Error:
FixturesWithForeignKeyViolationsTest#test_does_not_raise_if_no_fk_violations:
RuntimeError: Foreign key violations found in your fixture data. Ensure you aren't referring to labels that don't exist on associations.
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:641:in `block in insert'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:629:in `each'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:629:in `insert'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:615:in `read_and_insert'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:567:in `create_fixtures'
    /home/yahonda/src/github.com/rails/rails/activerecord/test/cases/fixtures_test.rb:844:in `block (2 levels) in test_does_not_raise_if_no_fk_violations'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/testing/assertions.rb:34:in `assert_nothing_raised'
    /home/yahonda/src/github.com/rails/rails/activerecord/test/cases/fixtures_test.rb:843:in `block in test_does_not_raise_if_no_fk_violations'
    /home/yahonda/src/github.com/rails/rails/activerecord/test/cases/fixtures_test.rb:857:in `with_verify_foreign_keys_for_fixtures'
    /home/yahonda/src/github.com/rails/rails/activerecord/test/cases/fixtures_test.rb:842:in `test_does_not_raise_if_no_fk_violations'

bin/test test/cases/fixtures_test.rb:835

Finished in 0.045767s, 43.6991 runs/s, 174.7966 assertions/s.
2 runs, 8 assertions, 0 failures, 1 errors, 0 skips
$
```

* postgresql adapter

```ruby
$ ARCONN=postgresql bin/test test/cases/adapters/postgresql/explain_test.rb test/cases/fixtures_test.rb -n "/^(?:PostgreSQLExplainTest#(?:test_explain_with_eager_loading)|FixturesWithForeignKeyViolationsTest#(?:test_does_not_raise_if_no_fk_violations))$/" --seed 16926
Using postgresql
Run options: -n "/^(?:PostgreSQLExplainTest#(?:test_explain_with_eager_loading)|FixturesWithForeignKeyViolationsTest#(?:test_does_not_raise_if_no_fk_violations))$/" --seed 16926

.E

Error:
FixturesWithForeignKeyViolationsTest#test_does_not_raise_if_no_fk_violations:
RuntimeError: Foreign key violations found in your fixture data. Ensure you aren't referring to labels that don't exist on associations.
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:641:in `block in insert'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:629:in `each'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:629:in `insert'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:615:in `read_and_insert'
    /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/fixtures.rb:567:in `create_fixtures'
    /home/yahonda/src/github.com/rails/rails/activerecord/test/cases/fixtures_test.rb:844:in `block (2 levels) in test_does_not_raise_if_no_fk_violations'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/testing/assertions.rb:34:in `assert_nothing_raised'
    /home/yahonda/src/github.com/rails/rails/activerecord/test/cases/fixtures_test.rb:843:in `block in test_does_not_raise_if_no_fk_violations'
    /home/yahonda/src/github.com/rails/rails/activerecord/test/cases/fixtures_test.rb:857:in `with_verify_foreign_keys_for_fixtures'
    /home/yahonda/src/github.com/rails/rails/activerecord/test/cases/fixtures_test.rb:842:in `test_does_not_raise_if_no_fk_violations'

bin/test test/cases/fixtures_test.rb:835

Finished in 0.121193s, 16.5027 runs/s, 49.5080 assertions/s.
2 runs, 6 assertions, 0 failures, 1 errors, 0 skips
$
```

* It does not reproduce with mysql2 adapter because MySQL does not have
  features to validate existing foreign keys, but added
  :author_addresses in Mysql2ExplainTest for consistency.

Follow up #42674
2021-07-21 16:54:35 +09:00
Jean Boussier
48f4c88214 Fix changelog entry for active_record.partial_inserts 2021-07-21 09:12:36 +02:00
Ryuta Kamizono
e50b0e3ab3 Fixup CHANGELOGs [ci skip] 2021-07-21 10:08:08 +09:00
Ryuta Kamizono
a206aecedf chore: Use e.g. which is the more used spelling
```
% git grep -i '\be\.g\.' | wc -l
290
```
2021-07-21 09:17:54 +09:00
Eileen M. Uchitelle
fad8d8a0f3
Merge pull request #42803 from gregschmit/gns/fix-migrate-status-order
db:migrate:status: Sort migration ID as integer for consistency.
2021-07-20 08:29:10 -04:00
George Claghorn
fc3acf2e8d Add change tracking methods for belongs_to associations
Permit checking whether a belongs_to association has been pointed to a new target record in the previous save and whether it will point to a new target record in the next save.

    post.category # => #<Category id: 1, name: "Ruby">

    post.category = Category.second   # => #<Category id: 2, name: "Programming">
    post.category_changed?            # => true
    post.category_previously_changed? # => false

    post.save!

    post.category_changed?            # => false
    post.category_previously_changed? # => true
2021-07-19 14:39:13 -04:00
Santiago Bartesaghi
60d99db356 Fix minor typo [ci skip] 2021-07-19 12:01:29 -03:00
eileencodes
10ca60a16b
Add option to disable schema dumb per-database
Dumping the schema is on by default for all databases in an application. To turn it off for a
specific database use the `schema_dump` option:

```yaml
  # config/database.yml

  production:
  schema_dump: false
```

Co-authored-by: Luis Vasconcellos <vasconcelloslf@gmail.com>
2021-07-19 07:58:49 -04:00
Gregory N. Schmit
78955b925a
Sort migration ID as int in db:migrate:status for consistency. 2021-07-16 13:10:31 -05:00