Skip to content

Commit

Permalink
Fixes on notifications (#4061)
Browse files Browse the repository at this point in the history
* Fixes following recent changes

* Reverted to bool to match the rest of the stack

* changelog

* Fixed tests
  • Loading branch information
glpatcern committed Aug 2, 2023
1 parent a06384a commit 9cfa273
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 27 deletions.
5 changes: 5 additions & 0 deletions changelog/unreleased/notif-fixes.md
Original file line number Diff line number Diff line change
@@ -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
6 changes: 1 addition & 5 deletions pkg/notification/db_changes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
16 changes: 8 additions & 8 deletions pkg/notification/db_sqlite.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,33 @@
-- 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
);

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;
10 changes: 5 additions & 5 deletions pkg/notification/manager/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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 = ?
`

Expand Down Expand Up @@ -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
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/notification/manager/sql/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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(&notificationID)
err = db.QueryRow("SELECT id FROM notifications WHERE ref = ?", n2.Ref).Scan(&notificationID)
Expect(err).ToNot(HaveOccurred())
var newRecipientCount int
err = db.QueryRow(selectNotificationRecipientsSQL, notificationID).Scan(&newRecipientCount)
Expand All @@ -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))
})
Expand All @@ -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(&notificationID)
err = db.QueryRow("SELECT id FROM notifications WHERE ref = ?", m.Ref).Scan(&notificationID)
Expect(err).ToNot(HaveOccurred())
var newRecipientCount int
err = db.QueryRow(selectNotificationRecipientsSQL, notificationID).Scan(&newRecipientCount)
Expand Down Expand Up @@ -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())
})
Expand All @@ -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))
})
Expand Down
Binary file modified pkg/notification/manager/sql/test.sqlite
Binary file not shown.

0 comments on commit 9cfa273

Please sign in to comment.