Add created column to albums, bookmarks, tags.

This commit is contained in:
voussoir 2021-01-07 23:05:43 -08:00
parent ca74e2e1d4
commit 49f3f7a208
4 changed files with 86 additions and 3 deletions

View file

@ -41,7 +41,7 @@ ffmpeg = _load_ffmpeg()
# Database ######################################################################################### # Database #########################################################################################
DATABASE_VERSION = 16 DATABASE_VERSION = 17
DB_VERSION_PRAGMA = f''' DB_VERSION_PRAGMA = f'''
PRAGMA user_version = {DATABASE_VERSION}; PRAGMA user_version = {DATABASE_VERSION};
''' '''
@ -72,6 +72,7 @@ CREATE TABLE IF NOT EXISTS albums(
id TEXT PRIMARY KEY NOT NULL, id TEXT PRIMARY KEY NOT NULL,
title TEXT, title TEXT,
description TEXT, description TEXT,
created INT,
author_id TEXT, author_id TEXT,
FOREIGN KEY(author_id) REFERENCES users(id) FOREIGN KEY(author_id) REFERENCES users(id)
); );
@ -82,6 +83,7 @@ CREATE TABLE IF NOT EXISTS bookmarks(
id TEXT PRIMARY KEY NOT NULL, id TEXT PRIMARY KEY NOT NULL,
title TEXT, title TEXT,
url TEXT, url TEXT,
created INT,
author_id TEXT, author_id TEXT,
FOREIGN KEY(author_id) REFERENCES users(id) FOREIGN KEY(author_id) REFERENCES users(id)
); );
@ -122,6 +124,7 @@ CREATE TABLE IF NOT EXISTS tags(
id TEXT PRIMARY KEY NOT NULL, id TEXT PRIMARY KEY NOT NULL,
name TEXT NOT NULL, name TEXT NOT NULL,
description TEXT, description TEXT,
created INT,
author_id TEXT, author_id TEXT,
FOREIGN KEY(author_id) REFERENCES users(id) FOREIGN KEY(author_id) REFERENCES users(id)
); );

View file

@ -273,6 +273,7 @@ class Album(ObjectBase, GroupableMixin):
self.id = db_row['id'] self.id = db_row['id']
self.title = self.normalize_title(db_row['title']) self.title = self.normalize_title(db_row['title'])
self.description = self.normalize_description(db_row['description']) self.description = self.normalize_description(db_row['description'])
self.created = db_row['created']
self.author_id = self.normalize_author_id(db_row['author_id']) self.author_id = self.normalize_author_id(db_row['author_id'])
self.group_getter_many = self.photodb.get_albums_by_id self.group_getter_many = self.photodb.get_albums_by_id
@ -520,6 +521,7 @@ class Album(ObjectBase, GroupableMixin):
'id': self.id, 'id': self.id,
'description': self.description, 'description': self.description,
'title': self.title, 'title': self.title,
'created': self.created,
'author': self.get_author().jsonify() if self.author_id else None, 'author': self.get_author().jsonify() if self.author_id else None,
} }
if not minimal: if not minimal:
@ -635,6 +637,7 @@ class Bookmark(ObjectBase):
self.id = db_row['id'] self.id = db_row['id']
self.title = self.normalize_title(db_row['title']) self.title = self.normalize_title(db_row['title'])
self.url = self.normalize_url(db_row['url']) self.url = self.normalize_url(db_row['url'])
self.created = db_row['created']
self.author_id = self.normalize_author_id(db_row['author_id']) self.author_id = self.normalize_author_id(db_row['author_id'])
def __repr__(self): def __repr__(self):
@ -714,6 +717,7 @@ class Bookmark(ObjectBase):
'id': self.id, 'id': self.id,
'author': self.get_author().jsonify() if self.author_id else None, 'author': self.get_author().jsonify() if self.author_id else None,
'url': self.url, 'url': self.url,
'created': self.created,
'title': self.title, 'title': self.title,
} }
return j return j
@ -1321,6 +1325,7 @@ class Tag(ObjectBase, GroupableMixin):
# from previous character / length rules. # from previous character / length rules.
self.name = db_row['name'] self.name = db_row['name']
self.description = self.normalize_description(db_row['description']) self.description = self.normalize_description(db_row['description'])
self.created = db_row['created']
self.author_id = self.normalize_author_id(db_row['author_id']) self.author_id = self.normalize_author_id(db_row['author_id'])
self.group_getter_many = self.photodb.get_tags_by_id self.group_getter_many = self.photodb.get_tags_by_id
@ -1543,6 +1548,7 @@ class Tag(ObjectBase, GroupableMixin):
'type': 'tag', 'type': 'tag',
'id': self.id, 'id': self.id,
'name': self.name, 'name': self.name,
'created': self.created,
} }
if not minimal: if not minimal:
j['author'] = self.get_author().jsonify() if self.author_id else None j['author'] = self.get_author().jsonify() if self.author_id else None

