]>
Commit | Line | Data |
---|---|---|
1 | 'use strict' | |
2 | ||
3 | module.exports = function (sequelize, DataTypes) { | |
4 | const Application = sequelize.define('Application', | |
5 | { | |
6 | migrationVersion: { | |
7 | type: DataTypes.INTEGER, | |
8 | defaultValue: 0, | |
9 | allowNull: false, | |
10 | validate: { | |
11 | isInt: true | |
12 | } | |
13 | } | |
14 | }, | |
15 | { | |
16 | classMethods: { | |
17 | loadMigrationVersion, | |
18 | updateMigrationVersion | |
19 | } | |
20 | } | |
21 | ) | |
22 | ||
23 | return Application | |
24 | } | |
25 | ||
26 | // --------------------------------------------------------------------------- | |
27 | ||
28 | function loadMigrationVersion (callback) { | |
29 | const query = { | |
30 | attributes: [ 'migrationVersion' ] | |
31 | } | |
32 | ||
33 | return this.findOne(query).asCallback(function (err, data) { | |
34 | const version = data ? data.migrationVersion : 0 | |
35 | ||
36 | return callback(err, version) | |
37 | }) | |
38 | } | |
39 | ||
40 | function updateMigrationVersion (newVersion, transaction, callback) { | |
41 | const options = { | |
42 | where: {} | |
43 | } | |
44 | ||
45 | if (!callback) { | |
46 | transaction = callback | |
47 | } else { | |
48 | options.transaction = transaction | |
49 | } | |
50 | ||
51 | return this.update({ migrationVersion: newVersion }, options).asCallback(callback) | |
52 | } |