From 217c97d4dbf3556331a7035585bcf2a5ee425558 Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Krieger Date: Mon, 8 Jul 2024 22:35:55 -0300 Subject: [PATCH] feat(data): add postgraphile views --- go.mod | 6 +- go.sum | 11 ++- internal/node/services.go | 3 +- .../000002_create_postgraphile_view.down.sql | 4 + .../000002_create_postgraphile_view.up.sql | 77 +++++++++++++++++++ 5 files changed, 91 insertions(+), 10 deletions(-) create mode 100644 internal/repository/migrations/000002_create_postgraphile_view.down.sql create mode 100644 internal/repository/migrations/000002_create_postgraphile_view.up.sql diff --git a/go.mod b/go.mod index 7150ee474..60f9ed4d9 100644 --- a/go.mod +++ b/go.mod @@ -16,9 +16,12 @@ require github.com/BurntSushi/toml v1.4.0 require ( github.com/Khan/genqlient v0.7.0 github.com/deepmap/oapi-codegen/v2 v2.1.0 + github.com/golang-migrate/migrate/v4 v4.17.1 + github.com/jackc/pgx/v5 v5.6.0 github.com/lmittmann/tint v1.0.4 github.com/mattn/go-isatty v0.0.20 github.com/oapi-codegen/runtime v1.1.1 + github.com/testcontainers/testcontainers-go/modules/postgres v0.32.0 golang.org/x/sync v0.7.0 golang.org/x/text v0.16.0 ) @@ -62,7 +65,6 @@ require ( github.com/go-openapi/jsonpointer v0.21.0 // indirect github.com/go-openapi/swag v0.23.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect - github.com/golang-migrate/migrate/v4 v4.17.1 // indirect github.com/google/uuid v1.6.0 // indirect github.com/gorilla/websocket v1.5.3 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect @@ -72,7 +74,6 @@ require ( github.com/invopop/yaml v0.2.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect - github.com/jackc/pgx/v5 v5.6.0 // indirect github.com/jackc/puddle/v2 v2.2.1 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/klauspost/compress v1.17.9 // indirect @@ -102,7 +103,6 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/stretchr/objx v0.5.2 // indirect github.com/supranational/blst v0.3.12 // indirect - github.com/testcontainers/testcontainers-go/modules/postgres v0.32.0 // indirect github.com/tklauser/go-sysconf v0.3.14 // indirect github.com/tklauser/numcpus v0.8.0 // indirect github.com/vektah/gqlparser/v2 v2.5.16 // indirect diff --git a/go.sum b/go.sum index e36bc1924..29f0d46bc 100644 --- a/go.sum +++ b/go.sum @@ -98,10 +98,10 @@ github.com/deepmap/oapi-codegen/v2 v2.1.0 h1:I/NMVhJCtuvL9x+S2QzZKpSjGi33oDZwPRd github.com/deepmap/oapi-codegen/v2 v2.1.0/go.mod h1:R1wL226vc5VmCNJUvMyYr3hJMm5reyv25j952zAVXZ8= github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48 h1:fRzb/w+pyskVMQ+UbP35JkH8yB7MYb4q/qhBarqZE6g= github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA= +github.com/dhui/dktest v0.4.1 h1:/w+IWuDXVymg3IrRJCHHOkMK10m9aNVMOyD0X12YVTg= +github.com/dhui/dktest v0.4.1/go.mod h1:DdOqcUpL7vgyP4GlF3X3w7HbSlz8cEQzwewPveYEQbA= github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E= -github.com/docker/docker v25.0.5+incompatible h1:UmQydMduGkrD5nQde1mecF/YnSbTOaPeFIeP5C4W+DE= -github.com/docker/docker v25.0.5+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/docker/docker v27.0.3+incompatible h1:aBGI9TeQ4MPlhquTQKq9XbK79rKFVwXNUAYz9aXyEBE= github.com/docker/docker v27.0.3+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj1Br63c= @@ -310,8 +310,6 @@ github.com/supranational/blst v0.3.12 h1:Vfas2U2CFHhniv2QkUm2OVa1+pGTdqtpqm9NnhU github.com/supranational/blst v0.3.12/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc= -github.com/testcontainers/testcontainers-go v0.30.0 h1:jmn/XS22q4YRrcMwWg0pAwlClzs/abopbsBzrepyc4E= -github.com/testcontainers/testcontainers-go v0.30.0/go.mod h1:K+kHNGiM5zjklKjgTtcrEetF3uhWbMUyqAQoyoh8Pf0= github.com/testcontainers/testcontainers-go v0.32.0 h1:ug1aK08L3gCHdhknlTTwWjPHPS+/alvLJU/DRxTD/ME= github.com/testcontainers/testcontainers-go v0.32.0/go.mod h1:CRHrzHLQhlXUsa5gXjTOfqIEJcrK5+xMDmBr/WMI88E= github.com/testcontainers/testcontainers-go/modules/postgres v0.32.0 h1:ZE4dTdswj3P0j71nL+pL0m2e5HTXJwPoIFr+DDgdPaU= @@ -391,6 +389,8 @@ golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA= +golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= @@ -427,9 +427,8 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gotest.tools/v3 v3.5.0 h1:Ljk6PdHdOhAb5aDMWXjDLMMhph+BpztA4v1QdqEW2eY= -gotest.tools/v3 v3.5.0/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU= gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU= +gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU= launchpad.net/gocheck v0.0.0-20140225173054-000000000087 h1:Izowp2XBH6Ya6rv+hqbceQyw/gSGoXfH/UPoTGduL54= launchpad.net/gocheck v0.0.0-20140225173054-000000000087/go.mod h1:hj7XX3B/0A+80Vse0e+BUHsHMTEhd0O4cpUHr/e/BUM= rsc.io/tmplfunc v0.0.3 h1:53XFQh69AfOa8Tw0Jm7t+GV7KZhOi6jzsCzTtKbMvzU= diff --git a/internal/node/services.go b/internal/node/services.go index cd5cd46af..704d72e6e 100644 --- a/internal/node/services.go +++ b/internal/node/services.go @@ -153,11 +153,12 @@ func newPostgraphileService(c config.NodeConfig, workDir string) services.Comman s.Args = append(s.Args, "--no-setof-functions-contain-nulls") s.Args = append(s.Args, "--no-ignore-rbac") s.Args = append(s.Args, "--enable-query-batching") + s.Args = append(s.Args, "--enhance-graphiql") s.Args = append(s.Args, "--extended-errors", "errcode") s.Args = append(s.Args, "--append-plugins", "@graphile-contrib/pg-simplify-inflector") s.Args = append(s.Args, "--legacy-relations", "omit") s.Args = append(s.Args, "--connection", fmt.Sprintf("%v", c.PostgresEndpoint.Value)) - s.Args = append(s.Args, "--schema", "public") + s.Args = append(s.Args, "--schema", "graphql") s.Args = append(s.Args, "--host", "0.0.0.0") s.Args = append(s.Args, "--port", fmt.Sprint(getPort(c, portOffsetPostgraphile))) s.Env = append(s.Env, os.Environ()...) diff --git a/internal/repository/migrations/000002_create_postgraphile_view.down.sql b/internal/repository/migrations/000002_create_postgraphile_view.down.sql new file mode 100644 index 000000000..0ba224155 --- /dev/null +++ b/internal/repository/migrations/000002_create_postgraphile_view.down.sql @@ -0,0 +1,4 @@ +-- (c) Cartesi and individual authors (see AUTHORS) +-- SPDX-License-Identifier: Apache-2.0 (see LICENSE) + +DROP SCHEMA graphql CASCADE; \ No newline at end of file diff --git a/internal/repository/migrations/000002_create_postgraphile_view.up.sql b/internal/repository/migrations/000002_create_postgraphile_view.up.sql new file mode 100644 index 000000000..39b123090 --- /dev/null +++ b/internal/repository/migrations/000002_create_postgraphile_view.up.sql @@ -0,0 +1,77 @@ + +-- (c) Cartesi and individual authors (see AUTHORS) +-- SPDX-License-Identifier: Apache-2.0 (see LICENSE) + +CREATE SCHEMA IF NOT EXISTS graphql; + +CREATE OR REPLACE VIEW graphql."applications" AS + SELECT + "address", + "template_hash", + "snapshot_uri", + "last_processed_block", + "epoch_length", + "status" + FROM + "application"; + +CREATE OR REPLACE VIEW graphql."inputs" AS + SELECT + "index", + "status", + "block_number", + "raw_data", + "machine_hash", + "outputs_hash", + "application_address" + FROM + "input"; + +CREATE OR REPLACE VIEW graphql."outputs" AS + SELECT + o."index", + o."raw_data", + o."output_hashes_siblings", + i."index" as "input_index" + FROM + "output" o + INNER JOIN + "input" i on o."input_id"=i."id"; + +CREATE OR REPLACE VIEW graphql."reports" AS + SELECT + r."index", + r."raw_data", + i."index" as "input_index" + FROM + "report" r + INNER JOIN + "input" i on r."input_id"=i."id"; + +CREATE OR REPLACE VIEW graphql."claims" AS + SELECT + c."index", + c."output_merkle_root_hash", + c."status", + c."application_address", + o."index" as "output_index" + FROM + "claim" c + INNER JOIN + "application" a ON c."application_address"=a."address" + INNER JOIN + "input" i ON a."address"=i."application_address" + INNER JOIN + "output" o ON i."id"=o."input_id"; + +COMMENT ON VIEW graphql."inputs" is + E'@foreignKey (application_address) references applications(address)|@fieldName applicationByApplicationAddress'; + +COMMENT ON VIEW graphql."outputs" is + E'@foreignKey (input_index) references inputs(index)|@fieldName inputByInputIndex'; + +COMMENT ON VIEW graphql."reports" is + E'@foreignKey (input_index) references inputs(index)|@fieldName inputByInputIndex'; + +COMMENT ON VIEW graphql."claims" is + E'@foreignKey (output_index) references outputs(index)|@fieldName outputByOutputIndex\n@foreignKey (application_address) references applications(address)|@fieldName applicationByApplicationAddress'; \ No newline at end of file