Skip to content

Commit 315db8e

Browse files
authored
Handle schema_dump_path being unavailable in older Rails versions (#264)
1 parent f32f92c commit 315db8e

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

lib/ruby_lsp/ruby_lsp_rails/server.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Server
2626

2727
extend T::Sig
2828

29-
sig { params(model_name: String).returns(T.nilable(T::Hash[Symbol, T.untyped])) }
29+
sig { params(model_name: String).returns(T::Hash[Symbol, T.untyped]) }
3030
def resolve_database_info_from_model(model_name)
3131
const = ActiveSupport::Inflector.safe_constantize(model_name)
3232
unless const && defined?(ActiveRecord) && const < ActiveRecord::Base && !const.abstract_class?
@@ -35,14 +35,18 @@ def resolve_database_info_from_model(model_name)
3535
}
3636
end
3737

38-
schema_file = ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(const.connection.pool.db_config)
39-
40-
{
38+
info = {
4139
result: {
4240
columns: const.columns.map { |column| [column.name, column.type] },
43-
schema_file: ::Rails.root + schema_file,
4441
},
4542
}
43+
44+
if ActiveRecord::Tasks::DatabaseTasks.respond_to?(:schema_dump_path)
45+
info[:result][:schema_file] =
46+
ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(const.connection.pool.db_config)
47+
48+
end
49+
info
4650
rescue => e
4751
{
4852
error: e.message,

test/ruby_lsp_rails/runner_client_test.rb

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class RunnerClientTest < ActiveSupport::TestCase
1616
assert_predicate @client, :stopped?
1717
end
1818

19+
# These are integration tests which start the server. For the more fine-grained tests, see `server_test.rb`.
20+
1921
test "#model returns information for the requested model" do
2022
# These columns are from the schema in the dummy app: test/dummy/db/schema.rb
2123
columns = [
@@ -31,16 +33,8 @@ class RunnerClientTest < ActiveSupport::TestCase
3133
assert_match(%r{db/schema\.rb$}, response.fetch(:schema_file))
3234
end
3335

34-
test "returns nil if model doesn't exist" do
35-
assert_nil @client.model("Foo")
36-
end
37-
38-
test "returns nil if class is not a model" do
39-
assert_nil @client.model("Time")
40-
end
41-
42-
test "returns nil if class is an abstract model" do
43-
assert_nil @client.model("ApplicationRecord")
36+
test "returns nil if the request returns a nil response" do
37+
assert_nil @client.model("ApplicationRecord") # ApplicationRecord is abstract
4438
end
4539
end
4640
end

test/ruby_lsp_rails/server_test.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# typed: true
2+
# frozen_string_literal: true
3+
4+
require "test_helper"
5+
require "ruby_lsp/ruby_lsp_rails/server"
6+
7+
class ServerTest < ActiveSupport::TestCase
8+
setup do
9+
@server = RubyLsp::Rails::Server.new
10+
end
11+
12+
test "returns nil if model doesn't exist" do
13+
response = @server.resolve_database_info_from_model("Foo")
14+
assert_nil(response.fetch(:result))
15+
end
16+
17+
test "returns nil if class is not a model" do
18+
response = @server.resolve_database_info_from_model("Time")
19+
assert_nil(response.fetch(:result))
20+
end
21+
22+
test "returns nil if class is an abstract model" do
23+
response = @server.resolve_database_info_from_model("ApplicationRecord")
24+
assert_nil(response.fetch(:result))
25+
end
26+
27+
test "handles older Rails version which don't have `schema_dump_path`" do
28+
ActiveRecord::Tasks::DatabaseTasks.send(:alias_method, :old_schema_dump_path, :schema_dump_path)
29+
ActiveRecord::Tasks::DatabaseTasks.undef_method(:schema_dump_path)
30+
response = @server.resolve_database_info_from_model("User")
31+
assert_nil(response.fetch(:result)[:schema_file])
32+
ensure
33+
ActiveRecord::Tasks::DatabaseTasks.send(:alias_method, :schema_dump_path, :old_schema_dump_path)
34+
end
35+
end

0 commit comments

Comments
 (0)