Skip to content

Commit d16ac8c

Browse files
authored
api: fix proof/root query to be more specific (#67)
1 parent 52ff3a8 commit d16ac8c

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

api/migrations/migrations.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,23 @@ var Migrations = []migrate.Migration{
8585
SET NOT NULL;
8686
`,
8787
},
88+
{
89+
Name: "2022-08-31.0.root-func-index.sql",
90+
SQL: `
91+
CREATE OR REPLACE FUNCTION proofs_array (data jsonb)
92+
RETURNS text[]
93+
AS $CODE$
94+
BEGIN
95+
RETURN ARRAY (
96+
SELECT
97+
jsonb_array_elements(data) ->> 'proof');
98+
END
99+
$CODE$
100+
LANGUAGE plpgsql
101+
IMMUTABLE;
102+
103+
CREATE INDEX proofs_arr_idx ON trees USING GIN ((proofs_array(proofs)));
104+
DROP INDEX "trees_proofs_idx";
105+
`,
106+
},
88107
}

api/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (s *Server) GetRoot(w http.ResponseWriter, r *http.Request) {
4545
const q = `
4646
SELECT root
4747
FROM trees
48-
WHERE proofs @> $1
48+
WHERE proofs_array(proofs) @> proofs_array($1)
4949
LIMIT 1
5050
`
5151

0 commit comments

Comments
 (0)