Commit Graph

9118 Commits

Author SHA1 Message Date
Kasper Timm Hansen
6f0cda8f8e
Merge pull request #33985 from eugeneius/attribute_methods_schema_cache
Only define attribute methods from schema cache
2019-01-03 15:03:06 +01:00
Vladimir Dementyev
6ea356e978
fix activerecord reaper_test 2019-01-02 17:55:48 -05:00
Ryuta Kamizono
260b273106
Merge pull request #34836 from kamipo/class_level_update_without_ids
Restore an ability that class level `update` without giving ids
2019-01-02 23:42:16 +09:00
Ryuta Kamizono
0dec2b5aa3 Add assertions for ActiveRecord::Base.current_role
Since the `current_role` is public API.
2019-01-02 09:12:11 +09:00
Ryuta Kamizono
3d5db4920a Add test case for preventing_writes?
Since the `preventing_writes?` is public API.
2019-01-02 08:42:50 +09:00
Ryuta Kamizono
4c05434973 Restore an ability that class level update without giving ids
That ability was introduced at #11898 as `Relation#update` without
giving ids, so the ability on the class level is not documented and not
tested.

c83e30d which fixes #33470 has lost two undocumented abilities.

One has fixed at 5c65688, but I missed the ability on the class level.

Removing any feature should not be suddenly happened in a stable version
even if that is not documented.

I've restored the ability and added test case to avoid any regression in
the future.

Fixes #34743.
2019-01-02 06:23:52 +09:00
Ryuta Kamizono
600a66f60c Fix TypeError: no implicit conversion of Arel::Attributes::Attribute into String properly
This reverts 27c6c07 since `arel_attr.to_s` is not right way to avoid
the type error.

That to_s returns `"#<struct Arel::Attributes::Attribute ...>"`, there
is no reason to match the regex to the inspect form.

And also, the regex path is not covered by our test cases. I've tweaked
the regex for redundant part and added assertions for the regex path.
2019-01-02 04:15:32 +09:00
Ryuta Kamizono
c1b14aded2 Deprecate passing migrations_paths to connection.assume_migrated_upto_version
Since #31727, `migrations_paths` in `assume_migrated_upto_version` is no
longer used.
2018-12-28 09:21:09 +09:00
Ryuta Kamizono
9ed7c7e9e2 Remove redundant assignning to current_env
This was introduced at cfa1df4b07 (diff-b36b9c41be30b05dc14d09d7f3b192efR436).
2018-12-28 08:03:29 +09:00
Yasuo Honda
05781f1f9c No need to handle if FrozenError is available
Rails 6 requires Ruby 2.5, which introduces `FrozenError`
https://docs.ruby-lang.org/en/2.5.0/NEWS.html

Related to #31520
2018-12-23 13:26:20 +00:00
Eileen M. Uchitelle
b00e46bd6d
Merge pull request #34753 from eileencodes/raise-less-confusing-error-if-handler-doesnt-exist
Raise helpful error when role doesn't exist
2018-12-21 11:43:35 -05:00
Eileen Uchitelle
22a1265828 Raise helpful error when role doesn't exist
If you try to call `connected_to` with a role that doesn't have an
established connection you used to get an error that said:

```
>> ActiveRecord::Base.connected_to(role: :i_dont_exist) { Home.first }

ActiveRecord::ConnectionNotEstablished Exception: No connection pool
with 'primary' found.
```

This is confusing because the connection could be established but we
spelled the role wrong.

I've changed this to raise if the `role` used in `connected_to` doesn't
have an associated handler. Users who encounter this should either check
that the role is spelled correctly (writin -> writing), establish a
connection to that role in the model with connects_to, or use the
`database` keyword for the `role`.

I think this will provide a less confusing error message for those
starting out with multiple databases.
2018-12-21 11:10:10 -05:00
Ryuta Kamizono
721e26767c
Merge pull request #34742 from kamipo/row_format_dynamic_by_default
MySQL: `ROW_FORMAT=DYNAMIC` create table option by default
2018-12-21 17:39:31 +09:00
Ryuta Kamizono
892e38c78e Enable Style/RedundantBegin cop to avoid newly adding redundant begin block
Currently we sometimes find a redundant begin block in code review
(e.g. https://github.com/rails/rails/pull/33604#discussion_r209784205).

I'd like to enable `Style/RedundantBegin` cop to avoid that, since
rescue/else/ensure are allowed inside do/end blocks in Ruby 2.5
(https://bugs.ruby-lang.org/issues/12906), so we'd probably meets with
that situation than before.
2018-12-21 06:12:42 +09:00
Ryuta Kamizono
d5699198a4 Module#{attr,attr_accessor,attr_reader,attr_writer} become public since Ruby 2.5
https://bugs.ruby-lang.org/issues/14132
2018-12-21 02:05:27 +09:00
Ryuta Kamizono
8034dde023 Module#{define_method,alias_method,undef_method,remove_method} become public since Ruby 2.5
https://bugs.ruby-lang.org/issues/14133
2018-12-21 01:39:18 +09:00
Ryuta Kamizono
a1652c196e MySQL: ROW_FORMAT=DYNAMIC create table option by default
Since MySQL 5.7.9, the `innodb_default_row_format` option defines the
default row format for InnoDB tables. The default setting is `DYNAMIC`.

The row format is required for indexing on `varchar(255)` with `utf8mb4`
columns.

As long as using MySQL 5.6, CI won't be passed even if MySQL server
setting is properly configured the same as MySQL 5.7
(`innodb_file_per_table = 1`, `innodb_file_format = 'Barracuda'`, and
`innodb_large_prefix = 1`) since InnoDB table is created as the row
format `COMPACT` by default on MySQL 5.6, therefore indexing on string
with `utf8mb4` columns aren't succeeded.

Making `ROW_FORMAT=DYNAMIC` create table option by default for legacy
MySQL version would mitigate the indexing issue on the user side, and it
makes CI would be passed on MySQL 5.6 which is configured properly.
2018-12-19 08:53:16 +09:00
Ryuta Kamizono
d99984d48c Add test case for ce48b5a366482d4b4c4c053e1e39e79d71987197 2018-12-18 11:18:18 +09:00
Ryuta Kamizono
f907b418ae Enable Layout/SpaceAfterSemicolon cop to avoid newly adding odd spacing
Ref 59ff1ba30d (diff-38fb97fba84b1ef0f311c4110a597c44R35)
2018-12-13 18:06:04 +09:00
Ryuta Kamizono
7d83ef4f7b Ensure that preventing writes is invoked before materialize_transactions consistently 2018-12-12 23:14:26 +09:00
John Hawthorn
b67a3f5cac Add AR::Base.connected_to?
This can be used to check the currently connected role. It's meant to
mirror AR::Base.connected_to
2018-12-11 14:24:02 -08:00
Ryuta Kamizono
5742344024 An empty transaction does not raise the ReadOnlyError if preventing writes
BEGIN transaction would cause COMMIT or ROLLBACK, so unless COMMIT and
ROLLBACK aren't treated as write queries as well as BEGIN, the
`ReadOnlyError` would be raised.
2018-12-11 21:07:37 +09:00
Rafael França
b310827506
Merge pull request #34666 from vinistock/upgrade_rubocop_and_fix_offenses
Upgrade Rubocop to 0.61.1 and fix offenses
2018-12-10 17:38:47 -05:00
Ryuta Kamizono
10457475b9 An explain query does not raise the ReadOnlyError if preventing writes 2018-12-11 07:23:00 +09:00
Ryuta Kamizono
07f0f1a8c7 Don't treat begin and rollback transactions as write queries
Otherwise `save` method would raise the `ReadOnlyError` against `BEGIN`
and `ROLLBACK` queries.
2018-12-11 06:40:38 +09:00
Vinicius Stock
3b7a4d3d75
Upgrade Rubocop to 0.61.1 and fix offenses 2018-12-10 19:22:56 -02:00
Ryuta Kamizono
bde898c1df Prevent write queries for exec_query
Follow up #34505.
2018-12-11 04:51:54 +09:00
Eileen M. Uchitelle
bdc96802a7
Merge pull request #34632 from rails/rename-write-on-read-error
Rename error that occurs when writing on a read
2018-12-07 17:54:33 -05:00
Bogdan
18f83faf71 #create_or_find_by/!: add more tests and fix docs (#34653)
* `#create_or_find_by/!`: add more tests

* Fix docs of `create_or_find_by`

This method uses `find_by!` internally.
2018-12-08 07:46:30 +09:00
Eileen Uchitelle
db54afba2e Rename error that occurs when writing on a read
I originally named this `StatementInvalid` because that's what we do in
GitHub, but `@tenderlove` pointed out that this means apps can't test
for or explitly rescue this error. `StatementInvalid` is pretty broad so
I've renamed this to `ReadOnlyError`.
2018-12-07 08:53:47 -05:00
Gannon McGibbon
93f19071ad Fix join table column quoting with SQLite. 2018-12-05 11:56:14 -05:00
yuuji.yaginuma
65c4b1b50d Fix unstable test_serialized_attribute_works_under_concurrent_initial_access test
Since bd62389307e138ee0f274a9d62697567a3334ea0, isolate test of `test_serialized_attribute_works_under_concurrent_initial_access` fails.

```
$ ./bin/test -w test/cases/serialized_attribute_test.rb -n test_serialized_attribute_works_under_concurrent_initial_access
Using sqlite3
Run options: -n test_serialized_attribute_works_under_concurrent_initial_access --seed 32129

# Running:

E

Error:
SerializedAttributeTest#test_serialized_attribute_works_under_concurrent_initial_access:
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table:
```

If duplicate an unloaded model, it seems that method invocation for that class
is not guaranteed. Use the original class to avoid it.
2018-12-05 13:30:43 +09:00
Rafael França
bd62389307
Merge pull request #34560 from gmcgibbon/fix_decorate_leak_on_serial_attr_test
Fix attribute decoration leak on serialized attribute test
2018-12-04 14:05:36 -05:00
Alfred Dominic
c4d157aafe option to disable scopes that ActiveRecord.enum generates by default 2018-12-04 15:56:31 +05:30
Ryuta Kamizono
609c58bfa6 Merge pull request #34609 from kamipo/delete_all_on_collection_proxy
Ensure that `delete_all` on collection proxy returns affected count
2018-12-04 15:48:22 +09:00
Ryuta Kamizono
4d7354aa35 Ensure that delete_all on collection proxy returns affected count
Unlike the `Relation#delete_all`, `delete_all` on collection proxy
doesn't return affected count. Since the `CollectionProxy` is a subclass
of the `Relation`, this inconsistency is probably not intended, so it
should return the count consistently.
2018-12-04 14:33:31 +09:00
Gannon McGibbon
67bca35a64 Reset scope after collection delete
Reset scope after delete on collection association to clear stale
offsets of removed records.
2018-12-04 00:16:42 -05:00
Abdallah Samman
89b4612ffc Fix NumericData.average test on ruby 2.6 2018-12-03 12:59:38 +02:00
utilum
d1c2f4d0d9 Assigned but unused variable - bird
See: https://travis-ci.org/rails/rails/jobs/462233144#L1384
2018-12-03 01:36:01 +01:00
Eileen Uchitelle
f39d72d526 Add ability to prevent writes to a database
This PR adds the ability to prevent writes to a database even if the
database user is able to write (ie the database is a primary and not a
replica).

This is useful for a few reasons: 1) when converting your database from
a single db to a primary/replica setup - you can fix all the writes on
reads early on, 2) when we implement automatic database switching or
when an app is manually switching connections this feature can be used
to ensure reads are reading and writes are writing. We want to make sure
we raise if we ever try to write in read mode, regardless of database
type and 3) for local development if you don't want to set up multiple
databases but do want to support rw/ro queries.

