diff --git a/internal/app/datastore.go b/internal/app/datastore.go index aa9186d..6f3e3ae 100644 --- a/internal/app/datastore.go +++ b/internal/app/datastore.go @@ -42,6 +42,7 @@ type ImageMetadataProvider interface { ImageMetadataAll() ([]ImageMetaData, error) SaveImageMetadata(m ImageMetaData) error SavePiwigoIdAndUpdateUploadFlag(md5Sum string, piwigoId int) error + DeleteMarkedImages() error } type localDataStore struct { @@ -220,7 +221,7 @@ func (d *localDataStore) SaveImageMetadata(img ImageMetaData) error { return err } - logrus.Tracef("Commiting metadata for image %s", img.String()) + logrus.Tracef("Committing metadata for image %s", img.String()) return tx.Commit() } @@ -261,7 +262,34 @@ func (d *localDataStore) SavePiwigoIdAndUpdateUploadFlag(md5Sum string, piwigoId return err } - logrus.Tracef("Commiting piwigo id %d for file with md5sum %s", piwigoId, md5Sum) + logrus.Tracef("Committing piwigo id %d for file with md5sum %s", piwigoId, md5Sum) + return tx.Commit() +} + +func (d *localDataStore) DeleteMarkedImages() error { + logrus.Trace("Deleting marked records from database...") + db, err := d.openDatabase() + if err != nil { + return err + } + defer db.Close() + + tx, err := db.Begin() + if err != nil { + return err + } + + _, err = tx.Exec("DELETE FROM image WHERE deleteRequired = 1") + if err != nil { + logrus.Errorf("Rolling back transaction of deleting marked images") + errTx := tx.Rollback() + if errTx != nil { + logrus.Errorf("Rollback of transaction for piwigo delete failed!") + } + return err + } + + logrus.Tracef("Committing deleted images from database") return tx.Commit() } diff --git a/internal/app/datastore_mock_test.go b/internal/app/datastore_mock_test.go index 0904abb..9bd9cd7 100644 --- a/internal/app/datastore_mock_test.go +++ b/internal/app/datastore_mock_test.go @@ -32,6 +32,20 @@ func (m *MockImageMetadataProvider) EXPECT() *MockImageMetadataProviderMockRecor return m.recorder } +// DeleteMarkedImages mocks base method +func (m *MockImageMetadataProvider) DeleteMarkedImages() error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteMarkedImages") + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteMarkedImages indicates an expected call of DeleteMarkedImages +func (mr *MockImageMetadataProviderMockRecorder) DeleteMarkedImages() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteMarkedImages", reflect.TypeOf((*MockImageMetadataProvider)(nil).DeleteMarkedImages)) +} + // ImageMetadata mocks base method func (m *MockImageMetadataProvider) ImageMetadata(arg0 string) (ImageMetaData, error) { m.ctrl.T.Helper() diff --git a/internal/app/datastore_test.go b/internal/app/datastore_test.go index e346194..e923fa7 100644 --- a/internal/app/datastore_test.go +++ b/internal/app/datastore_test.go @@ -253,7 +253,39 @@ func Test_update_piwigoId_by_checksum_found_no_image(t *testing.T) { imgLoad := loadMetadataShouldNotFail("update", dataStore, filePath, t) EnsureMetadataAreEqual("SavePiwigoIdAndUpdateUploadFlag", img, imgLoad, t) +} +func Test_deleteMarkedImages_should_remove_records(t *testing.T) { + if !dbinitOk { + t.Skip("Skipping test as TestDataStoreInitialize failed!") + } + dataStore := setupDatabase(t) + defer cleanupDatabase(t) + + img1 := getExampleImageMetadata("blah/foo/bar.jpg") + + img2 := getExampleImageMetadata("blah/foo/bar2.jpg") + img2.DeleteRequired = true + + saveImageShouldNotFail("allimages", dataStore, img1, t) + img1.ImageId = 1 + + saveImageShouldNotFail("allimages", dataStore, img2, t) + img2.ImageId = 2 + + err := dataStore.DeleteMarkedImages() + if err != nil { + t.Fatalf("Could not delete marked records! %s", err) + } + + images, err := dataStore.ImageMetadataAll() + if err != nil { + t.Fatalf("Could not query images to upload! %s", err) + } + + if len(images) != 1 { + t.Fatalf("Got incorrect number of images (%d). Expected one.", len(images)) + } } func saveImageShouldNotFail(action string, dataStore *localDataStore, img ImageMetaData, t *testing.T) {