Skip to content

Commit ee0947e

Browse files
authored
fix: on conflict criteria is ambiguous and cannot be used for upsert (#2197)
## Why is this change needed? Shuttle defines the messages table with two unique constraints, one for the hash, another for the triple of hash, fid and type. Postgres does not allow multiple ON CONFLICT criteria, and ambiguous conflicts are essentially surfaced as uniqueness violation failures in spite of ON CONFLICT criteria given. This change alters the merge to instead attempt insert, doing nothing on any conflict, then resolving with an update if necessary and the original conflict resolution criteria is satisfied. Addresses Issue #2167 ## Merge Checklist _Choose all relevant options below by adding an `x` now or at any time before submitting for review_ - [x] PR title adheres to the [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/) standard - [x] PR has a [changeset](https://github.com/farcasterxyz/hub-monorepo/blob/main/CONTRIBUTING.md#35-adding-changesets) - [x] PR has been tagged with a change label(s) (i.e. documentation, feature, bugfix, or chore) - [x] PR includes [documentation](https://github.com/farcasterxyz/hub-monorepo/blob/main/CONTRIBUTING.md#32-writing-docs) if necessary. <!-- start pr-codex --> --- ## PR-Codex overview This PR updates the conflict resolution logic in `MessageProcessor` to fix ambiguity issues during upsert operations. ### Detailed summary - Updated conflict resolution logic to use `doNothing()` method instead of complex conditions - Improved handling of conflict criteria for upsert operations > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent e2b1c7c commit ee0947e

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

.changeset/tidy-apricots-unite.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@farcaster/shuttle": patch
3+
---
4+
5+
fix: on conflict criteria is ambiguous and cannot be used for upsert

packages/shuttle/src/shuttle/messageProcessor.ts

+29-22
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class MessageProcessor {
4747
}
4848

4949
// @ts-ignore
50-
const result = await trx
50+
let result = await trx
5151
.insertInto("messages")
5252
.values({
5353
fid: message.data.fid,
@@ -62,28 +62,35 @@ export class MessageProcessor {
6262
...opData,
6363
})
6464
.returning(["id"])
65-
.onConflict((oc) =>
66-
oc
67-
.columns(["hash", "fid", "type"])
68-
// In case the signer was changed, make sure to always update it
69-
.doUpdateSet({
70-
signatureScheme: message.signatureScheme,
71-
signer: message.signer,
72-
raw: Message.encode(message).finish(),
73-
...opData,
74-
})
75-
.where(({ eb, or }) =>
76-
or([
77-
eb("excluded.deletedAt", "is not", null).and("messages.deletedAt", "is", null),
78-
eb("excluded.deletedAt", "is", null).and("messages.deletedAt", "is not", null),
79-
eb("excluded.prunedAt", "is not", null).and("messages.prunedAt", "is", null),
80-
eb("excluded.prunedAt", "is", null).and("messages.prunedAt", "is not", null),
81-
eb("excluded.revokedAt", "is not", null).and("messages.revokedAt", "is", null),
82-
eb("excluded.revokedAt", "is", null).and("messages.revokedAt", "is not", null),
83-
]),
84-
),
85-
)
65+
.onConflict((oc) => oc.doNothing())
8666
.executeTakeFirst();
67+
68+
if (!result) {
69+
const data = message.data;
70+
result = await trx
71+
.updateTable("messages")
72+
.set({
73+
signatureScheme: message.signatureScheme,
74+
signer: message.signer,
75+
raw: Message.encode(message).finish(),
76+
...opData,
77+
})
78+
.returning(["id"])
79+
.where((eb) =>
80+
eb.and([
81+
eb("hash", "=", message.hash),
82+
eb("fid", "=", data.fid),
83+
eb("type", "=", data.type),
84+
eb.or([
85+
eb("deletedAt", !opData.deletedAt ? "is not" : "is", null),
86+
eb("prunedAt", !opData.prunedAt ? "is not" : "is", null),
87+
eb("revokedAt", !opData.revokedAt ? "is not" : "is", null),
88+
]),
89+
]),
90+
)
91+
.executeTakeFirst();
92+
}
93+
8794
return !!result;
8895
}
8996

0 commit comments

Comments
 (0)