Skip to content

Commit f5f2379

Browse files
authored
Merge pull request #286 from codex-team/285-fix-join-route-user-duplication
fix: adds unique index on note and user id of teams table
2 parents 27cc4a7 + 7bf4dfe commit f5f2379

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- remove exists duplicate entries from database
2+
DELETE FROM note_teams
3+
WHERE id IN (
4+
SELECT id
5+
FROM (
6+
SELECT
7+
id,
8+
ROW_NUMBER() OVER (
9+
PARTITION BY note_id, user_id
10+
ORDER BY id
11+
) AS row_num
12+
FROM note_teams
13+
) AS duplicates
14+
WHERE duplicates.row_num > 1
15+
);
16+
17+
-- adds unique index
18+
CREATE UNIQUE INDEX IF NOT EXISTS note_teams_note_id_user_id_unique_idx
19+
ON public.note_teams (note_id, user_id);

src/presentation/http/router/noteSettings.test.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -762,12 +762,6 @@ describe('NoteSettings API', () => {
762762
creatorId: user.id,
763763
});
764764

765-
await global.db.insertNoteTeam({
766-
noteId: note.id,
767-
userId: user.id,
768-
role: MemberRole.Write,
769-
});
770-
771765
const accessToken = global.auth(user.id);
772766

773767
const response = await global.api?.fakeRequest({

src/repository/storage/postgres/orm/sequelize/teams.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ export default class TeamsSequelizeStorage {
103103
tableName: this.tableName,
104104
sequelize: this.database,
105105
timestamps: false,
106+
indexes: [
107+
// Create a unique index on noteId and userId
108+
{
109+
unique: true,
110+
fields: ['note_id', 'user_id'],
111+
},
112+
],
106113
});
107114
}
108115

0 commit comments

Comments
 (0)