renamed ImageMetaData.CategoryId to CategoryPiwigoId -> Db rebuild required

This commit is contained in:
Philipp Häfelfinger 2019-04-06 23:01:53 +02:00
parent b99e990747
commit 2a4a8e2e01
4 changed files with 53 additions and 38 deletions

View File

@ -29,20 +29,20 @@ func (cat *CategoryData) String() string {
} }
type ImageMetaData struct { type ImageMetaData struct {
ImageId int ImageId int
PiwigoId int PiwigoId int
FullImagePath string FullImagePath string
Filename string Filename string
Md5Sum string Md5Sum string
LastChange time.Time LastChange time.Time
CategoryPath string CategoryPath string
CategoryId int CategoryPiwigoId int
UploadRequired bool UploadRequired bool
DeleteRequired bool DeleteRequired bool
} }
func (img *ImageMetaData) String() string { func (img *ImageMetaData) String() string {
return fmt.Sprintf("ImageMetaData{ImageId:%d, PiwigoId:%d, CategoryId:%d, RelPath:%s, File:%s, Md5:%s, Change:%sS, catpath:%s, UploadRequired: %t, DeleteRequired: %t}", img.ImageId, img.PiwigoId, img.CategoryId, img.FullImagePath, img.Filename, img.Md5Sum, img.LastChange.String(), img.CategoryPath, img.UploadRequired, img.DeleteRequired) return fmt.Sprintf("ImageMetaData{ImageId:%d, PiwigoId:%d, CategoryPiwigoId:%d, RelPath:%s, File:%s, Md5:%s, Change:%sS, catpath:%s, UploadRequired: %t, DeleteRequired: %t}", img.ImageId, img.PiwigoId, img.CategoryPiwigoId, img.FullImagePath, img.Filename, img.Md5Sum, img.LastChange.String(), img.CategoryPath, img.UploadRequired, img.DeleteRequired)
} }
type CategoryProvider interface { type CategoryProvider interface {
@ -114,7 +114,7 @@ func (d *LocalDataStore) ImageMetadata(fullImagePath string) (ImageMetaData, err
} }
defer db.Close() defer db.Close()
stmt, err := db.Prepare("SELECT imageId, piwigoId, fullImagePath, fileName, md5sum, lastChanged, categoryPath, categoryId, uploadRequired, deleteRequired FROM image WHERE fullImagePath = ?") stmt, err := db.Prepare("SELECT imageId, piwigoId, fullImagePath, fileName, md5sum, lastChanged, categoryPath, categoryPiwigoId, uploadRequired, deleteRequired FROM image WHERE fullImagePath = ?")
if err != nil { if err != nil {
return img, err return img, err
} }
@ -147,7 +147,7 @@ func (d *LocalDataStore) ImageMetadataAll() ([]ImageMetaData, error) {
} }
defer db.Close() defer db.Close()
rows, err := db.Query("SELECT imageId, piwigoId, fullImagePath, fileName, md5sum, lastChanged, categoryPath, categoryId, uploadRequired, deleteRequired FROM image") rows, err := db.Query("SELECT imageId, piwigoId, fullImagePath, fileName, md5sum, lastChanged, categoryPath, categoryPiwigoId, uploadRequired, deleteRequired FROM image")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -176,7 +176,7 @@ func (d *LocalDataStore) ImageMetadataToDelete() ([]ImageMetaData, error) {
} }
defer db.Close() defer db.Close()
rows, err := db.Query("SELECT imageId, piwigoId, fullImagePath, fileName, md5sum, lastChanged, categoryPath, categoryId, uploadRequired, deleteRequired FROM image WHERE deleteRequired = 1") rows, err := db.Query("SELECT imageId, piwigoId, fullImagePath, fileName, md5sum, lastChanged, categoryPath, categoryPiwigoId, uploadRequired, deleteRequired FROM image WHERE deleteRequired = 1")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -205,7 +205,7 @@ func (d *LocalDataStore) ImageMetadataToUpload() ([]ImageMetaData, error) {
} }
defer db.Close() defer db.Close()
rows, err := db.Query("SELECT imageId, piwigoId, fullImagePath, fileName, md5sum, lastChanged, categoryPath, categoryId, uploadRequired, deleteRequired FROM image WHERE uploadRequired = 1 and deleteRequired = 0 order by fullImagePath asc") rows, err := db.Query("SELECT imageId, piwigoId, fullImagePath, fileName, md5sum, lastChanged, categoryPath, categoryPiwigoId, uploadRequired, deleteRequired FROM image WHERE uploadRequired = 1 and deleteRequired = 0 order by fullImagePath asc")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -226,7 +226,7 @@ func (d *LocalDataStore) ImageMetadataToUpload() ([]ImageMetaData, error) {
} }
func ReadImageMetadataFromRow(rows *sql.Rows, img *ImageMetaData) error { func ReadImageMetadataFromRow(rows *sql.Rows, img *ImageMetaData) error {
err := rows.Scan(&img.ImageId, &img.PiwigoId, &img.FullImagePath, &img.Filename, &img.Md5Sum, &img.LastChange, &img.CategoryPath, &img.CategoryId, &img.UploadRequired, &img.DeleteRequired) err := rows.Scan(&img.ImageId, &img.PiwigoId, &img.FullImagePath, &img.Filename, &img.Md5Sum, &img.LastChange, &img.CategoryPath, &img.CategoryPiwigoId, &img.UploadRequired, &img.DeleteRequired)
return err return err
} }
@ -331,11 +331,11 @@ func (d *LocalDataStore) DeleteMarkedImages() error {
} }
func (d *LocalDataStore) insertImageMetaData(tx *sql.Tx, data ImageMetaData) error { func (d *LocalDataStore) insertImageMetaData(tx *sql.Tx, data ImageMetaData) error {
stmt, err := tx.Prepare("INSERT INTO image (piwigoId, fullImagePath, fileName, md5sum, lastChanged, categoryPath, categoryId, uploadRequired, deleteRequired) VALUES (?,?,?,?,?,?,?,?,?)") stmt, err := tx.Prepare("INSERT INTO image (piwigoId, fullImagePath, fileName, md5sum, lastChanged, categoryPath, categoryPiwigoId, uploadRequired, deleteRequired) VALUES (?,?,?,?,?,?,?,?,?)")
if err != nil { if err != nil {
return err return err
} }
_, err = stmt.Exec(data.PiwigoId, data.FullImagePath, data.Filename, data.Md5Sum, data.LastChange, data.CategoryPath, data.CategoryId, data.UploadRequired, data.DeleteRequired) _, err = stmt.Exec(data.PiwigoId, data.FullImagePath, data.Filename, data.Md5Sum, data.LastChange, data.CategoryPath, data.CategoryPiwigoId, data.UploadRequired, data.DeleteRequired)
return err return err
} }
@ -359,7 +359,7 @@ func (d *LocalDataStore) createTablesIfNeeded(db *sql.DB) error {
"md5sum NVARCHAR(50) NOT NULL," + "md5sum NVARCHAR(50) NOT NULL," +
"lastChanged DATETIME NOT NULL," + "lastChanged DATETIME NOT NULL," +
"categoryPath NVARCHAR(1000) NOT NULL," + "categoryPath NVARCHAR(1000) NOT NULL," +
"categoryId INTEGER NULL," + "categoryPiwigoId INTEGER NULL," +
"uploadRequired BIT NOT NULL," + "uploadRequired BIT NOT NULL," +
"deleteRequired BIT NOT NULL" + "deleteRequired BIT NOT NULL" +
");") ");")
@ -368,14 +368,29 @@ func (d *LocalDataStore) createTablesIfNeeded(db *sql.DB) error {
} }
_, err = db.Exec("CREATE UNIQUE INDEX IF NOT EXISTS UX_ImageFullImagePath ON image (fullImagePath);") _, err = db.Exec("CREATE UNIQUE INDEX IF NOT EXISTS UX_ImageFullImagePath ON image (fullImagePath);")
if err != nil {
return err
}
_, err = db.Exec("CREATE TABLE IF NOT EXISTS category (" +
"CategoryId INTEGER PRIMARY KEY," +
"PiwigoId INTEGER NULL," +
"PiwigoParentId INTEGER NULL," +
"Name NVARCHAR(255) NOT NULL," +
"Key NVARCHAR(1000) NOT NULL" +
");")
if err != nil {
return err
}
return err return err
} }
func (d *LocalDataStore) updateImageMetaData(tx *sql.Tx, data ImageMetaData) error { func (d *LocalDataStore) updateImageMetaData(tx *sql.Tx, data ImageMetaData) error {
stmt, err := tx.Prepare("UPDATE image SET piwigoId = ?, fullImagePath = ?, fileName = ?, md5sum = ?, lastChanged = ?, categoryPath = ?, categoryId = ?, uploadRequired = ?, deleteRequired = ? WHERE imageId = ?") stmt, err := tx.Prepare("UPDATE image SET piwigoId = ?, fullImagePath = ?, fileName = ?, md5sum = ?, lastChanged = ?, categoryPath = ?, categoryPiwigoId = ?, uploadRequired = ?, deleteRequired = ? WHERE imageId = ?")
if err != nil { if err != nil {
return err return err
} }
_, err = stmt.Exec(data.PiwigoId, data.FullImagePath, data.Filename, data.Md5Sum, data.LastChange, data.CategoryPath, data.CategoryId, data.UploadRequired, data.DeleteRequired, data.ImageId) _, err = stmt.Exec(data.PiwigoId, data.FullImagePath, data.Filename, data.Md5Sum, data.LastChange, data.CategoryPath, data.CategoryPiwigoId, data.UploadRequired, data.DeleteRequired, data.ImageId)
return err return err
} }

