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

Update sql prompt on db change #18903

Merged

Conversation

sjanusz-r7
Copy link
Contributor

This PR is a UX improvement. It adds the correct handling for changing of databases in the context of a MySQL or MSSQL session. The prompt now gets updated with an up-to-date value.
This PR also aligns the SQL clients to use the current_database method.

MySQL

Before

MySQL @ 127.0.0.1:3306 () > query show databases
Response
========

    #  Database
    -  --------
    0  information_schema
    1  my_docker_db
    2  my_new_db
    3  mysql
    4  performance_schema
    5  sys

MySQL @ 127.0.0.1:3306 () > query use mysql
[-] Error running command query: NoMethodError undefined method `entries' for nil:NilClass
MySQL @ 127.0.0.1:3306 () > query use garbage
[-] Query has failed. Reasons: Unknown database 'garbage'

After

MySQL @ 127.0.0.1:3306 () > query show databases
Response
========

    #  Database
    -  --------
    0  information_schema
    1  my_docker_db
    2  my_new_db
    3  mysql
    4  performance_schema
    5  sys

MySQL @ 127.0.0.1:3306 () > query use mysql
[*] Query executed successfully
MySQL @ 127.0.0.1:3306 (mysql) > query use sys
[*] Query executed successfully
MySQL @ 127.0.0.1:3306 (sys) > query use garbage
[-] Query has failed. Reasons: Unknown database 'garbage'

MSSQL

Before

MSSQL @ 192.168.112.3:1433 (master) > query select name from master.dbo.sysdatabases
Response
========

    #  name
    -  ----
    0  master
    1  tempdb
    2  model
    3  msdb

MSSQL @ 192.168.112.3:1433 (master) > query use msdb
[-] Error running command query: NoMethodError undefined method `map' for nil:NilClass
MSSQL @ 192.168.112.3:1433 (msdb) > query use master
[-] Error running command query: NoMethodError undefined method `map' for nil:NilClass
MSSQL @ 192.168.112.3:1433 (master) > query use garbage
[-] Query has failed. Reasons: SQL Server Error #911 (State:1 Severity:16): Database 'garbage' does not exist. Make sure that the name is entered correctly.

After

MSSQL @ 192.168.112.3:1433 (master) > query select name from master.dbo.sysdatabases
Response
========

    #  name
    -  ----
    0  master
    1  tempdb
    2  model
    3  msdb

MSSQL @ 192.168.112.3:1433 (master) > query use msdb
[*] Query executed successfully
MSSQL @ 192.168.112.3:1433 (msdb) > query use master
[*] Query executed successfully
MSSQL @ 192.168.112.3:1433 (master) > query use garbage
[-] Query has failed. Reasons: SQL Server Error #911 (State:1 Severity:16): Database 'garbage' does not exist. Make sure that the name is entered correctly.

Bogus & Correct Queries

The following ensures we get the correct output for good and bogus queries:

MSSQL @ 192.168.112.3:1433 (master) > query use hello
[-] Query has failed. Reasons: SQL Server Error #911 (State:1 Severity:16): Database 'hello' does not exist. Make sure that the name is entered correctly.
MSSQL @ 192.168.112.3:1433 (master) > query use msdb
[*] Query executed successfully
MSSQL @ 192.168.112.3:1433 (msdb) > query select @@version
Response
========

    #  NULL
    -  ----
    0  Microsoft SQL Server 2022 (RTM) - 16.0.1000.6 (X64)
        Oct  8 2022 05:58:25
        Copyright (C) 2022 Microsoft Corporation
        Express Edition (64-bit) on Windows Server 2022 Standard 10.0 <X64> (Build 20348: ) (Hypervisor)

...

MySQL @ 127.0.0.1:3306 (sys) > query use hello
[-] Query has failed. Reasons: Unknown database 'hello'
MySQL @ 127.0.0.1:3306 (sys) > query show databases
Response
========

    #  Database
    -  --------
    0  information_schema
    1  my_docker_db
    2  my_new_db
    3  mysql
    4  performance_schema
    5  sys

...

PostgreSQL @ 127.0.0.1:5432 (template1) > query select version
[-] Query has failed. Reasons: ERROR    VERROR  C42703  Mcolumn "version" does not exist        P8      Fparse_relation.c       L3713   RerrorMissingColumn
PostgreSQL @ 127.0.0.1:5432 (template1) > query select version()
[*] SELECT 1

Response
========

    #  version
    -  -------
    0  PostgreSQL 16.1 (Debian 16.1-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit

Verification

  • Get a PostgreSQL, MSSQL and MySQL session
  • Verify you can change the database by typing in query use ....
  • Ensure typing in correct queries results in the expected output with no errors
  • Ensure typing in bogus queries results in errors being output

@cgranleese-r7
Copy link
Contributor

Everything worked as expected 👍

MySQL

Before

image

After

image

MSSQL

Before

image

After

image

PostgreSQL

image

@cgranleese-r7 cgranleese-r7 self-assigned this Feb 29, 2024
@cgranleese-r7 cgranleese-r7 added the rn-enhancement release notes enhancement label Feb 29, 2024
@sjanusz-r7 sjanusz-r7 force-pushed the update-sql-prompt-on-db-change branch from 41bf0f5 to 8184035 Compare March 1, 2024 11:45
@cgranleese-r7
Copy link
Contributor

Tested again after the recent changes and everything seems to be working as expected 👍

MySQL

image

MSSQL

image

@cgranleese-r7 cgranleese-r7 merged commit 97ef5cf into rapid7:master Mar 4, 2024
33 of 34 checks passed
@cgranleese-r7
Copy link
Contributor

cgranleese-r7 commented Mar 4, 2024

Release Notes

This PR improves the UX by correctly handling databases changes by updating the prompt to now get the appropriate database value in the context of a MySQL or MSSQL session.

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.

None yet

3 participants