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

Add support for rails console --sandbox for multiple database applications #40704

Merged
merged 1 commit into from Dec 9, 2020

Conversation

alpaca-tc
Copy link
Contributor

@alpaca-tc alpaca-tc commented Nov 28, 2020

Summary

With a multiple database application rails console --sandbox becomes problematic. We can't rollback any modifications for every non-primary database because we can't open transaction for the connections that haven't yet been loaded.

class UserDefinedBaseClass < ActiveRecord::Base
  # sandbox console can't handle new connection like the followings.
  establish_connection(...)
  connects_to(database: { ... })
end

To solve this I handled that if the database is connected then it will open transaction. This way can handle all connections.

ActiveRecord::Base.connection.begin_transaction(joinable: false)

at_exit do
ActiveRecord::Base.connection.rollback_transaction
Copy link
Contributor Author

@alpaca-tc alpaca-tc Nov 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to call rollback_transaction manually because the all connections will be rolled back on disconnected/exited.

This comment was marked as spam.

@alpaca-tc alpaca-tc changed the title Add support for rails console --sandbox for multiple database applications wip Add support for rails console --sandbox for multiple database applications Nov 28, 2020
@alpaca-tc alpaca-tc changed the title wip Add support for rails console --sandbox for multiple database applications Add support for rails console --sandbox for multiple database applications Nov 28, 2020
ActiveRecord::Base.connection.begin_transaction(joinable: false)

at_exit do
ActiveRecord::Base.connection.rollback_transaction

This comment was marked as spam.

@@ -1,3 +1,9 @@
* Add support for `rails console --sandbox` for multiple database applications.

Multiple database applications will now be rolled back on exit.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if this changelog is really necessary. It implies it's adding support for multiple databases but it's really fixing a bug. We don't add changelogs when fixing a bug. Can you remove this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done 👍

With a multiple database application `rails console --sandbox` becomes
problematic. We can't rollback any modifications for every database
because we can't open transaction for the connections that haven't not
yet been loaded.

To solve this I handled that if the database is connected then it
will open transaction.
@eileencodes eileencodes merged commit ff0b9a4 into rails:master Dec 9, 2020
kamipo pushed a commit that referenced this pull request Jan 9, 2021
Add support for `rails console --sandbox` for multiple database applications
@kamipo
Copy link
Member

kamipo commented Jan 9, 2021

Backported this to 6-1-stable 9cc99d7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants