github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/db/db_common/errors.go (about)

     1  package db_common
     2  
     3  import (
     4  	"errors"
     5  	"github.com/jackc/pgx/v5/pgconn"
     6  	"regexp"
     7  )
     8  
     9  func IsRelationNotFoundError(err error) bool {
    10  	_, _, isRelationNotFound := GetMissingSchemaFromIsRelationNotFoundError(err)
    11  	return isRelationNotFound
    12  }
    13  
    14  func GetMissingSchemaFromIsRelationNotFoundError(err error) (string, string, bool) {
    15  	if err == nil {
    16  		return "", "", false
    17  	}
    18  	var pgErr *pgconn.PgError
    19  	ok := errors.As(err, &pgErr)
    20  	if !ok || pgErr.Code != "42P01" {
    21  		return "", "", false
    22  	}
    23  
    24  	r := regexp.MustCompile(`^relation "(.*)\.(.*)" does not exist$`)
    25  	captureGroups := r.FindStringSubmatch(pgErr.Message)
    26  	if len(captureGroups) == 3 {
    27  
    28  		return captureGroups[1], captureGroups[2], true
    29  	}
    30  
    31  	// maybe there is no schema
    32  	r = regexp.MustCompile(`^relation "(.*)" does not exist$`)
    33  	captureGroups = r.FindStringSubmatch(pgErr.Message)
    34  	if len(captureGroups) == 2 {
    35  		return "", captureGroups[1], true
    36  	}
    37  	return "", "", true
    38  }