Commit Graph

1499 Commits

Author SHA1 Message Date
John Hawthorn
ba093a5ada Sync test DB from schema using its SHA1
Previously, we used the migration status to determine whether the test
database(s) needed to be reloaded from the schema. This worked in most
cases, but if a schema.rb was modified outside of migrations or if a
migration was rolled back, it would require a manual db:test:prepare.

This commit updates load_schema to record the SHA1 of the loaded schema
file inside of the ar_internal_metadata table. We can then use this SHA
to determine whether we should reload the schema.

This ensures that the test DB stays exactly in sync with the schema
file, including rollbacks which fixes a test marked TODO.
2019-08-06 12:44:22 -07:00
Akira Matsuda
af2129b4c7 Use try only when we're unsure if the receiver would respond_to the method 2019-08-01 17:58:00 +09:00
eileencodes
cd148be072 Fix db:seed
The `rake db:seed` command was broken for the primary environment if the
application is using multiple databases. We never implemented `rake
db:seed` for other databases (coming soon), but that shouldn't break the
default case.

The reason this was broken was because `abort_if_pending_migrations`
would loop through the configs for all databases and check for
migrations but would leave the last established connection. So `db:seed`
was looking in the wrong database for the table to seed.

This PR doesn't fix the fact that `db:seed` doesn't work for multiple
databases but does fix the default case.

