Skip to content

Commit

Permalink
feat(hubble): add migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiserKarel committed Sep 2, 2023
1 parent 03071cc commit 79861f2
Show file tree
Hide file tree
Showing 28 changed files with 295 additions and 5 deletions.
3 changes: 2 additions & 1 deletion hubble/hasura/config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
version: 3
endpoint: http://localhost:8080
endpoint: https://noble-pika-27.hasura.app
admin_secret: 3N5Mt2f4Y1AC7dE663AsGqRy66yiHBuZ3RMgUjM6X4Q3Ma8G2yKmVoasdasdsadasda
metadata_directory: metadata
actions:
kind: synchronous
Expand Down
1 change: 1 addition & 0 deletions hubble/hasura/metadata/api_limits.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
13 changes: 13 additions & 0 deletions hubble/hasura/metadata/backend_configs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
dataconnector:
athena:
uri: http://localhost:8081/api/v1/athena
mariadb:
uri: http://localhost:8081/api/v1/mariadb
mongodb:
uri: http://localhost:8082
mysql8:
uri: http://localhost:8081/api/v1/mysql
oracle:
uri: http://localhost:8081/api/v1/oracle
snowflake:
uri: http://localhost:8081/api/v1/snowflake
21 changes: 20 additions & 1 deletion hubble/hasura/metadata/databases/databases.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
[]
- name: default
kind: postgres
configuration:
connection_info:
database_url:
from_env: PG_DATABASE_URL
isolation_level: read-committed
use_prepared_statements: false
logical_models:
- fields:
- name: receiver
type:
nullable: false
scalar: text
- name: amount
type:
nullable: true
scalar: text
name: coin_received
tables: "!include default/tables/tables.yaml"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
table:
name: addresses
schema: public
28 changes: 28 additions & 0 deletions hubble/hasura/metadata/databases/default/tables/public_blocks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
table:
name: blocks
schema: public
object_relationships:
- name: chain
using:
foreign_key_constraint_on: chain_id
array_relationships:
- name: events
using:
foreign_key_constraint_on:
column: block_id
table:
name: events
schema: public
select_permissions:
- role: user
permission:
columns:
- is_finalized
- chain_id
- height
- id
- hash
filter: {}
limit: 50
allow_aggregations: true
comment: ""
11 changes: 11 additions & 0 deletions hubble/hasura/metadata/databases/default/tables/public_chains.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
table:
name: chains
schema: public
array_relationships:
- name: blocks
using:
foreign_key_constraint_on:
column: chain_id
table:
name: blocks
schema: public
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
table:
name: coin_receiveds
schema: public
object_relationships:
- name: event
using:
manual_configuration:
column_mapping:
id: id
insertion_order: null
remote_table:
name: events
schema: public
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
table:
name: coin_spents
schema: public
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
table:
name: coinbases
schema: public
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
table:
name: commissions
schema: public
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
table:
name: events
schema: public
object_relationships:
- name: block
using:
foreign_key_constraint_on: block_id
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
table:
name: messages
schema: public
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
table:
name: mints
schema: public
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
table:
name: rewards
schema: public
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
table:
name: transfers
schema: public
12 changes: 12 additions & 0 deletions hubble/hasura/metadata/databases/default/tables/tables.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
- "!include public_addresses.yaml"
- "!include public_blocks.yaml"
- "!include public_chains.yaml"
- "!include public_coin_receiveds.yaml"
- "!include public_coin_spents.yaml"
- "!include public_coinbases.yaml"
- "!include public_commissions.yaml"
- "!include public_events.yaml"
- "!include public_messages.yaml"
- "!include public_mints.yaml"
- "!include public_rewards.yaml"
- "!include public_transfers.yaml"
1 change: 1 addition & 0 deletions hubble/hasura/metadata/graphql_schema_introspection.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
disabled_for_roles: []
1 change: 1 addition & 0 deletions hubble/hasura/metadata/inherited_roles.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
2 changes: 2 additions & 0 deletions hubble/hasura/metadata/metrics_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
analyze_query_variables: true
analyze_response_body: true
1 change: 1 addition & 0 deletions hubble/hasura/metadata/network.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions hubble/hasura/metadata/opentelemetry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions hubble/hasura/metadata/rest_endpoints.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
138 changes: 138 additions & 0 deletions hubble/hasura/migrations/default/1693576035737_init/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
SET check_function_bodies = false;
CREATE TABLE public.addresses (
address text NOT NULL,
chain_id integer NOT NULL,
id integer NOT NULL
);
CREATE SEQUENCE public.addresses_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE public.addresses_id_seq OWNED BY public.addresses.id;
CREATE TABLE public.blocks (
hash text NOT NULL,
chain_id integer NOT NULL,
height integer NOT NULL,
id integer NOT NULL,
is_finalized boolean DEFAULT false
);
CREATE SEQUENCE public.blocks_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE public.blocks_id_seq OWNED BY public.blocks.id;
CREATE TABLE public.chains (
id integer NOT NULL,
chain_id text NOT NULL
);
CREATE SEQUENCE public.chains_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE public.chains_id_seq OWNED BY public.chains.id;
CREATE TABLE public.events (
id integer NOT NULL,
index integer NOT NULL,
data jsonb NOT NULL,
block_id integer NOT NULL
);
CREATE VIEW public.coin_receiveds AS
SELECT events.id,
(((events.data -> 'attributes'::text) -> 0) ->> 'value'::text) AS receiver,
"substring"((((events.data -> 'attributes'::text) -> 1) ->> 'value'::text), '^\d+'::text) AS amount,
"substring"((((events.data -> 'attributes'::text) -> 1) ->> 'value'::text), '[a-zA-Z].*'::text) AS denom
FROM public.events
WHERE ((events.data ->> 'type'::text) = 'coin_received'::text);
CREATE VIEW public.coin_spents AS
SELECT events.id,
(((events.data -> 'attributes'::text) -> 0) ->> 'value'::text) AS spender,
"substring"((((events.data -> 'attributes'::text) -> 1) ->> 'value'::text), '^\d+'::text) AS amount,
"substring"((((events.data -> 'attributes'::text) -> 1) ->> 'value'::text), '[a-zA-Z].*'::text) AS denom
FROM public.events
WHERE ((events.data ->> 'type'::text) = 'coin_spent'::text);
CREATE VIEW public.coinbases AS
SELECT events.id,
(((events.data -> 'attributes'::text) -> 0) ->> 'value'::text) AS minter,
"substring"((((events.data -> 'attributes'::text) -> 1) ->> 'value'::text), '^\d+'::text) AS amount,
"substring"((((events.data -> 'attributes'::text) -> 1) ->> 'value'::text), '[a-zA-Z].*'::text) AS denom
FROM public.events
WHERE ((events.data ->> 'type'::text) = 'coinbase'::text);
CREATE VIEW public.commissions AS
SELECT events.id,
(((events.data -> 'attributes'::text) -> 1) ->> 'value'::text) AS validator,
"substring"((((events.data -> 'attributes'::text) -> 0) ->> 'value'::text), '^\d+'::text) AS amount,
"substring"((((events.data -> 'attributes'::text) -> 0) ->> 'value'::text), '[a-zA-Z].*'::text) AS denom
FROM public.events
WHERE ((events.data ->> 'type'::text) = 'commission'::text);
CREATE SEQUENCE public.events_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE public.events_id_seq OWNED BY public.events.id;
CREATE VIEW public.messages AS
SELECT events.id,
(((events.data -> 'attributes'::text) -> 0) ->> 'value'::text) AS sender
FROM public.events
WHERE ((events.data ->> 'type'::text) = 'message'::text);
CREATE VIEW public.mints AS
SELECT events.id,
(((events.data -> 'attributes'::text) -> 0) ->> 'value'::text) AS bonded_ratio,
(((events.data -> 'attributes'::text) -> 1) ->> 'value'::text) AS inflation,
(((events.data -> 'attributes'::text) -> 2) ->> 'value'::text) AS annual_provisions,
(((events.data -> 'attributes'::text) -> 3) ->> 'value'::text) AS amount
FROM public.events
WHERE ((events.data ->> 'type'::text) = 'mint'::text);
CREATE VIEW public.rewards AS
SELECT events.id,
(((events.data -> 'attributes'::text) -> 1) ->> 'value'::text) AS validator,
"substring"((((events.data -> 'attributes'::text) -> 0) ->> 'value'::text), '^\d+'::text) AS amount,
"substring"((((events.data -> 'attributes'::text) -> 0) ->> 'value'::text), '[a-zA-Z].*'::text) AS denom
FROM public.events
WHERE ((events.data ->> 'type'::text) = 'rewards'::text);
CREATE VIEW public.transfers AS
SELECT events.id,
(((events.data -> 'attributes'::text) -> 0) ->> 'value'::text) AS recipient,
(((events.data -> 'attributes'::text) -> 1) ->> 'value'::text) AS sender,
"substring"((((events.data -> 'attributes'::text) -> 2) ->> 'value'::text), '^\d+'::text) AS amount,
"substring"((((events.data -> 'attributes'::text) -> 2) ->> 'value'::text), '[a-zA-Z].*'::text) AS denom
FROM public.events
WHERE ((events.data ->> 'type'::text) = 'transfer'::text);
ALTER TABLE ONLY public.addresses ALTER COLUMN id SET DEFAULT nextval('public.addresses_id_seq'::regclass);
ALTER TABLE ONLY public.blocks ALTER COLUMN id SET DEFAULT nextval('public.blocks_id_seq'::regclass);
ALTER TABLE ONLY public.chains ALTER COLUMN id SET DEFAULT nextval('public.chains_id_seq'::regclass);
ALTER TABLE ONLY public.events ALTER COLUMN id SET DEFAULT nextval('public.events_id_seq'::regclass);
ALTER TABLE ONLY public.addresses
ADD CONSTRAINT addresses_address_chain_id_key UNIQUE (address, chain_id);
ALTER TABLE ONLY public.addresses
ADD CONSTRAINT addresses_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.blocks
ADD CONSTRAINT blocks_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.chains
ADD CONSTRAINT chains_id_key UNIQUE (id);
ALTER TABLE ONLY public.chains
ADD CONSTRAINT chains_pkey PRIMARY KEY (id, chain_id);
ALTER TABLE ONLY public.events
ADD CONSTRAINT events_pkey PRIMARY KEY (id);
CREATE INDEX coin_received_idx ON public.events USING btree (((data ->> 'type'::text)));
CREATE INDEX idx_blocks_chain_id ON public.blocks USING btree (chain_id);
CREATE INDEX idx_blocks_chain_id_height ON public.blocks USING btree (chain_id, height);
CREATE INDEX idx_blocks_height_desc ON public.blocks USING btree (height DESC);
CREATE INDEX idx_chains_chain_id ON public.chains USING btree (chain_id);
CREATE INDEX idx_events_block_id ON public.events USING btree (block_id);
CREATE INDEX receiver ON public.events USING gin (((((data -> 'attributes'::text) -> 0) -> 'value'::text))) WHERE ((data ->> 'type'::text) = 'coin_received'::text);
ALTER TABLE ONLY public.blocks
ADD CONSTRAINT blocks_chain_id_fkey FOREIGN KEY (chain_id) REFERENCES public.chains(id) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE ONLY public.events
ADD CONSTRAINT events_block_id_fkey FOREIGN KEY (block_id) REFERENCES public.blocks(id) ON UPDATE CASCADE ON DELETE CASCADE;
Empty file added hubble/hasura/seeds/.gitkeep
Empty file.
11 changes: 11 additions & 0 deletions hubble/hubble.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@
Env = [ "SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt" ];
};
};

