mirror of
https://github.com/turbot/steampipe.git
synced 2026-03-01 17:01:00 -05:00
37 lines
1.3 KiB
Go
37 lines
1.3 KiB
Go
package workspace
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/turbot/steampipe/db/db_common"
|
|
"github.com/turbot/steampipe/query/queryresult"
|
|
"github.com/turbot/steampipe/utils"
|
|
)
|
|
|
|
// EnsureSessionData determines whether session scoped data (introspection tables and prepared statements)
|
|
// exists for this session, and if not, creates it
|
|
func EnsureSessionData(ctx context.Context, source *SessionDataSource, client db_common.Client) error {
|
|
utils.LogTime("workspace.EnsureSessionData start")
|
|
defer utils.LogTime("workspace.EnsureSessionData end")
|
|
|
|
// check for introspection tables
|
|
result, err := client.ExecuteSync(ctx, "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema LIKE 'pg_temp%' AND table_name='steampipe_mod' ", true)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// since we are quering with a 'select count...', we will always have exactly one cell with the value
|
|
count := result.Rows[0].(*queryresult.RowResult).Data[0].(int64)
|
|
|
|
// if the steampipe_mod table is missing, assume we have no session data - go ahead and create it
|
|
if count == 0 {
|
|
err = db_common.CreatePreparedStatements(context.Background(), source.PreparedStatementSource, client)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = db_common.CreateIntrospectionTables(ctx, source.IntrospectionTableSource, client); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|