Skip to content

Commit

Permalink
Merge pull request #393 from matrix-org/j94/no-error-on-duplicate-act…
Browse files Browse the repository at this point in the history
…ivity

Fix PostgreSQL errors when a metric activity is recorded twice
  • Loading branch information
Christian Paul authored Apr 16, 2020
2 parents 0251ad4 + f6f62bf commit 95470fa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/393.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix PostgreSQL errors when a metric activity is recorded twice
5 changes: 4 additions & 1 deletion src/datastore/postgres/PgDatastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,10 @@ export class PgDatastore implements Datastore {
date = date || new Date();
const userId = (user instanceof SlackGhost) ? user.toEntry().id : user.userId;

await this.postgresDb.none("INSERT INTO metrics_activities (user_id, room_id, date) VALUES(${userId}, ${roomId}, ${date})", {
await this.postgresDb.none(
"INSERT INTO metrics_activities (user_id, room_id, date) " +
"VALUES(${userId}, ${roomId}, ${date}) " +
"ON CONFLICT ON CONSTRAINT cons_activities_unique DO NOTHING;", {
date: `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`,
roomId: room.toEntry().id,
userId,
Expand Down
24 changes: 24 additions & 0 deletions src/tests/integration/SharedDatastoreTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,28 @@ export const doDatastoreTests = (ds: () => Datastore, roomsAfterEach: () => void
});
});
});

describe("metrics", () => {
it("should not throw when an activity is upserted twice", async () => {
const user = SlackGhost.fromEntry(null as any, {
display_name: "A displayname",
avatar_url: "Some avatar",
id: "someid1",
slack_id: "FOOBAR",
team_id: "BARBAZ",
}, null);
const room = new BridgedRoom({} as any, {
inbound_id: "a_remote_id",
matrix_room_id: "a_matrix_id",
slack_channel_id: "a_channel_id",
slack_channel_name: "a_channel_name",
slack_team_id: "a_team_id",
slack_webhook_uri: "a_webhook_uri",
puppet_owner: undefined,
}, {} as any);
const date = new Date();
await ds().upsertActivityMetrics(user, room, date);
await ds().upsertActivityMetrics(user, room, date);
});
});
};

0 comments on commit 95470fa

Please sign in to comment.