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

Allow for property tables to live on a separate db #106

Merged
merged 4 commits into from
Nov 8, 2023

Conversation

HeyNonster
Copy link
Contributor

@HeyNonster HeyNonster commented Nov 6, 2023

This PR adds the ability for the property tables of a model to live on a separate database to the model itself.

It does so by adding attr_accessor (to the class level) of
ActiveRecord::Base that allows you to set a specific connection class
for the property sets tables.

This is useful if you want to have the tables for your model live on one
database, but all of the property sets tables live on a separate database.

Usage:

class Account < MainConnectionClass
  # Ensure you set this _before_ configuring the property sets.
  self.property_sets_connection_class = SeparateDatabase

  property_set :settings do
    property :foo
  end
end

@HeyNonster HeyNonster force-pushed the nony--configure-superclass branch 15 times, most recently from abad7cb to 48a5f4e Compare November 7, 2023 12:54
@HeyNonster HeyNonster changed the title tmp Allow for property tables to live on a separate db Nov 7, 2023
@HeyNonster HeyNonster force-pushed the nony--configure-superclass branch from 48a5f4e to 6a948f0 Compare November 7, 2023 13:03
@HeyNonster HeyNonster marked this pull request as ready for review November 7, 2023 13:03
@HeyNonster HeyNonster requested a review from a team as a code owner November 7, 2023 13:03
lib/property_sets.rb Outdated Show resolved Hide resolved
README.md Show resolved Hide resolved
This commit adds `attr_accessor` (to the class level) of
`ActiveRecord::Base` that allows you to set a specific connection class
for the property sets tables.

This is useful if you want to have the tables for your model live on one
database, but all of the property sets tables live on a separate database.

Usage:

```ruby
class Account < MainConnectionClass
  # Ensure you set this _before_ configuring the property sets.
  self.property_sets_connection_class = SeparateDatabase

  property_set :settings do
    property :foo
  end
end
```

Co-authored-by: Thomas Countz <[email protected]>
Copy link
Member

@Thomascountz Thomascountz left a comment

Choose a reason for hiding this comment

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

🚀 Small nit. Shit it!

This commit re-organizes the spec support files:
  - Has `Account` inherit from a new abstract model
  `MainDatabase`. (this effectively changes nothing, but we'll use it later)
  - Moves all of the models into one file
  - Moves the database configurations into a `database_config.rb` file
  - Moves the migrations from `database` => `database_migrations.rb`
@HeyNonster HeyNonster force-pushed the nony--configure-superclass branch 2 times, most recently from 6388cac to 519ad23 Compare November 7, 2023 15:11
spec/property_sets_spec.rb Outdated Show resolved Hide resolved
This commit creates two separate databases `test` and `test_alt_database`
and abstract `ActiveRecord` models (MainDatabase, AltDatabase) that
connect to each of those, respectively. The existing models all inherit
from the `MainDatabase` and thus their behavior is unchanged.

This creates a new model called `Parent::AccountAltDb` which is just a
copy of `Parent::Account` but instead we call
`self.property_sets_connection_class = AltDatabase` on it to instruct
`property_sets` to use the connection (and database) from the
`AltDatabase` class when creating and looking for the property_sets
tables. We also run the same migrations we ran for `Account` but now for
`AccountAltDb` and on the `AltDatabase` connection.

We then run the property_sets specs twice: once with `Parent::Account`
and again with `Parent::AccountAltDb` to ensure that they pass, no
matter which database the property tables live on:

```ruby
describe Parent::Account do
  it_behaves_like "different account models", Parent::Account
end

describe Parent::AccountAltDb do
  it_behaves_like "different account models", Parent::AccountAltDb
end
```
This tests the gem with `legacy_connection_handling = true` and
`legacy_connection_handling = false` to test the new connection handling
logic which was added in Rails 6.1. When `false` we now use the
new `connects_to` syntax to configure the models.
@HeyNonster HeyNonster force-pushed the nony--configure-superclass branch from 519ad23 to 33edd9b Compare November 8, 2023 08:22
@HeyNonster HeyNonster merged commit f59e961 into master Nov 8, 2023
39 checks passed
@HeyNonster HeyNonster deleted the nony--configure-superclass branch November 8, 2023 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants