Skip to content
Open
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
6 changes: 4 additions & 2 deletions lib/rails_erd/diagram/tbls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ def column_payload(attr)
end

def indexes_payload(model)
return [] unless model.connection.respond_to?(:indexes)
indexes = model.connection_pool.with_connection do |connection|
connection.respond_to?(:indexes) ? connection.indexes(model.table_name) : []
end

model.connection.indexes(model.table_name).map do |idx|
indexes.map do |idx|
columns = Array(idx.columns).map(&:to_s)
{
name: idx.name,
Expand Down
4 changes: 3 additions & 1 deletion lib/rails_erd/domain/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ def limit_description # @private :nodoc:
private

def native_type
@model.connection.native_database_types[type] or {}
@model.connection_pool.with_connection do |connection|
connection.native_database_types[type] or {}
end
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,23 @@ def create_models(*names)
end
end

# Rails 7.2+ can forbid the implicit permanent checkout done by
# ActiveRecord::Base.connection. The check only applies while the thread holds
# no leased connection, so release the one schema setup leased. Earlier
# versions have no such setting, so the block runs unchanged there.
def with_permanent_connection_checkout_disallowed
return yield unless ActiveRecord.respond_to?(:permanent_connection_checkout=)

previous = ActiveRecord.permanent_connection_checkout
ActiveRecord::Base.connection_pool.release_connection
ActiveRecord.permanent_connection_checkout = :disallowed
begin
yield
ensure
ActiveRecord.permanent_connection_checkout = previous
end
end

def collect_stdout
stdout = $stdout
$stdout = StringIO.new
Expand Down
11 changes: 11 additions & 0 deletions test/unit/attribute_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,17 @@ def create_attribute(model, name)
end
end

test "type_description should use a scoped connection when permanent connection checkout is disallowed" do
with_native_limit :string, 456 do
create_model "Foo"
add_column :foos, :my_str, :string, :limit => 255
attribute = create_attribute(Foo, "my_str")
with_permanent_connection_checkout_disallowed do
assert_equal "string (255)", attribute.type_description
end
end
end

test "type_description should append hair space and low asterisk if field is mandatory" do
create_model "Foo", :a => :integer do
validates_presence_of :a
Expand Down
9 changes: 9 additions & 0 deletions test/unit/graphviz_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ def find_dot_edge_styles(diagram)
assert result.end_with?(".svg"), "Expected filename to end with .svg, got: #{result}"
end

test "create should write a pdf when permanent connection checkout is disallowed" do
create_model "Foo", :name => :string
result = with_permanent_connection_checkout_disallowed do
Diagram::Graphviz.create(:filetype => :pdf)
end
assert result.end_with?(".pdf"), "Expected filename to end with .pdf, got: #{result}"
assert_equal "%PDF-", File.binread(result, 5)
end

test "rank direction should be tb for horizontal orientation" do
create_simple_domain
assert_equal '"TB"', diagram(:orientation => "horizontal").graph[:rankdir].to_s
Expand Down
14 changes: 14 additions & 0 deletions test/unit/tbls_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,20 @@ def constraint(table, type)
assert_equal %w[authors books], fk_targets
end

test "indexes are emitted when permanent connection checkout is disallowed" do
create_model "Widget", name: :string
ActiveRecord::Base.connection.add_index :widgets, :name, unique: true, name: "index_widgets_on_name"

widgets = with_permanent_connection_checkout_disallowed { table(payload, "widgets") }

assert_equal [{
name: "index_widgets_on_name",
def: "CREATE UNIQUE INDEX index_widgets_on_name ON widgets (name)",
table: "widgets",
columns: ["name"],
}], widgets[:indexes]
end

# Schema-level payload =====================================================
test "top-level payload has a name and a tables array" do
create_simple_domain
Expand Down