Backend: Add write mutex to entities and improve test fixtures #164

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
Michael Mayer 2020-01-06 04:24:49 +01:00
parent 01ca94d536
commit 582113fed4
15 changed files with 51 additions and 6 deletions

View file

@ -1 +1,7 @@
INSERT INTO cameras (camera_slug, camera_model, camera_make, camera_notes, created_at, updated_at) VALUES('iphone_5', 'iPhone 5', 'Apple', 'TEST FIXTURE', '2001-01-01', '2001-01-02')
INSERT INTO cameras (id, camera_slug, camera_model, camera_make, camera_type, camera_owner, camera_description, camera_notes, created_at, updated_at, deleted_at) VALUES (1, 'unknown', 'Unknown', '', '', '', '', '', '2020-01-06 02:06:29', '2020-01-06 02:07:26', null);
INSERT INTO cameras (id, camera_slug, camera_model, camera_make, camera_type, camera_owner, camera_description, camera_notes, created_at, updated_at, deleted_at) VALUES (2, 'apple-iphone-se', 'iPhone SE', 'Apple', '', '', '', '', '2020-01-06 02:06:30', '2020-01-06 02:07:28', null);
INSERT INTO cameras (id, camera_slug, camera_model, camera_make, camera_type, camera_owner, camera_description, camera_notes, created_at, updated_at, deleted_at) VALUES (3, 'canon-eos-5d', 'EOS 5D', 'Canon', '', '', '', '', '2020-01-06 02:06:32', '2020-01-06 02:06:32', null);
INSERT INTO cameras (id, camera_slug, camera_model, camera_make, camera_type, camera_owner, camera_description, camera_notes, created_at, updated_at, deleted_at) VALUES (4, 'canon-eos-7d', 'EOS 7D', 'Canon', '', '', '', '', '2020-01-06 02:06:33', '2020-01-06 02:06:33', null);
INSERT INTO cameras (id, camera_slug, camera_model, camera_make, camera_type, camera_owner, camera_description, camera_notes, created_at, updated_at, deleted_at) VALUES (5, 'canon-eos-6d', 'EOS 6D', 'Canon', '', '', '', '', '2020-01-06 02:06:35', '2020-01-06 02:06:54', null);
INSERT INTO cameras (id, camera_slug, camera_model, camera_make, camera_type, camera_owner, camera_description, camera_notes, created_at, updated_at, deleted_at) VALUES (6, 'apple-iphone-6', 'iPhone 6', 'Apple', '', '', '', '', '2020-01-06 02:06:42', '2020-01-06 02:06:42', null);
INSERT INTO cameras (id, camera_slug, camera_model, camera_make, camera_type, camera_owner, camera_description, camera_notes, created_at, updated_at, deleted_at) VALUES (7, 'apple-iphone-7', 'iPhone 7', 'Apple', '', '', '', '', '2020-01-06 02:06:51', '2020-01-06 02:06:51', null);

View file

@ -177,6 +177,7 @@ func (c *Config) ImportSQL(filename string) {
}
statements := strings.Split(string(contents), ";\n")
q := c.Db().Unscoped()
for _, stmt := range statements {
// Skip empty lines and comments
@ -184,7 +185,11 @@ func (c *Config) ImportSQL(filename string) {
continue
}
if _, err := c.Db().CommonDB().Query(stmt); err != nil {
var result struct {}
err := q.Raw(stmt).Scan(&result).Error
if err != nil {
log.Error(err)
}
}

View file

@ -51,6 +51,8 @@ func NewCamera(modelName string, makeName string) *Camera {
}
func (m *Camera) FirstOrCreate(db *gorm.DB) *Camera {
writeMutex.Lock()
defer writeMutex.Unlock()
if err := db.FirstOrCreate(m, "camera_model = ? AND camera_make = ?", m.CameraModel, m.CameraMake).Error; err != nil {
log.Errorf("camera: %s", err)
}

View file

@ -51,6 +51,8 @@ func NewCountry(countryCode string, countryName string) *Country {
}
func (m *Country) FirstOrCreate(db *gorm.DB) *Country {
writeMutex.Lock()
defer writeMutex.Unlock()
if err := db.FirstOrCreate(m, "id = ?", m.ID).Error; err != nil {
log.Errorf("country: %s", err)
}

View file

@ -11,6 +11,7 @@ package entity
import (
"strconv"
"sync"
"time"
"github.com/jinzhu/gorm"
@ -19,6 +20,7 @@ import (
)
var log = event.Log
var writeMutex = sync.Mutex{}
func logError(result *gorm.DB) {
if result.Error != nil {

View file

@ -24,6 +24,8 @@ func NewKeyword(keyword string) *Keyword {
}
func (m *Keyword) FirstOrCreate(db *gorm.DB) *Keyword {
writeMutex.Lock()
defer writeMutex.Unlock()
if err := db.FirstOrCreate(m, "keyword = ?", m.Keyword).Error; err != nil {
log.Errorf("keyword: %s", err)
}

View file

@ -53,6 +53,8 @@ func NewLabel(labelName string, labelPriority int) *Label {
}
func (m *Label) FirstOrCreate(db *gorm.DB) *Label {
writeMutex.Lock()
defer writeMutex.Unlock()
if err := db.FirstOrCreate(m, "label_slug = ?", m.LabelSlug).Error; err != nil {
log.Errorf("label: %s", err)
}

View file

@ -1,6 +1,7 @@
package entity
import (
"strings"
"time"
"github.com/gosimple/slug"
@ -27,6 +28,9 @@ func (Lens) TableName() string {
}
func NewLens(modelName string, makeName string) *Lens {
modelName = strings.TrimSpace(modelName)
makeName = strings.TrimSpace(makeName)
if modelName == "" {
modelName = "Unknown"
}
@ -43,7 +47,9 @@ func NewLens(modelName string, makeName string) *Lens {
}
func (m *Lens) FirstOrCreate(db *gorm.DB) *Lens {
if err := db.FirstOrCreate(m, "lens_model = ? AND lens_make = ?", m.LensModel, m.LensMake).Error; err != nil {
writeMutex.Lock()
defer writeMutex.Unlock()
if err := db.FirstOrCreate(m, "lens_slug = ?", m.LensSlug).Error; err != nil {
log.Errorf("lens: %s", err)
}

View file

@ -45,6 +45,9 @@ func NewLocation(lat, lng float64) *Location {
}
func (m *Location) Find(db *gorm.DB) error {
writeMutex.Lock()
defer writeMutex.Unlock()
if err := db.First(m, "id = ?", m.ID).Error; err == nil {
m.Place = FindPlace(m.PlaceID, db)
return nil

View file

@ -31,6 +31,8 @@ func NewPhotoAlbum(photoUUID, albumUUID string) *PhotoAlbum {
}
func (m *PhotoAlbum) FirstOrCreate(db *gorm.DB) *PhotoAlbum {
writeMutex.Lock()
defer writeMutex.Unlock()
if err := db.FirstOrCreate(m, "photo_uuid = ? AND album_uuid = ?", m.PhotoUUID, m.AlbumUUID).Error; err != nil {
log.Errorf("photo album: %s", err)
}

View file

@ -21,6 +21,8 @@ func NewPhotoKeyword(photoID, keywordID uint) *PhotoKeyword {
}
func (m *PhotoKeyword) FirstOrCreate(db *gorm.DB) *PhotoKeyword {
writeMutex.Lock()
defer writeMutex.Unlock()
if err := db.FirstOrCreate(m, "photo_id = ? AND keyword_id = ?", m.PhotoID, m.KeywordID).Error; err != nil {
log.Errorf("photo keyword: %s", err)
}

View file

@ -30,6 +30,8 @@ func NewPhotoLabel(photoId, labelId uint, uncertainty int, source string) *Photo
}
func (m *PhotoLabel) FirstOrCreate(db *gorm.DB) *PhotoLabel {
writeMutex.Lock()
defer writeMutex.Unlock()
if err := db.FirstOrCreate(m, "photo_id = ? AND label_id = ?", m.PhotoID, m.LabelID).Error; err != nil {
log.Errorf("photo label: %s", err)
}

View file

@ -72,6 +72,8 @@ func (m *Place) Find(db *gorm.DB) error {
}
func (m *Place) FirstOrCreate(db *gorm.DB) *Place {
writeMutex.Lock()
defer writeMutex.Unlock()
if err := db.FirstOrCreate(m, "id = ? OR loc_label = ?", m.ID, m.LocLabel).Error; err != nil {
log.Debugf("place: %s for token %s or label \"%s\"", err.Error(), m.ID, m.LocLabel)
}

View file

@ -34,6 +34,13 @@ func TestMediaFile_DateCreated(t *testing.T) {
assert.Equal(t, "2013-11-26 13:53:55 +0000 UTC", date.String())
assert.Empty(t, err)
})
t.Run("dog_created_1919.jpg", func(t *testing.T) {
mediaFile, err := NewMediaFile(conf.ExamplesPath() + "/dog_created_1919.jpg")
assert.Nil(t, err)
date := mediaFile.DateCreated().UTC()
assert.Equal(t, "1919-05-04 05:59:26 +0000 UTC", date.String())
assert.Empty(t, err)
})
}
func TestMediaFile_HasTimeAndPlace(t *testing.T) {

View file

@ -10,10 +10,10 @@ import (
// Example for using database fixtures defined in assets/resources/examples/fixtures.sql
func TestCamera_FirstOrCreate(t *testing.T) {
t.Run("iphone_5", func(t *testing.T) {
camera := entity.NewCamera("iPhone 5", "Apple")
t.Run("iphone-se", func(t *testing.T) {
camera := entity.NewCamera("iPhone SE", "Apple")
c := config.TestConfig()
camera.FirstOrCreate(c.Db())
assert.Equal(t, "TEST FIXTURE", camera.CameraNotes)
assert.Equal(t, uint(2), camera.ID)
})
}