-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: use rpc for pins upsert #1088
Changes from 2 commits
de9cc75
057d8f1
6c8733d
ed11834
6c47486
7a21c4b
e9cb95d
e4b1538
c137431
058b009
1fc1159
bb6d70a
fd21596
634095c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -452,20 +452,25 @@ export class DBClient { | |
* @param {Array<import('./db-client-types').PinsUpsertInput>} pins | ||
*/ | ||
async upsertPins (pins) { | ||
const now = new Date().toISOString() | ||
const { error } = await this._client | ||
.from('pin') | ||
.upsert(pins.map(pin => ({ | ||
id: pin.id, | ||
status: pin.status, | ||
content_cid: pin.cid, | ||
pin_location_id: pin.locationId, | ||
updated_at: now | ||
})), { count: 'exact', returning: 'minimal' }) | ||
const { data, error } = await this._client.rpc('upsert_pins', { | ||
data: { | ||
pins: pins.map((pin) => ({ | ||
status: pin.status, | ||
cid: pin.cid, | ||
location: { | ||
peer_id: pin.location.peerId, | ||
peer_name: pin.location.peerName, | ||
region: pin.location.region | ||
} | ||
})) | ||
} | ||
}) | ||
|
||
if (error) { | ||
throw new DBError(error) | ||
} | ||
|
||
return data | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I'm not mistaken the rpc call is currently returning void, so there's no actual useful data to return here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I think we could be consistent and return Pin Ids There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Amended to return pinIds. Also amended |
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -214,6 +214,44 @@ BEGIN | |
END | ||
$$; | ||
|
||
CREATE OR REPLACE FUNCTION upsert_pins(data json) RETURNS VOID | ||
LANGUAGE plpgsql | ||
volatile | ||
PARALLEL UNSAFE | ||
AS | ||
$$ | ||
DECLARE | ||
pin_data json; | ||
pin_location_result_id BIGINT; | ||
pin_result_id BIGINT; | ||
BEGIN | ||
FOREACH pin_data IN array json_arr_to_json_element_array(data -> 'pins') | ||
LOOP | ||
-- Add to pin_location table if new | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we be calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Corrected to re-use |
||
INSERT INTO pin_location (peer_id, peer_name, region) | ||
VALUES (pin_data -> 'location' ->> 'peer_id', | ||
pin_data -> 'location' ->> 'peer_name', | ||
pin_data -> 'location' ->> 'region') | ||
-- Force update on conflict to get result, otherwise needs a follow up select | ||
ON CONFLICT ( peer_id ) DO UPDATE | ||
SET "peer_name" = pin_data -> 'location' ->> 'peer_name', | ||
"region" = pin_data -> 'location' ->> 'region' | ||
RETURNING id INTO pin_location_result_id; | ||
|
||
INSERT INTO pin (content_cid, status, pin_location_id, updated_at) | ||
VALUES (pin_data ->> 'cid', | ||
(pin_data ->> 'status')::pin_status_type, | ||
pin_location_result_id, | ||
(NOW())::timestamptz) | ||
-- Force update on conflict to get result, otherwise needs a follow up select | ||
ON CONFLICT ( content_cid, pin_location_id ) DO UPDATE | ||
SET "status" = (pin_data ->> 'status')::pin_status_type, | ||
"updated_at" = NOW() | ||
RETURNING id INTO pin_result_id; | ||
END LOOP; | ||
END | ||
$$; | ||
|
||
CREATE OR REPLACE FUNCTION upsert_pin(data json) RETURNS TEXT | ||
LANGUAGE plpgsql | ||
volatile | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Be aware this will change soon (today) to have one more thing: https://github.com/web3-storage/web3.storage/pull/1093/files
So let's wait on that PR to get in and update this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merged latest to include ipfs peer id.