Adds db helpers to vcs
This commit is contained in:
parent
e9ba0cd843
commit
3f6a520863
1 changed files with 31 additions and 0 deletions
31
schema/db_helpers.py
Normal file
31
schema/db_helpers.py
Normal file
|
@ -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)
|
Loading…
Reference in a new issue