Skip to content

Commit

Permalink
refactor(connection): TailpipeConnection now supports indexes/partiti…
Browse files Browse the repository at this point in the history
…ons in addition to to/from. Closes #632
  • Loading branch information
graza-io authored Feb 11, 2025
1 parent 691e6fd commit 782a4b6
Showing 1 changed file with 56 additions and 5 deletions.
61 changes: 56 additions & 5 deletions connection/tailpipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
"time"

"github.com/hashicorp/hcl/v2"
"github.com/turbot/go-kit/helpers"
"github.com/zclconf/go-cty/cty"

"github.com/turbot/go-kit/helpers"
)

const TailpipeConnectionType = "tailpipe"
Expand All @@ -25,9 +26,12 @@ type TailpipeConnectResponse struct {
type TailpipeConnection struct {
ConnectionImpl

From *string `cty:"from" hcl:"from"`
To *string `cty:"to" hcl:"to"`
// if an option is passed to GetConnectionString, it may override the From and To values
From *string `cty:"from" hcl:"from"`
To *string `cty:"to" hcl:"to"`
Indexes *[]string `cty:"indexes" hcl:"indexes"`
Partitions *[]string `cty:"partitions" hcl:"partitions"`

// if an option is passed to GetConnectionString, it may override the From, To, Indexes or Partitions values
OverrideFilters *TailpipeDatabaseFilters

// store a maps of connection strings, keyed by the filters used to create the db
Expand Down Expand Up @@ -76,6 +80,14 @@ func (c *TailpipeConnection) GetConnectionString(opts ...ConnectionStringOpt) (s
args = append(args, "--to", to.Format(time.RFC3339))
}

if len(filters.Indexes) > 0 {
args = append(args, "--index", fmt.Sprintf("\"%s\"", strings.Join(filters.Indexes, ",")))
}

if len(filters.Partitions) > 0 {
args = append(args, "--partition", fmt.Sprintf("\"%s\"", strings.Join(filters.Partitions, ",")))
}

// see if we already have a connection string for these filters
filterKey := filters.String()
if connectionString, ok := c.connectionStrings[filterKey]; ok {
Expand Down Expand Up @@ -147,6 +159,30 @@ func (c *TailpipeConnection) Equals(otherConnection PipelingConnection) bool {
return false
}

if c.Indexes == nil && other.Indexes != nil {
return false
}

if c.Indexes != nil && other.Indexes == nil {
return false
}

if c.Indexes != nil && other.Indexes != nil && !slices.Equal(*c.Indexes, *other.Indexes) {
return false
}

if c.Partitions == nil && other.Partitions != nil {
return false
}

if c.Partitions != nil && other.Partitions == nil {
return false
}

if c.Partitions != nil && other.Partitions != nil && !slices.Equal(*c.Partitions, *other.Partitions) {
return false
}

return c.GetConnectionImpl().Equals(other.GetConnectionImpl())
}

Expand All @@ -172,6 +208,14 @@ func (c *TailpipeConnection) getFilters() *TailpipeDatabaseFilters {
res.To = &to
}

if c.Indexes != nil && len(*c.Indexes) > 0 {
res.Indexes = *c.Indexes
}

if c.Partitions != nil && len(*c.Partitions) > 0 {
res.Partitions = *c.Partitions
}

// if we have overrides, use them
if c.OverrideFilters != nil {
if c.OverrideFilters.From != nil {
Expand All @@ -184,8 +228,15 @@ func (c *TailpipeConnection) getFilters() *TailpipeDatabaseFilters {
res.To = overrideTo
}
}

if len(c.OverrideFilters.Indexes) > 0 {
res.Indexes = c.OverrideFilters.Indexes
}

if len(c.OverrideFilters.Partitions) > 0 {
res.Partitions = c.OverrideFilters.Partitions
}
}
// TODO partitions and indexes https://github.com/turbot/powerpipe/issues/644

return res
}
Expand Down

0 comments on commit 782a4b6

Please # to comment.