Add created timestamp column to the rel tables.

Could open new possibilities for tracking, ranking, or just
curiosity.
This commit is contained in:
voussoir 2022-10-29 14:21:06 -07:00
parent 172a539f24
commit 35dbdd27cf
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB
3 changed files with 137 additions and 10 deletions

View file

@ -41,7 +41,7 @@ ffmpeg = _load_ffmpeg()
# Database #########################################################################################
DATABASE_VERSION = 22
DATABASE_VERSION = 23
DB_INIT = f'''
CREATE TABLE IF NOT EXISTS albums(
@ -135,6 +135,7 @@ CREATE INDEX IF NOT EXISTS index_users_username on users(username COLLATE NOCASE
CREATE TABLE IF NOT EXISTS album_associated_directories(
albumid INT NOT NULL,
directory TEXT NOT NULL COLLATE NOCASE,
created INT,
FOREIGN KEY(albumid) REFERENCES albums(id)
);
CREATE INDEX IF NOT EXISTS index_album_associated_directories_albumid on
@ -145,6 +146,8 @@ CREATE INDEX IF NOT EXISTS index_album_associated_directories_directory on
CREATE TABLE IF NOT EXISTS album_group_rel(
parentid INT NOT NULL,
memberid INT NOT NULL,
created INT,
PRIMARY KEY(parentid, memberid),
FOREIGN KEY(parentid) REFERENCES albums(id),
FOREIGN KEY(memberid) REFERENCES albums(id)
);
@ -154,20 +157,19 @@ CREATE INDEX IF NOT EXISTS index_album_group_rel_memberid on album_group_rel(mem
CREATE TABLE IF NOT EXISTS album_photo_rel(
albumid INT NOT NULL,
photoid INT NOT NULL,
created INT,
PRIMARY KEY(albumid, photoid),
FOREIGN KEY(albumid) REFERENCES albums(id),
FOREIGN KEY(photoid) REFERENCES photos(id)
);
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 id_numbers(
tab TEXT NOT NULL,
last_id TEXT NOT NULL
);
----------------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS photo_tag_rel(
photoid INT NOT NULL,
tagid INT NOT NULL,
created INT,
PRIMARY KEY(photoid, tagid),
FOREIGN KEY(photoid) REFERENCES photos(id),
FOREIGN KEY(tagid) REFERENCES tags(id)
);
@ -178,6 +180,8 @@ CREATE INDEX IF NOT EXISTS index_photo_tag_rel_photoid_tagid on photo_tag_rel(ph
CREATE TABLE IF NOT EXISTS tag_group_rel(
parentid INT NOT NULL,
memberid INT NOT NULL,
created INT,
PRIMARY KEY(parentid, memberid),
FOREIGN KEY(parentid) REFERENCES tags(id),
FOREIGN KEY(memberid) REFERENCES tags(id)
);
@ -186,7 +190,8 @@ CREATE INDEX IF NOT EXISTS index_tag_group_rel_memberid on tag_group_rel(memberi
----------------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS tag_synonyms(
name TEXT NOT NULL,
mastername TEXT NOT NULL
mastername TEXT NOT NULL,
created INT
);
CREATE INDEX IF NOT EXISTS index_tag_synonyms_name on tag_synonyms(name);
'''

View file

@ -122,6 +122,7 @@ class GroupableMixin(metaclass=abc.ABCMeta):
data = {
'parentid': self.id,
'memberid': member.id,
'created': helpers.now().timestamp(),
}
self.photodb.insert(table=self.group_table, pairs=data)
@ -332,7 +333,11 @@ class Album(ObjectBase, GroupableMixin):
return
log.info('Adding directory "%s" to %s.', path.absolute_path, self)
data = {'albumid': self.id, 'directory': path.absolute_path}
data = {
'albumid': self.id,
'directory': path.absolute_path,
'created': helpers.now().timestamp(),
}
self.photodb.insert(table='album_associated_directories', pairs=data)
@decorators.required_feature('album.edit')
@ -375,7 +380,11 @@ class Album(ObjectBase, GroupableMixin):
def _add_photo(self, photo):
log.info('Adding photo %s to %s.', photo, self)
data = {'albumid': self.id, 'photoid': photo.id}
data = {
'albumid': self.id,
'photoid': photo.id,
'created': helpers.now().timestamp(),
}
self.photodb.insert(table='album_photo_rel', pairs=data)
@decorators.required_feature('album.edit')
@ -965,7 +974,8 @@ class Photo(ObjectBase):
data = {
'photoid': self.id,
'tagid': tag.id
'tagid': tag.id,
'created': helpers.now().timestamp(),
}
self.photodb.insert(table='photo_tag_rel', pairs=data)
data = {
@ -1661,6 +1671,7 @@ class Tag(ObjectBase, GroupableMixin):
data = {
'name': synname,
'mastername': self.name,
'created': helpers.now().timestamp(),
}
self.photodb.insert(table='tag_synonyms', pairs=data)

View file

@ -888,6 +888,117 @@ def upgrade_21_to_22(photodb):
photodb.execute('DROP INDEX IF EXISTS index_photos_override_filename')
photodb.execute('CREATE INDEX IF NOT EXISTS index_photos_basename on photos(basename COLLATE NOCASE)')
def upgrade_22_to_23(photodb):
'''
In this version, the rel tables received explicit primary keys and
a `created` column.
'''
m = Migrator(photodb)
m.tables['album_associated_directories']['create'] = '''
CREATE TABLE IF NOT EXISTS album_associated_directories(
albumid INT NOT NULL,
directory TEXT NOT NULL COLLATE NOCASE,
created INT,
FOREIGN KEY(albumid) REFERENCES albums(id)
);
'''
m.tables['album_associated_directories']['transfer'] = '''
INSERT INTO album_associated_directories SELECT
albumid,
directory,
(SELECT created FROM albums WHERE id=albumid)
FROM album_associated_directories_old;
'''
m.tables['album_group_rel']['create'] = '''
CREATE TABLE IF NOT EXISTS album_group_rel(
parentid INT NOT NULL,
memberid INT NOT NULL,
created INT,
PRIMARY KEY(parentid, memberid),
FOREIGN KEY(parentid) REFERENCES albums(id),
FOREIGN KEY(memberid) REFERENCES albums(id)
);
'''
m.tables['album_group_rel']['transfer'] = '''
INSERT INTO album_group_rel SELECT
parentid,
memberid,
MAX((SELECT created FROM albums WHERE id=parentid), (SELECT created FROM albums WHERE id=memberid))
FROM album_group_rel_old;
'''
m.tables['album_photo_rel']['create'] = '''
CREATE TABLE IF NOT EXISTS album_photo_rel(
albumid INT NOT NULL,
photoid INT NOT NULL,
created INT,
PRIMARY KEY(albumid, photoid),
FOREIGN KEY(albumid) REFERENCES albums(id),
FOREIGN KEY(photoid) REFERENCES photos(id)
);
'''
m.tables['album_photo_rel']['transfer'] = '''
INSERT INTO album_photo_rel SELECT
albumid,
photoid,
MAX((SELECT created FROM albums WHERE id=albumid), (SELECT created FROM photos WHERE id=photoid))
FROM album_photo_rel_old;
'''
m.tables['photo_tag_rel']['create'] = '''
CREATE TABLE IF NOT EXISTS photo_tag_rel(
photoid INT NOT NULL,
tagid INT NOT NULL,
created INT,
PRIMARY KEY(photoid, tagid),
FOREIGN KEY(photoid) REFERENCES photos(id),
FOREIGN KEY(tagid) REFERENCES tags(id)
);
'''
m.tables['photo_tag_rel']['transfer'] = '''
INSERT INTO photo_tag_rel SELECT
photoid,
tagid,
MAX((SELECT created FROM photos WHERE id=photoid), (SELECT created FROM tags WHERE id=tagid))
FROM photo_tag_rel_old;
'''
m.tables['tag_group_rel']['create'] = '''
CREATE TABLE IF NOT EXISTS tag_group_rel(
parentid INT NOT NULL,
memberid INT NOT NULL,
created INT,
PRIMARY KEY(parentid, memberid),
FOREIGN KEY(parentid) REFERENCES tags(id),
FOREIGN KEY(memberid) REFERENCES tags(id)
);
'''
m.tables['tag_group_rel']['transfer'] = '''
INSERT INTO tag_group_rel SELECT
parentid,
memberid,
MAX((SELECT created FROM tags WHERE id=parentid), (SELECT created FROM tags WHERE id=memberid))
FROM tag_group_rel_old;
'''
m.tables['tag_synonyms']['create'] = '''
CREATE TABLE IF NOT EXISTS tag_synonyms(
name TEXT NOT NULL,
mastername TEXT NOT NULL,
created INT
);
'''
m.tables['tag_synonyms']['transfer'] = '''
INSERT INTO tag_synonyms SELECT
name,
mastername,
(SELECT COALESCE(created, 0) FROM tags WHERE tags.name=name)
FROM tag_synonyms_old;
'''
m.go()
def upgrade_all(data_directory):
'''
Given the directory containing a phototagger database, apply all of the