Skip to content

Commit 7bf8925

Browse files
mfmmqclaude
andauthored
Allow idempotent database registration for Rails reloading (#147)
* Allow idempotent database registration for Rails reloading When using Rails' code reloading in development, the `to_prepare` callback is executed multiple times, causing Nandi to attempt re-registering databases. This would previously fail with "Database already registered" error. This change allows re-registration of databases when the configuration is identical to the existing one, making the registration idempotent. This is particularly important for Rails applications using the `Rails.application.reloader.to_prepare` pattern. The change maintains backward compatibility by still raising an error when attempting to register a database with a different configuration. Co-Authored-By: Claude <[email protected]> * Bump version to 2.0.1 for bug fix release --------- Co-authored-by: Claude <[email protected]>
1 parent fccfc37 commit 7bf8925

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

lib/nandi/multi_database.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,14 @@ class Database
5454
# @return [String]
5555
attr_accessor :output_directory
5656

57-
attr_reader :name, :default
57+
attr_reader :name, :default, :raw_config
5858

5959
attr_accessor :migration_directory,
6060
:lockfile_name
6161

6262
def initialize(name:, config:)
6363
@name = name
64+
@raw_config = config
6465
@default = @name == :primary || config[:default] == true
6566

6667
# Paths and files
@@ -115,6 +116,10 @@ def default
115116

116117
def register(name, config)
117118
name = name.to_sym
119+
120+
# Allow re-registration with identical config (for Rails reloading)
121+
return @databases[name] if @databases.key?(name) && @databases[name].raw_config == config
122+
118123
raise ArgumentError, "Database #{name} already registered" if @databases.key?(name)
119124

120125
@databases[name] = Database.new(name: name, config: config)

lib/nandi/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Nandi
4-
VERSION = "2.0.0"
4+
VERSION = "2.0.1"
55
end

spec/nandi/multi_database_spec.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,25 @@
5454
expect(multi_db.default.name).to eq(:primary)
5555
end
5656

57-
it "raises error for duplicate database registration" do
57+
it "raises error for duplicate database registration with different config" do
5858
expect do
5959
multi_db.register(:primary, migration_directory: "db/new")
6060
end.to raise_error(ArgumentError, "Database primary already registered")
6161
end
6262

63+
it "allows re-registration with identical config (for Rails reloading)" do
64+
original_config = { migration_directory: "db/safe_migrations", output_directory: "db/migrate" }
65+
66+
# First registration
67+
db1 = multi_db.register(:reloadable, original_config)
68+
69+
# Re-registration with same config should return the same database object
70+
db2 = multi_db.register(:reloadable, original_config)
71+
72+
expect(db2).to eq(db1)
73+
expect(multi_db.names.count(:reloadable)).to eq(1)
74+
end
75+
6376
it "converts string names to symbols" do
6477
multi_db.register("string_name", migration_directory: "db/string")
6578
expect(multi_db.names).to include(:string_name)

0 commit comments

Comments
 (0)