Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn if we can't read the yaml to create database tasks #36560

Merged

Conversation

Projects
None yet
2 participants
@eileencodes
Copy link
Member

commented Jun 26, 2019

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 #36540 the ERB
is too complex and we can't overwrite with the DummyCompilier we used in
#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

cc/ @tenderlove @jhawthorn @rafaelfranca @klyonrad

@eileencodes eileencodes added this to the 6.0.0 milestone Jun 26, 2019

@eileencodes eileencodes self-assigned this Jun 26, 2019

@eileencodes

This comment has been minimized.

Copy link
Member Author

commented Jun 26, 2019

Oh I'm missing a commit. One sec.

@eileencodes eileencodes force-pushed the eileencodes:warn-if-database-yml-cant-be-read branch from af5ea55 to 64d526a Jun 26, 2019

begin
Rails.application.config.load_database_yaml
rescue
$stdout.puts "Rails couldn't infer whether you are using multiple databases from your database.yml. If you'd like to use this feature, please simplify your ERB."

This comment has been minimized.

Copy link
@eileencodes

eileencodes Jun 26, 2019

Author Member

Not sure what to say really - maybe we don't warn at all?

@eileencodes

This comment has been minimized.

Copy link
Member Author

commented Jun 26, 2019

Ok fixed the commit.

database: db/development.sqlite3
development:
<<: *sqlite

This comment has been minimized.

Copy link
@rafaelfranca

rafaelfranca Jun 26, 2019

Member

Should not this be an ERB? This seems to be a yaml that we can read without problem.

This comment has been minimized.

Copy link
@eileencodes

eileencodes Jun 26, 2019

Author Member

Oops yes, will fix that.

This comment has been minimized.

Copy link
@eileencodes

eileencodes Jun 26, 2019

Author Member

Actually this is straight up a bad test 😢 Working on fixing it.

@eileencodes eileencodes force-pushed the eileencodes:warn-if-database-yml-cant-be-read branch from 64d526a to 4eb7b40 Jun 26, 2019

begin
Rails.application.config.load_database_yaml
rescue
$stdout.puts "Rails couldn't infer whether you are using multiple databases from your database.yml. If you'd like to use this feature, please simplify your ERB."

This comment has been minimized.

Copy link
@rafaelfranca

rafaelfranca Jun 26, 2019

Member

$stderr maybe?

I think for the message we could do:

"Rails couldn't infer whether you are using multiple databases from your database.yml and can't generate the test for the non-primary databases. If you'd like to use this feature, please simplify your ERB."

This comment has been minimized.

Copy link
@eileencodes

eileencodes Jun 27, 2019

Author Member

Done.

@@ -2,6 +2,8 @@

require "active_record"

@databases = ActiveRecord::Tasks::DatabaseTasks.setup_initial_database_yaml

This comment has been minimized.

Copy link
@rafaelfranca

rafaelfranca Jun 26, 2019

Member

I think a local variable works, but I'm fine with an instance variable

This comment has been minimized.

Copy link
@eileencodes

eileencodes Jun 27, 2019

Author Member

Fixed.

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 #36540 the ERB
is too complex and we can't overwrite with the DummyCompilier we used in
#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

@eileencodes eileencodes force-pushed the eileencodes:warn-if-database-yml-cant-be-read branch from 4eb7b40 to df6b0de Jun 27, 2019

@eileencodes eileencodes merged commit 4024612 into rails:master Jun 27, 2019

2 checks passed

buildkite/rails Build #61930 passed (10 minutes, 8 seconds)
Details
codeclimate All good!
Details

@eileencodes eileencodes deleted the eileencodes:warn-if-database-yml-cant-be-read branch Jun 27, 2019

eileencodes added a commit that referenced this pull request Jun 27, 2019

Merge pull request #36560 from eileencodes/warn-if-database-yml-cant-…
…be-read

Warn if we can't read the yaml to create database tasks
@eileencodes

This comment has been minimized.

Copy link
Member Author

commented Jun 27, 2019

Backported to 6-0-stable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.