@@ -40,12 +40,7 @@ CREATE TABLE market_piece_metadata (
40
40
piece_cid TEXT NOT NULL PRIMARY KEY ,
41
41
42
42
version INT NOT NULL DEFAULT 2 ,
43
- <<<<<<< HEAD
44
43
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP AT TIME ZONE ' UTC' ,
45
- =======
46
-
47
- created_at TIMESTAMPTZ NOT NULL DEFAULT TIMEZONE(' UTC' , NOW()),
48
- >>>>>>> d1327dc (incomplete basic UI code)
49
44
50
45
indexed BOOLEAN NOT NULL DEFAULT FALSE,
51
46
indexed_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP AT TIME ZONE ' UTC' ,
@@ -244,7 +239,8 @@ CREATE TABLE market_offline_urls (
244
239
CONSTRAINT market_offline_urls_uuid_unique UNIQUE (uuid)
245
240
);
246
241
247
- CREATE TABLE libp2p_keys (
242
+ -- This table is used for coordinating libp2p nodes
243
+ CREATE TABLE libp2p (
248
244
sp_id BIGINT NOT NULL ,
249
245
priv_key BYTEA NOT NULL ,
250
246
listen_address TEXT NOT NULL ,
@@ -272,6 +268,60 @@ CREATE TABLE direct_deals (
272
268
keep_unsealed_copy BOOLEAN
273
269
);
274
270
271
+ -- -- Function used to update the libp2p table
272
+ CREATE OR REPLACE FUNCTION insert_or_update_libp2p (
273
+ _sp_id BIGINT ,
274
+ _listen_address TEXT ,
275
+ _announce_address TEXT ,
276
+ _no_announce_address TEXT ,
277
+ _running_on TEXT
278
+ )
279
+ RETURNS BYTEA AS $$
280
+ DECLARE
281
+ _priv_key BYTEA ;
282
+ _current_running_on TEXT ;
283
+ _current_updated_at TIMESTAMPTZ ;
284
+ BEGIN
285
+ -- Check if the sp_id exists and retrieve the current values
286
+ SELECT priv_key, running_on, updated_at INTO _priv_key, _current_running_on, _current_updated_at
287
+ FROM libp2p
288
+ WHERE sp_id = _sp_id;
289
+
290
+ -- Raise an exception if no row was found
291
+ IF NOT FOUND THEN
292
+ RAISE EXCEPTION ' libp2p key for sp_id "%" does not exist' , _sp_id;
293
+ END IF;
294
+
295
+ -- If the sp_id exists and running_on is NULL or matches _running_on
296
+ IF _current_running_on IS NULL OR _current_running_on = _running_on THEN
297
+ -- Update the record with the provided values and set updated_at to NOW
298
+ UPDATE libp2p
299
+ SET
300
+ listen_address = _listen_address,
301
+ announce_address = _announce_address,
302
+ no_announce_address = _no_announce_address,
303
+ running_on = _running_on,
304
+ updated_at = NOW() AT TIME ZONE ' UTC'
305
+ WHERE sp_id = _sp_id;
306
+ ELSIF _current_updated_at > NOW() - INTERVAL ' 10 seconds' THEN
307
+ -- Raise an exception if running_on is different and updated_at is recent
308
+ RAISE EXCEPTION ' Libp2p node already running on "%"' , _current_running_on;
309
+ ELSE
310
+ -- Update running_on and other columns if updated_at is older than 10 seconds
311
+ UPDATE libp2p
312
+ SET
313
+ listen_address = _listen_address,
314
+ announce_address = _announce_address,
315
+ no_announce_address = _no_announce_address,
316
+ running_on = _running_on,
317
+ updated_at = NOW() AT TIME ZONE ' UTC'
318
+ WHERE sp_id = _sp_id;
319
+ END IF;
320
+
321
+ RETURN _priv_key;
322
+ END;
323
+ $$ LANGUAGE plpgsql;
324
+
275
325
-- Add host column to allow local file based
276
326
-- piece park
277
327
ALTER TABLE parked_piece_refs
@@ -299,9 +349,6 @@ CREATE TABLE market_legacy_deals (
299
349
publish_cid TEXT NOT NULL ,
300
350
chain_deal_id BIGINT NOT NULL ,
301
351
302
- piece_cid TEXT NOT NULL ,
303
- piece_size BIGINT NOT NULL ,
304
-
305
352
fast_retrieval BOOLEAN NOT NULL ,
306
353
307
354
created_at TIMESTAMPTZ NOT NULL ,
0 commit comments