Move DB_INIT to constants; auto parse column names.

This commit is contained in:
voussoir 2018-01-13 16:48:48 -08:00
parent ec4b5b014b
commit 14750eafc5
2 changed files with 155 additions and 198 deletions

View file

@ -14,85 +14,142 @@ except converter.ffmpeg.FFMpegError:
FILENAME_BADCHARS = '\\/:*?<>|"' FILENAME_BADCHARS = '\\/:*?<>|"'
ALLOWED_ORDERBY_COLUMNS = [ # Note: Setting user_version pragma in init sequence is safe because it only
'extension', # happens after the out-of-date check occurs, so no chance of accidentally
'width', # overwriting it.
'height', DATABASE_VERSION = 8
'ratio', DB_INIT = '''
'area', PRAGMA count_changes = OFF;
'duration', PRAGMA cache_size = 10000;
'bytes', PRAGMA user_version = {user_version};
'created',
'tagged_at',
'random',
]
SQL_LASTID_COLUMNS = [ ----------------------------------------------------------------------------------------------------
'table', CREATE TABLE IF NOT EXISTS album_associated_directories(
'last_id', albumid TEXT,
] directory TEXT COLLATE NOCASE
SQL_ALBUM_DIRECTORY_COLUMNS = [ );
'albumid', CREATE INDEX IF NOT EXISTS index_album_associated_directories_albumid on
'directory', album_associated_directories(albumid);
] CREATE INDEX IF NOT EXISTS index_album_associated_directories_directory on
SQL_ALBUM_COLUMNS = [ album_associated_directories(directory);
'id', ----------------------------------------------------------------------------------------------------
'title', CREATE TABLE IF NOT EXISTS album_group_rel(
'description', parentid TEXT,
] memberid TEXT
SQL_BOOKMARK_COLUMNS = [ );
'id', CREATE INDEX IF NOT EXISTS index_album_group_rel_parentid on album_group_rel(parentid);
'title', CREATE INDEX IF NOT EXISTS index_album_group_rel_memberid on album_group_rel(memberid);
'url', ----------------------------------------------------------------------------------------------------
'author_id', CREATE TABLE IF NOT EXISTS album_photo_rel(
] albumid TEXT,
SQL_PHOTO_COLUMNS = [ photoid TEXT
'id', );
'filepath', CREATE INDEX IF NOT EXISTS index_album_photo_rel_albumid on album_photo_rel(albumid);
'override_filename', CREATE INDEX IF NOT EXISTS index_album_photo_rel_photoid on album_photo_rel(photoid);
'extension', ----------------------------------------------------------------------------------------------------
'width', CREATE TABLE IF NOT EXISTS albums(
'height', id TEXT,
'ratio', title TEXT,
'area', description TEXT
'duration', );
'bytes', CREATE INDEX IF NOT EXISTS index_albums_id on albums(id);
'created', ----------------------------------------------------------------------------------------------------
'thumbnail', CREATE TABLE IF NOT EXISTS bookmarks(
'tagged_at', id TEXT,
'author_id', title TEXT,
] url TEXT,
SQL_TAG_COLUMNS = [ author_id TEXT
'id', );
'name', CREATE INDEX IF NOT EXISTS index_bookmarks_id on bookmarks(id);
'description', CREATE INDEX IF NOT EXISTS index_bookmarks_author on bookmarks(author_id);
] ----------------------------------------------------------------------------------------------------
SQL_SYN_COLUMNS = [ CREATE TABLE IF NOT EXISTS id_numbers(
'name', tab TEXT,
'master', last_id TEXT
] );
SQL_ALBUMGROUP_COLUMNS = [ ----------------------------------------------------------------------------------------------------
'parentid', CREATE TABLE IF NOT EXISTS photo_tag_rel(
'memberid', photoid TEXT,
] tagid TEXT
SQL_ALBUMPHOTO_COLUMNS = [ );
'albumid', CREATE INDEX IF NOT EXISTS index_photo_tag_rel_photoid on photo_tag_rel(photoid);
'photoid', CREATE INDEX IF NOT EXISTS index_photo_tag_rel_tagid on photo_tag_rel(tagid);
] ----------------------------------------------------------------------------------------------------
SQL_PHOTOTAG_COLUMNS = [ CREATE TABLE IF NOT EXISTS photos(
'photoid', id TEXT,
'tagid', filepath TEXT COLLATE NOCASE,
] override_filename TEXT COLLATE NOCASE,
SQL_TAGGROUP_COLUMNS = [ extension TEXT,
'parentid', width INT,
'memberid', height INT,
] ratio REAL,
SQL_USER_COLUMNS = [ area INT,
'id', duration INT,
'username', bytes INT,
'password', created INT,
'created', thumbnail TEXT,
] tagged_at INT,
author_id TEXT
);
CREATE INDEX IF NOT EXISTS index_photos_id on photos(id);
CREATE INDEX IF NOT EXISTS index_photos_filepath on photos(filepath COLLATE NOCASE);
CREATE INDEX IF NOT EXISTS index_photos_override_filename on
photos(override_filename COLLATE NOCASE);
CREATE INDEX IF NOT EXISTS index_photos_created on photos(created);
CREATE INDEX IF NOT EXISTS index_photos_extension on photos(extension);
CREATE INDEX IF NOT EXISTS index_photos_author_id on photos(author_id);
----------------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS tag_group_rel(
parentid TEXT,
memberid TEXT
);
CREATE INDEX IF NOT EXISTS index_tag_group_rel_parentid on tag_group_rel(parentid);
CREATE INDEX IF NOT EXISTS index_tag_group_rel_memberid on tag_group_rel(memberid);
----------------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS tag_synonyms(
name TEXT,
mastername TEXT
);
CREATE INDEX IF NOT EXISTS index_tag_synonyms_name on tag_synonyms(name);
----------------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS tags(
id TEXT,
name TEXT,
description TEXT
);
CREATE INDEX IF NOT EXISTS index_tags_id on tags(id);
CREATE INDEX IF NOT EXISTS index_tags_name on tags(name);
----------------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS users(
id TEXT,
username TEXT COLLATE NOCASE,
password BLOB,
created INT
);
CREATE INDEX IF NOT EXISTS index_users_id on users(id);
CREATE INDEX IF NOT EXISTS index_users_username on users(username COLLATE NOCASE);
'''.format(user_version=DATABASE_VERSION)
def _extract_column_names(table):
statement = DB_INIT.split('CREATE TABLE IF NOT EXISTS %s(' % table)[1]
statement = statement.split(');')[0]
statement = statement.replace('\n', ' ')
columns = statement.split(',')
columns = [column.strip().split(' ')[0] for column in columns]
return columns
SQL_LASTID_COLUMNS = _extract_column_names('id_numbers')
SQL_ALBUM_DIRECTORY_COLUMNS = _extract_column_names('album_associated_directories')
SQL_ALBUM_COLUMNS = _extract_column_names('albums')
SQL_BOOKMARK_COLUMNS = _extract_column_names('bookmarks')
SQL_PHOTO_COLUMNS = _extract_column_names('photos')
SQL_TAG_COLUMNS = _extract_column_names('tags')
SQL_SYN_COLUMNS = _extract_column_names('tag_synonyms')
SQL_ALBUMGROUP_COLUMNS = _extract_column_names('album_group_rel')
SQL_ALBUMPHOTO_COLUMNS = _extract_column_names('album_photo_rel')
SQL_PHOTOTAG_COLUMNS = _extract_column_names('photo_tag_rel')
SQL_TAGGROUP_COLUMNS = _extract_column_names('tag_group_rel')
SQL_USER_COLUMNS = _extract_column_names('users')
_sql_dictify = lambda columns: {key:index for (index, key) in enumerate(columns)} _sql_dictify = lambda columns: {key:index for (index, key) in enumerate(columns)}
SQL_ALBUM = _sql_dictify(SQL_ALBUM_COLUMNS) SQL_ALBUM = _sql_dictify(SQL_ALBUM_COLUMNS)
@ -108,6 +165,19 @@ SQL_TAG = _sql_dictify(SQL_TAG_COLUMNS)
SQL_TAGGROUP = _sql_dictify(SQL_TAGGROUP_COLUMNS) SQL_TAGGROUP = _sql_dictify(SQL_TAGGROUP_COLUMNS)
SQL_USER = _sql_dictify(SQL_USER_COLUMNS) SQL_USER = _sql_dictify(SQL_USER_COLUMNS)
ALLOWED_ORDERBY_COLUMNS = [
'extension',
'width',
'height',
'ratio',
'area',
'duration',
'bytes',
'created',
'tagged_at',
'random',
]
# Errors and warnings # Errors and warnings
WARNING_MINMAX_INVALID = 'Field "{field}": "{value}" is not a valid request. Ignored.' WARNING_MINMAX_INVALID = 'Field "{field}": "{value}" is not a valid request. Ignored.'

View file

@ -27,123 +27,6 @@ from voussoirkit import sqlhelpers
logging.basicConfig() logging.basicConfig()
# Note: Setting user_version pragma in init sequence is safe because it only
# happens after the out-of-date check occurs, so no chance of accidentally
# overwriting it.
DATABASE_VERSION = 8
DB_INIT = '''
PRAGMA count_changes = OFF;
PRAGMA cache_size = 10000;
PRAGMA user_version = {user_version};
----------------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS album_associated_directories(
albumid TEXT,
directory TEXT COLLATE NOCASE
);
CREATE INDEX IF NOT EXISTS index_album_associated_directories_albumid on
album_associated_directories(albumid);
CREATE INDEX IF NOT EXISTS index_album_associated_directories_directory on
album_associated_directories(directory);
----------------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS album_group_rel(
parentid TEXT,
memberid TEXT
);
CREATE INDEX IF NOT EXISTS index_album_group_rel_parentid on album_group_rel(parentid);
CREATE INDEX IF NOT EXISTS index_album_group_rel_memberid on album_group_rel(memberid);
----------------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS album_photo_rel(
albumid TEXT,
photoid TEXT
);
CREATE INDEX IF NOT EXISTS index_album_photo_rel_albumid on album_photo_rel(albumid);
CREATE INDEX IF NOT EXISTS index_album_photo_rel_photoid on album_photo_rel(photoid);
----------------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS albums(
id TEXT,
title TEXT,
description TEXT
);
CREATE INDEX IF NOT EXISTS index_albums_id on albums(id);
----------------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS bookmarks(
id TEXT,
title TEXT,
url TEXT,
author_id TEXT
);
CREATE INDEX IF NOT EXISTS index_bookmarks_id on bookmarks(id);
CREATE INDEX IF NOT EXISTS index_bookmarks_author on bookmarks(author_id);
----------------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS id_numbers(
tab TEXT,
last_id TEXT
);
----------------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS photo_tag_rel(
photoid TEXT,
tagid TEXT
);
CREATE INDEX IF NOT EXISTS index_photo_tag_rel_photoid on photo_tag_rel(photoid);
CREATE INDEX IF NOT EXISTS index_photo_tag_rel_tagid on photo_tag_rel(tagid);
----------------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS photos(
id TEXT,
filepath TEXT COLLATE NOCASE,
override_filename TEXT COLLATE NOCASE,
extension TEXT,
width INT,
height INT,
ratio REAL,
area INT,
duration INT,
bytes INT,
created INT,
thumbnail TEXT,
tagged_at INT,
author_id TEXT
);
CREATE INDEX IF NOT EXISTS index_photos_id on photos(id);
CREATE INDEX IF NOT EXISTS index_photos_filepath on photos(filepath COLLATE NOCASE);
CREATE INDEX IF NOT EXISTS index_photos_override_filename on
photos(override_filename COLLATE NOCASE);
CREATE INDEX IF NOT EXISTS index_photos_created on photos(created);
CREATE INDEX IF NOT EXISTS index_photos_extension on photos(extension);
CREATE INDEX IF NOT EXISTS index_photos_author_id on photos(author_id);
----------------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS tag_group_rel(
parentid TEXT,
memberid TEXT
);
CREATE INDEX IF NOT EXISTS index_tag_group_rel_parentid on tag_group_rel(parentid);
CREATE INDEX IF NOT EXISTS index_tag_group_rel_memberid on tag_group_rel(memberid);
----------------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS tag_synonyms(
name TEXT,
mastername TEXT
);
CREATE INDEX IF NOT EXISTS index_tag_synonyms_name on tag_synonyms(name);
----------------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS tags(
id TEXT,
name TEXT,
description TEXT
);
CREATE INDEX IF NOT EXISTS index_tags_id on tags(id);
CREATE INDEX IF NOT EXISTS index_tags_name on tags(name);
----------------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS users(
id TEXT,
username TEXT COLLATE NOCASE,
password BLOB,
created INT
);
CREATE INDEX IF NOT EXISTS index_users_id on users(id);
CREATE INDEX IF NOT EXISTS index_users_username on users(username COLLATE NOCASE);
'''.format(user_version=DATABASE_VERSION)
def _helper_filenamefilter(subject, terms): def _helper_filenamefilter(subject, terms):
basename = subject.lower() basename = subject.lower()
return all(term in basename for term in terms) return all(term in basename for term in terms)
@ -1143,10 +1026,14 @@ class PhotoDB(PDBAlbumMixin, PDBBookmarkMixin, PDBPhotoMixin, PDBTagMixin, PDBUs
if existing_database: if existing_database:
cur.execute('PRAGMA user_version') cur.execute('PRAGMA user_version')
existing_version = cur.fetchone()[0] existing_version = cur.fetchone()[0]
if existing_version != DATABASE_VERSION: if existing_version != constants.DATABASE_VERSION:
raise exceptions.DatabaseOutOfDate(current=existing_version, new=DATABASE_VERSION) exc = exceptions.DatabaseOutOfDate(
current=existing_version,
new=constants.DATABASE_VERSION,
)
raise exc
statements = DB_INIT.split(';') statements = constants.DB_INIT.split(';')
for statement in statements: for statement in statements:
cur.execute(statement) cur.execute(statement)
self.sql.commit() self.sql.commit()