Improved SFW and tag media seed file to allow updates.

This commit is contained in:
DebaucheryLibrarian 2020-09-05 02:57:24 +02:00
parent bba73c4f31
commit c96e10b33d
3 changed files with 25 additions and 28 deletions

View File

@ -41,7 +41,7 @@ function initTagsActions(store, _router) {
}
}
}
photos: tagsPhotos {
photos: tagsPhotos(orderBy: MEDIA_BY_MEDIA_ID__INDEX_ASC) {
media {
id
thumbnail

View File

@ -45,26 +45,21 @@ exports.up = knex => Promise.resolve()
table.text('comment');
table.text('group');
table.boolean('is_sfw')
.defaultTo(false);
table.unique('hash');
table.unique('source');
table.datetime('created_at')
.defaultTo(knex.fn.now());
}))
.then(() => knex.schema.createTable('media_sfw', (table) => {
table.text('id', 21)
.primary();
table.text('media_id', 21)
.references('id')
.inTable('media')
.unique();
}))
.then(() => { // eslint-disable-line arrow-body-style
// allow vim fold
return knex.raw(`
CREATE FUNCTION get_random_sfw_media_id() RETURNS varchar AS $$
SELECT media_id FROM media_sfw
SELECT id FROM media
WHERE is_sfw = true
ORDER BY random()
LIMIT 1;
$$ LANGUAGE sql STABLE;

View File

@ -1,4 +1,4 @@
const nanoid = require('nanoid/non-secure');
// const nanoid = require('nanoid/non-secure');
const upsert = require('../src/utils/upsert');
const sfw = Object.entries({
@ -577,6 +577,7 @@ const sfw = Object.entries({
thumbnail: `sfw/${category}/thumbs/${photo}.jpeg`,
lazy: `sfw/${category}/lazy/${photo}.jpeg`,
mime: 'image/jpeg',
is_sfw: true,
sfw_media_id: null,
group: category,
index,
@ -649,12 +650,12 @@ const tagPosters = [
['trainbang', 'poster', 'Kali Roses in "Passing Me Around" for Blacked'],
['tap', 'poster', 'Kristy Black in SZ1986 for LegalPorno'],
]
.map(([slug, filename, comment], index) => ({
id: nanoid(),
.map(([slug, fileIndex, comment], index) => ({
id: `${slug}-${fileIndex}`,
tagSlug: slug,
path: `tags/${slug}/${filename}.jpeg`,
thumbnail: `tags/${slug}/thumbs/${filename}.jpeg`,
lazy: `tags/${slug}/lazy/${filename}.jpeg`,
path: `tags/${slug}/${fileIndex}.jpeg`,
thumbnail: `tags/${slug}/thumbs/${fileIndex}.jpeg`,
lazy: `tags/${slug}/lazy/${fileIndex}.jpeg`,
mime: 'image/jpeg',
index,
comment,
@ -755,18 +756,9 @@ const tagPhotos = [
/* eslint-disable max-len */
exports.seed = knex => Promise.resolve()
.then(async () => {
const { inserted } = await upsert('media', sfw, 'id');
await upsert('media', sfw, 'id');
const sfwMediaIds = inserted.map(mediaEntry => ({
id: nanoid(),
media_id: mediaEntry.id,
}));
await upsert('media_sfw', sfwMediaIds, 'media_id');
})
.then(async () => {
const tagMedia = tagPosters.concat(tagPhotos);
const tags = await knex('tags').whereIn('slug', tagMedia.map(item => item.tagSlug));
const { inserted, updated } = await upsert('media', tagMedia.map(({
@ -788,8 +780,18 @@ exports.seed = knex => Promise.resolve()
media_id: mediaIdsByPath[photo.path],
}));
return Promise.all([
await Promise.all([
upsert('tags_posters', tagPosterEntries, 'tag_id', knex),
upsert('tags_photos', tagPhotoEntries, ['tag_id', 'media_id'], knex),
]);
// clean up (re)moved tag media
await Promise.all([
knex('tags_posters')
.whereNotIn('media_id', tagPosters.map(photo => photo.id))
.delete(),
knex('tags_photos')
.whereNotIn('media_id', tagPhotos.map(photo => photo.id))
.delete(),
]);
});