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

Commit

Permalink
update 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 2, 2020
1 parent 02672dd commit fd269e9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
22 changes: 16 additions & 6 deletions internal/nitrod/nitro_service_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (s *NitroService) ImportDatabase(stream NitroService_ImportDatabaseServer)
file, err := s.createFile(os.TempDir(), "nitro-db-upload")
if err != nil {
s.logger.Println("Error creating a temp file for the upload:", err.Error())
return status.Errorf(codes.Internal, "Error creating a temp file for the upload")
return status.Errorf(codes.Internal, "Unable creating a temp file for the upload")
}

// handle the file streaming requests
Expand All @@ -35,7 +35,7 @@ func (s *NitroService) ImportDatabase(stream NitroService_ImportDatabaseServer)
break
}
if err != nil {
return status.Errorf(codes.Internal, "unable to create the stream", err)
return status.Errorf(codes.Internal, "unable to create the stream: %s", err.Error())
}

// set the variables for later use only if they are not empty
Expand Down Expand Up @@ -71,14 +71,14 @@ func (s *NitroService) ImportDatabase(stream NitroService_ImportDatabaseServer)
f, err := os.Open(file.Name())
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.Error())
return status.Errorf(codes.Unknown, "error opening the database import file: err: %s", err.Error())
}

// create the gzip reader
reader, err := gzip.NewReader(f)
if err != nil {
s.logger.Println("error creating the gzip reader", err.Error())
return status.Errorf(codes.Unknown, "error reading the compressed file. %w", err.Error())
return status.Errorf(codes.Unknown, "error reading the compressed file. %s", err.Error())
}
reader.Multistream(true)

Expand Down Expand Up @@ -137,13 +137,23 @@ func (s *NitroService) importDatabase(mysql bool, container, database, file stri
s.logger.Println(string(output))
return err
}

s.logger.Printf("Created the database %q\n", database)

// copy the file into the containers tmp dir
if output, err := s.command.Run("/bin/bash", []string{"-c", fmt.Sprintf("docker cp %s %s:/", file, container)}); err != nil {
s.logger.Println(string(output))
return err
}
s.logger.Printf("Copied the file %q into the %q\n", file, container)

s.logger.Printf("Beginning import of file %q", file)

// remove the /tmp prefix since mysql defaults to /
sp := strings.Split(file, "/")
f := sp[len(sp)-1]

// import the database
output, err := s.command.Run("/bin/bash", []string{"-c", fmt.Sprintf(scripts.FmtDockerMysqlImportDatabase, file, container, database)})
output, err := s.command.Run("/bin/bash", []string{"-c", fmt.Sprintf("docker exec -i %q mysql -unitro -pnitro %s < /%s", container, database, f)})
if err != nil {
s.logger.Println(string(output))
return err
Expand Down
37 changes: 37 additions & 0 deletions internal/nitrod/nitro_service_import_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package nitrod

import (
"log"
"testing"
)

func TestNitroService_ImportDatabase(t *testing.T) {
type fields struct {
command Runner
logger *log.Logger
}
type args struct {
stream NitroService_ImportDatabaseServer
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &NitroService{
command: tt.fields.command,
logger: tt.fields.logger,
}
if err := s.ImportDatabase(tt.args.stream); (err != nil) != tt.wantErr {
t.Errorf("ImportDatabase() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit fd269e9

Please # to comment.