Skip to content

Commit

Permalink
Merge pull request #367 from checkr/zz/add-exclude-snapshots-to-expor…
Browse files Browse the repository at this point in the history
…t-sqlite

Add exclude_snapshots query param to export/sqlite
  • Loading branch information
zhouzhuojie authored May 29, 2020
2 parents d9bca69 + 56205c8 commit eddc50d
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 8 deletions.
7 changes: 7 additions & 0 deletions docs/api_docs/bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,13 @@ paths:
tags:
- export
operationId: getExportSqlite
parameters:
- in: query
name: exclude_snapshots
type: boolean
description: >
export without snapshots data - useful for smaller db without
snapshots
description: >-
Export sqlite3 format of the db dump, which is converted from the main
database.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/checkr/flagr

go 1.12
go 1.14

require (
cloud.google.com/go v0.37.4
Expand Down
12 changes: 7 additions & 5 deletions pkg/handler/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ import (
"github.com/sirupsen/logrus"
)

var exportSQLiteHandler = func(export.GetExportSqliteParams) middleware.Responder {
f, done, err := exportSQLiteFile()
var exportSQLiteHandler = func(p export.GetExportSqliteParams) middleware.Responder {
f, done, err := exportSQLiteFile(p.ExcludeSnapshots)
defer done()
if err != nil {
return export.NewGetExportSqliteDefault(500).WithPayload(ErrorMessage("%s", err))
}
return export.NewGetExportSqliteOK().WithPayload(f)
}

var exportSQLiteFile = func() (file io.ReadCloser, done func(), err error) {
var exportSQLiteFile = func(excludeSnapshots *bool) (file io.ReadCloser, done func(), err error) {
fname := fmt.Sprintf("/tmp/flagr_%d.sqlite", rand.Int31())
done = func() {
os.Remove(fname)
Expand All @@ -37,8 +37,10 @@ var exportSQLiteFile = func() (file io.ReadCloser, done func(), err error) {
if err := exportFlags(tmpDB); err != nil {
return nil, done, err
}
if err := exportFlagSnapshots(tmpDB); err != nil {
return nil, done, err
if excludeSnapshots == nil || !*excludeSnapshots {
if err := exportFlagSnapshots(tmpDB); err != nil {
return nil, done, err
}
}
if err := exportFlagEntityTypes(tmpDB); err != nil {
return nil, done, err
Expand Down
13 changes: 11 additions & 2 deletions pkg/handler/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/checkr/flagr/pkg/entity"
"github.com/checkr/flagr/pkg/util"
"github.com/checkr/flagr/swagger_gen/restapi/operations/export"
"github.com/prashantv/gostub"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -63,8 +64,16 @@ func TestExportSQLiteFile(t *testing.T) {
defer db.Close()
defer gostub.StubFunc(&getDB, db).Reset()

t.Run("happy code path", func(t *testing.T) {
f, done, err := exportSQLiteFile()
t.Run("happy code path and export everything in db", func(t *testing.T) {
f, done, err := exportSQLiteFile(nil)
defer done()

assert.NoError(t, err)
assert.NotNil(t, f)
})

t.Run("happy code path and exclude_snapshots", func(t *testing.T) {
f, done, err := exportSQLiteFile(util.BoolPtr(true))
defer done()

assert.NoError(t, err)
Expand Down
6 changes: 6 additions & 0 deletions swagger/export_sqlite.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ get:
tags:
- export
operationId: getExportSqlite
parameters:
- in: query
name: exclude_snapshots
type: boolean
description: >
export without snapshots data - useful for smaller db without snapshots
description: Export sqlite3 format of the db dump, which is converted from the main database.
produces:
- application/octet-stream
Expand Down
16 changes: 16 additions & 0 deletions swagger_gen/restapi/embedded_spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit eddc50d

Please sign in to comment.