Skip to content

Instantly share code, notes, and snippets.

@wuriyanto48
Created September 7, 2018 03:12
Show Gist options
  • Save wuriyanto48/ab29635eecde455afc0bf7e340dd95ab to your computer and use it in GitHub Desktop.
Save wuriyanto48/ab29635eecde455afc0bf7e340dd95ab to your computer and use it in GitHub Desktop.
Database migration in Mongo Db from JSON data
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"time"
"gopkg.in/mgo.v2/bson"
"github.com/Bhinneka/activity-service/config"
"github.com/Bhinneka/activity-service/src/review/v1/domain"
)
// Review data struct
type Review struct {
ID string `json:"ID"`
PartID string `json:"PartID"`
Name string `json:"Name"`
Email string `json:"Email"`
Title string `json:"Title"`
Content string `json:"Content"`
Rating int `json:"Rating"`
IsUser int `json:"IsUser"`
IsVerified int `json:"IsVerified"`
IsFirst int `json:"IsFirst"`
IsCertified int `json:"IsCertified"`
CreatorDateTime time.Time `json:"CreatorDateTime"`
CreatorIP string `json:"CreatorIP"`
CreatorNo string `json:"CreatorNo"`
EditorDateTime time.Time `json:"EditorDateTime"`
EditorIP string `json:"EditorIP"`
EditorNo string `json:"EditorNo"`
}
// ReviewVote data struct
type ReviewVote struct {
VoteID int `json:"VoteID"`
ReviewID string `json:"ReviewID"`
VoteValue int `json:"VoteValue"`
UserID string `json:"UserID"`
CreatorIP string `json:"CreatorIP"`
CreatorDateTime time.Time `json:"CreatorDateTime"`
}
// ConvertReviewToReviewDomain function
func ConvertReviewToReviewDomain(r Review) domain.Review {
var review domain.Review
review.ID = bson.NewObjectId()
review.SKU = r.PartID
review.Name = r.Name
review.Email = r.Email
review.Title = r.Title
review.Content = r.Content
review.Rating = r.Rating
review.OrderID = ""
review.MerchantID = ""
review.Status = "publish"
review.CreatorID = r.CreatorNo
review.CreatorIP = r.CreatorIP
review.Created = r.CreatorDateTime
review.EditorID = r.EditorNo
review.EditorIP = r.EditorIP
review.LastModified = time.Now()
return review
}
// Reviews type of Review List
type Reviews []Review
// ReviewVotes type of ReviewVote List
type ReviewVotes []ReviewVote
func main() {
file, err := os.Open("user_review.json")
if err != nil {
fmt.Println("Open file ", err)
}
defer file.Close()
jsonData, err := ioutil.ReadAll(file)
if err != nil {
fmt.Println("Open file ", err)
}
jsonData = bytes.Replace(jsonData, []byte("\x03"), []byte(""), -1)
jsonData = bytes.Replace(jsonData, []byte("\x10"), []byte(""), -1)
jsonData = bytes.TrimPrefix(jsonData, []byte("\xef\xbb\xbf"))
var reviews Reviews
err = json.Unmarshal(jsonData, &reviews)
if err != nil {
fmt.Println("Unmarshal ", err)
}
newReviews := make([]interface{}, 56935)
for k, v := range reviews {
r := ConvertReviewToReviewDomain(v)
newReviews[k] = r
}
db, err := config.GetMongoDB()
if err != nil {
fmt.Println("MongoDB Error, ", err)
os.Exit(2)
}
uc := db.C("reviews")
err = uc.Insert(newReviews...)
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment