From b1865047187bf6fc9d4ad3b56a0d4eb6b2817c2b Mon Sep 17 00:00:00 2001 From: MeiMei <30769358+mei23@users.noreply.github.com> Date: Wed, 17 Apr 2019 00:45:33 +0900 Subject: [PATCH] =?UTF-8?q?Meta=E3=81=AE=E3=82=A2=E3=82=AF=E3=82=BB?= =?UTF-8?q?=E3=82=B9=E3=81=A7=E3=83=88=E3=83=A9=E3=83=B3=E3=82=B6=E3=82=AF?= =?UTF-8?q?=E3=82=B7=E3=83=A7=E3=83=B3=E3=82=92=E5=BC=B5=E3=82=8B=E3=82=88?= =?UTF-8?q?=E3=81=86=E3=81=AB=20(#4720)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * admin/instanceでmetaをキャッシュしないように * Metaのアクセスにトランザクションをかける --- src/client/app/admin/views/instance.vue | 2 +- src/misc/fetch-meta.ts | 26 ++++++++++++------- src/server/api/endpoints/admin/update-meta.ts | 20 +++++++++----- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/client/app/admin/views/instance.vue b/src/client/app/admin/views/instance.vue index bc2a5fba8..cca4e5f66 100644 --- a/src/client/app/admin/views/instance.vue +++ b/src/client/app/admin/views/instance.vue @@ -195,7 +195,7 @@ export default Vue.extend({ }, created() { - this.$root.getMeta().then(meta => { + this.$root.getMeta(true).then(meta => { this.maintainerName = meta.maintainerName; this.maintainerEmail = meta.maintainerEmail; this.disableRegistration = meta.disableRegistration; diff --git a/src/misc/fetch-meta.ts b/src/misc/fetch-meta.ts index a459f0772..800f0b9e6 100644 --- a/src/misc/fetch-meta.ts +++ b/src/misc/fetch-meta.ts @@ -1,13 +1,21 @@ import { Meta } from '../models/entities/meta'; -import { Metas } from '../models'; +import { getConnection } from 'typeorm'; export default async function(): Promise { - const meta = await Metas.findOne(); - if (meta) { - return meta; - } else { - return Metas.save({ - id: 'x' - } as Meta); - } + return await getConnection().transaction(async transactionalEntityManager => { + // バグでレコードが複数出来てしまっている可能性があるので新しいIDを優先する + const meta = await transactionalEntityManager.findOne(Meta, { + order: { + id: 'DESC' + } + }); + + if (meta) { + return meta; + } else { + return await transactionalEntityManager.save(Meta, { + id: 'x' + }) as Meta; + } + }); } diff --git a/src/server/api/endpoints/admin/update-meta.ts b/src/server/api/endpoints/admin/update-meta.ts index e242ac71a..3c6380acb 100644 --- a/src/server/api/endpoints/admin/update-meta.ts +++ b/src/server/api/endpoints/admin/update-meta.ts @@ -1,6 +1,6 @@ import $ from 'cafy'; import define from '../../define'; -import { Metas } from '../../../../models'; +import { getConnection } from 'typeorm'; import { Meta } from '../../../../models/entities/meta'; export const meta = { @@ -505,11 +505,17 @@ export default define(meta, async (ps) => { set.swPrivateKey = ps.swPrivateKey; } - const meta = await Metas.findOne(); + await getConnection().transaction(async transactionalEntityManager => { + const meta = await transactionalEntityManager.findOne(Meta, { + order: { + id: 'DESC' + } + }); - if (meta) { - await Metas.update(meta.id, set); - } else { - await Metas.save(set); - } + if (meta) { + await transactionalEntityManager.update(Meta, meta.id, set); + } else { + await transactionalEntityManager.save(Meta, set); + } + }); });