Commit 416af402 authored by nanahira's avatar nanahira

update transaction

parent f308299e
import moment from "moment"; import moment from "moment";
import bunyan from "bunyan"; import bunyan from "bunyan";
import {Connection, ConnectionOptions, createConnection} from "typeorm"; import {Connection, ConnectionOptions, createConnection, EntityManager} from "typeorm";
import {CloudReplay} from "./entities/CloudReplay"; import {CloudReplay} from "./entities/CloudReplay";
import {CloudReplayPlayer} from "./entities/CloudReplayPlayer"; import {CloudReplayPlayer} from "./entities/CloudReplayPlayer";
import {Ban} from "./entities/Ban"; import {Ban} from "./entities/Ban";
...@@ -43,6 +43,23 @@ export class DataManager { ...@@ -43,6 +43,23 @@ export class DataManager {
this.ready = false; this.ready = false;
this.log = log; this.log = log;
} }
private async transaction(fun: (mdb: EntityManager) => Promise<boolean>) {
const runner = await this.db.createQueryRunner();
await runner.connect();
await runner.startTransaction();
let result = false;
try {
result = await fun(runner.manager);
} catch(e) {
result = false;
this.log.warn(`Failed running transaction: ${e.toString()}`)
}
if(result) {
await runner.commitTransaction();
} else {
await runner.rollbackTransaction();
}
}
async init() { async init() {
this.db = await createConnection({ this.db = await createConnection({
type: "mysql", type: "mysql",
...@@ -99,15 +116,17 @@ export class DataManager { ...@@ -99,15 +116,17 @@ export class DataManager {
const player = CloudReplayPlayer.fromPlayerInfo(p); const player = CloudReplayPlayer.fromPlayerInfo(p);
return player; return player;
}); });
await this.db.transaction(async (mdb) => { await this.transaction(async (mdb) => {
try { try {
const nreplay = await mdb.save(replay); const nreplay = await mdb.save(replay);
for (let player of players) { for (let player of players) {
player.cloudReplay = nreplay; player.cloudReplay = nreplay;
} }
await mdb.save(players); await mdb.save(players);
return true;
} catch (e) { } catch (e) {
this.log.warn(`Failed to save replay R#${replay.id}: ${e.toString()}`); this.log.warn(`Failed to save replay R#${replay.id}: ${e.toString()}`);
return false;
} }
}); });
} }
...@@ -262,7 +281,6 @@ export class DataManager { ...@@ -262,7 +281,6 @@ export class DataManager {
return allDuelLogs.map(duelLog => duelLog.replayFileName); return allDuelLogs.map(duelLog => duelLog.replayFileName);
} }
async clearDuelLog() { async clearDuelLog() {
//await this.db.transaction(async (mdb) => {
const runner = this.db.createQueryRunner(); const runner = this.db.createQueryRunner();
try { try {
await runner.connect(); await runner.connect();
...@@ -276,7 +294,6 @@ export class DataManager { ...@@ -276,7 +294,6 @@ export class DataManager {
await runner.rollbackTransaction(); await runner.rollbackTransaction();
this.log.warn(`Failed to clear duel logs: ${e.toString()}`); this.log.warn(`Failed to clear duel logs: ${e.toString()}`);
} }
//});
} }
async saveDuelLog(name: string, roomId: number, cloudReplayId: number, replayFilename: string, roomMode: number, duelCount: number, playerInfos: DuelLogPlayerInfo[]) { async saveDuelLog(name: string, roomId: number, cloudReplayId: number, replayFilename: string, roomMode: number, duelCount: number, playerInfos: DuelLogPlayerInfo[]) {
const duelLog = new DuelLog(); const duelLog = new DuelLog();
...@@ -288,15 +305,17 @@ export class DataManager { ...@@ -288,15 +305,17 @@ export class DataManager {
duelLog.roomMode = roomMode; duelLog.roomMode = roomMode;
duelLog.duelCount = duelCount; duelLog.duelCount = duelCount;
const players = playerInfos.map(p => DuelLogPlayer.fromDuelLogPlayerInfo(p)); const players = playerInfos.map(p => DuelLogPlayer.fromDuelLogPlayerInfo(p));
await this.db.transaction(async (mdb) => { await this.transaction(async (mdb) => {
try { try {
const savedDuelLog = await mdb.save(duelLog); const savedDuelLog = await mdb.save(duelLog);
for (let player of players) { for (let player of players) {
player.duelLog = savedDuelLog; player.duelLog = savedDuelLog;
} }
await mdb.save(players); await mdb.save(players);
return true;
} catch (e) { } catch (e) {
this.log.warn(`Failed to save duel log ${name}: ${e.toString()}`); this.log.warn(`Failed to save duel log ${name}: ${e.toString()}`);
return false;
} }
}); });
...@@ -344,7 +363,7 @@ export class DataManager { ...@@ -344,7 +363,7 @@ export class DataManager {
} }
async migrateChatColors(data: any) { async migrateChatColors(data: any) {
await this.db.transaction(async (mdb) => { await this.transaction(async (mdb) => {
try { try {
const users: User[] = []; const users: User[] = [];
for(let key in data) { for(let key in data) {
...@@ -358,9 +377,10 @@ export class DataManager { ...@@ -358,9 +377,10 @@ export class DataManager {
users.push(user); users.push(user);
} }
await mdb.save(users); await mdb.save(users);
return true;
} catch (e) { } catch (e) {
this.log.warn(`Failed to migrate chat color data: ${e.toString()}`); this.log.warn(`Failed to migrate chat color data: ${e.toString()}`);
return null; return false;
} }
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment