Metaのアクセスでトランザクションを張るように (#4720)

* admin/instanceでmetaをキャッシュしないように

* Metaのアクセスにトランザクションをかける
This commit is contained in:
MeiMei 2019-04-17 00:45:33 +09:00 committed by syuilo
parent fc27890f13
commit b186504718
3 changed files with 31 additions and 17 deletions

View file

@ -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;

View file

@ -1,13 +1,21 @@
import { Meta } from '../models/entities/meta';
import { Metas } from '../models';
import { getConnection } from 'typeorm';
export default async function(): Promise<Meta> {
const meta = await Metas.findOne();
return await getConnection().transaction(async transactionalEntityManager => {
// バグでレコードが複数出来てしまっている可能性があるので新しいIDを優先する
const meta = await transactionalEntityManager.findOne(Meta, {
order: {
id: 'DESC'
}
});
if (meta) {
return meta;
} else {
return Metas.save({
return await transactionalEntityManager.save(Meta, {
id: 'x'
} as Meta);
}) as Meta;
}
});
}

View file

@ -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();
if (meta) {
await Metas.update(meta.id, set);
} else {
await Metas.save(set);
await getConnection().transaction(async transactionalEntityManager => {
const meta = await transactionalEntityManager.findOne(Meta, {
order: {
id: 'DESC'
}
});
if (meta) {
await transactionalEntityManager.update(Meta, meta.id, set);
} else {
await transactionalEntityManager.save(Meta, set);
}
});
});