Files
steampipe/pkg/db/db_common/sql_connections.go
kaidaguerre 40804a3201 Execute RefreshConnections asyncronously and optimise for high connection count. Add connection_state table.
- Execute RefreshConnections asyncronously
- Add connection_state table to indicate the loading state of connections
- Optimise RefreshConnections by cloning connection schemas
- Add locking to ensure only a single instance of RefreshConnections runs
- Start executing queries without waiting for connections to load, add smart error handling to wait for required connection
- Optimise autocomplete for high connection count
- Autocomplete and inspect data available before all conections are refreshed
- Update file watcher to respond to CHMOD, so thaat it pickes up deletion of file contents

 Closes #3394
 Closes #3267
2023-05-10 09:05:08 +01:00

64 lines
2.6 KiB
Go

package db_common
import (
"fmt"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"strings"
)
func GetCommentsQueryForPlugin(connectionName string, p map[string]*proto.TableSchema) string {
var statements strings.Builder
for t, schema := range p {
table := PgEscapeName(t)
schemaName := PgEscapeName(connectionName)
if schema.Description != "" {
tableDescription := PgEscapeString(schema.Description)
statements.WriteString(fmt.Sprintf("COMMENT ON FOREIGN TABLE %s.%s is %s;\n", schemaName, table, tableDescription))
}
for _, c := range schema.Columns {
if c.Description != "" {
column := PgEscapeName(c.Name)
columnDescription := PgEscapeString(c.Description)
statements.WriteString(fmt.Sprintf("COMMENT ON COLUMN %s.%s.%s is %s;\n", schemaName, table, column, columnDescription))
}
}
}
return statements.String()
}
func GetUpdateConnectionQuery(localSchema, remoteSchema string) string {
// escape the name
localSchema = PgEscapeName(localSchema)
var statements strings.Builder
// Each connection has a unique schema. The schema, and all objects inside it,
// are owned by the root user.
statements.WriteString(fmt.Sprintf("drop schema if exists %s cascade;\n", localSchema))
statements.WriteString(fmt.Sprintf("create schema %s;\n", localSchema))
statements.WriteString(fmt.Sprintf("comment on schema %s is 'steampipe plugin: %s';\n", localSchema, remoteSchema))
// Steampipe users are allowed to use the new schema
statements.WriteString(fmt.Sprintf("grant usage on schema %s to steampipe_users;\n", localSchema))
// Permissions are limited to select only, and should be granted for all new
// objects. Steampipe users cannot create tables or modify data in the
// connection schema - they need to use the public schema for that. These
// commands alter the defaults for any objects created in the future.
// See https://www.postgresql.org/docs/12/ddl-priv.html
statements.WriteString(fmt.Sprintf("alter default privileges in schema %s grant select on tables to steampipe_users;\n", localSchema))
// If there are any objects already then grant their permissions now. (This
// should not actually do anything at this point.)
statements.WriteString(fmt.Sprintf("grant select on all tables in schema %s to steampipe_users;\n", localSchema))
// Import the foreign schema into this connection.
statements.WriteString(fmt.Sprintf("import foreign schema \"%s\" from server steampipe into %s;\n", remoteSchema, localSchema))
return statements.String()
}
func GetDeleteConnectionQuery(name string) string {
return fmt.Sprintf("DROP SCHEMA IF EXISTS %s CASCADE;\n", PgEscapeName(name))
}