/
ip_addresses.go
53 lines (44 loc) · 1.22 KB
/
ip_addresses.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package netflix
import (
"github.com/bionic-dev/bionic/types"
"github.com/gocarina/gocsv"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"os"
)
type IPAddress struct {
gorm.Model
ESN string `csv:"esn" gorm:"uniqueIndex:netflix_ip_addresses_key"`
Country string `csv:"country"`
LocalizedDeviceDescription string `csv:"localizedDeviceDescription"`
DeviceDescription string `csv:"deviceDescription"`
IP string `csv:"ip"`
RegionCodeDisplayName string `csv:"regionCodeDisplayName"`
Time types.DateTime `csv:"ts" gorm:"uniqueIndex:netflix_ip_addresses_key"`
}
func (r IPAddress) TableName() string {
return tablePrefix + "ip_addresses"
}
func (p *netflix) importIPAddresses(inputPath string) error {
if _, err := os.Stat(inputPath); os.IsNotExist(err) {
return nil
}
file, err := os.Open(inputPath)
if err != nil {
return err
}
var items []IPAddress
if err := gocsv.UnmarshalFile(file, &items); err != nil {
return err
}
err = p.DB().
Clauses(clause.OnConflict{
DoNothing: true,
}).
CreateInBatches(items, 100).
Error
if err != nil {
return err
}
return nil
}