View File

@ -312,14 +312,14 @@ func EnsureMetadataAreEqual(action string, img ImageMetaData, imgLoad ImageMetaD
func getExampleImageMetadata(filePath string) ImageMetaData { func getExampleImageMetadata(filePath string) ImageMetaData {
return ImageMetaData{ return ImageMetaData{
FullImagePath: filePath, FullImagePath: filePath,
PiwigoId: 1, PiwigoId: 1,
Md5Sum: "aabbccddeeff", Md5Sum: "aabbccddeeff",
LastChange: time.Now().UTC(), LastChange: time.Now().UTC(),
Filename: "bar.jpg", Filename: "bar.jpg",
CategoryPath: "blah/foo", CategoryPath: "blah/foo",
CategoryId: 100, CategoryPiwigoId: 100,
UploadRequired: true, UploadRequired: true,
} }
} }

View File

@ -60,7 +60,7 @@ func synchronizeLocalImageMetadataScanNewFiles(fileSystemNodes map[string]*local
category, err := categoryDb.GetCategoryByKey(metadata.CategoryPath) category, err := categoryDb.GetCategoryByKey(metadata.CategoryPath)
if err == nil { if err == nil {
metadata.CategoryId = category.PiwigoId metadata.CategoryPiwigoId = category.PiwigoId
} else { } else {
logrus.Warnf("No category found for image %s - %s", file.Path, err) logrus.Warnf("No category found for image %s - %s", file.Path, err)
} }
@ -129,7 +129,7 @@ func UploadImages(piwigoCtx piwigo.PiwigoImageApi, metadataProvider datastore.Im
for _, img := range images { for _, img := range images {
imgId, err := piwigoCtx.UploadImage(img.PiwigoId, img.FullImagePath, img.Md5Sum, img.CategoryId) imgId, err := piwigoCtx.UploadImage(img.PiwigoId, img.FullImagePath, img.Md5Sum, img.CategoryPiwigoId)
if err != nil { if err != nil {
logrus.Warnf("could not upload image %s. Continuing with the next image.", img.FullImagePath) logrus.Warnf("could not upload image %s. Continuing with the next image.", img.FullImagePath)
continue continue

View File

@ -56,7 +56,7 @@ func Test_synchronize_local_image_metadata_should_add_new_metadata(t *testing.T)
fileSystemNodes[testFileSystemNode.Key] = testFileSystemNode fileSystemNodes[testFileSystemNode.Key] = testFileSystemNode
image := createImageMetaDataFromFilesystem(testFileSystemNode, 0, true, false) image := createImageMetaDataFromFilesystem(testFileSystemNode, 0, true, false)
image.CategoryId = category.PiwigoId image.CategoryPiwigoId = category.PiwigoId
image.CategoryPath = category.Key image.CategoryPath = category.Key
db := NewMockImageMetadataProvider(mockCtrl) db := NewMockImageMetadataProvider(mockCtrl)
@ -536,12 +536,12 @@ func testChecksumCalculator(file string) (string, error) {
func createTestImageMetaData(piwigoId int) datastore.ImageMetaData { func createTestImageMetaData(piwigoId int) datastore.ImageMetaData {
img := datastore.ImageMetaData{ img := datastore.ImageMetaData{
ImageId: 1, ImageId: 1,
PiwigoId: piwigoId, PiwigoId: piwigoId,
FullImagePath: "/nonexisting/file.jpg", FullImagePath: "/nonexisting/file.jpg",
UploadRequired: true, UploadRequired: true,
Md5Sum: "1234", Md5Sum: "1234",
CategoryId: 2, CategoryPiwigoId: 2,
} }
return img return img
} }