Local queries (#641)
* Read local queries from pgweb home directory * Refactor local query functionality * Allow picking local query in the query tab * WIP * Disable local query dropdown during execution * Only allow local queries running in a single session mode * Add middleware to enforce local query endpoint availability * Fix query check * Add query store tests * Make query store errors portable * Skip building specific tests on windows
This commit is contained in:
@@ -585,3 +585,44 @@ func (client *Client) hasHistoryRecord(query string) bool {
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
type ConnContext struct {
|
||||
Host string
|
||||
User string
|
||||
Database string
|
||||
Mode string
|
||||
}
|
||||
|
||||
func (c ConnContext) String() string {
|
||||
return fmt.Sprintf(
|
||||
"host=%q user=%q database=%q mode=%q",
|
||||
c.Host, c.User, c.Database, c.Mode,
|
||||
)
|
||||
}
|
||||
|
||||
// ConnContext returns information about current database connection
|
||||
func (client *Client) GetConnContext() (*ConnContext, error) {
|
||||
url, err := neturl.Parse(client.ConnectionString)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
||||
defer cancel()
|
||||
|
||||
connCtx := ConnContext{
|
||||
Host: url.Hostname(),
|
||||
Mode: "default",
|
||||
}
|
||||
|
||||
if command.Opts.ReadOnly {
|
||||
connCtx.Mode = "readonly"
|
||||
}
|
||||
|
||||
row := client.db.QueryRowContext(ctx, "SELECT current_user, current_database()")
|
||||
if err := row.Scan(&connCtx.User, &connCtx.Database); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &connCtx, nil
|
||||
}
|
||||
|
||||
@@ -660,6 +660,15 @@ func testTablesStats(t *testing.T) {
|
||||
assert.Equal(t, columns, result.Columns)
|
||||
}
|
||||
|
||||
func testConnContext(t *testing.T) {
|
||||
result, err := testClient.GetConnContext()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "localhost", result.Host)
|
||||
assert.Equal(t, "postgres", result.User)
|
||||
assert.Equal(t, "booktown", result.Database)
|
||||
assert.Equal(t, "default", result.Mode)
|
||||
}
|
||||
|
||||
func TestAll(t *testing.T) {
|
||||
if onWindows() {
|
||||
t.Log("Unit testing on Windows platform is not supported.")
|
||||
@@ -698,6 +707,7 @@ func TestAll(t *testing.T) {
|
||||
testReadOnlyMode(t)
|
||||
testDumpExport(t)
|
||||
testTablesStats(t)
|
||||
testConnContext(t)
|
||||
|
||||
teardownClient()
|
||||
teardown(t, true)
|
||||
|
||||
Reference in New Issue
Block a user