diff --git a/changelog/unreleased/notif-fixes.md b/changelog/unreleased/notif-fixes.md new file mode 100644 index 0000000000..24a7e5ceb6 --- /dev/null +++ b/changelog/unreleased/notif-fixes.md @@ -0,0 +1,5 @@ +Bugfix: Fixes on notifications + +This is to align the code to the latest schema for notifications + +https://github.com/cs3org/reva/pull/4061 diff --git a/pkg/notification/db_changes.sql b/pkg/notification/db_changes.sql index 234c2a2558..8b2ccc804d 100644 --- a/pkg/notification/db_changes.sql +++ b/pkg/notification/db_changes.sql @@ -45,9 +45,5 @@ CREATE INDEX `notification_recipients_ix1` ON `notification_recipients` (`recipi -- changes for added notifications on oc shares -ALTER TABLE oc_share ADD notify_uploads BOOL DEFAULT false; +ALTER TABLE oc_share ADD notify_uploads BOOL DEFAULT false NOT NULL; ALTER TABLE oc_share ADD notify_uploads_extra_recipients VARCHAR(2048); - -UPDATE oc_share SET notify_uploads = false; - -ALTER TABLE oc_share MODIFY notify_uploads BOOL DEFAULT false NOT NULL; diff --git a/pkg/notification/db_sqlite.sql b/pkg/notification/db_sqlite.sql index 8e110fe153..3d691352e1 100644 --- a/pkg/notification/db_sqlite.sql +++ b/pkg/notification/db_sqlite.sql @@ -19,7 +19,7 @@ -- This file can be used to quickstart a SQLite DB for running the tests in -- ./manager/sql/sql_test.go -CREATE TABLE `cbox_notifications` ( +CREATE TABLE `notifications` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `ref` VARCHAR(3072) UNIQUE NOT NULL, `template_name` VARCHAR(320) NOT NULL @@ -27,25 +27,25 @@ CREATE TABLE `cbox_notifications` ( COMMIT; -CREATE TABLE `cbox_notification_recipients` ( +CREATE TABLE `notification_recipients` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `notification_id` INTEGER NOT NULL, `recipient` VARCHAR(320) NOT NULL, FOREIGN KEY (notification_id) - REFERENCES cbox_notifications (id) + REFERENCES notifications (id) ON DELETE CASCADE ); COMMIT; -CREATE INDEX `cbox_notifications_ix0` ON `cbox_notifications` (`ref`); +CREATE INDEX `notifications_ix0` ON `notifications` (`ref`); -CREATE INDEX `cbox_notification_recipients_ix0` ON `cbox_notification_recipients` (`notification_id`); -CREATE INDEX `cbox_notification_recipients_ix1` ON `cbox_notification_recipients` (`recipient`); +CREATE INDEX `notification_recipients_ix0` ON `notification_recipients` (`notification_id`); +CREATE INDEX `notification_recipients_ix1` ON `notification_recipients` (`recipient`); COMMIT; -INSERT INTO `cbox_notifications` (`id`, `ref`, `template_name`) VALUES (1, "notification-test", "notification-template-test"); -INSERT INTO `cbox_notification_recipients` (`id`, `notification_id`, `recipient`) VALUES (1, 1, "jdoe"), (2, 1, "testuser"); +INSERT INTO `notifications` (`id`, `ref`, `template_name`) VALUES (1, "notification-test", "notification-template-test"); +INSERT INTO `notification_recipients` (`id`, `notification_id`, `recipient`) VALUES (1, 1, "jdoe"), (2, 1, "testuser"); COMMIT; diff --git a/pkg/notification/manager/sql/sql.go b/pkg/notification/manager/sql/sql.go index c1af5b15a9..d0760bdf48 100644 --- a/pkg/notification/manager/sql/sql.go +++ b/pkg/notification/manager/sql/sql.go @@ -80,7 +80,7 @@ func (m *mgr) UpsertNotification(n notification.Notification) error { } // Create/update notification - stmt, err := m.db.Prepare("REPLACE INTO cbox_notifications (ref, template_name) VALUES (?, ?)") + stmt, err := m.db.Prepare("REPLACE INTO notifications (ref, template_name) VALUES (?, ?)") if err != nil { return err } @@ -99,7 +99,7 @@ func (m *mgr) UpsertNotification(n notification.Notification) error { return err } - stmt, err = tx.Prepare("REPLACE INTO cbox_notification_recipients (notification_id, recipient) VALUES (?, ?)") + stmt, err = tx.Prepare("REPLACE INTO notification_recipients (notification_id, recipient) VALUES (?, ?)") if err != nil { _ = tx.Rollback() return err @@ -126,8 +126,8 @@ func (m *mgr) UpsertNotification(n notification.Notification) error { func (m *mgr) GetNotification(ref string) (*notification.Notification, error) { query := ` SELECT n.id, n.ref, n.template_name, nr.recipient - FROM cbox_notifications AS n - JOIN cbox_notification_recipients AS nr ON n.id = nr.notification_id + FROM notifications AS n + JOIN notification_recipients AS nr ON n.id = nr.notification_id WHERE n.ref = ? ` @@ -171,7 +171,7 @@ func (m *mgr) DeleteNotification(ref string) error { } // Delete notification - stmt, err := m.db.Prepare("DELETE FROM cbox_notifications WHERE ref = ?") + stmt, err := m.db.Prepare("DELETE FROM notifications WHERE ref = ?") if err != nil { return err } diff --git a/pkg/notification/manager/sql/sql_test.go b/pkg/notification/manager/sql/sql_test.go index 60080ffab0..5bfe1f3378 100644 --- a/pkg/notification/manager/sql/sql_test.go +++ b/pkg/notification/manager/sql/sql_test.go @@ -48,8 +48,8 @@ var _ = Describe("SQL manager for notifications", func() { nn *notification.Notification ref string err error - selectNotificationsSQL = "SELECT ref, template_name FROM cbox_notifications WHERE ref = ?" - selectNotificationRecipientsSQL = "SELECT COUNT(*) FROM cbox_notification_recipients WHERE notification_id = ?" + selectNotificationsSQL = "SELECT ref, template_name FROM notifications WHERE ref = ?" + selectNotificationRecipientsSQL = "SELECT COUNT(*) FROM notification_recipients WHERE notification_id = ?" ) AfterEach(func() { @@ -103,7 +103,7 @@ var _ = Describe("SQL manager for notifications", func() { It("should create notification recipients entries", func() { var notificationID int - err = db.QueryRow("SELECT id FROM cbox_notifications WHERE ref = ?", n2.Ref).Scan(¬ificationID) + err = db.QueryRow("SELECT id FROM notifications WHERE ref = ?", n2.Ref).Scan(¬ificationID) Expect(err).ToNot(HaveOccurred()) var newRecipientCount int err = db.QueryRow(selectNotificationRecipientsSQL, notificationID).Scan(&newRecipientCount) @@ -129,7 +129,7 @@ var _ = Describe("SQL manager for notifications", func() { It("should not increase the number of entries in the notification table", func() { var count int - err = db.QueryRow("SELECT COUNT(*) FROM cbox_notifications").Scan(&count) + err = db.QueryRow("SELECT COUNT(*) FROM notifications").Scan(&count) Expect(err).ToNot(HaveOccurred()) Expect(count).To(Equal(1)) }) @@ -143,14 +143,14 @@ var _ = Describe("SQL manager for notifications", func() { It("should delete old entries in notification recipients", func() { var count int - err = db.QueryRow("SELECT COUNT(*) FROM cbox_notification_recipients WHERE recipient = 'testuser'").Scan(&count) + err = db.QueryRow("SELECT COUNT(*) FROM notification_recipients WHERE recipient = 'testuser'").Scan(&count) Expect(err).ToNot(HaveOccurred()) Expect(count).To(BeZero()) }) It("should create new entries in notification recipients", func() { var notificationID int - err = db.QueryRow("SELECT id FROM cbox_notifications WHERE ref = ?", m.Ref).Scan(¬ificationID) + err = db.QueryRow("SELECT id FROM notifications WHERE ref = ?", m.Ref).Scan(¬ificationID) Expect(err).ToNot(HaveOccurred()) var newRecipientCount int err = db.QueryRow(selectNotificationRecipientsSQL, notificationID).Scan(&newRecipientCount) @@ -217,14 +217,14 @@ var _ = Describe("SQL manager for notifications", func() { It("should delete the notification from the database", func() { var count int - err = db.QueryRow("SELECT COUNT(*) FROM cbox_notifications WHERE ref = ?", ref).Scan(&count) + err = db.QueryRow("SELECT COUNT(*) FROM notifications WHERE ref = ?", ref).Scan(&count) Expect(err).ToNot(HaveOccurred()) Expect(count).To(BeZero()) }) It("should cascade the deletions to notification_recipients table", func() { var count int - err = db.QueryRow("SELECT COUNT(*) FROM cbox_notification_recipients WHERE notification_id = ?", 1).Scan(&count) + err = db.QueryRow("SELECT COUNT(*) FROM notification_recipients WHERE notification_id = ?", 1).Scan(&count) Expect(err).ToNot(HaveOccurred()) Expect(count).To(BeZero()) }) @@ -241,7 +241,7 @@ var _ = Describe("SQL manager for notifications", func() { isNotFoundError, _ := err.(*notification.NotFoundError) Expect(isNotFoundError).ToNot(BeNil()) var count int - err = db.QueryRow("SELECT COUNT(*) FROM cbox_notifications").Scan(&count) + err = db.QueryRow("SELECT COUNT(*) FROM notifications").Scan(&count) Expect(err).ToNot(HaveOccurred()) Expect(count).To(Equal(1)) }) diff --git a/pkg/notification/manager/sql/test.sqlite b/pkg/notification/manager/sql/test.sqlite index 7bf9289cdd..e00cc53787 100644 Binary files a/pkg/notification/manager/sql/test.sqlite and b/pkg/notification/manager/sql/test.sqlite differ