-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add simple data import and export (#1630)
## Relevant issue(s) Resolves #1544 ## Description This PR adds import and export functionality to the http api and cli. It can export to json to reduce the potential file size. At this stage csv output was not implemented as it would require extensive type casting (everything in csv is a string) on both writing and reading from the csv.
- Loading branch information
Showing
51 changed files
with
4,471 additions
and
144 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
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,123 @@ | ||
// Copyright 2023 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package http | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
"github.com/sourcenetwork/defradb/client" | ||
"github.com/sourcenetwork/defradb/errors" | ||
) | ||
|
||
func exportHandler(rw http.ResponseWriter, req *http.Request) { | ||
db, err := dbFromContext(req.Context()) | ||
if err != nil { | ||
handleErr(req.Context(), rw, err, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
cfg := &client.BackupConfig{} | ||
err = getJSON(req, cfg) | ||
if err != nil { | ||
handleErr(req.Context(), rw, err, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
err = validateBackupConfig(req.Context(), cfg, db) | ||
if err != nil { | ||
handleErr(req.Context(), rw, err, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
err = db.BasicExport(req.Context(), cfg) | ||
if err != nil { | ||
handleErr(req.Context(), rw, err, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
sendJSON( | ||
req.Context(), | ||
rw, | ||
simpleDataResponse("result", "success"), | ||
http.StatusOK, | ||
) | ||
} | ||
|
||
func importHandler(rw http.ResponseWriter, req *http.Request) { | ||
db, err := dbFromContext(req.Context()) | ||
if err != nil { | ||
handleErr(req.Context(), rw, err, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
cfg := &client.BackupConfig{} | ||
err = getJSON(req, cfg) | ||
if err != nil { | ||
handleErr(req.Context(), rw, err, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
err = validateBackupConfig(req.Context(), cfg, db) | ||
if err != nil { | ||
handleErr(req.Context(), rw, err, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
err = db.BasicImport(req.Context(), cfg.Filepath) | ||
if err != nil { | ||
handleErr(req.Context(), rw, err, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
sendJSON( | ||
req.Context(), | ||
rw, | ||
simpleDataResponse("result", "success"), | ||
http.StatusOK, | ||
) | ||
} | ||
|
||
func validateBackupConfig(ctx context.Context, cfg *client.BackupConfig, db client.DB) error { | ||
if !isValidPath(cfg.Filepath) { | ||
return errors.New("invalid file path") | ||
} | ||
|
||
if cfg.Format != "" && strings.ToLower(cfg.Format) != "json" { | ||
return errors.New("only JSON format is supported at the moment") | ||
} | ||
for _, colName := range cfg.Collections { | ||
_, err := db.GetCollectionByName(ctx, colName) | ||
if err != nil { | ||
return errors.Wrap("collection does not exist", err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func isValidPath(filepath string) bool { | ||
// if a file exists, return true | ||
if _, err := os.Stat(filepath); err == nil { | ||
return true | ||
} | ||
|
||
// if not, attempt to write to the path and if successful, | ||
// remove the file and return true | ||
var d []byte | ||
if err := os.WriteFile(filepath, d, 0o644); err == nil { | ||
_ = os.Remove(filepath) | ||
return true | ||
} | ||
|
||
return false | ||
} |
Oops, something went wrong.