2017-05-22 14:58:25 -04:00
|
|
|
import * as Sequelize from 'sequelize'
|
|
|
|
|
|
|
|
import { addMethodsToModel } from './utils'
|
|
|
|
import {
|
|
|
|
ApplicationClass,
|
|
|
|
ApplicationAttributes,
|
|
|
|
ApplicationInstance,
|
|
|
|
|
|
|
|
ApplicationMethods
|
|
|
|
} from './application-interface'
|
|
|
|
|
|
|
|
let Application: Sequelize.Model<ApplicationInstance, ApplicationAttributes>
|
|
|
|
let loadMigrationVersion: ApplicationMethods.LoadMigrationVersion
|
|
|
|
let updateMigrationVersion: ApplicationMethods.UpdateMigrationVersion
|
|
|
|
|
2017-06-11 11:35:32 -04:00
|
|
|
export default function defineApplication (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
|
2017-05-22 14:58:25 -04:00
|
|
|
Application = sequelize.define<ApplicationInstance, ApplicationAttributes>('Application',
|
2016-12-11 15:50:51 -05:00
|
|
|
{
|
2016-12-25 03:44:57 -05:00
|
|
|
migrationVersion: {
|
2016-12-11 15:50:51 -05:00
|
|
|
type: DataTypes.INTEGER,
|
2016-12-28 09:49:23 -05:00
|
|
|
defaultValue: 0,
|
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
|
|
|
isInt: true
|
|
|
|
}
|
2016-12-11 15:50:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
const classMethods = [ loadMigrationVersion, updateMigrationVersion ]
|
|
|
|
addMethodsToModel(Application, classMethods)
|
|
|
|
|
2016-12-11 15:50:51 -05:00
|
|
|
return Application
|
|
|
|
}
|
2016-09-26 16:36:36 -04:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
loadMigrationVersion = function (callback: ApplicationMethods.LoadMigrationVersionCallback) {
|
2016-12-11 15:50:51 -05:00
|
|
|
const query = {
|
2016-12-25 03:44:57 -05:00
|
|
|
attributes: [ 'migrationVersion' ]
|
2016-09-26 16:36:36 -04:00
|
|
|
}
|
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
return Application.findOne(query).asCallback(function (err, data) {
|
2017-03-04 04:40:09 -05:00
|
|
|
const version = data ? data.migrationVersion : null
|
2016-09-26 16:36:36 -04:00
|
|
|
|
|
|
|
return callback(err, version)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
updateMigrationVersion = function (newVersion: number, transaction: Sequelize.Transaction, callback: ApplicationMethods.UpdateMigrationVersionCallback) {
|
2017-05-22 14:58:25 -04:00
|
|
|
const options: Sequelize.UpdateOptions = {
|
2017-06-10 16:15:25 -04:00
|
|
|
where: {},
|
|
|
|
transaction: transaction
|
2016-12-25 03:44:57 -05:00
|
|
|
}
|
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
return Application.update({ migrationVersion: newVersion }, options).asCallback(callback)
|
2016-09-26 16:36:36 -04:00
|
|
|
}
|