-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add import/export functionality
- Loading branch information
Showing
42 changed files
with
2,564 additions
and
2,938 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
name: Database Tests | ||
on: | ||
push: | ||
paths-ignore: | ||
- '*.md' | ||
- '.all-contributorsrc' | ||
|
||
jobs: | ||
## Postgres Tests | ||
postgres: | ||
name: Postgres | ||
runs-on: ubuntu-latest | ||
|
||
services: | ||
postgres: | ||
image: postgres@sha256:c132d7802dcc127486a403fb9e9a52d9df2e3ab84037c5de8395ed6ba2743e20 | ||
ports: | ||
# will assign a random free host port | ||
- 5432/tcp | ||
# needed because the postgres container does not provide a healthcheck | ||
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 | ||
env: | ||
POSTGRES_DB: flipt_test | ||
POSTGRES_USER: postgres | ||
POSTGRES_PASSWORD: '' | ||
|
||
steps: | ||
- name: Setup Go | ||
uses: actions/setup-go@v1 | ||
with: | ||
go-version: 1.13.7 | ||
id: go | ||
|
||
- name: Checkout | ||
uses: actions/checkout@v1 | ||
|
||
- name: Restore Cache | ||
uses: actions/cache@preview | ||
id: cache | ||
with: | ||
path: ~/go/pkg/mod | ||
key: ${{ runner.os }}-${{ hashFiles('**/go.sum') }} | ||
|
||
- name: Unit Test (Postgres) | ||
run: DB_URL="postgres://postgres@localhost:${{ job.services.postgres.ports['5432'] }}/flipt_test?sslmode=disable" go test -count=1 -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
sq "github.com/Masterminds/squirrel" | ||
"github.com/markphelps/flipt/storage" | ||
"github.com/markphelps/flipt/storage/db" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
type Document struct { | ||
Flags []*Flag `yaml:"flags,omitempty"` | ||
Segments []*Segment `yaml:"segments,omitempty"` | ||
} | ||
|
||
type Flag struct { | ||
Key string `yaml:"key,omitempty"` | ||
Name string `yaml:"name,omitempty"` | ||
Description string `yaml:"description,omitempty"` | ||
Enabled bool `yaml:"enabled"` | ||
Variants []*Variant `yaml:"variants,omitempty"` | ||
Rules []*Rule `yaml:"rules,omitempty"` | ||
} | ||
|
||
type Variant struct { | ||
Key string `yaml:"key,omitempty"` | ||
Name string `yaml:"name,omitempty"` | ||
Description string `yaml:"description,omitempty"` | ||
} | ||
|
||
type Rule struct { | ||
SegmentKey string `yaml:"segment,omitempty"` | ||
Rank uint `yaml:"rank,omitempty"` | ||
Distributions []*Distribution `yaml:"distributions,omitempty"` | ||
} | ||
|
||
type Distribution struct { | ||
VariantKey string `yaml:"variant,omitempty"` | ||
Rollout float32 `yaml:"rollout,omitempty"` | ||
} | ||
|
||
type Segment struct { | ||
Key string `yaml:"key,omitempty"` | ||
Name string `yaml:"name,omitempty"` | ||
Description string `yaml:"description,omitempty"` | ||
Constraints []*Constraint `yaml:"constraints,omitempty"` | ||
} | ||
|
||
type Constraint struct { | ||
Type string `yaml:"type,omitempty"` | ||
Property string `yaml:"property,omitempty"` | ||
Operator string `yaml:"operator,omitempty"` | ||
Value string `yaml:"value,omitempty"` | ||
} | ||
|
||
const batchSize = 25 | ||
|
||
var exportFilename = "" | ||
|
||
func runExport(_ []string) error { | ||
ctx := context.Background() | ||
ctx, cancel := context.WithCancel(ctx) | ||
|
||
defer cancel() | ||
|
||
interrupt := make(chan os.Signal, 1) | ||
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM) | ||
|
||
go func() { | ||
<-interrupt | ||
cancel() | ||
}() | ||
|
||
sql, driver, err := db.Open(cfg.Database.URL) | ||
if err != nil { | ||
return fmt.Errorf("opening db: %w", err) | ||
} | ||
|
||
defer sql.Close() | ||
|
||
var ( | ||
builder sq.StatementBuilderType | ||
stmtCacher = sq.NewStmtCacher(sql) | ||
) | ||
|
||
switch driver { | ||
case db.SQLite: | ||
builder = sq.StatementBuilder.RunWith(stmtCacher) | ||
case db.Postgres: | ||
builder = sq.StatementBuilder.PlaceholderFormat(sq.Dollar).RunWith(stmtCacher) | ||
} | ||
|
||
// default to stdout | ||
var out io.WriteCloser = os.Stdout | ||
|
||
// export to file | ||
if exportFilename != "" { | ||
logger.Debugf("exporting to %q", exportFilename) | ||
|
||
out, err = os.Create(exportFilename) | ||
if err != nil { | ||
return fmt.Errorf("creating output file: %w", err) | ||
} | ||
|
||
fmt.Fprintf(out, "# exported by Flipt (%s) on %s\n\n", version, time.Now().UTC().Format(time.RFC3339)) | ||
} | ||
|
||
defer out.Close() | ||
|
||
var ( | ||
flagStore = db.NewFlagStore(builder) | ||
segmentStore = db.NewSegmentStore(builder) | ||
ruleStore = db.NewRuleStore(builder, sql) | ||
|
||
enc = yaml.NewEncoder(out) | ||
doc = new(Document) | ||
) | ||
|
||
defer enc.Close() | ||
|
||
var remaining = true | ||
|
||
// export flags/variants in batches | ||
for batch := uint64(0); remaining; batch++ { | ||
flags, err := flagStore.ListFlags(ctx, storage.WithOffset(batch*batchSize), storage.WithLimit(batchSize)) | ||
if err != nil { | ||
return fmt.Errorf("getting flags: %w", err) | ||
} | ||
|
||
remaining = len(flags) == batchSize | ||
|
||
for _, f := range flags { | ||
flag := &Flag{ | ||
Key: f.Key, | ||
Name: f.Name, | ||
Description: f.Description, | ||
Enabled: f.Enabled, | ||
} | ||
|
||
// map variant id => variant key | ||
variantKeys := make(map[string]string) | ||
|
||
for _, v := range f.Variants { | ||
flag.Variants = append(flag.Variants, &Variant{ | ||
Key: v.Key, | ||
Name: v.Name, | ||
Description: v.Description, | ||
}) | ||
|
||
variantKeys[v.Id] = v.Key | ||
} | ||
|
||
// export rules for flag | ||
rules, err := ruleStore.ListRules(ctx, flag.Key) | ||
if err != nil { | ||
return fmt.Errorf("getting rules for flag %q: %w", flag.Key, err) | ||
} | ||
|
||
for _, r := range rules { | ||
rule := &Rule{ | ||
SegmentKey: r.SegmentKey, | ||
Rank: uint(r.Rank), | ||
} | ||
|
||
for _, d := range r.Distributions { | ||
rule.Distributions = append(rule.Distributions, &Distribution{ | ||
VariantKey: variantKeys[d.VariantId], | ||
Rollout: d.Rollout, | ||
}) | ||
} | ||
|
||
flag.Rules = append(flag.Rules, rule) | ||
} | ||
|
||
doc.Flags = append(doc.Flags, flag) | ||
} | ||
} | ||
|
||
remaining = true | ||
|
||
// export segments/constraints in batches | ||
for batch := uint64(0); remaining; batch++ { | ||
segments, err := segmentStore.ListSegments(ctx, storage.WithOffset(batch*batchSize), storage.WithLimit(batchSize)) | ||
if err != nil { | ||
return fmt.Errorf("getting segments: %w", err) | ||
} | ||
|
||
remaining = len(segments) == batchSize | ||
|
||
for _, s := range segments { | ||
segment := &Segment{ | ||
Key: s.Key, | ||
Name: s.Name, | ||
Description: s.Description, | ||
} | ||
|
||
for _, c := range s.Constraints { | ||
segment.Constraints = append(segment.Constraints, &Constraint{ | ||
Type: c.Type.String(), | ||
Property: c.Property, | ||
Operator: c.Operator, | ||
Value: c.Value, | ||
}) | ||
} | ||
|
||
doc.Segments = append(doc.Segments, segment) | ||
} | ||
} | ||
|
||
if err := enc.Encode(doc); err != nil { | ||
return fmt.Errorf("exporting: %w", err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.