hubble-migrations = pkgs.stdenv.mkDerivation {
name = "hubble-migrations";
src = [./hasura ];
installPhase = ''
mkdir -p $out
cp -r $src/metadata $out
cp -r $src/migrations $out
cp -r $src/seeds $out
'';
};
};
};
}
2 changes: 1 addition & 1 deletion networks/devnet.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
postgres = import ./services/postgres.nix { };
};

hasura-services = import ./services/hasura.nix { };
hasura-services = import ./services/hasura.nix { migrations = self'.packages.hubble-migrations };

devnet = {
project.name = "devnet";
Expand Down
9 changes: 7 additions & 2 deletions networks/services/hasura.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ ... }:
{ migrations, ... }:
{
hasura.service = {
image = "hasura/graphql-engine:v2.33.0";
image = "hasura/graphql-engine:v2.33.0.cli-migrations-v3";
tty = true;
stop_signal = "SIGINT";
ports = [
Expand All @@ -15,6 +15,11 @@
HASURA_GRAPHQL_ADMIN_SECRET = "secret";
HASURA_GRAPHQL_METADATA_DEFAULTS = ''{"backend_configs":{"dataconnector":{"athena":{"uri":"http://data-connector-agent:8081/api/v1/athena"},"mariadb":{"uri":"http://data-connector-agent:8081/api/v1/mariadb"},"mysql8":{"uri":"http://data-connector-agent:8081/api/v1/mysql"},"oracle":{"uri":"http://data-connector-agent:8081/api/v1/oracle"},"snowflake":{"uri":"http://data-connector-agent:8081/api/v1/snowflake"}}}}'';
};
volumes = [
"${migrations}/metatdata:/hasura-metatdata"
"${migrations}/migrations:/hasura-migrations"
];

depends_on = {
data-connector-agent = {
condition = "service_healthy";
Expand Down

0 comments on commit 79861f2

Please sign in to comment.