Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

Hasura prep command in flusher #2395

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG_UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

### Emitter & Flusher

- (feat) [\#2389](https://github.com/bandprotocol/bandchain/pull/2389) implement hasura command in flusher
- (bugs) [\#2641](https://github.com/bandprotocol/bandchain/pull/2641) Fix bug flusher when update validator and remove reporter
- (impv) [\#2572](https://github.com/bandprotocol/bandchain/pull/2572) cdb: Implemented view table for track vote statistic

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ services:
image: bandchain_flusher:latest
networks:
bandchain:
command: sh -c "sleep 30 && python main.py init bandchain test --db postgres:[email protected]:5432/postgres"
command: sh -c "sleep 30 && python main.py init bandchain test --db postgres:[email protected]:5432/postgres && python main.py hasura postgrespassword --db postgres:[email protected]:5432/postgres"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want docker to use new user, please use the new user on hasura service too.


flusher-daemon:
image: bandchain_flusher:latest
Expand Down
1 change: 1 addition & 0 deletions flusher/flusher/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import flusher.init
import flusher.sync
import flusher.hasura
55 changes: 55 additions & 0 deletions flusher/flusher/hasura.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import click

from .cli import cli

from sqlalchemy import create_engine


@cli.command()
@click.argument("password")
@click.option(
"--db",
help="Database URI connection string.",
default="localhost:5432/postgres",
show_default=True,
)
def hasura(password, db):
engine = create_engine("postgresql+psycopg2://" + db, echo=True)

engine.execute("""DROP ROLE IF EXISTS hasura;""")
engine.execute(f"""CREATE USER hasura WITH PASSWORD '{password}';""")
engine.execute("""CREATE EXTENSION IF NOT EXISTS pgcrypto;""")
engine.execute("""CREATE SCHEMA IF NOT EXISTS hdb_catalog;""")
engine.execute("""CREATE SCHEMA IF NOT EXISTS hdb_views;""")

# make the user an owner of system schemas
engine.execute("""ALTER SCHEMA hdb_catalog OWNER TO hasura;""")
engine.execute("""ALTER SCHEMA hdb_views OWNER TO hasura;""")

# grant select permissions on information_schema and pg_catalog. This is
# required for hasura to query list of available tables
engine.execute("""GRANT SELECT ON ALL TABLES IN SCHEMA information_schema TO hasura;""")
engine.execute("""GRANT SELECT ON ALL TABLES IN SCHEMA pg_catalog TO hasura;""")

# Below permissions are optional. This is dependent on what access to your
# tables/schemas - you want give to hasura. If you want expose the public
# schema for GraphQL query then give permissions on public schema to the
# hasura user.
# Be careful to use these in your production db. Consult the postgres manual or
# your DBA and give appropriate permissions.

# grant all privileges on all tables in the public schema. This can be customised:
# For example, if you only want to use GraphQL regular queries and not mutations,
# then you can set: GRANT SELECT ON ALL TABLES...
engine.execute("""GRANT USAGE ON SCHEMA public TO hasura;""")
engine.execute("""GRANT SELECT ON ALL TABLES IN SCHEMA public TO hasura;""")
engine.execute("""GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO hasura;""")
engine.execute("""GRANT ALL ON ALL FUNCTIONS IN SCHEMA public TO hasura;""")

engine.execute(
"""GRANT SELECT ON information_schema.table_constraints, information_schema.key_column_usage, information_schema.columns, information_schema.views, information_schema.schemata, information_schema.routines TO hasura;"""
)
engine.execute(
"""GRANT SELECT ON pg_catalog.pg_constraint, pg_catalog.pg_class, pg_catalog.pg_namespace, pg_catalog.pg_attribute, pg_catalog.pg_proc, pg_catalog.pg_available_extensions, pg_catalog.pg_statio_all_tables, pg_catalog.pg_description TO hasura;"""
)
engine.execute("""ALTER ROLE hasura SET statement_timeout='5s'""")