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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ jobs:
specs:
runs-on: ubuntu-latest

name: Ruby ${{ matrix.ruby-version }}, Rails ${{ matrix.ruby-version }}, LCH ${{ matrix.legacy_connection_handling }}

strategy:
fail-fast: false
matrix:
Expand All @@ -17,11 +19,15 @@ jobs:
gemfile:
- rails6.1
- rails7.0
legacy_connection_handling:
- 'true'
- 'false'
include:
- {ruby-version: '2.7', gemfile: rails6.0}
- {ruby-version: '3.0', gemfile: rails6.0}
- {ruby-version: '2.7', gemfile: rails6.0, legacy_connection_handling: 'true'}
- {ruby-version: '3.0', gemfile: rails6.0, legacy_connection_handling: 'true'}
env:
BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
LEGACY_CONNECTION_HANDLING: ${{ matrix.legacy_connection_handling }}
steps:
- uses: zendesk/checkout@v2
- name: Set up Ruby
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

* Property tables can now live on a separate database to their parent models. This is achieved, on a per-model basis, by configuring the connection class that will be used by property sets. e.g. set `self.property_sets_connection_class = Foo` on the model to instruct `property_sets` to use `Foo`'s database connection when looking for the property sets tables.

## [3.10.0] - 2023-09-18

