Commit Graph

17622 Commits

Author SHA1 Message Date
Carlos Antonio da Silva
c7e3cf2561
Merge pull request #49562 from akhilgkrishnan/update-postgres-naming-to-postgresql
Update postgres naming to PostgreSQL
2023-10-10 15:51:45 -03:00
Jean Boussier
ce2eb72667
Merge pull request #49544 from hieuk09/bug/fix-sqlite3-table-name-quote
SQLite: add additional quote to table name only when it’s not quoted
2023-10-10 17:40:12 +02:00
Akhil G Krishnan
80a5fb50c2 Update postgres naming to PostgreSQL 2023-10-10 13:44:20 +05:30
Hieu Nguyen
6da327c591 SQLite - Remove quote when extracting table ref in insert query 2023-10-10 08:27:40 +08:00
Jean Boussier
a9b68558d0
Merge pull request #49488 from pjambet/pj/add-materialized-to-null-transaction
Add materialized? to NullTransaction
2023-10-09 18:45:58 +02:00
Jean Boussier
7b1bdbb177
Merge pull request #49504 from fatkodima/fix-pg-retrieving-identity-columns
Fix auto populating `IDENTITY` columns for PostgreSQL
2023-10-09 18:42:06 +02:00
Andrew Haines
63854aa473
Check if internal metadata is enabled in create_table_and_set_flags method 2023-10-09 14:39:06 +01:00
Jonathan Hefner
4c98fc27c4 Autolink references for AR::Calculations#async_* [ci-skip]
This also fixes several minor typos.
2023-10-07 12:26:33 -05:00
Jonathan Hefner
bf853ac3fe Remove unnecessary alias references [ci-skip]
RDoc lists method aliases automatically.
2023-10-07 12:26:33 -05:00
Jonathan Hefner
ed08eea99f Capitalize framework names [ci-skip] 2023-10-07 12:26:26 -05:00
Jonathan Hefner
c8b3642ede Ensure AR::Core#{slice,values_at} appear in RDoc [ci-skip]
When a `:method:` doc is immediately followed by the `private` keyword,
RDoc will hide that doc as if it were a private method.

In #48324, the `:method:` docs for `ActiveRecord::Core#slice` and
`ActiveRecord::Core#values_at` were swapped in an attempt to force
`values_at` to appear in the rendered docs.  However, doing so causes
`slice` to dissappear from the rendered docs.

To ensure that `slice` and `values_at` both appear in the rendered docs,
this commit moves their `:method:` docs away from the `private` keyword
in `ActiveRecord::Core`.
2023-10-07 12:26:14 -05:00
Jonathan Hefner
e8166c4c70 Hyphenate "database specific" [ci-skip]
"database-specific" is a compound adjective, and thus hyphenated.
2023-10-07 12:02:22 -05:00
Jonathan Hefner
77e5d4e87f Rephrase advice about forms for nested attributes [ci-skip] 2023-10-07 11:55:34 -05:00
fatkodima
6ba6aa32db Fix reverting rename_table for older migrations 2023-10-07 11:51:25 +03:00
Jason Meller
fc5c8d0cfe
Ensure #signed_id outputs url_safe strings (#49507) 2023-10-06 15:24:37 +02:00
fatkodima
f592d50837 Fix auto populating IDENTITY columns for PostgreSQL
Fixes #49502.

This bug was introduced in https://github.com/rails/rails/pull/48241. Since `IDENTITY` columns
in PostgreSQL do not have defaults, they are not auto populated via the changes made in that PR.
Thus we need to consider them separately.
2023-10-05 23:42:29 +03:00
Pierre Jambet
3d68d224a3
Add materialized? to NullTransaction
The NullTransaction implements the null object pattern for the
Transaction class, but it was missing the `materialized?` method.

This caused issues in cases where we'd want to check if the current
transaction is materialized, where we'd need to check if the connection
is open first:

```ruby
current_transaction = ActiveRecord::Base.connection.current_transaction
current_transaction.open? && current_transaction.materialized?
```

With this change we can skip the extra check on `open?`

```ruby
current_transaction = ActiveRecord::Base.connection.current_transaction
current_transaction.materialized?
```
2023-10-04 21:04:47 -04:00
Rafael Mendonça França
b71c75e467
Merge pull request #49391 from ipc103/transaction-tracking-on-reconnect
Ensure transaction tracking finishes when reconnecting
2023-10-05 01:57:26 +02:00
Ian Candy
306ef99bbb Ensure transaction tracking finishes when reconnecting
We noticed a couple of spots where transaction tracking events
were potentially incorrect.

When the connection reconnects with `restore_transactions: true`,
we were keeping the original start time for the transaction. In this case
it seems more accurate to treat these as separte transactions where the first
one finishes as incomplete.

Instead of forcing the adapter to tell the transaction manager to
suspend (or `detach` or `lost`) the transactions, we can keep that
logic encapsulated inside of the Transaction class for when to broadcast
a finish event. This means that we won't capture a finish event if you
manually call `reconnect!` without `restore_transactions: true`, but
that might be worthy of the tradeoff since this is a rare use-case anyway.

We also start raising here if the TransactionInstrumenter receives `#start`
when already started, or `#finish` when not started. This ensures that we don't
quietly end up in any weird states.

When marking that a transaction is incomplete, we also want to check
that the transaction is materialized to avoid finishing our instrumentation
if it hasn't already started. Also added a test to simulate losing a connection
without ever materializing a transaction.

Co-authored-by: Daniel Colson <composerinteralia@github.com>
2023-10-04 19:35:22 -04:00
Rafael Mendonça França
fed3125ea1
Merge pull request #49470 from rails/rm-eager-load-model-schema
Load the model schema when running test in eager load context
2023-10-04 05:51:39 -04:00
fatkodima
ef1968bd9f Fix uniqueness validation to correctly work with expression indexes 2023-10-04 11:18:24 +03:00
Petrik
d26f1a8777 Use gender neutral pronoun for author [ci skip] 2023-10-03 21:32:26 +02:00
Rafael Mendonça França
70843ff7b2
Load the model schema when running test in eager load context
Some protections like the one that checks if an enum is pointing to
a valid column in the table only works when the database schema is
loaded to the model.

Before this change, if schema cache wasn't present and the right
combinations of configurations were not set, developers would only see
this exception in production.

With this change, those errors would be caught on CI, as soon the
tests are loaded.
2023-10-03 13:20:40 +00:00
Rafael Mendonça França
7d58fe941a
Fix typo 2023-10-02 11:09:57 +00:00
Jean Boussier
618db1377f Ensure PG connection is configured before looking up types
Fix: https://github.com/rails/rails/issues/49439

Because Postgres adapter types are connection dependent,
we can't lookup types without first connecting to the database.

Note, this really isn't good, ideally types should be stored in the
schema cache so that we don't have to extract types every time.
2023-10-02 09:48:09 +02:00
Rafael Mendonça França
0ba14ebab8
Revert "Merge pull request #49056 from joshuay03/raise-on-duplicate-accepts-nested-attributes-for"
This reverts commit b6166e8da2a395f55b275ac9fdc8b957fb97d54a, reversing
changes made to 5415d3a19ad4eb26a4e57bb31c06187ea5172812.
2023-10-01 20:50:23 +00:00
Niklas Häusele
898a248132
rails db:drop now removes -shm and -wal sqlite files (#49436)
fixes #49435
2023-10-01 20:03:21 +00:00
Rafael Mendonça França
db2ef1d250
Merge pull request #49417 from Edouard-chin/ec-logger-fix
Fix the BroadcastLogger being initialized too late:
2023-09-29 15:45:03 -04:00
Edouard CHIN
40cb50e06e Fix the BroadcastLogger being initialized too late:
- An oversight of #48615 is that it changes the `Rails.logger` to be
  a broadcast logger after the app is booted. Anything referencing
  `Rails.logger` during the boot process will get a simple logger and
  ultimately resulting in logs not being broadcasted.

  For example `ActionController::Base.logger.info("abc")` would
  just output logs in the `development.log` file, not on STDOUT.

  ----

  The only solution I could think of is to create a BroadcastLogger
  earlier at boot, and add logger to that broadcast when needed (instead
  of modiyfing the `Rails.logger` variable).
2023-09-29 15:42:47 +02:00
Jean Boussier
2da1852ac9 Delay Adapter#check_version to #configure_connection
Ideally we should be able to checkout an Adapter instance without
triggering a connection to the server.

For some adapters like Trilogy that is the case today, but only if
you have a schema cache loaded, otherwise `check_version` will trigger
a query.

I think this check can be delayed to when we actually use the connection.
2023-09-29 08:53:16 +02:00
Koichi ITO
0bdf160f3a Tweak the docs for async_* APIs [ci skip]
This PR adds period at the end of sentences to maintain consistency in the `async_*` API document.
2023-09-29 11:19:21 +09:00
Jean Boussier
a1c0173ee3 Move the database version cache from schema cache to pool config
Ref: https://github.com/rails/rails/pull/49378

As discussed with Matthew Draper, we have a bit of a chicken and egg
problem with the schema cache and the database version.

The database version is stored in the cache to avoid a query,
but the schema cache need to query the schema version in the database
to be revalidated.

So `check_version` depends on `schema_cache`, which depends on
`Migrator.current_version`, which depends on `configure_connection`
which depends on `check_version`.

But ultimately, we think storing the server version in the cache
is incorrect, because upgrading a DB server is orthogonal from
regenerating the schema cache.

So not persisting the version in cache is better. Instead we store
it in the pool config, so that we only check it once per process
and per database.
2023-09-28 15:18:38 +02:00
Nikita Vasilevsky
1f51409215
Use id in favor of send(primary_key)
There is no reason to send `primary_key` to the record to get the value
of the primary key. `id` method does exactly that and it's a better
abstraction. By implication it also fixes the problem with composite
primary keys.
2023-09-27 16:43:54 +00:00
Nikita Vasilevsky
92a8513995
[ci skip] do not use backticks in docs 2023-09-27 13:22:13 +00:00
Rafael Mendonça França
fb6c6007d0
Development of Rails 7.2 starts now
🎉
2023-09-27 03:59:11 +00:00
Rafael Mendonça França
e5386cb402
Preparing for 7.1.0.rc1 release 2023-09-27 03:08:31 +00:00
Rafael Mendonça França
acfa045405
Revert typography change in user facing errors
This change would force a lot of existing applications and libraries
to update their tests.

We included it in the beta to collect feedback from the community and
we had some comments about how negative this change would be.

Developers that care about the typography of their error messages
can easily change it in their applications using the translation
files, so there is no need to inflict pain in the upgrade process
by changing the default in the framework.

Revert "Merge PR #45463"

This reverts commit 9f60cd8dc7d963b1843b66d9639715b4a04c9c65, reversing
changes made to 35d574dbfda68d09fe1fb532f45a3e32f14c571d.
2023-09-26 21:45:03 +00:00
Jonathan Hefner
5fcc61076d Use monospace font for code snippets and options [ci-skip] 2023-09-26 15:48:31 -05:00
Felix Tscheulin
468d3d0deb
Fix duplicate escaping of quotes for check constraint expressions in MySQL schema
Mysql automatically escapes quotes in generated check constraints expressions.
When Rails dumps the schema (into schema.rb) the generated schema also contains,
the quotes with duplicated escaping (\\\\').
To fix this, we remove the escaping, that MySQL provides from the fetched
expression.
2023-09-26 16:19:37 +00:00
Ryuta Kamizono
4d42741a53 Fix typo "an unique" -> "a unique" [ci skip] 2023-09-26 19:58:49 +09:00
Ryuta Kamizono
b2790b6680 Rename back unique keys to unique constraints
As we (I and @yahonda) talked about the naming in person, naming unique
constraints as unique keys is very confusing to me.
All documents and descriptions says it's unique constraints, but naming
unique keys leads to misunderstanding it's a short-hand of unique
indexes.
Just naming it unique constraints is not misleading.
2023-09-26 17:52:09 +09:00
Rafael Mendonça França
4c72cc2b04
Merge pull request #48615 from Edouard-chin/ec-logger
Add a public API for broadcasting logs
2023-09-25 17:13:58 -04:00
Edouard CHIN
1fbd812c47
Add a public API for broadcasting logs:
- ## Context

  While working on https://github.com/rails/rails/pull/44695, I
  realised that Broadcasting was still a private API, although it’s
  commonly used. Rafael mentioned that making it public would require
  some refactor because of the original implementation which was hard
  to understand and maintain.

  ### Changing how broadcasting works:

  Broadcasting in a nutshell worked by “transforming” an existing
  logger into a broadcasted one.
  The logger would then be responsible to log and format its own
  messages as well as passing the message along to other logger it
  broadcasts to.

  The problem with this approach was the following:

  - Heavy use of metaprogramming.
  - Accessing the loggers in the broadcast wasn’t possible.
    Removing a logger from the broadcast either.
  - More importantly, modifying the main logger (the one that broadcasts
    logs to the others) wasn’t possible and the main source of
    misunderstanding.

    ```ruby
      logger = Logger.new(STDOUT)
      stderr_logger = Logger.new(STDER))
      logger.extend(AS::Logger.broadcast(stderr_logger))

      logger.level = DEBUG # This modifies the level on all other loggers
      logger.formatter = … # Modified the formatter on all other loggers
    ```

  To keep the contract unchanged on what Rails.logger returns, the new
  BroadcastLogger class implement duck typing with all methods
  that has the vanilla Ruby Logger class.

  It's a simple and boring PORO that keeps an array of all the loggers
  that are part of the broadcast and iterate over whenever a log is
  sent.
  Now, users can access all loggers inside the broadcast and modify
  them on the fly. They can also remove any logger from the broadcast
  at any time.

  ```ruby
  # Before

  stdout_logger = Logger.new(STDOUT)
  stderr_logger = Logger.new(STDER)
  file_logger = Logger.new(“development.log”)
  stdout_logger.extend(AS::Logger.broadcast(stderr_logger))
  stdout_logger.extend(AS::Logger.broadcast(file_logger))

  # After

  broadcast = BroadcastLogger.new(stdout_logger, stderr_logger, file_logger)
  ```

  I also think that now, it should be more clear for users that the
  broadcast sole job is to pass everything to the whole loggers in
  the broadcast. So there should be no surprise that all loggers in
  the broadcast get their level modified when they call
  `broadcast.level = DEBUG` .

  It’s also easier to wrap your head around more complex setup such
  as broadcasting logs to another broadcast:
  `broadcast.broadcast_to(stdout_logger, other_broadcast)`
2023-09-25 20:40:51 +00:00
Eileen M. Uchitelle
f57d54cde4
Merge pull request #49336 from fatkodima/fix-mysql-expression-index-dumping
Fix MySQL expression index dumping with escaped quotes
2023-09-25 10:22:34 -06:00
Jean Boussier
99e2999f96
Merge pull request #48462 from iamradioactive/remove_additional_query
Do not revalidate encrypted attribute with current encrypted_type
2023-09-25 13:50:07 +02:00
Stephen Margheim
c6d7ffc7b8 Performance tune the SQLite3 adapter connection configuration
For Rails applications, the Write-Ahead-Log in normal syncing mode with a capped journal size, a healthy shared memory buffer and a shared cache will perform, on average, 2× better.
2023-09-24 21:34:00 +02:00
Stephen Margheim
236a144b50 Allow SQLite3 busy_handler to be configured with simple max number of retries
Retrying busy connections without delay is a preferred practice for performance-sensitive applications. Add support for a `database.yml` `retries` integer, which is used in a simple `busy_handler` function to retry busy connections without exponential backoff up to the max number of `retries`
.
2023-09-22 11:55:05 +02:00
Jean Boussier
31482d2867 Get rid of ActiveRecord::Core#===
This was added back in 0.9.5 because at the time single associations (e.g. has_one)
didn't directly return the record but a proxy

https://github.com/rails/rails/commit/97849debf33

But toaward 3.1, we got rid of that proxy: 1644663ba7
and the associated test was deleted.

However the `===` method stayed.

This has been around for so long that removing it may
break some rare applications, but I doubt it would be hard to
fix, and this isn't a documented behavior.

Co-Authored-By: Nate Matykiewicz <natematykiewicz@gmail.com>
2023-09-21 08:53:28 +02:00
fatkodima
69b32bd95b Fix MySQL expression index dumping with escaped quotes 2023-09-20 20:30:21 +03:00
Stephen Margheim
9bbbf4b8b9 The SQLite3 adapter now supports supports_insert_returning?
Implementing the full `supports_insert_returning?` contract means the SQLite3 adapter supports auto-populated columns (#48241) as well as custom primary keys.
2023-09-20 19:28:17 +02:00