View file

@ -109,6 +109,7 @@ class PDBAlbumMixin:
'id': album_id, 'id': album_id,
'title': title, 'title': title,
'description': description, 'description': description,
'created': helpers.now(),
'author_id': author_id, 'author_id': author_id,
} }
self.sql_insert(table='albums', data=data) self.sql_insert(table='albums', data=data)
@ -196,9 +197,10 @@ class PDBBookmarkMixin:
data = { data = {
'id': bookmark_id, 'id': bookmark_id,
'author_id': author_id,
'title': title, 'title': title,
'url': url, 'url': url,
'created': helpers.now(),
'author_id': author_id,
} }
self.sql_insert(table='bookmarks', data=data) self.sql_insert(table='bookmarks', data=data)
@ -411,6 +413,10 @@ class PDBCacheManagerMixin:
yield thing yield thing
def get_things_by_sql(self, thing_type, query, bindings=None): def get_things_by_sql(self, thing_type, query, bindings=None):
'''
Use an arbitrary SQL query to select things from the database.
Your query should *only* select the id column.
'''
thing_rows = self.sql_select(query, bindings) thing_rows = self.sql_select(query, bindings)
thing_ids = (thing_id for (thing_id,) in thing_rows) thing_ids = (thing_id for (thing_id,) in thing_rows)
return self.get_things_by_id(thing_type, thing_ids) return self.get_things_by_id(thing_type, thing_ids)
@ -1283,6 +1289,7 @@ class PDBTagMixin:
'id': tag_id, 'id': tag_id,
'name': tagname, 'name': tagname,
'description': description, 'description': description,
'created': helpers.now(),
'author_id': author_id, 'author_id': author_id,
} }
self.sql_insert(table='tags', data=data) self.sql_insert(table='tags', data=data)
@ -1469,8 +1476,8 @@ class PDBUserMixin:
'id': user_id, 'id': user_id,
'username': username, 'username': username,
'password': hashed_password, 'password': hashed_password,
'created': helpers.now(),
'display_name': display_name, 'display_name': display_name,
'created': helpers.now(),
} }
self.sql_insert(table='users', data=data) self.sql_insert(table='users', data=data)

View file

@ -505,6 +505,73 @@ def upgrade_15_to_16(photodb):
basename = os.path.basename(filepath) basename = os.path.basename(filepath)
photodb.sql_execute('UPDATE photos SET basename = ? WHERE id == ?', [basename, id]) photodb.sql_execute('UPDATE photos SET basename = ? WHERE id == ?', [basename, id])
def upgrade_16_to_17(photodb):
'''
Added the created column to albums, bookmarks, tags.
'''
m = Migrator(photodb)
m.tables['albums']['create'] = '''
CREATE TABLE albums(
id TEXT PRIMARY KEY NOT NULL,
title TEXT,
description TEXT,
created INT,
author_id TEXT,
FOREIGN KEY(author_id) REFERENCES users(id)
);
'''
m.tables['albums']['transfer'] = '''
INSERT INTO albums SELECT
id,
title,
description,
0,
author_id
FROM albums_old;
'''
m.tables['bookmarks']['create'] = '''
CREATE TABLE bookmarks(
id TEXT PRIMARY KEY NOT NULL,
title TEXT,
url TEXT,
created INT,
author_id TEXT,
FOREIGN KEY(author_id) REFERENCES users(id)
);
'''
m.tables['bookmarks']['transfer'] = '''
INSERT INTO bookmarks SELECT
id,
title,
url,
0,
author_id
FROM bookmarks_old;
'''
m.tables['tags']['create'] = '''
CREATE TABLE tags(
id TEXT PRIMARY KEY NOT NULL,
name TEXT NOT NULL,
description TEXT,
created INT,
author_id TEXT,
FOREIGN KEY(author_id) REFERENCES users(id)
);
'''
m.tables['tags']['transfer'] = '''
INSERT INTO tags SELECT
id,
name,
description,
0,
author_id
FROM tags_old;
'''
m.go()
def upgrade_all(data_directory): def upgrade_all(data_directory):
''' '''
Given the directory containing a phototagger database, apply all of the Given the directory containing a phototagger database, apply all of the