* Property models now inherit from the same parent as their owners (this unblocks [using multiple databases natively in Rails](https://guides.rubyonrails.org/active_record_multiple_databases.html)).
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,35 @@ end
add_index :account_settings, [ :account_id, :name ], :unique => true
```

### Storage table(s) on separate databases

By default, `property_sets` looks for the storage table(s) on the same database as the model. If you need the storage tables to live on a different database you can configure a custom connection class on a per-model basis:

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

connects_to(database: { writing: foo })
end

class SeparateDatabase < ActiveRecord::Base
self.abstract_class = true

connects_to(database: { writing: bar })
end

class Account < MainConnectionClass
# Ensure you set this _before_ configuring the property sets.
HeyNonster marked this conversation as resolved.
Show resolved Hide resolved
self.property_sets_connection_class = SeparateDatabase

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

In the above example, the `Accounts` table would live on the `foo` database and the storage table(s) will be written to the `bar` database.

## Requirements

* ActiveRecord
Expand Down
4 changes: 3 additions & 1 deletion lib/property_sets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def self.ensure_property_set_class(association, owner_class_name)
end

def self.parent_for_property_class(namespace, owner_class_name)
namespace.const_get(owner_class_name).connection_class_for_self
owner_class = namespace.const_get(owner_class_name)

owner_class.property_sets_connection_class || owner_class.connection_class_for_self
rescue NameError
::ActiveRecord::Base
end
Expand Down
2 changes: 2 additions & 0 deletions lib/property_sets/active_record_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
module PropertySets
module ActiveRecordExtension
module ClassMethods
attr_accessor :property_sets_connection_class
Thomascountz marked this conversation as resolved.
Show resolved Hide resolved

def property_set(association, options = {}, &block)
unless include?(PropertySets::ActiveRecordExtension::InstanceMethods)
self.send(:prepend, PropertySets::ActiveRecordExtension::InstanceMethods)
Expand Down
2 changes: 0 additions & 2 deletions spec/inheritance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
require 'active_record'
require 'property_sets'

ENV["RAILS_ENV"] = "test"

yaml_config = "spec/support/database.yml"
ActiveRecord::Base.configurations = begin
YAML.safe_load(IO.read(yaml_config), aliases: true)
Expand Down
32 changes: 26 additions & 6 deletions spec/property_sets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
end

it "pass-through any options from the second parameter" do
class AnotherThing < ActiveRecord::Base
class AnotherThing < MainDatabase
self.table_name = "things" # cheat and reuse things table
end

Expand All @@ -49,6 +49,11 @@ class AnotherThing < ActiveRecord::Base

expect(AnotherThing.new.settings.extensions).to include(::Parent::Account::Woot)
end
end

RSpec.shared_examples "different account models" do |account_klass|
let(:account) { account_klass.create(:name => "Name") }
let(:relation) { account_klass.reflections["settings"] }

it "support protecting attributes" do
expect(account.settings.protected?(:pro)).to be true
Expand Down Expand Up @@ -111,7 +116,11 @@ class AnotherThing < ActiveRecord::Base
end

it "reject settings with an invalid name" do
s = Parent::AccountSetting.new(:account => account)
# Because we are running these specs with two separate classes
# (Parent::Account & Parent::AccountAltDb), we need to build the
# settings class class name manually.
settings_klass = Object.const_get("#{account_klass}Setting")
s = settings_klass.new(account.model_name.element.to_sym => account)

valids = %w(hello hel_lo hell0) + [:hello]
invalids = %w(_hello)
Expand Down Expand Up @@ -157,7 +166,10 @@ class AnotherThing < ActiveRecord::Base
it "reference the owner instance when constructing a new record" do
record = account.settings.lookup(:baz)
expect(record).to be_new_record
expect(record.account.id).to eq(account.id)
# Because we are running these specs with two separate classes
# (Parent::Account & Parent::AccountAltDb), we need to build the
# method name manually (:account vs :account_alt_db).
expect(record.send(account.model_name.element.to_sym).id).to eq(account.id)
end

it "reference the owner instance when constructing a new record ...on a new record" do
Expand Down Expand Up @@ -368,23 +380,23 @@ class AnotherThing < ActiveRecord::Base
it "creates changed attributes" do
account.update_attribute(:old, "it works!")
expect(account.previous_changes["old"].last).to eq("it works!")
expect(Parent::Account.find(account.id).old).to eq("it works!")
expect(account_klass.find(account.id).old).to eq("it works!")
end

it "updates changed attributes for existing property_set data" do
account.settings.hep = "saved previously"
account.save
account.update_attribute(:old, "it works!")
expect(account.previous_changes["old"].last).to eq("it works!")
expect(Parent::Account.find(account.id).old).to eq("it works!")
expect(account_klass.find(account.id).old).to eq("it works!")
end

it "updates changed attributes for existing property_set data after set through forwarded method" do
account.old = "saved previously"
account.save
account.update_attribute(:old, "it works!")
expect(account.previous_changes["old"].last).to eq("it works!")
expect(Parent::Account.find(account.id).old).to eq("it works!")
expect(account_klass.find(account.id).old).to eq("it works!")
end
end

Expand Down Expand Up @@ -510,3 +522,11 @@ class AnotherThing < ActiveRecord::Base
end
end
end

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
18 changes: 15 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@
require 'active_record'
require 'active_record/fixtures'

ENV["RAILS_ENV"] = "test"

LEGACY_CONNECTION_HANDLING = (ENV["LEGACY_CONNECTION_HANDLING"] == "true")

case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
when '7.0'
ActiveRecord.legacy_connection_handling = LEGACY_CONNECTION_HANDLING
when '6.1'
ActiveRecord::Base.legacy_connection_handling = LEGACY_CONNECTION_HANDLING
end

require 'property_sets'
require 'property_sets/delegator'
require 'support/database'
require 'support/account'
require 'support/thing'

require 'support/database_config'
require 'support/models'
require 'support/database_migrations'

I18n.enforce_available_locales = false

Expand Down
61 changes: 0 additions & 61 deletions spec/support/account.rb

This file was deleted.

99 changes: 0 additions & 99 deletions spec/support/database.rb

This file was deleted.

16 changes: 16 additions & 0 deletions spec/support/database_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

config = {
:test => {
:test_database => {
:adapter => "sqlite3",
:database => ":memory:",
},
:test_alt_database => {
:adapter => "sqlite3",
:database => ":memory:"
}
}
}

ActiveRecord::Base.configurations = config
Loading
Loading