Skip to content

Commit

Permalink
feat(gobblr): truncate and insert the file data
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsimonemms committed Dec 12, 2022
1 parent 331573b commit ccd9ff9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
16 changes: 13 additions & 3 deletions cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.
package cmd

import (
"encoding/json"
"fmt"
"os"
"path"

Expand All @@ -41,10 +43,18 @@ var dbCmd = &cobra.Command{
//
// There can be only one PersistentPostRun command.

// @todo(sje): log number of items inserted
_, err := gobblr.Execute(dbOpts.DataPath, dbOpts.Driver)
inserted, err := gobblr.Execute(dbOpts.DataPath, dbOpts.Driver)
if err != nil {
return err
}

return err
jsonData, err := json.MarshalIndent(inserted, "", " ")
if err != nil {
return err
}
fmt.Println(string(jsonData))

return nil
},
}

Expand Down
12 changes: 11 additions & 1 deletion pkg/drivers/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,19 @@ func (db *SQL) Close() error {
}

func (db *SQL) InsertBulk(table string, data []map[string]interface{}) (int, error) {
result := db.activeConnection.Table(table).CreateInBatches(data, 100)
if err := result.Error; err != nil {
return 0, err
}
return len(data), nil
}

func (db *SQL) Truncate(table string) error {
return nil
// Gorm doesn't have a truncate method - this will not reset the "id" field
result := db.activeConnection.
Session(&gorm.Session{AllowGlobalUpdate: true}).
Table(table).
Delete("")

return result.Error
}
31 changes: 29 additions & 2 deletions pkg/gobblr/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ import (
"github.com/mrsimonemms/gobblr/pkg/drivers"
)

func Execute(dataPath string, db drivers.Driver) (map[string]int, error) {
inserted := make(map[string]int, 0)
type Inserted struct {
Table string `json:"table"`
Count int `json:"count"`
}

func Execute(dataPath string, db drivers.Driver) ([]Inserted, error) {
inserted := make([]Inserted, 0)
var err error

// Connect to database
Expand Down Expand Up @@ -36,6 +41,28 @@ func Execute(dataPath string, db drivers.Driver) (map[string]int, error) {
if len(fileData.Data) == 0 {
return inserted, fmt.Errorf("no data in file: %s", file.Path)
}

// Clear out any existing data
if err := db.Truncate(file.TableName); err != nil {
return nil, err
}

// Insert the data to the table
tableInserted, err := db.InsertBulk(file.TableName, fileData.Data)
if err != nil {
// Insertion failed - truncate
if err := db.Truncate(file.TableName); err != nil {
// Failed to truncate
return nil, err
}
return nil, err
}

// Store the result for output
inserted = append(inserted, Inserted{
Table: file.TableName,
Count: tableInserted,
})
}

return inserted, err
Expand Down

0 comments on commit ccd9ff9

Please sign in to comment.