@secondlayer/shared 4.2.0 → 4.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,99 @@
1
+ import { type Kysely, sql } from "kysely";
2
+
3
+ export async function up(db: Kysely<unknown>): Promise<void> {
4
+ await sql`
5
+ CREATE TABLE IF NOT EXISTS subgraph_operations (
6
+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
7
+ subgraph_id uuid NOT NULL REFERENCES subgraphs(id) ON DELETE CASCADE,
8
+ subgraph_name text NOT NULL,
9
+ account_id text,
10
+ kind text NOT NULL CHECK (kind IN ('reindex', 'backfill')),
11
+ status text NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'running', 'completed', 'failed', 'cancelled')),
12
+ from_block bigint,
13
+ to_block bigint,
14
+ cancel_requested boolean NOT NULL DEFAULT false,
15
+ locked_by text,
16
+ locked_until timestamptz,
17
+ started_at timestamptz,
18
+ finished_at timestamptz,
19
+ processed_blocks bigint,
20
+ error text,
21
+ created_at timestamptz NOT NULL DEFAULT now(),
22
+ updated_at timestamptz NOT NULL DEFAULT now()
23
+ )
24
+ `.execute(db);
25
+
26
+ await sql`
27
+ CREATE UNIQUE INDEX IF NOT EXISTS subgraph_operations_active_unique
28
+ ON subgraph_operations (subgraph_id)
29
+ WHERE status IN ('queued', 'running')
30
+ `.execute(db);
31
+
32
+ await sql`
33
+ CREATE INDEX IF NOT EXISTS subgraph_operations_claim_idx
34
+ ON subgraph_operations (created_at)
35
+ WHERE status = 'queued'
36
+ `.execute(db);
37
+
38
+ await sql`
39
+ CREATE INDEX IF NOT EXISTS subgraph_operations_stale_running_idx
40
+ ON subgraph_operations (locked_until)
41
+ WHERE status = 'running'
42
+ `.execute(db);
43
+
44
+ await sql`
45
+ CREATE OR REPLACE FUNCTION notify_subgraph_operation_new()
46
+ RETURNS trigger AS $$
47
+ BEGIN
48
+ PERFORM pg_notify('subgraph_operations:new', NEW.id::text);
49
+ RETURN NEW;
50
+ END;
51
+ $$ LANGUAGE plpgsql
52
+ `.execute(db);
53
+
54
+ await sql`
55
+ DROP TRIGGER IF EXISTS subgraph_operations_insert_notify ON subgraph_operations
56
+ `.execute(db);
57
+ await sql`
58
+ CREATE TRIGGER subgraph_operations_insert_notify
59
+ AFTER INSERT ON subgraph_operations
60
+ FOR EACH ROW
61
+ WHEN (NEW.status = 'queued')
62
+ EXECUTE FUNCTION notify_subgraph_operation_new()
63
+ `.execute(db);
64
+
65
+ await sql`
66
+ DROP TRIGGER IF EXISTS subgraph_operations_cancel_notify ON subgraph_operations
67
+ `.execute(db);
68
+ await sql`
69
+ CREATE TRIGGER subgraph_operations_cancel_notify
70
+ AFTER UPDATE OF cancel_requested ON subgraph_operations
71
+ FOR EACH ROW
72
+ WHEN (NEW.cancel_requested = true AND OLD.cancel_requested IS DISTINCT FROM NEW.cancel_requested)
73
+ EXECUTE FUNCTION notify_subgraph_operation_new()
74
+ `.execute(db);
75
+ }
76
+
77
+ export async function down(db: Kysely<unknown>): Promise<void> {
78
+ await sql`
79
+ DROP TRIGGER IF EXISTS subgraph_operations_cancel_notify ON subgraph_operations
80
+ `.execute(db);
81
+ await sql`
82
+ DROP TRIGGER IF EXISTS subgraph_operations_insert_notify ON subgraph_operations
83
+ `.execute(db);
84
+ await sql`
85
+ DROP FUNCTION IF EXISTS notify_subgraph_operation_new()
86
+ `.execute(db);
87
+ await sql`
88
+ DROP INDEX IF EXISTS subgraph_operations_stale_running_idx
89
+ `.execute(db);
90
+ await sql`
91
+ DROP INDEX IF EXISTS subgraph_operations_claim_idx
92
+ `.execute(db);
93
+ await sql`
94
+ DROP INDEX IF EXISTS subgraph_operations_active_unique
95
+ `.execute(db);
96
+ await sql`
97
+ DROP TABLE IF EXISTS subgraph_operations
98
+ `.execute(db);
99
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@secondlayer/shared",
3
- "version": "4.2.0",
3
+ "version": "4.3.0",
4
4
  "type": "module",
5
5
  "main": "./dist/src/index.js",
6
6
  "types": "./dist/src/index.d.ts",
@@ -33,6 +33,10 @@
33
33
  "types": "./dist/src/db/queries/subgraph-gaps.d.ts",
34
34
  "import": "./dist/src/db/queries/subgraph-gaps.js"
35
35
  },
36
+ "./db/queries/subgraph-operations": {
37
+ "types": "./dist/src/db/queries/subgraph-operations.d.ts",
38
+ "import": "./dist/src/db/queries/subgraph-operations.js"
39
+ },
36
40
  "./db/jsonb": {
37
41
  "types": "./dist/src/db/jsonb.d.ts",
38
42
  "import": "./dist/src/db/jsonb.js"