Split DB_INIT into pragmas and full init, like Etiquette.

This commit is contained in:
voussoir 2020-04-03 11:23:51 -07:00
parent 11a147fbcd
commit 1bc3df5efe

View file

@ -7,6 +7,7 @@ from . import exceptions
from . import helpers from . import helpers
from . import ytapi from . import ytapi
from voussoirkit import pathclass
from voussoirkit import sqlhelpers from voussoirkit import sqlhelpers
@ -21,10 +22,18 @@ logging.getLogger('requests.packages.urllib3.connectionpool').setLevel(logging.W
logging.getLogger('requests.packages.urllib3.util.retry').setLevel(logging.WARNING) logging.getLogger('requests.packages.urllib3.util.retry').setLevel(logging.WARNING)
DATABASE_VERSION = 4 DATABASE_VERSION = 4
DB_INIT = ''' DB_VERSION_PRAGMA = '''
PRAGMA user_version = {user_version};
'''
DB_PRAGMAS = '''
PRAGMA count_changes = OFF; PRAGMA count_changes = OFF;
PRAGMA cache_size = 10000; PRAGMA cache_size = 10000;
PRAGMA user_version = {user_version}; '''
DB_INIT = f'''
BEGIN;
----------------------------------------------------------------------------------------------------
{DB_PRAGMAS}
{DB_VERSION_PRAGMA}
CREATE TABLE IF NOT EXISTS channels( CREATE TABLE IF NOT EXISTS channels(
id TEXT, id TEXT,
name TEXT, name TEXT,
@ -49,6 +58,8 @@ CREATE INDEX IF NOT EXISTS index_video_author_download on videos(author_id, down
CREATE INDEX IF NOT EXISTS index_video_id on videos(id); CREATE INDEX IF NOT EXISTS index_video_id on videos(id);
CREATE INDEX IF NOT EXISTS index_video_published on videos(published); CREATE INDEX IF NOT EXISTS index_video_published on videos(published);
CREATE INDEX IF NOT EXISTS index_video_download on videos(download); CREATE INDEX IF NOT EXISTS index_video_download on videos(download);
----------------------------------------------------------------------------------------------------
COMMIT;
'''.format(user_version=DATABASE_VERSION) '''.format(user_version=DATABASE_VERSION)
SQL_CHANNEL_COLUMNS = [ SQL_CHANNEL_COLUMNS = [
@ -84,29 +95,53 @@ def assert_is_abspath(path):
class YCDLDB: class YCDLDB:
def __init__(self, youtube, database_filename=None, youtube_dl_function=None): def __init__(
self,
youtube,
database_filename=None,
youtube_dl_function=None,
skip_version_check=False,
):
self.youtube = youtube self.youtube = youtube
if database_filename is None: if database_filename is None:
database_filename = DEFAULT_DBNAME database_filename = DEFAULT_DBNAME
existing_database = os.path.exists(database_filename) self.database_filepath = pathclass.Path(database_filename)
existing_database = self.database_filepath.exists
self.sql = sqlite3.connect(database_filename) self.sql = sqlite3.connect(database_filename)
self.cur = self.sql.cursor() self.cur = self.sql.cursor()
if existing_database: if existing_database:
self.cur.execute('PRAGMA user_version') if not skip_version_check:
existing_version = self.cur.fetchone()[0] self._check_version()
if existing_version != DATABASE_VERSION: self._load_pragmas()
raise exceptions.DatabaseOutOfDate(current=existing_version, new=DATABASE_VERSION) else:
self._first_time_setup()
if youtube_dl_function: if youtube_dl_function:
self.youtube_dl_function = youtube_dl_function self.youtube_dl_function = youtube_dl_function
else: else:
self.youtube_dl_function = YOUTUBE_DL_COMMAND self.youtube_dl_function = YOUTUBE_DL_COMMAND
statements = DB_INIT.split(';') def _check_version(self):
for statement in statements: '''
self.cur.execute(statement) Compare database's user_version against DATABASE_VERSION,
raising exceptions.DatabaseOutOfDate if not correct.
'''
existing = self.sql.execute('PRAGMA user_version').fetchone()[0]
if existing != DATABASE_VERSION:
raise exceptions.DatabaseOutOfDate(
existing=existing,
new=DATABASE_VERSION,
filepath=self.database_filepath,
)
def _first_time_setup(self):
self.sql.executescript(DB_INIT)
self.sql.commit()
def _load_pragmas(self):
self.sql.executescript(DB_PRAGMAS)
self.sql.commit() self.sql.commit()
def add_channel( def add_channel(