Skip to content

Commit

Permalink
Merge pull request #286 from codex-team/285-fix-join-route-user-dupli…
Browse files Browse the repository at this point in the history
…cation

fix: adds unique index on note and user id of teams table
  • Loading branch information
Tushar504 authored Feb 5, 2025
2 parents 27cc4a7 + 7bf4dfe commit f5f2379
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- remove exists duplicate entries from database
DELETE FROM note_teams
WHERE id IN (
SELECT id
FROM (
SELECT
id,
ROW_NUMBER() OVER (
PARTITION BY note_id, user_id
ORDER BY id
) AS row_num
FROM note_teams
) AS duplicates
WHERE duplicates.row_num > 1
);

-- adds unique index
CREATE UNIQUE INDEX IF NOT EXISTS note_teams_note_id_user_id_unique_idx
ON public.note_teams (note_id, user_id);
6 changes: 0 additions & 6 deletions src/presentation/http/router/noteSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -762,12 +762,6 @@ describe('NoteSettings API', () => {
creatorId: user.id,
});

await global.db.insertNoteTeam({
noteId: note.id,
userId: user.id,
role: MemberRole.Write,
});

const accessToken = global.auth(user.id);

const response = await global.api?.fakeRequest({
Expand Down
7 changes: 7 additions & 0 deletions src/repository/storage/postgres/orm/sequelize/teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ export default class TeamsSequelizeStorage {
tableName: this.tableName,
sequelize: this.database,
timestamps: false,
indexes: [
// Create a unique index on noteId and userId
{
unique: true,
fields: ['note_id', 'user_id'],
},
],
});
}

Expand Down

0 comments on commit f5f2379

Please sign in to comment.