Skip to content

Commit 55a6366

Browse files
committed
Handle structure.sql
1 parent 257cbc1 commit 55a6366

File tree

5 files changed

+53
-3
lines changed

5 files changed

+53
-3
lines changed

app/controllers/rails_ruby_lsp/models_controller.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@ class ModelsController < ApplicationController
99
def show
1010
const = Object.const_get(params[:id]) # rubocop:disable Sorbet/ConstantsFromStrings
1111

12+
begin
13+
schema_file = ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(const.connection.pool.db_config)
14+
rescue => e
15+
warn("Could not locate schema: #{e.message}")
16+
end
17+
1218
if const < ActiveRecord::Base
1319
render(json: {
1420
columns: const.columns.map { |column| [column.name, column.type] },
21+
schema_file: schema_file,
1522
})
1623
else
1724
head(:not_found)

lib/ruby_lsp/rails_ruby_lsp/hover.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ def on_const(node)
2525
model = RailsClient.instance.model(node.value)
2626
return if model.nil?
2727

28-
schema_file = File.join(RailsClient.instance.root, "db", "schema.rb")
28+
schema_file = model[:schema_file]
2929
content = +""
30-
content << "[Schema](file://#{schema_file})\n\n" if File.exist?(schema_file)
30+
if schema_file
31+
content << "[Schema](file://#{schema_file})\n\n"
32+
end
3133
content << model[:columns].map { |name, type| "**#{name}**: #{type}\n" }.join("\n")
3234
contents = RubyLsp::Interface::MarkupContent.new(kind: "markdown", value: content)
3335
@response = RubyLsp::Interface::Hover.new(range: range_from_syntax_tree_node(node), contents: contents)

test/controllers/rails_ruby_lsp/models_controller_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ module RailsRubyLsp
77
class ModelsControllerTest < ActionDispatch::IntegrationTest
88
T.unsafe(self).include(Engine.routes.url_helpers)
99

10-
test "GET show returns column information for exsting models" do
10+
test "GET show returns column information for existing models" do
1111
get model_url(id: "User")
1212
assert_response(:success)
1313
assert_equal(
1414
{
15+
"schema_file" => "#{RailsClient.instance.root}/db/schema.rb",
1516
"columns" => [
1617
["id", "integer"],
1718
["first_name", "string"],

test/lib/hover_test.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module RailsRubyLsp
77
class HoverTest < ActiveSupport::TestCase
88
test "hook returns model column information" do
99
expected_response = {
10+
schema_file: "#{RailsClient.instance.root}/db/schema.rb",
1011
columns: [
1112
["id", "integer"],
1213
["first_name", "string"],
@@ -41,5 +42,42 @@ class HoverTest < ActiveSupport::TestCase
4142
**updated_at**: datetime
4243
CONTENT
4344
end
45+
46+
test "handles `db/structure.sql` instead of `db/schema.rb`" do
47+
expected_response = {
48+
schema_file: "#{RailsClient.instance.root}/db/structure.sql",
49+
columns: [],
50+
}
51+
52+
listener = Hover.new
53+
54+
stub_http_request("200", expected_response.to_json) do
55+
RailsClient.instance.stub(:check_if_server_is_running!, true) do
56+
RubyLsp::EventEmitter.new(listener).emit_for_target(Const("User"))
57+
end
58+
end
59+
60+
assert_includes(
61+
T.must(listener.response).contents.value,
62+
"[Schema](file://#{RailsClient.instance.root}/db/structure.sql)",
63+
)
64+
end
65+
66+
test "handles neither `db/structure.sql` nor `db/schema.rb` being present" do
67+
expected_response = {
68+
schema_file: nil,
69+
columns: [],
70+
}
71+
72+
listener = Hover.new
73+
74+
stub_http_request("200", expected_response.to_json) do
75+
RailsClient.instance.stub(:check_if_server_is_running!, true) do
76+
RubyLsp::EventEmitter.new(listener).emit_for_target(Const("User"))
77+
end
78+
end
79+
80+
refute_match(/Schema/, T.must(listener.response).contents.value)
81+
end
4482
end
4583
end

test/test_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
ActiveSupport::TestCase.fixtures(:all)
2222
end
2323

24+
$VERBOSE = nil unless ENV["VERBOSE"] || ENV["CI"]
25+
2426
module ActiveSupport
2527
class TestCase
2628
include SyntaxTree::DSL

0 commit comments

Comments
 (0)