-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
working values file, added scripts for initializing the database and …
…working on fixing database seeding Signed-off-by: instamenta <[email protected]>
- Loading branch information
1 parent
e7a1083
commit a30a18e
Showing
5 changed files
with
274 additions
and
25 deletions.
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
examples/custom-mirror-node-database/scripts/init-001.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
cat > init1.sh << 'EOF' | ||
Check failure on line 1 in examples/custom-mirror-node-database/scripts/init-001.sh Codacy Production / Codacy Static Code Analysisexamples/custom-mirror-node-database/scripts/init-001.sh#L1
|
||
#!/bin/bash | ||
set -e | ||
export HEDERA_MIRROR_GRAPHQL_DB_HOST="localhost" | ||
export HEDERA_MIRROR_GRAPHQL_DB_NAME="mirror_node" | ||
export HEDERA_MIRROR_GRAPHQL_DB_PASSWORD="XXXXXXXXXXXX" | ||
export HEDERA_MIRROR_GRAPHQL_DB_USERNAME="mirror_graphql" | ||
export HEDERA_MIRROR_GRPC_DB_HOST="localhost" | ||
export HEDERA_MIRROR_GRPC_DB_NAME="mirror_node" | ||
export HEDERA_MIRROR_GRPC_DB_PASSWORD="XXXXXXXXXXXX" | ||
export HEDERA_MIRROR_GRPC_DB_USERNAME="mirror_grpc" | ||
export HEDERA_MIRROR_IMPORTER_DB_HOST="localhost" | ||
export HEDERA_MIRROR_IMPORTER_DB_NAME="mirror_node" | ||
export HEDERA_MIRROR_IMPORTER_DB_OWNER="mirror_node" | ||
export HEDERA_MIRROR_IMPORTER_DB_OWNERPASSWORD="XXXXXXXXXXXX" | ||
export HEDERA_MIRROR_IMPORTER_DB_PASSWORD="XXXXXXXXXXXX" | ||
export HEDERA_MIRROR_IMPORTER_DB_RESTPASSWORD="XXXXXXXXXXXX" | ||
export HEDERA_MIRROR_IMPORTER_DB_RESTUSERNAME="mirror_rest" | ||
export HEDERA_MIRROR_IMPORTER_DB_SCHEMA="public" | ||
export HEDERA_MIRROR_IMPORTER_DB_TEMPSCHEMA="temporary" | ||
export HEDERA_MIRROR_IMPORTER_DB_USERNAME="mirror_importer" | ||
export HEDERA_MIRROR_RESTJAVA_DB_HOST="localhost" | ||
export HEDERA_MIRROR_RESTJAVA_DB_NAME="mirror_node" | ||
export HEDERA_MIRROR_RESTJAVA_DB_PASSWORD="XXXXXXXXXXXX" | ||
export HEDERA_MIRROR_RESTJAVA_DB_USERNAME="mirror_rest_java" | ||
export HEDERA_MIRROR_REST_DB_HOST="localhost" | ||
export HEDERA_MIRROR_REST_DB_NAME="mirror_node" | ||
export HEDERA_MIRROR_REST_DB_PASSWORD="XXXXXXXXXXXX" | ||
export HEDERA_MIRROR_REST_DB_USERNAME="mirror_rest" | ||
export HEDERA_MIRROR_ROSETTA_DB_HOST="localhost" | ||
export HEDERA_MIRROR_ROSETTA_DB_NAME="mirror_node" | ||
export HEDERA_MIRROR_ROSETTA_DB_PASSWORD="XXXXXXXXXXXX" | ||
export HEDERA_MIRROR_ROSETTA_DB_USERNAME="mirror_rosetta" | ||
export HEDERA_MIRROR_WEB3_DB_HOST="localhost" | ||
export HEDERA_MIRROR_WEB3_DB_NAME="mirror_node" | ||
export HEDERA_MIRROR_WEB3_DB_PASSWORD="XXXXXXXXXXXX" | ||
export HEDERA_MIRROR_WEB3_DB_USERNAME="mirror_web3" | ||
PGHBACONF="/opt/bitnami/postgresql/conf/pg_hba.conf" | ||
if [[ -f "${PGHBACONF}" ]]; then | ||
cp "${PGHBACONF}" "${PGHBACONF}.bak" | ||
echo "local all all trust" > "${PGHBACONF}" | ||
pg_ctl reload | ||
fi | ||
psql -d "user=postgres connect_timeout=3" \ | ||
--set ON_ERROR_STOP=1 \ | ||
--set "dbName=${HEDERA_MIRROR_IMPORTER_DB_NAME}" \ | ||
--set "dbSchema=${HEDERA_MIRROR_IMPORTER_DB_SCHEMA}" \ | ||
--set "graphQLPassword=${HEDERA_MIRROR_GRAPHQL_DB_PASSWORD}" \ | ||
--set "graphQLUsername=${HEDERA_MIRROR_GRAPHQL_DB_USERNAME}" \ | ||
--set "grpcPassword=${HEDERA_MIRROR_GRPC_DB_PASSWORD}" \ | ||
--set "grpcUsername=${HEDERA_MIRROR_GRPC_DB_USERNAME}" \ | ||
--set "importerPassword=${HEDERA_MIRROR_IMPORTER_DB_PASSWORD}" \ | ||
--set "importerUsername=${HEDERA_MIRROR_IMPORTER_DB_USERNAME}" \ | ||
--set "ownerUsername=${HEDERA_MIRROR_IMPORTER_DB_OWNER}" \ | ||
--set "ownerPassword=${HEDERA_MIRROR_IMPORTER_DB_OWNERPASSWORD}" \ | ||
--set "restPassword=${HEDERA_MIRROR_IMPORTER_DB_RESTPASSWORD}" \ | ||
--set "restUsername=${HEDERA_MIRROR_IMPORTER_DB_RESTUSERNAME}" \ | ||
--set "restJavaPassword=${HEDERA_MIRROR_RESTJAVA_DB_PASSWORD}" \ | ||
--set "restJavaUsername=${HEDERA_MIRROR_RESTJAVA_DB_USERNAME}" \ | ||
--set "rosettaPassword=${HEDERA_MIRROR_ROSETTA_DB_PASSWORD}" \ | ||
--set "rosettaUsername=${HEDERA_MIRROR_ROSETTA_DB_USERNAME}" \ | ||
--set "web3Password=${HEDERA_MIRROR_WEB3_DB_PASSWORD}" \ | ||
--set "web3Username=${HEDERA_MIRROR_WEB3_DB_USERNAME}" \ | ||
--set "tempSchema=${HEDERA_MIRROR_IMPORTER_DB_TEMPSCHEMA}" <<__SQL__ | ||
-- Create database & owner | ||
create user :ownerUsername with login password :'ownerPassword'; | ||
create database :dbName with owner :ownerUsername; | ||
-- Create roles | ||
create role readonly; | ||
create role readwrite in role readonly; | ||
create role temporary_admin in role readwrite; | ||
-- Create users | ||
create user :graphQLUsername with login password :'graphQLPassword' in role readonly; | ||
create user :grpcUsername with login password :'grpcPassword' in role readonly; | ||
create user :importerUsername with login password :'importerPassword' in role readwrite admin :ownerUsername; | ||
create user :restJavaUsername with login password :'restJavaPassword' in role readonly; | ||
create user :rosettaUsername with login password :'rosettaPassword' in role readonly; | ||
create user :web3Username with login password :'web3Password' in role readonly; | ||
alter user :ownerUsername with createrole; | ||
-- Grant temp schema admin privileges | ||
grant temporary_admin to :ownerUsername; | ||
grant temporary_admin to :importerUsername; | ||
-- Add extensions | ||
\connect :dbName | ||
create extension if not exists btree_gist; | ||
create extension if not exists pg_stat_statements; | ||
create extension if not exists pg_trgm; | ||
-- Create schema | ||
\connect :dbName :ownerUsername | ||
create schema if not exists :dbSchema authorization :ownerUsername; | ||
grant usage on schema :dbSchema to public; | ||
revoke create on schema :dbSchema from public; | ||
-- Create temp table schema | ||
create schema if not exists :tempSchema authorization temporary_admin; | ||
grant usage on schema :tempSchema to public; | ||
revoke create on schema :tempSchema from public; | ||
-- Grant readonly privileges | ||
grant connect on database :dbName to readonly; | ||
grant select on all tables in schema :dbSchema, :tempSchema to readonly; | ||
grant select on all sequences in schema :dbSchema, :tempSchema to readonly; | ||
grant usage on schema :dbSchema, :tempSchema to readonly; | ||
alter default privileges in schema :dbSchema, :tempSchema grant select on tables to readonly; | ||
alter default privileges in schema :dbSchema, :tempSchema grant select on sequences to readonly; | ||
-- Grant readwrite privileges | ||
grant insert, update, delete on all tables in schema :dbSchema to readwrite; | ||
grant usage on all sequences in schema :dbSchema to readwrite; | ||
alter default privileges in schema :dbSchema grant insert, update, delete on tables to readwrite; | ||
alter default privileges in schema :dbSchema grant usage on sequences to readwrite; | ||
-- Alter search path | ||
\connect postgres postgres | ||
alter database :dbName set search_path = :dbSchema, public, :tempSchema; | ||
__SQL__ | ||
if [[ -f "${PGHBACONF}.bak" ]]; then | ||
mv "${PGHBACONF}.bak" "${PGHBACONF}" | ||
pg_ctl reload | ||
fi | ||
EOF | ||
chmod +x init1.sh | ||
./init1.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
cat > init2.sh << 'EOF' | ||
Check failure on line 1 in examples/custom-mirror-node-database/scripts/init-002.sh Codacy Production / Codacy Static Code Analysisexamples/custom-mirror-node-database/scripts/init-002.sh#L1
|
||
#!/bin/bash | ||
set -e | ||
# Define PostgreSQL host and credentials | ||
export PG_HOST="my-postgresql.database.svc.cluster.local" | ||
export PG_USER="postgres" | ||
export PG_PASSWORD="XXXXXXXXXXXX" | ||
export PG_DB="mirror_node" | ||
# Execute the SQL commands | ||
psql -h "$PG_HOST" -U "$PG_USER" -d "$PG_DB" <<EOF | ||
DO | ||
$$ | ||
BEGIN | ||
CREATE OR REPLACE FUNCTION if_role_not_exists(rolename name, sql text) RETURNS text AS | ||
$$ | ||
BEGIN | ||
IF NOT EXISTS (SELECT * FROM pg_roles WHERE rolname = rolename) THEN | ||
EXECUTE sql; | ||
RETURN format('Executed ''%s''', sql); | ||
ELSE | ||
RETURN format('Role ''%s'' already exists', rolename); | ||
END IF; | ||
END; | ||
$$ | ||
LANGUAGE plpgsql; | ||
-- Create owner | ||
PERFORM if_role_not_exists('postgres', 'CREATE USER postgres WITH CREATEROLE LOGIN PASSWORD ''XXXXXXXXXXXX'''); | ||
ALTER DATABASE mirror_node SET search_path = public, public, temporary; | ||
ALTER DATABASE mirror_node OWNER TO postgres; | ||
-- Create roles | ||
PERFORM if_role_not_exists('readonly', 'CREATE ROLE readonly'); | ||
PERFORM if_role_not_exists('readwrite', 'CREATE ROLE readwrite IN ROLE readonly'); | ||
PERFORM if_role_not_exists('temporary_admin', 'CREATE ROLE temporary_admin IN ROLE readwrite'); | ||
-- Create users | ||
PERFORM if_role_not_exists('mirror_graphql', 'CREATE USER mirror_graphql WITH LOGIN PASSWORD ''XXXXXXXXXXXX'' IN ROLE readonly'); | ||
PERFORM if_role_not_exists('mirror_grpc', 'CREATE USER mirror_grpc WITH LOGIN PASSWORD ''XXXXXXXXXXXX'' IN ROLE readonly'); | ||
PERFORM if_role_not_exists('mirror_importer', 'CREATE USER mirror_importer WITH LOGIN PASSWORD ''XXXXXXXXXXXX'' IN ROLE readwrite'); | ||
PERFORM if_role_not_exists('mirror_rest', 'CREATE USER mirror_rest WITH LOGIN PASSWORD ''XXXXXXXXXXXX'' IN ROLE readonly'); | ||
PERFORM if_role_not_exists('mirror_rest_java', 'CREATE USER mirror_rest_java WITH LOGIN PASSWORD ''XXXXXXXXXXXX'' IN ROLE readonly'); | ||
PERFORM if_role_not_exists('mirror_rosetta', 'CREATE USER mirror_rosetta WITH LOGIN PASSWORD ''XXXXXXXXXXXX'' IN ROLE readonly'); | ||
PERFORM if_role_not_exists('mirror_web3', 'CREATE USER mirror_web3 WITH LOGIN PASSWORD ''XXXXXXXXXXXX'' IN ROLE readonly'); | ||
-- Set statement timeouts | ||
ALTER USER mirror_graphql SET statement_timeout TO '10000'; | ||
ALTER USER mirror_grpc SET statement_timeout TO '10000'; | ||
ALTER USER mirror_rest SET statement_timeout TO '20000'; | ||
ALTER USER mirror_rest_java SET statement_timeout TO '20000'; | ||
ALTER USER mirror_rosetta SET statement_timeout TO '10000'; | ||
ALTER USER mirror_web3 SET statement_timeout TO '10000'; | ||
-- Grants | ||
GRANT temporary_admin TO postgres; | ||
GRANT temporary_admin TO mirror_importer; | ||
-- Create schema and permissions | ||
CREATE SCHEMA IF NOT EXISTS public AUTHORIZATION postgres; | ||
GRANT USAGE ON SCHEMA public TO public; | ||
REVOKE CREATE ON SCHEMA public FROM public; | ||
CREATE SCHEMA IF NOT EXISTS temporary AUTHORIZATION temporary_admin; | ||
GRANT USAGE ON SCHEMA temporary TO public; | ||
REVOKE CREATE ON SCHEMA temporary FROM public; | ||
-- Read-only privileges | ||
GRANT CONNECT ON DATABASE mirror_node TO readonly; | ||
GRANT SELECT ON ALL TABLES IN SCHEMA public, temporary TO readonly; | ||
GRANT USAGE ON SCHEMA public, temporary TO readonly; | ||
ALTER DEFAULT PRIVILEGES IN SCHEMA public, temporary GRANT SELECT ON TABLES TO readonly; | ||
-- Read-write privileges | ||
GRANT INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO readwrite; | ||
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT INSERT, UPDATE, DELETE ON TABLES TO readwrite; | ||
-- Extensions | ||
CREATE EXTENSION IF NOT EXISTS btree_gist; | ||
CREATE EXTENSION IF NOT EXISTS pg_stat_statements; | ||
CREATE EXTENSION IF NOT EXISTS pg_trgm; | ||
ALTER TYPE timestamptz OWNER TO postgres; | ||
END; | ||
$$; | ||
EOF | ||
./init1.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.