Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
add support for zip files during import
Browse files Browse the repository at this point in the history
Signed-off-by: Jason McCallister <jason@craftcms.com>
  • Loading branch information
jasonmccallister committed Oct 5, 2020
1 parent e163cf2 commit 0d0acc4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: install scripts

VERSION ?= 1.0.2
VERSION ?= 1.1.0
NITRO_DEFAULT_MACHINE ?= nitro-dev

build:
Expand Down
6 changes: 0 additions & 6 deletions internal/cmd/db_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ var dbImportCommand = &cobra.Command{
return nil
}

// if the file is compressed, stop here for now
if req.Compressed {
fmt.Println("Importing compressed backup files is not yet supported")
return nil
}

// create a new prompt
p := prompt.NewPrompt()

Expand Down
39 changes: 23 additions & 16 deletions internal/nitrod/nitro_service_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"io/ioutil"
"os"
"strings"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -33,6 +34,7 @@ func (s *NitroService) ImportDatabase(stream NitroService_ImportDatabaseServer)
s.logger.Println("Error creating a temp file for the upload:", err.Error())
return status.Errorf(codes.Internal, "Unable creating a temp file for the upload")
}
defer file.Close()

options.File = file.Name()

Expand Down Expand Up @@ -77,17 +79,17 @@ func (s *NitroService) ImportDatabase(stream NitroService_ImportDatabaseServer)
if options.Compressed {
s.logger.Println("The file is compressed, extracting now")

// open the recently saved file
f, err := os.Open(file.Name())
uncompressedFile, err := s.createFile(os.TempDir(), "nitro-compressed-db-")
if err != nil {
s.logger.Println("error opening the database import file: ", file.Name())
return status.Errorf(codes.Unknown, "error opening the database import file: err: %s", err.Error())
s.logger.Println("error creating the compressed db file:", err.Error())
return err
}
defer uncompressedFile.Close()

// create the gzip reader
switch options.CompressionType {
case "gz":
reader, err := gzip.NewReader(f)
reader, err := gzip.NewReader(file)
if err != nil {
s.logger.Println("error creating the gzip reader", err.Error())
return status.Errorf(codes.Unknown, "error reading the compressed file. %s", err.Error())
Expand All @@ -97,24 +99,29 @@ func (s *NitroService) ImportDatabase(stream NitroService_ImportDatabaseServer)

defer reader.Close()
default:
reader, err := zip.OpenReader(f.Name())
reader, err := zip.OpenReader(file.Name())
if err != nil {
return err
}

defer reader.Close()
}

compressedFile, err := s.createFile(os.TempDir(), "nitro-compressed-db-")
if err != nil {
s.logger.Println("error creating the compressed db file:", err.Error())
return err
}
for _, z := range reader.File {
// only accept sql files
if strings.Contains(z.Name, ".sql") {
b, err := z.Open()
if err != nil {
return err
}

// copy the contents into the uncompressed temp file
if _, err := io.Copy(uncompressedFile, b); err != nil {
return err
}
}
}

if err := stream.SendAndClose(&ServiceResponse{Message: "Successfully imported the database"}); err != nil {
return status.Errorf(codes.Internal, "unable to send the response %v", err)
options.File = uncompressedFile.Name()
}
options.File = compressedFile.Name()
}

// import the database
Expand Down

0 comments on commit 0d0acc4

Please # to comment.