Skip to content

Commit ed241b3

Browse files
committed
Ensure schema_dump_path method is available
1 parent ba16bf3 commit ed241b3

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
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 && 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/server_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
test "handles older Rails version which don't have `schema_dump_path`" do
9+
ActiveRecord::Tasks::DatabaseTasks.send(:alias_method, :old_schema_dump_path, :schema_dump_path)
10+
ActiveRecord::Tasks::DatabaseTasks.undef_method(:schema_dump_path)
11+
response = RubyLsp::Rails::Server.new.resolve_database_info_from_model("User")
12+
assert_nil(response.fetch(:result)[:schema_file])
13+
ensure
14+
ActiveRecord::Tasks::DatabaseTasks.send(:alias_method, :schema_dump_path, :old_schema_dump_path)
15+
end
16+
end

0 commit comments

Comments
 (0)