This should be used in conjunction with `connected_to` in write mode.
For example:

```
ActiveRecord::Base.connected_to(role: :writing) do
  Dog.connection.while_preventing_writes do
    Dog.create! # will raise because we're preventing writes
  end
end

ActiveRecord::Base.connected_to(role: :reading) do
  Dog.connection.while_preventing_writes do
    Dog.first # will not raise because we're not writing
  end
end
```
2018-11-30 09:28:04 -05:00
Ryuta Kamizono
5c6316dbb8
Merge pull request #34572 from kamipo/fix_scoping_with_query_method
Fix the scoping with query methods in the scope block
2018-11-30 21:41:12 +09:00
Ryuta Kamizono
3090b35848 Fix the scoping with query methods in the scope block
Follow up #33394.

#33394 only fixes the case of scoping with klass methods in the scope
block which invokes `klass.all`.
Query methods in the scope block also need to invoke `klass.all` to be
affected by the scoping.
2018-11-30 05:50:40 +09:00
Gannon McGibbon
72e63c71bb Allow aliased attributes in update
Allow aliased attributes to be used in `#update_columns` and `#update`.
2018-11-29 14:49:17 -05:00
Gannon McGibbon
dcbf44e8ae Fix attribute decoration leak on serialized attribute test 2018-11-29 10:50:33 -05:00
Gannon McGibbon
03fadebe0e Allow spaces in postgres table names
Fixes issue where "user post" is misinterpreted as "\"user\".\"post\""
when quoting table names with the postgres adapter.
2018-11-28 16:20:22 -05:00
Sean Griffin
9c4d3c341c
Merge pull request #33835 from schneems/schneems/faster_cache_version
Use raw time string from DB to generate ActiveRecord#cache_version
2018-11-27 18:51:20 -07:00
Rafael França
ddaca7ccec
Merge pull request #34528 from DmitryTsepelev/fix-ignored-attributes
Additional types of ResultSet should not contain keys of #attributes_to_define_after_schema_loads
2018-11-27 14:43:01 -05:00
Ryuta Kamizono
e4aec40578 More exercise singular association query
Follow up ba4e68f577efc76f351d30a2914e29942b97830e.
2018-11-28 03:34:20 +09:00
Ryuta Kamizono
ba4e68f577 Ensure that singular association should execute limited query 2018-11-28 01:48:33 +09:00
DmitryTsepelev
d34c1fc3d6 Cached columns_hash fields should be excluded from ResultSet#column_types 2018-11-27 16:30:26 +03:00