Skip to content

Commit

Permalink
Offer to run migrations only if not recently added
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Jan 8, 2025
1 parent 6701658 commit 77f986f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/ruby_lsp/ruby_lsp_rails/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
58 changes: 58 additions & 0 deletions test/ruby_lsp_rails/addon_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 77f986f

Please sign in to comment.