Browse Source

Upgrade Sequelize from v3 to v4

master
Menci 6 years ago
parent
commit
26d66ceef2
  1. 41
      app.js
  2. 11
      models/common.js
  3. 4
      models/contest_player.js
  4. 8
      models/contest_ranklist.js
  5. 3
      models/custom_test.js
  6. 7
      models/judge_state.js
  7. 4
      package.json

41
app.js

@ -88,11 +88,50 @@ global.syzoj = {
}, },
async connectDatabase() { async connectDatabase() {
let Sequelize = require('sequelize'); let Sequelize = require('sequelize');
let Op = Sequelize.Op;
let operatorsAliases = {
$eq: Op.eq,
$ne: Op.ne,
$gte: Op.gte,
$gt: Op.gt,
$lte: Op.lte,
$lt: Op.lt,
$not: Op.not,
$in: Op.in,
$notIn: Op.notIn,
$is: Op.is,
$like: Op.like,
$notLike: Op.notLike,
$iLike: Op.iLike,
$notILike: Op.notILike,
$regexp: Op.regexp,
$notRegexp: Op.notRegexp,
$iRegexp: Op.iRegexp,
$notIRegexp: Op.notIRegexp,
$between: Op.between,
$notBetween: Op.notBetween,
$overlap: Op.overlap,
$contains: Op.contains,
$contained: Op.contained,
$adjacent: Op.adjacent,
$strictLeft: Op.strictLeft,
$strictRight: Op.strictRight,
$noExtendRight: Op.noExtendRight,
$noExtendLeft: Op.noExtendLeft,
$and: Op.and,
$or: Op.or,
$any: Op.any,
$all: Op.all,
$values: Op.values,
$col: Op.col
};
this.db = new Sequelize(this.config.db.database, this.config.db.username, this.config.db.password, { this.db = new Sequelize(this.config.db.database, this.config.db.username, this.config.db.password, {
host: this.config.db.host, host: this.config.db.host,
dialect: this.config.db.dialect, dialect: this.config.db.dialect,
storage: this.config.db.storage ? this.utils.resolvePath(this.config.db.storage) : null, storage: this.config.db.storage ? this.utils.resolvePath(this.config.db.storage) : null,
logging: syzoj.production ? false : syzoj.log logging: syzoj.production ? false : syzoj.log,
operatorsAliases: operatorsAliases
}); });
global.Promise = Sequelize.Promise; global.Promise = Sequelize.Promise;
this.db.countQuery = async (sql, options) => (await this.db.query(`SELECT COUNT(*) FROM (${sql}) AS \`__tmp_table\``, options))[0][0]['COUNT(*)']; this.db.countQuery = async (sql, options) => (await this.db.query(`SELECT COUNT(*) FROM (${sql}) AS \`__tmp_table\``, options))[0][0]['COUNT(*)'];

11
models/common.js

@ -19,6 +19,8 @@
'use strict'; 'use strict';
let Sequelize = require('sequelize');
class Model { class Model {
constructor(record) { constructor(record) {
this.record = record; this.record = record;
@ -29,9 +31,9 @@ class Model {
let model = this.getModel(); let model = this.getModel();
let obj = JSON.parse(JSON.stringify(this.record.get({ plain: true }))); let obj = JSON.parse(JSON.stringify(this.record.get({ plain: true })));
for (let key in obj) { for (let key in obj) {
if (model.tableAttributes[key].json) { if (model.tableAttributes[key].type instanceof Sequelize.JSON) {
try { try {
this[key] = eval(`(${obj[key]})`); this[key] = JSON.parse(obj[key]);
} catch (e) { } catch (e) {
this[key] = {}; this[key] = {};
} }
@ -43,8 +45,7 @@ class Model {
let model = this.getModel(); let model = this.getModel();
let obj = JSON.parse(JSON.stringify(this.record.get({ plain: true }))); let obj = JSON.parse(JSON.stringify(this.record.get({ plain: true })));
for (let key in obj) { for (let key in obj) {
if (model.tableAttributes[key].json) obj[key] = JSON.stringify(this[key]); obj[key] = this[key];
else obj[key] = this[key];
} }
return obj; return obj;
} }
@ -76,7 +77,7 @@ class Model {
} }
static async fromID(id) { static async fromID(id) {
return this.fromRecord(this.model.findById(id)) return this.fromRecord(this.model.findByPk(id));
} }
static async findOne(options) { static async findOne(options) {

4
models/contest_player.js

@ -31,7 +31,7 @@ let model = db.define('contest_player', {
user_id: { type: Sequelize.INTEGER }, user_id: { type: Sequelize.INTEGER },
score: { type: Sequelize.INTEGER }, score: { type: Sequelize.INTEGER },
score_details: { type: Sequelize.TEXT, json: true }, score_details: { type: Sequelize.JSON },
time_spent: { type: Sequelize.INTEGER } time_spent: { type: Sequelize.INTEGER }
}, { }, {
timestamps: false, timestamps: false,
@ -53,7 +53,7 @@ class ContestPlayer extends Model {
contest_id: 0, contest_id: 0,
user_id: 0, user_id: 0,
score: 0, score: 0,
score_details: '{}', score_details: {},
time_spent: 0 time_spent: 0
}, val))); }, val)));
} }

8
models/contest_ranklist.js

@ -28,8 +28,8 @@ let ContestPlayer = syzoj.model('contest_player');
let model = db.define('contest_ranklist', { let model = db.define('contest_ranklist', {
id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true }, id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
ranking_params: { type: Sequelize.TEXT, json: true }, ranking_params: { type: Sequelize.JSON },
ranklist: { type: Sequelize.TEXT, json: true } ranklist: { type: Sequelize.JSON }
}, { }, {
timestamps: false, timestamps: false,
tableName: 'contest_ranklist' tableName: 'contest_ranklist'
@ -39,8 +39,8 @@ let Model = require('./common');
class ContestRanklist extends Model { class ContestRanklist extends Model {
static async create(val) { static async create(val) {
return ContestRanklist.fromRecord(ContestRanklist.model.build(Object.assign({ return ContestRanklist.fromRecord(ContestRanklist.model.build(Object.assign({
ranking_params: '{}', ranking_params: {},
ranklist: '{}' ranklist: {}
}, val))); }, val)));
} }

3
models/custom_test.js

@ -38,7 +38,7 @@ let model = db.define('custom_test', {
pending: { type: Sequelize.BOOLEAN }, pending: { type: Sequelize.BOOLEAN },
memory: { type: Sequelize.INTEGER }, memory: { type: Sequelize.INTEGER },
result: { type: Sequelize.TEXT('medium'), json: true }, result: { type: Sequelize.JSON },
user_id: { type: Sequelize.INTEGER }, user_id: { type: Sequelize.INTEGER },
@ -76,6 +76,7 @@ class CustomTest extends Model {
time: 0, time: 0,
memory: 0, memory: 0,
result: {},
status: 'Waiting', status: 'Waiting',
}, val))); }, val)));
} }

7
models/judge_state.js

@ -45,9 +45,9 @@ let model = db.define('judge_state', {
max_memory: { type: Sequelize.INTEGER }, max_memory: { type: Sequelize.INTEGER },
// For NOI contest // For NOI contest
compilation: { type: Sequelize.TEXT('medium'), json: true }, compilation: { type: Sequelize.JSON },
result: { type: Sequelize.TEXT('medium'), json: true }, result: { type: Sequelize.JSON },
user_id: { type: Sequelize.INTEGER }, user_id: { type: Sequelize.INTEGER },
@ -107,7 +107,8 @@ class JudgeState extends Model {
total_time: null, total_time: null,
max_memory: null, max_memory: null,
status: 'Unknown', status: 'Unknown',
result: null, compilation: {},
result: {},
task_id: randomstring.generate(10), task_id: randomstring.generate(10),
is_public: false is_public: false
}, val))); }, val)));

4
package.json

@ -46,7 +46,7 @@
"moment": "^2.15.0", "moment": "^2.15.0",
"msgpack-lite": "^0.1.26", "msgpack-lite": "^0.1.26",
"multer": "^1.2.0", "multer": "^1.2.0",
"mysql": "^2.11.1", "mysql2": "^1.6.2",
"node-7z": "^0.4.0", "node-7z": "^0.4.0",
"nodemailer": "^4.1.0", "nodemailer": "^4.1.0",
"pygmentize-bundled-cached": "^1.1.0", "pygmentize-bundled-cached": "^1.1.0",
@ -54,7 +54,7 @@
"request": "^2.74.0", "request": "^2.74.0",
"request-promise": "^4.1.1", "request-promise": "^4.1.1",
"sendmail": "^1.1.1", "sendmail": "^1.1.1",
"sequelize": "^3.24.3", "sequelize": "^4.41.0",
"session-file-store": "^1.0.0", "session-file-store": "^1.0.0",
"socket.io": "^2.0.3", "socket.io": "^2.0.3",
"sqlite3": "^3.1.4", "sqlite3": "^3.1.4",

Loading…
Cancel
Save