github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/workspace/session_data.go (about)

     1  package workspace
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  
     7  	"github.com/jackc/pgx/v5"
     8  	"github.com/turbot/steampipe/pkg/db/db_common"
     9  	"github.com/turbot/steampipe/pkg/db/db_local"
    10  	"github.com/turbot/steampipe/pkg/steampipeconfig/modconfig"
    11  	"github.com/turbot/steampipe/pkg/utils"
    12  )
    13  
    14  // EnsureSessionData determines whether introspection tables
    15  // exists for this session, and if not, creates them if needed
    16  func EnsureSessionData(ctx context.Context, source *modconfig.ResourceMaps, conn *pgx.Conn) error {
    17  	utils.LogTime("workspace.EnsureSessionData start")
    18  	defer utils.LogTime("workspace.EnsureSessionData end")
    19  
    20  	if conn == nil {
    21  		return errors.New("nil conn passed to EnsureSessionData")
    22  	}
    23  
    24  	return db_common.ExecuteSystemClientCall(ctx, conn, func(ctx context.Context, tx pgx.Tx) error {
    25  		// check for introspection tables
    26  		// if the steampipe_mod table is missing, assume we have no session data - go ahead and create it
    27  		row := tx.QueryRow(ctx, "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema LIKE 'pg_temp%' AND table_name='steampipe_mod' ")
    28  		var count int
    29  		if err := row.Scan(&count); err != nil {
    30  			return err
    31  		}
    32  		if count == 0 {
    33  			if err := db_local.CreateIntrospectionTables(ctx, source, tx); err != nil {
    34  				return err
    35  			}
    36  		}
    37  		return nil
    38  	})
    39  }