Skip to content

Add reloading for schema changes #282

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 1 commit into from
Mar 6, 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
9 changes: 9 additions & 0 deletions lib/ruby_lsp/ruby_lsp_rails/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ def create_document_symbol_listener(response_builder, dispatcher)
DocumentSymbol.new(response_builder, dispatcher)
end

sig { params(changes: T::Array[{ uri: String, type: Integer }]).void }
def workspace_did_change_watched_files(changes)
if changes.any? do |change|
change[:uri].end_with?("db/schema.rb") || change[:uri].end_with?("structure.sql")
Copy link
Contributor Author

@andyw8 andyw8 Mar 6, 2024

Choose a reason for hiding this comment

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

Normally we would only have to support db/structure.sql but in Shopify/shopify we have additional structure files.

end
@client.trigger_reload
end
end

sig { override.returns(String) }
def name
"Ruby LSP Rails"
Expand Down
9 changes: 9 additions & 0 deletions lib/ruby_lsp/ruby_lsp_rails/runner_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ def model(name)
nil
end

sig { void }
def trigger_reload
warn("Reloading Rails application")
send_notification("reload")
rescue IncompleteMessageError
warn("Ruby LSP Rails failed to trigger reload")
nil
end

sig { void }
def shutdown
warn("Ruby LSP Rails shutting down server")
Expand Down
3 changes: 3 additions & 0 deletions lib/ruby_lsp/ruby_lsp_rails/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def execute(request, params = {})
VOID
when "model"
resolve_database_info_from_model(params.fetch(:name))
when "reload"
::Rails.application.reloader.reload!
VOID
else
VOID
end
Expand Down
39 changes: 39 additions & 0 deletions test/ruby_lsp_rails/addon_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,45 @@ class AddonTest < ActiveSupport::TestCase
addon = Addon.new
assert_equal("Ruby LSP Rails", addon.name)
end

test "sends reload notification if db/schema.rb is changed" do
changes = [
{
uri: "file://#{dummy_root}/db/schema.rb",
type: RubyLsp::Constant::FileChangeType::CHANGED,
},
]

RunnerClient.any_instance.expects(:send_notification).with("reload").once
addon = Addon.new
addon.workspace_did_change_watched_files(changes)
end

test "sends reload notification if a *structure.sql file is changed" do
changes = [
{
uri: "file://#{dummy_root}/db/structure.sql",
type: RubyLsp::Constant::FileChangeType::CHANGED,
},
]

RunnerClient.any_instance.expects(:send_notification).with("reload").once
addon = Addon.new
addon.workspace_did_change_watched_files(changes)
end

test "does not send reload notification if schema is not changed" do
changes = [
{
uri: "file://#{dummy_root}/app/models/foo.rb",
type: RubyLsp::Constant::FileChangeType::CHANGED,
},
]

RunnerClient.any_instance.expects(:send_notification).never
addon = Addon.new
addon.workspace_did_change_watched_files(changes)
end
end
end
end
4 changes: 0 additions & 4 deletions test/ruby_lsp_rails/hover_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,6 @@ def hover_on_source(source, position)
assert_nil(response.error)
response.response
end

def dummy_root
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Move into test_helper so that we can share.

File.expand_path("#{__dir__}/../../test/dummy")
end
end
end
end
8 changes: 8 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@
DEBUGGER__::CONFIG[:skip_path] =
Array(DEBUGGER__::CONFIG[:skip_path]) + Gem.loaded_specs["sorbet-runtime"].full_require_paths
end

module ActiveSupport
class TestCase
def dummy_root
File.expand_path("#{__dir__}/dummy")
end
end
end