From 9cfa2739aa1d23a1592212ae2c1685cfca8c7ed9 Mon Sep 17 00:00:00 2001 From: Giuseppe Lo Presti Date: Wed, 2 Aug 2023 17:27:42 +0200 Subject: [PATCH] Fixes on notifications (#4061) * Fixes following recent changes * Reverted to bool to match the rest of the stack * changelog * Fixed tests --- changelog/unreleased/notif-fixes.md | 5 +++++ pkg/notification/db_changes.sql | 6 +----- pkg/notification/db_sqlite.sql | 16 ++++++++-------- pkg/notification/manager/sql/sql.go | 10 +++++----- pkg/notification/manager/sql/sql_test.go | 18 +++++++++--------- pkg/notification/manager/sql/test.sqlite | Bin 32768 -> 32768 bytes 6 files changed, 28 insertions(+), 27 deletions(-) create mode 100644 changelog/unreleased/notif-fixes.md 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 7bf9289cdda218362bae3993b3e552879705b4f0..e00cc5378734cb5e835a91e515d5cdf564decd79 100644 GIT binary patch delta 490 zcmZo@U}|V!njkHx&A`CG0mLvMI#I`1TAM*H+>4j*1_L{{6$4)z??!$`o+kc2?%Ui} zn*{~BxmbWw$0s_<$^q%EFr6^EQI3;c+}E11(Q&c^zu4pgZvM#=dBi6NaGNkHfmo7C z3K|JTsmYlInW=dt37U;691!)sVD%TE>VL!3tANz!<(FipWhN(r@oh^vCLW3aQM ziz}CAV-O3wxVk!HQ!>~z6n&Hb@k#Te88!Jk?;~a#2F;0$s`i$wY~sG^j0~B1DXA63 zg*llesqu-WCHY`}Jc?2AhA3QSV2}k(Z1m$SU}qP%RA+4D1%`n1y$7At6Erf70;a{BuzxVZYbhPWy?I|e&Dy0~&_ zHYT#LiyIm;Hsyki#MBEGnS7R4S|kBeGGX#}-bboG7~;W3L=!SX2b&QJ8e9qxP{qzJ zZfVHaC=LmdbNph$m{zh)ZseAqe4n2y6T4x+Ku$~0bY^7}4>x3F0Gm`?n3GwO8lPBN zk`Ly`W11Roh$+SlOakDPut|VTf$ca0|4aVkz}Q~KKY5eAD60Y!vnwMg2Dq6-8JP_^ M!TL9Uw&!&K0ICVM#sB~S