From 3f6a520863c899b673fce7bb1b8974d3f741e2c1 Mon Sep 17 00:00:00 2001 From: Oleg Silkin Date: Tue, 21 May 2019 09:00:40 -0400 Subject: [PATCH] Adds db helpers to vcs --- schema/db_helpers.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 schema/db_helpers.py diff --git a/schema/db_helpers.py b/schema/db_helpers.py new file mode 100644 index 0000000..37f8717 --- /dev/null +++ b/schema/db_helpers.py @@ -0,0 +1,31 @@ +import logging +import sqlite3 + +from lbry_comment_server.settings import config + +logger = logging.getLogger(__name__) + + +def setup_database(db_path): + logger.info('Creating db schema from %s in %s', + config['PATH']['SCHEMA'], db_path) + with sqlite3.connect(db_path) as conn: + with open(config['PATH']['SCHEMA'], 'r') as ddl: + with conn: + conn.executescript(ddl.read()) + + +def teardown_database(db_path): + logger.info('Dropping all tables from %s', db_path) + with sqlite3.connect(db_path) as conn: + conn.executescript(""" + DROP VIEW IF EXISTS COMMENTS_ON_CLAIMS; + DROP VIEW IF EXISTS COMMENT_REPLIES; + DROP TABLE IF EXISTS COMMENT; + DROP TABLE IF EXISTS CHANNEL; + """) + + +def backup_database(conn: sqlite3.Connection, back_fp): + with sqlite3.connect(back_fp) as back: + conn.backup(back)