mirror of
https://github.com/yzernik/squeaknode.git
synced 2026-08-13 12:33:25 +02:00
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:
parent
a9aabf7633
commit
278fa38db6
7 changed files with 16 additions and 9 deletions
|
|
@ -1,4 +1,5 @@
|
|||
include LICENSE
|
||||
include createdb.sql
|
||||
include init.sql
|
||||
graft tests
|
||||
global-exclude *.pyc
|
||||
|
|
|
|||
8
init.sql
8
init.sql
|
|
@ -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
3
itests/createdb.sql
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
CREATE DATABASE squeakserver;
|
||||
|
||||
\connect squeakserver;
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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 = """
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue