Fix setting up schema in createdb and postgres params (#189)

This commit is contained in:
Jonathan Zernik 2020-07-31 22:08:19 -07:00 committed by GitHub
parent d9783d8c9a
commit d8da438247
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 32 deletions

View file

@ -1,3 +1,8 @@
CREATE DATABASE squeakserver;
\connect squeakserver;
CREATE SCHEMA IF NOT EXISTS mainnet;
CREATE SCHEMA IF NOT EXISTS testnet;
CREATE SCHEMA IF NOT EXISTS regtest;
CREATE SCHEMA IF NOT EXISTS simnet;

View file

@ -71,12 +71,13 @@ def load_admin_handler(lightning_client, squeak_node):
return SqueakAdminServerHandler(lightning_client, squeak_node,)
def load_db_params(config):
return parse_db_params(config)
def load_postgres_db(config):
def load_db_params(config, schema):
db_params = parse_db_params(config)
db_params['options'] = "--search_path={}".format(schema)
return db_params
def load_postgres_db(db_params):
return PostgresDb(db_params)
@ -144,14 +145,13 @@ def run_server(config):
SelectParams(network)
# load the db params
db_params = load_db_params(config)
db_params = load_db_params(config, network)
logger.info("db params: " + str(db_params))
# load postgres db
postgres_db = load_postgres_db(config)
postgres_db = load_postgres_db(db_params)
logger.info("postgres_db: " + str(postgres_db))
postgres_db.get_version()
postgres_db.use_schema(network)
postgres_db.init()
# load the price

View file

@ -18,12 +18,8 @@ logger = logging.getLogger(__name__)
class PostgresDb:
def __init__(self, params):
logger.info("Starting connection pool with params: {}".format(params))
self.connection_pool = pool.ThreadedConnectionPool(5, 20, **params)
self.params = params
@property
def user(self):
return self.params['user']
# Get Cursor
@contextmanager
@ -46,25 +42,6 @@ class PostgresDb:
db_version = curs.fetchone()
logger.info(db_version)
def use_schema(self, schema_name):
""" Create the schema for the given name. """
create_schema_sql = sql.SQL("CREATE SCHEMA IF NOT EXISTS {};").format(
sql.Identifier(schema_name)
)
use_schema_sql = sql.SQL("SET search_path TO {};").format(
sql.Identifier(schema_name),
)
use_schema_for_user_sql = sql.SQL("ALTER ROLE {} SET search_path TO {};").format(
sql.Identifier(self.user),
sql.Identifier(schema_name),
)
with self.get_cursor() as curs:
# execute a statement
logger.info("Creating schema: {}".format(schema_name))
curs.execute(create_schema_sql)
curs.execute(use_schema_sql)
curs.execute(use_schema_for_user_sql)
def init(self):
""" Create the tables and indices in the database. """
with self.get_cursor() as curs: