Skip to content

Implications of the connection attribute #425

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

Closed
ghost opened this issue Sep 23, 2020 · 3 comments
Closed

Implications of the connection attribute #425

ghost opened this issue Sep 23, 2020 · 3 comments

Comments

@ghost
Copy link

ghost commented Sep 23, 2020

Since the connection attribute is defined on models, is it possible to use a model with multiple databases at once; for example a production and testing database? And would it be possible to use multiple models within a transaction, since it seems based on #379 the transaction feature is accessed from a model?

@Blacksmoke16
Copy link
Contributor

is it possible to use a model with multiple databases at once

Not the way you're thinking at least. You would be able to register the connection differently based on ENV vars for example. I.e. parameterize Granite::Connections << Granite::Adapter::Mysql.new("mysql", "YOUR_DATABASE_URL")

would it be possible to use multiple models within a transaction

I think so yea. The transaction instance is obtained from a model's adapter. IMO this isn't ideal but it is what it is. I'm doing just that for another project of mine. Saving three objects within a transactions using save!. If one of them fails to save the previous one's don't get committed.

@ghost
Copy link
Author

ghost commented Sep 23, 2020

Okay, thanks. I guess I'll close this now.

By using a model with multiple databases I meant at the same time, not just with the same code. But I guess that could probably be accomplished by subclassing the model and just overriding the connection?

@ghost ghost closed this as completed Sep 23, 2020
@Blacksmoke16
Copy link
Contributor

with multiple databases I meant at the same time

Ah no, a model at the moment can only be associated with a single connection at a time

But I guess that could probably be accomplished by subclassing the model and just overriding the connection?

I don't actually think subclasses would work, you'd run into #123. Maybe distinct types using modules to include the properties?

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

No branches or pull requests

1 participant