Init db on startup (#43)

* Create db tables on startup

* Move createdb script for itest to itest folder

* Fix teardown of dockercompose containers
This commit is contained in:
Jonathan Zernik 2020-06-11 17:44:23 -07:00 committed by GitHub
parent a9aabf7633
commit 278fa38db6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 16 additions and 9 deletions

View file

@ -1,4 +1,5 @@
include LICENSE
include createdb.sql
include init.sql
graft tests
global-exclude *.pyc

View file

@ -1,8 +1,4 @@
CREATE DATABASE squeakserver;
\connect squeakserver;
CREATE TABLE squeak (
CREATE TABLE IF NOT EXISTS squeak (
hash CHAR(64) PRIMARY KEY,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
nVersion INTEGER NOT NULL,
@ -22,5 +18,5 @@ CREATE TABLE squeak (
content CHAR(1120)
);
CREATE INDEX idx_squeak_address
CREATE INDEX IF NOT EXISTS idx_squeak_address
ON squeak(address);

3
itests/createdb.sql Normal file
View file

@ -0,0 +1,3 @@
CREATE DATABASE squeakserver;
\connect squeakserver;

View file

@ -87,7 +87,7 @@ services:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
volumes:
- ../init.sql:/docker-entrypoint-initdb.d/init.sql
- ./createdb.sql:/docker-entrypoint-initdb.d/init.sql
sqkserver:
image: sqkserver

View file

@ -2,8 +2,6 @@
valid_params = {'host', 'database', 'user', 'password'}
# valid_params = {'host', 'user', 'password'}
def parse_db_params(config, section='postgresql'):
# get section, default to postgresql

View file

@ -154,6 +154,7 @@ def run_server(config):
postgres_db = load_postgres_db(config)
print('postgres_db: ' + str(postgres_db), flush=True)
postgres_db.get_connection()
postgres_db.init()
print('starting lightning client here...', flush=True)
lightning_client = load_lightning_client(config)

View file

@ -30,6 +30,14 @@ class PostgresDb():
db_version = curs.fetchone()
print(db_version)
def init(self):
""" Create the tables and indices in the database. """
with psycopg2.connect(**self.params) as conn:
with conn.cursor() as curs:
# execute a statement
print('Setting up database tables...')
curs.execute(open("init.sql", "r").read())
def insert_squeak(self, squeak):
""" Insert a new squeak. """
sql = """