Skip to content
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

Sql sessions have correct history manager #18933

Merged

Conversation

sjanusz-r7
Copy link
Contributor

This PR addresses the interactive query mode not having correctly separated input history between various SQL session types.
Now, each session type will have its own input history for normal query input in the session, as well as the interactive query mode. This support is provided for Reline and Readline as fallback.

Verification

  • Get a session for MySQL, MSSQL, and PostgreSQL
  • Run some queries in the context of the session by using query SELECT ... etc.
  • Confirm that the above query calls are not shared between session types.
  • Run some queries in the context of the interactive query mode (query -i) by using select version(); etc.
  • Confirm that the above interactive query calls are not shared between session types.
  • Confirm that the history persists between framework reboots.
  • Confirm that multi-line input in the interactive query prompt works as expected.

@sjanusz-r7 sjanusz-r7 force-pushed the sql-sessions-have-correct-history-manager branch from 1a31f2d to 8e1074a Compare March 7, 2024 15:59
name = session.type
query = {}

# Multiline (Reline) and fallback (Readline) have separate history contexts as they are two different libraries.
Copy link
Contributor

Choose a reason for hiding this comment

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

Just curious, what's the need for both libraries? If one session uses both at different times, would those two histories be merged somewhere/should they be/can they be?

Copy link
Contributor

@adfoster-r7 adfoster-r7 Mar 28, 2024

Choose a reason for hiding this comment

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

what's the need for both libraries?

Readline is our current library for prompting the user for input and providing tab suggestions on the command line; We're now using the Ruby Reline library (https://github.com/ruby/reline) for multi-line support for SQL queries

In the future we might only have reline - but that's not a blocker for this PR

clear_readline
if File.exist?(history_file)
File.readlines(history_file).each do |e|
::Readline::HISTORY << safe_undump(e.chomp)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this might be a weird hack/workaround; Maybe we should just align with the approach chosen by IRB (Which is coincidentally also backed by different backends of reline/readline)

https://github.com/ruby/irb/blob/1a1fbba020f1f504b2cb4d832ee4aa7af6aee0d3/lib/irb/history.rb#L71

@adfoster-r7 adfoster-r7 added the blocked Blocked by one or more additional tasks label Mar 14, 2024
@adfoster-r7 adfoster-r7 marked this pull request as draft March 14, 2024 11:00
@sjanusz-r7 sjanusz-r7 force-pushed the sql-sessions-have-correct-history-manager branch from 8e1074a to e2814d6 Compare March 26, 2024 11:16
@sjanusz-r7 sjanusz-r7 marked this pull request as ready for review March 26, 2024 11:36
@sjanusz-r7 sjanusz-r7 removed the blocked Blocked by one or more additional tasks label Mar 26, 2024
@adfoster-r7 adfoster-r7 merged commit c0d66fd into rapid7:master Mar 28, 2024
34 checks passed
@adfoster-r7
Copy link
Contributor

We might want to start putting all these history files into a single folder - as well as maybe having interactive not be after _history_:

image

But looks good to me 👍

@adfoster-r7
Copy link
Contributor

Release Notes

Updates the new SQL session types to correctly remember previous commands that the user has entered

def store_history_file(history_file)
return unless readline_available?
cmds = []
history_diff = ::Readline::HISTORY.length < MAX_HISTORY ? ::Readline::HISTORY.length : MAX_HISTORY
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like we've lost this logic, which is causing memory issues here: #19098

I think we should look into investigating and solving the underlying issue here instead of the 2000 history limit workaround, but we can tackle that separately

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rn-enhancement release notes enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants