From 77f986fdb2e1ce852f71dc800e99ed1cc4fd102d Mon Sep 17 00:00:00 2001 From: Andy Waite <13400+andyw8@users.noreply.github.com> Date: Wed, 8 Jan 2025 11:58:07 -0500 Subject: [PATCH 1/2] Offer to run migrations only if not recently added --- lib/ruby_lsp/ruby_lsp_rails/addon.rb | 12 +++++- test/ruby_lsp_rails/addon_test.rb | 58 ++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/lib/ruby_lsp/ruby_lsp_rails/addon.rb b/lib/ruby_lsp/ruby_lsp_rails/addon.rb index e10e9e91..88ee5c42 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/addon.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/addon.rb @@ -138,8 +138,12 @@ def workspace_did_change_watched_files(changes) @rails_runner_client.trigger_reload end + # Asking the user to run a migration which has just been created may be overly-aggressive, but we _do_ + # want to offer it after switching branchs. As a middle-ground, we only offer to run migrations if the + # migration was created more than a minute ago. if changes.any? do |c| - %r{db/migrate/.*\.rb}.match?(c[:uri]) && c[:type] != Constant::FileChangeType::CHANGED + %r{db/migrate/.*\.rb}.match?(c[:uri]) && + c[:type] == Constant::FileChangeType::CREATED && !changed_in_last_minute?(c[:uri]) end offer_to_run_pending_migrations @@ -178,6 +182,12 @@ def handle_window_show_message_response(title) private + sig { params(uri: String).returns(T::Boolean) } + def changed_in_last_minute?(uri) + path = URI(uri).to_standardized_path + File.stat(path).mtime > (Time.now - 60) + end + sig { params(id: String, title: String, percentage: T.nilable(Integer), message: T.nilable(String)).void } def begin_progress(id, title, percentage: nil, message: nil) return unless @global_state&.client_capabilities&.supports_progress && @outgoing_queue diff --git a/test/ruby_lsp_rails/addon_test.rb b/test/ruby_lsp_rails/addon_test.rb index 63213552..390fb2ff 100644 --- a/test/ruby_lsp_rails/addon_test.rb +++ b/test/ruby_lsp_rails/addon_test.rb @@ -92,6 +92,64 @@ class AddonTest < ActiveSupport::TestCase ensure T.must(outgoing_queue).close end + + test "offers to run migrations created more than 60 seconds ago" do + path = "#{dummy_root}/db/migrate/20210101000000_create_foos.rb" + begin + mtime = 1.minute.ago.to_time + FileUtils.touch(path, mtime: mtime) + changes = [ + { + uri: "file://#{path}", + type: RubyLsp::Constant::FileChangeType::CREATED, + }, + ] + + addon = Addon.new + addon.expects(:offer_to_run_pending_migrations).once + addon.workspace_did_change_watched_files(changes) + ensure + FileUtils.rm(path) + end + end + + test "does not offer to run migrations created less than 60 seconds ago" do + path = "#{dummy_root}/db/migrate/20210101000000_create_foos.rb" + begin + mtime = 30.seconds.ago.to_time + FileUtils.touch(path, mtime: mtime) + changes = [ + { + uri: "file://#{path}", + type: RubyLsp::Constant::FileChangeType::CREATED, + }, + ] + + addon = Addon.new + addon.expects(:offer_to_run_pending_migrations).never + addon.workspace_did_change_watched_files(changes) + ensure + FileUtils.rm(path) + end + end + + test "doesn't offers to run migrations if deleted" do + path = "#{dummy_root}/db/migrate/20210101000000_create_foos.rb" + changes = [ + { + uri: "file://#{path}", + type: Constant::FileChangeType::DELETED, + }, + { + uri: "file://#{path}", + type: RubyLsp::Constant::FileChangeType::CHANGED, + }, + ] + + addon = Addon.new + addon.expects(:offer_to_run_pending_migrations).never + addon.workspace_did_change_watched_files(changes) + end end end end From ff338d5fba256d975e4ee066b00da95e14151979 Mon Sep 17 00:00:00 2001 From: Andy Waite <13400+andyw8@users.noreply.github.com> Date: Wed, 8 Jan 2025 12:01:47 -0500 Subject: [PATCH 2/2] Update comment --- lib/ruby_lsp/ruby_lsp_rails/addon.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/ruby_lsp/ruby_lsp_rails/addon.rb b/lib/ruby_lsp/ruby_lsp_rails/addon.rb index 88ee5c42..0d650683 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/addon.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/addon.rb @@ -138,9 +138,9 @@ def workspace_did_change_watched_files(changes) @rails_runner_client.trigger_reload end - # Asking the user to run a migration which has just been created may be overly-aggressive, but we _do_ - # want to offer it after switching branchs. As a middle-ground, we only offer to run migrations if the - # migration was created more than a minute ago. + # It may be too agressive to ask a user to run a migration which has just been created. + # But we do want to offer it for situations such as switching branches or pulling from a remote. + # As a middle-ground, we only offer to run migrations if the # migration was created more than a minute ago. if changes.any? do |c| %r{db/migrate/.*\.rb}.match?(c[:uri]) && c[:type] == Constant::FileChangeType::CREATED && !changed_in_last_minute?(c[:uri])