Skip to content

Handle schema_dump_path being unavailable in older Rails versions #264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 21, 2024
Merged
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
14 changes: 9 additions & 5 deletions lib/ruby_lsp/ruby_lsp_rails/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Server

extend T::Sig

sig { params(model_name: String).returns(T.nilable(T::Hash[Symbol, T.untyped])) }
sig { params(model_name: String).returns(T::Hash[Symbol, T.untyped]) }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realised that we always return a hash.

def resolve_database_info_from_model(model_name)
const = ActiveSupport::Inflector.safe_constantize(model_name)
unless const && const < ActiveRecord::Base && !const.abstract_class?
Expand All @@ -35,14 +35,18 @@ def resolve_database_info_from_model(model_name)
}
end

schema_file = ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(const.connection.pool.db_config)

{
info = {
result: {
columns: const.columns.map { |column| [column.name, column.type] },
schema_file: ::Rails.root + schema_file,
},
}

if ActiveRecord::Tasks::DatabaseTasks.respond_to?(:schema_dump_path)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible we can make this also work in Rails 6 and earlier, if someone wants to open a PR.

info[:result][:schema_file] =
ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(const.connection.pool.db_config)

end
info
rescue => e
{
error: e.message,
Expand Down
14 changes: 4 additions & 10 deletions test/ruby_lsp_rails/runner_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class RunnerClientTest < ActiveSupport::TestCase
assert_predicate @client, :stopped?
end

# These are integration tests which start the server. For the more fine-grained tests, see `server_test.rb`.

test "#model returns information for the requested model" do
# These columns are from the schema in the dummy app: test/dummy/db/schema.rb
columns = [
Expand All @@ -31,16 +33,8 @@ class RunnerClientTest < ActiveSupport::TestCase
assert_match(%r{db/schema\.rb$}, response.fetch(:schema_file))
end

test "returns nil if model doesn't exist" do
assert_nil @client.model("Foo")
end

test "returns nil if class is not a model" do
assert_nil @client.model("Time")
end

test "returns nil if class is an abstract model" do
assert_nil @client.model("ApplicationRecord")
test "returns nil if the request returns a nil response" do
assert_nil @client.model("ApplicationRecord") # ApplicationRecord is abstract
end
end
end
Expand Down
35 changes: 35 additions & 0 deletions test/ruby_lsp_rails/server_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# typed: true
# frozen_string_literal: true

require "test_helper"
require "ruby_lsp/ruby_lsp_rails/server"

class ServerTest < ActiveSupport::TestCase
Copy link
Contributor Author

@andyw8 andyw8 Feb 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vinistock since the server is in a different process, this needs to be tested differently from the other aspects in runner_client_test.rb. But I think it would make sense to move some of the other tests to here too, and leave just #model returns information for the requested model as an integration test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed a commit to show how that would look.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine, but let's put the server in an instance variable instead of doing Server.new on every test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine, but let's put the server in an instance variable instead of doing Server.new on every test.

setup do
@server = RubyLsp::Rails::Server.new
end

test "returns nil if model doesn't exist" do
response = @server.resolve_database_info_from_model("Foo")
assert_nil(response.fetch(:result))
end

test "returns nil if class is not a model" do
response = @server.resolve_database_info_from_model("Time")
assert_nil(response.fetch(:result))
end

test "returns nil if class is an abstract model" do
response = @server.resolve_database_info_from_model("ApplicationRecord")
assert_nil(response.fetch(:result))
end

test "handles older Rails version which don't have `schema_dump_path`" do
ActiveRecord::Tasks::DatabaseTasks.send(:alias_method, :old_schema_dump_path, :schema_dump_path)
ActiveRecord::Tasks::DatabaseTasks.undef_method(:schema_dump_path)
response = @server.resolve_database_info_from_model("User")
assert_nil(response.fetch(:result)[:schema_file])
ensure
ActiveRecord::Tasks::DatabaseTasks.send(:alias_method, :schema_dump_path, :old_schema_dump_path)
end
end