From cf43b37adea71b13a2d4efaf859409c130048d3e Mon Sep 17 00:00:00 2001 From: Nattharat Wiriyakulnan Date: Wed, 5 Aug 2020 17:35:10 +0700 Subject: [PATCH 1/4] Hasura prep command in flusher --- docker-compose.yaml | 2 +- flusher/flusher/__init__.py | 1 + flusher/flusher/hasura.py | 52 +++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 flusher/flusher/hasura.py diff --git a/docker-compose.yaml b/docker-compose.yaml index 0e7af624ba..dffb75a7cd 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -125,7 +125,7 @@ services: image: bandchain_flusher:latest networks: bandchain: - command: sh -c "sleep 30 && python main.py init bandchain test --db postgres:postgrespassword@172.18.0.88:5432/postgres" + command: sh -c "sleep 30 && python main.py init bandchain test --db postgres:postgrespassword@172.18.0.88:5432/postgres && python main.py hasura postgrespassword --db postgres:postgrespassword@172.18.0.88:5432/postgres" flusher-daemon: image: bandchain_flusher:latest diff --git a/flusher/flusher/__init__.py b/flusher/flusher/__init__.py index 1357c2a203..4c631ea81d 100644 --- a/flusher/flusher/__init__.py +++ b/flusher/flusher/__init__.py @@ -1,2 +1,3 @@ import flusher.init import flusher.sync +import flusher.hasura diff --git a/flusher/flusher/hasura.py b/flusher/flusher/hasura.py new file mode 100644 index 0000000000..9b7f83e9ae --- /dev/null +++ b/flusher/flusher/hasura.py @@ -0,0 +1,52 @@ + +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"''') From 5fef6f2b018513e0faa4b32f7d75a53bc4dabb1b Mon Sep 17 00:00:00 2001 From: Nattharat Wiriyakulnan Date: Wed, 5 Aug 2020 17:47:40 +0700 Subject: [PATCH 2/4] update changelog --- CHANGELOG_UNRELEASED.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG_UNRELEASED.md b/CHANGELOG_UNRELEASED.md index 6005f4b17a..2671fae1bd 100644 --- a/CHANGELOG_UNRELEASED.md +++ b/CHANGELOG_UNRELEASED.md @@ -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 From 228abcf370b9df3350aec445e60f1a6649a85a28 Mon Sep 17 00:00:00 2001 From: Nattharat Wiriyakulnan Date: Wed, 7 Oct 2020 14:16:22 +0700 Subject: [PATCH 3/4] fix formating --- flusher/flusher/hasura.py | 43 +++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/flusher/flusher/hasura.py b/flusher/flusher/hasura.py index 9b7f83e9ae..6542d97873 100644 --- a/flusher/flusher/hasura.py +++ b/flusher/flusher/hasura.py @@ -1,10 +1,10 @@ - import click from .cli import cli from sqlalchemy import create_engine + @cli.command() @click.argument("password") @click.option( @@ -13,24 +13,23 @@ default="localhost:5432/postgres", show_default=True, ) -def hasura(password,db): +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;''') - + 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;''') + 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 + # 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;''') + 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 @@ -42,11 +41,15 @@ def hasura(password,db): # 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("""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"''') From 0f9ab38c322e7e6e082b8c8e53860ce455307180 Mon Sep 17 00:00:00 2001 From: Nattharat Wiriyakulnan Date: Wed, 7 Oct 2020 14:18:52 +0700 Subject: [PATCH 4/4] fix format --- flusher/flusher/hasura.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flusher/flusher/hasura.py b/flusher/flusher/hasura.py index 6542d97873..3068658315 100644 --- a/flusher/flusher/hasura.py +++ b/flusher/flusher/hasura.py @@ -52,4 +52,4 @@ def hasura(password, db): 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"''') + engine.execute("""ALTER ROLE hasura SET statement_timeout='5s'""")