Fixes #36817

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
2019-07-31 12:22:01 -04:00
Ryuta Kamizono
4c8c8c87b0 Address to rubocop offences 2019-07-31 17:48:14 +09:00
Akira Matsuda
0196551e60 Use match? where we don't need MatchData 2019-07-29 14:23:10 +09:00
Xavier Noria
bfc9065d58 Improves compatibility of require_dependency in zeitwerk mode [Closes #36774]
Applications are not supposed to use require_dependency in their own
code if running in zeitwerk mode, and require_dependency was initially
aliased to require with that use case in mind.

However, there are situations in which you cannot control the mode and
need to be compatible with both. There, you might need require_dependency
in case you are being executed in classic mode. Think about engines that
want to support both modes in their parent applications, for example.

Furthermore, Rails itself loads helpers using require_dependency.

Therefore, we need better compatibility.
2019-07-28 14:09:01 +02:00
Abhay Nikam
8fdf6d934e Removed webpacker:compile step from scaffold test as it is not required and assets are already precompiled in build_app step 2019-07-26 20:20:07 +05:30
Xavier Noria
59c6d29ffa let autoloaded? support modules with overridden names [closes #36757] 2019-07-25 23:14:29 +02:00
Andrew Kress
7f21e04e61 read configuration to determine excluded eager loaded directory (#36354)
* read config/webpacker.yml to determine which path to exclude for zeitwerk:check

* fix test errors

* more changes to fix test errors

* refactor webpacker_path

[Andrew Kress + Rafael Mendonça França]
2019-07-25 17:10:59 -04:00
Eileen M. Uchitelle
bee689dad9
Merge pull request #36741 from Edouard-chin/ec-system-test-url-options
Define the `url_options` needed for SytemTest inside the route proxy:
2019-07-24 08:16:23 -04:00
Wojciech Wnętrzak
5c1f6d1ff6
Fixed db:prepare task to not touch schema when dump_schema_after_migration is false. 2019-07-24 09:19:13 +02:00
Edouard CHIN
e6cf57df8a Define the url_options needed for SytemTest inside the route proxy:
- I made a change in https://github.com/rails/rails/pull/36691 to
  delegate route helper to a proxy class.
  This didn't take into account that the `url_options` we redefine
  in SystemTest would be ignored.

  This PR fixes that by definin the url_options inside the proxy
2019-07-24 01:18:00 +02:00
Gannon McGibbon
3cf65bcb8e Make Active Storage routes optional
Add configuration option to turn off drawing of Active Storage routes.
2019-07-22 12:35:04 -04:00
Yuji Yaginuma
c5a24c8ebb
Merge pull request #36603 from y-yagi/add_skip_collision_check_option
Add `skip-collision-check` option to generator
2019-07-18 07:17:47 +09:00
Eileen M. Uchitelle
79ef8310be
Merge pull request #36663 from igor04/load_database_yaml_fix
Prevent exception of loading database yaml with blank config file
2019-07-15 09:11:36 -04:00
igor04
0fa41f72fc Prevent exception of loading database yaml with blank config file [closes: #36661] 2019-07-12 19:23:09 +03:00
Jacob Bednarz
bf19b8774e Adds support for configuring HTTP Feature Policy (#33439)
A HTTP feature policy is Yet Another HTTP header for instructing the
browser about which features the application intends to make use of and
to lock down access to others. This is a new security mechanism that
ensures that should an application become compromised or a third party
attempts an unexpected action, the browser will override it and maintain
the intended UX.

WICG specification: https://wicg.github.io/feature-policy/

The end result is a HTTP header that looks like the following:

```
Feature-Policy: geolocation 'none'; autoplay https://example.com
```

This will prevent the browser from using geolocation and only allow
autoplay on `https://example.com`. Full feature list can be found over
in the WICG repository[1].

As of today Chrome and Safari have public support[2] for this
functionality with Firefox working on support[3] and Edge still pending
acceptance of the suggestion[4].

#### Examples

Using an initializer

```rb
# config/initializers/feature_policy.rb
Rails.application.config.feature_policy do |f|
  f.geolocation :none
  f.camera      :none
  f.payment     "https://secure.example.com"
  f.fullscreen  :self
end
```

In a controller

```rb
class SampleController < ApplicationController
  def index
    feature_policy do |f|
      f.geolocation "https://example.com"
    end
  end
end
```

Some of you might realise that the HTTP feature policy looks pretty
close to that of a Content Security Policy; and you're right. So much so
that I used the Content Security Policy DSL from #31162 as the starting
point for this change.

This change *doesn't* introduce support for defining a feature policy on
an iframe and this has been intentionally done to split the HTTP header
and the HTML element (`iframe`) support. If this is successful, I'll
look to add that on it's own.

Full documentation on HTTP feature policies can be found at
https://wicg.github.io/feature-policy/. Google have also published[5] a
great in-depth write up of this functionality.

[1]: https://github.com/WICG/feature-policy/blob/master/features.md
[2]: https://www.chromestatus.com/feature/5694225681219584
[3]: https://bugzilla.mozilla.org/show_bug.cgi?id=1390801
[4]: https://wpdev.uservoice.com/forums/257854-microsoft-edge-developer/suggestions/33507907-support-feature-policy
[5]: https://developers.google.com/web/updates/2018/06/feature-policy
2019-07-10 15:33:15 -07:00
yuuji.yaginuma
6fec4c3a0a Add skip-collision-check option to generator
Until Rails 5.2, generators can run same name multi times without destroying.
But Rails 6.0(with Zeitwerk) can't this. In Rails 6.0, an error occurs
due to class name collision check.

The check uses `const_defined?`, which assumes that the autoload object
is also defined.
https://ruby-doc.org/core-2.6.3/Module.html#method-i-const_defined-3F

It did not work until Rails 5.2, but Zeitwerk seems to be able to
correctly check this against the application's code.

However, this is a little inconvenient if want to run the generator
again like mistake an attribute name(need to run `destoy` before).

In order to solve this, this PR adds an option to skip the collision check.
With this option, you can overwrite files just as did until Rails 5.2.
2019-07-05 18:55:47 +09:00
Yuji Yaginuma
141b30630c
Merge pull request #36534 from y-yagi/fixes_35137
Add the ability to set the CSP nonce only to the specified directives
2019-07-03 08:23:48 +09:00
yuuji.yaginuma
4b621df384 Make bin/setup test pass even if the database does not exist 2019-06-30 10:15:26 +09:00
eileencodes
df6b0de7d9 Load initial database.yml once, and warn if we can't create tasks
For multiple databases we attempt to generate the tasks by reading the
database.yml before the Rails application is booted. This means that we
need to strip out ERB since it could be reading Rails configs.

In some cases like https://github.com/rails/rails/issues/36540 the ERB
is too complex and we can't overwrite with the DummyCompilier we used in
https://github.com/rails/rails/pull/35497. For the complex causes we
simply issue a warning that says we couldn't infer the database tasks
from the database.yml.

While working on this I decided to update the code to only load the
database.yml once initially so that we avoid having to issue the same
warning multiple times. Note that this had no performance impact in my
testing and is merely for not having to save the error off somewhere.
Also this feels cleaner.

Note that this will not break running tasks that exist, it will just
mean that tasks for multi-db like `db:create:other_db` will not be
generated. If the database.yml is actually unreadable it will blow up
during normal rake task calls.

Fixes #36540
2019-06-27 09:54:25 -04:00
yuuji.yaginuma
09d55b3022 Add the ability to set the CSP nonce only to the specified directives
I changed to set CSP nonce to `style-src` directive in #32932.
But this causes an issue when `unsafe-inline` is specified to `style-src`
(If a nonce is present, a nonce takes precedence over `unsafe-inline`).

So, I fixed to nonce directives configurable. By configure this, users
can make CSP as before.

Fixes #35137.
2019-06-22 12:44:37 +09:00
yuuji.yaginuma
ddb6d788d6 Make ActionDispatch::Response#content_type behavior configurable
I changed return value of `ActionDispatch::Response#content_type` in #36034.
But this change seems to an obstacle to upgrading. https://github.com/rails/rails/pull/36034#issuecomment-498795893

Therefore, I restored the behavior of `ActionDispatch::Response#content_type`
to 5.2 and deprecated old behavior. Also, made it possible to control the
behavior with the config.
2019-06-21 07:17:14 +09:00
eileencodes
7cc27d749c Move SchemaMigration to migration_context
This PR moves the `schema_migration` to `migration_context` so that we
can access the `schema_migration` per connection.

This does not change behavior of the SchemaMigration if you are using
one database. This also does not change behavior of any public APIs.
`Migrator` is private as is `MigrationContext` so we can change these as
needed.

We now need to pass a `schema_migration` to `Migrator` so that we can
run migrations on the right connection outside the context of a rake
task.

The bugs this fixes were discovered while debugging the issues around
the SchemaCache on initialization with multiple database. It was clear
that `get_all_versions` wouldn't work without these changes outside the
context of a rake task (because in the rake task we establish a
connection and change AR::Base.connection to the db we're running on).

Because the `SchemaCache` relies on the `SchemaMigration` information we
need to make sure we store it per-connection rather than on
ActiveRecord::Base.

[Eileen M. Uchitelle & Aaron Patterson]
2019-06-14 11:15:08 -04:00
Ryuta Kamizono
98a57aa5f6
Merge pull request #36472 from kamipo/empty_line_only_before_access_modifier
Enable `Layout/EmptyLinesAroundAccessModifier` cop
2019-06-13 18:36:23 +09:00
Ryuta Kamizono
75eb3d0cec Don't drop_table before schema cache tests
Since 5cab34449, `drop_table` clears schema cache.
2019-06-13 13:43:26 +09:00
Ryuta Kamizono
c81af6ae72 Enable Layout/EmptyLinesAroundAccessModifier cop
We sometimes say "✂️ newline after `private`" in a code review (e.g.
https://github.com/rails/rails/pull/18546#discussion_r23188776,
https://github.com/rails/rails/pull/34832#discussion_r244847195).

Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style
`EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059).

That cop and enforced style will reduce the our code review cost.
2019-06-13 12:00:45 +09:00
Mark Lee
cb8b57d07e Convert the db:abort_if_pending_migrations task to be multi-DB aware 2019-06-10 18:01:32 -07:00
Eileen M. Uchitelle
ccb30615a3
Merge pull request #36416 from freeletics/fix-db-prepare
Fixed db:prepare task for multiple databases.
2019-06-05 10:31:52 -04:00
Eileen M. Uchitelle
71a019efe8
Merge pull request #36394 from eileencodes/treat-application-record-as-primary
Treat ActiveRecord::Base and ApplicationRecord as "primary"
2019-06-05 10:01:18 -04:00
eileencodes
2f8b397258 Treat ActiveRecord::Base and ApplicationRecord as "primary"
When someone has a multi-db application their `ApplicationRecord` will
look like:

```ruby
class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true

  connects_to database: { writing: :primary, reading: :replica }
end
```

This will cause us to open 2 connections to ActiveRecord::Base's
database when we actually only want 1. This is because Rails sees
`ApplicationRecord` and thinks it's a new connection, not the existing
`ActiveRecord::Base` connection because the
`connection_specification_name` is different.

This PR changes `ApplicationRecord` classes to consider themselves the
same as the "primary" connection.

Fixes #36382
2019-06-05 09:29:29 -04:00
Wojciech Wnętrzak
87796b3c81
Fixed db:prepare task for multiple databases.
When one database existed already, but not the other,
during setup of missing one, existing database was wiped out.
2019-06-05 12:17:02 +02:00
yuuji.yaginuma
689b371a52 Extract default log file open operation to method
This allows customize a default log file(e.g. `reopen`) by an application.

Fixes #32211.
2019-06-05 11:19:15 +09:00
John Hawthorn
9fd02d181a Fail parallel tests if workers exit early
Previously, if a test worker exited early, the in-flight test it was
supposed to run wasn't reported as a failure.

If all workers exited immediately, this would be reported as ex.

    Finished in 1.708349s, 39.2192 runs/s, 79.0237 assertions/s.
    67 runs, 135 assertions, 0 failures, 0 errors, 2 skips

This commit validates that all workers finish running tests by ensuring
that the queue is empty after they exit. This works because we signal
the workers to exit by pushing nil onto the queue, so that there should
be a number of items left in the queue matching potentially missed
tests.
2019-05-30 21:34:47 -07:00
Vishal Telangre
ae9be13713
Fix: DummyCompiler embeds invalid YAML content
Fixes https://github.com/rails/rails/issues/36285.

Follow up of https://github.com/rails/rails/pull/36237.
2019-05-15 22:26:07 +05:30
eileencodes
5df84533d5 Fix database loading when ERB is single line ternary
*sigh* this seems like the never ending bug. I don't love or even like
this fix but it does _work_.

Rafael suggested using `dummy_key: dummy_value` but unfortunately
that doesn't work. So we're left with checking whethere there might be
ternary type things in the content and then assuming that we want to
replace the line with a key value pair.

Technically fixes https://github.com/rails/rails/issues/36088
2019-05-10 16:28:08 -04:00
Eileen M. Uchitelle
d155f61b64 Merge pull request #36196 from st0012/fix-29947
Hide malformed parameters from error page

Accidentally merged this to 6-0-stable so forward porting it to master
here instead.
2019-05-07 15:14:14 -04:00
Xavier Noria
b6e17b6a4b new config to opt-out from adding app directories to $LOAD_PATH 2019-04-28 13:48:26 +02:00
Xavier Noria
1b2efe5a11 upgrades Zeitwerk to 2.1.4
This commit more or less undoes 9b5401f, restores autoloaded? not to
touch the descendants tracker, and autoloaded_constants because it is
documented in the guide.
2019-04-23 01:18:21 +02:00
Rafael França
b62b038d9a
Revert "Include Caching module for ActionController::API" 2019-04-22 17:20:48 -04:00
Guillermo Iguaran
216ef01d14
Merge pull request #36038 from st0012/fix-35602
Include Caching module for ActionController::API
2019-04-22 16:05:51 -05:00
Richard Macklin
ef12ccfd8b Make system tests take failed screenshots in before_teardown hook
Previously we were calling the `take_failed_screenshot` method in an
`after_teardown` hook. However, this means that other teardown hooks
have to be executed before we take the screenshot. Since there can be
dynamic updates to the page after the assertion fails and before we
take a screenshot, it seems desirable to minimize that gap as much as
possible. Taking the screenshot in a `before_teardown` rather than an
`after_teardown` helps with that, and has a side benefit of allowing
us to remove the nested `ensure` commented on here:
https://github.com/rails/rails/pull/34411#discussion_r232819478
2019-04-20 19:32:30 -07:00
eileencodes
f9244c6585 Handle up/down for multiple databases
This change adds the ability to run up/down for a database in a multi-db
environment.

If you have an app with a primary and animals database the following
tasks will be generated:

```
VERSION=123 rake db:migrate:up:primary
VERSION=123 rake db:migrate:up:primary

VERSION=123 rake db:migrate:down:primary
VERSION=123 rake db:migrate:up:animals
```

I didn't generate descriptions with them since we don't generate a
description for a single database application.

In addition to this change I've made it so if your application has
multiple databases Rails will raise if you try to run `up` or `down`
without a namespace. This is because we don't know which DB you want to
run `up` or `down` against unless the app tells us, so it's safer to
just block it and recommend using namespaced versions of up/down
respectively.

The output for the raise looks like:

```
You're using a multiple database application. To use `db:migrate:down`
you must run the namespaced task with a VERSION. Available tasks are
db:migrate:down:primary and db:migrate:down:animals.
```
2019-04-19 13:14:24 -04:00
st0012
7814d1c0ae Make sure api controllers can perform caching as well
Currently ActionController::API doesn't include Caching module, so it
can't perform caching. And even if users include it later manually, it
won't inherit application's default cache store for action_controllers.
So the only way to solve this issue is to include Caching module in
ActionController::API, too.

This closes #35602
2019-04-19 22:50:42 +09:00
Genadi Samokovarov
54df392bc5 Manage ActionDispatch::ActionableExceptions from the default middleware stack 2019-04-19 14:15:22 +09:00
David Stosik
6f73a31c0c
Factorize bin/update in bin/setup, and make bin/setup idempotent
`bin/setup` and `bin/update` are currently almost the same file. The
only thing that keeps them apart is that one is running `bin/rails
db:setup` and the other `bin/rails db:migrate`.

I'm suggesting here that they should be a unique script, which needs to
be idempotent.

- New to a project, need to get started? `bin/setup`
- Need to install new dependencies that were added recently? `bin/setup`.

Before deprecating `bin/update`, I'm suggesting we just have it call
`bin/setup`.
2019-04-17 00:45:17 +02:00
Rafael França
19df75dc3d
Merge pull request #35997 from tjoyal/Rails/MailersController/do-not-leak-I18n-global-setting-changes
[Rails::MailersController] Do not leak I18n global setting changes
2019-04-16 15:45:20 -04:00
Thierry Joyal
963d11a000 [Rails::MailersController] Do not leak I18n global setting changes 2019-04-16 18:54:51 +00:00
utilum
742d2b2c66 Squash warning: possibly useless use of a constant
in void context
2019-04-16 18:45:25 +02:00
George Claghorn
f480cfabcd
Remove the Amazon SES ingress
It's unusable and not ready to ship in Rails 6.0. We'll rewrite it for 6.1.
2019-04-14 12:15:54 -04:00