From 9dc4b2f1510627299e6df3fe6d568d058c67d341 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Wed, 8 Nov 2017 07:25:21 -0800 Subject: [PATCH] added migration to change DOUBLE to DECIMAL --- migrations/ChangeAmountColumnType.js | 26 ------- migrations/ChangeCertificateColumnTypes.js | 74 ------------------- migrations/ChangeCertificateColumnTypes2.js | 42 +++++++++++ ...olumnType.js => ChangeClaimColumnTypes.js} | 42 +++++++---- migrations/ChangeHeightColumnType.js | 26 ------- migrations/ChangeValidAtHeightColumnType.js | 26 ------- models/certificate.js | 6 +- models/claim.js | 6 +- models/index.js | 17 +++-- 9 files changed, 87 insertions(+), 178 deletions(-) delete mode 100644 migrations/ChangeAmountColumnType.js delete mode 100644 migrations/ChangeCertificateColumnTypes.js create mode 100644 migrations/ChangeCertificateColumnTypes2.js rename migrations/{ChangeEffectiveAmountColumnType.js => ChangeClaimColumnTypes.js} (53%) delete mode 100644 migrations/ChangeHeightColumnType.js delete mode 100644 migrations/ChangeValidAtHeightColumnType.js diff --git a/migrations/ChangeAmountColumnType.js b/migrations/ChangeAmountColumnType.js deleted file mode 100644 index 434e10a3..00000000 --- a/migrations/ChangeAmountColumnType.js +++ /dev/null @@ -1,26 +0,0 @@ -module.exports = { - up: (queryInterface, Sequelize) => { - // logic for transforming into the new state - const p1 = queryInterface.changeColumn( - 'Claim', - 'amount', - { - type : Sequelize.DOUBLE, - allowNull: true, - } - ); - return Promise.all([p1]); - }, - down: (queryInterface, Sequelize) => { - // logic for reverting the changes - const p1 = queryInterface.changeColumn( - 'Claim', - 'amount', - { - type : Sequelize.STRING, - allowNull: true, - } - ); - return Promise.all([p1]); - }, -}; diff --git a/migrations/ChangeCertificateColumnTypes.js b/migrations/ChangeCertificateColumnTypes.js deleted file mode 100644 index 95e62ebf..00000000 --- a/migrations/ChangeCertificateColumnTypes.js +++ /dev/null @@ -1,74 +0,0 @@ -module.exports = { - up: (queryInterface, Sequelize) => { - // logic for transforming into the new state - const p1 = queryInterface.changeColumn( - 'Certificate', - 'amount', - { - type : Sequelize.DOUBLE, - allowNull: true, - } - ); - const p2 = queryInterface.changeColumn( - 'Certificate', - 'effectiveAmount', - { - type : Sequelize.DOUBLE, - allowNull: true, - } - ); - const p3 = queryInterface.changeColumn( - 'Certificate', - 'height', - { - type : Sequelize.INTEGER, - allowNull: true, - } - ); - const p4 = queryInterface.changeColumn( - 'Certificate', - 'validAtHeight', - { - type : Sequelize.INTEGER, - allowNull: true, - } - ); - return Promise.all([p1, p2, p3, p4]); - }, - down: (queryInterface, Sequelize) => { - // logic for reverting the changes - const p1 = queryInterface.changeColumn( - 'Certificate', - 'amount', - { - type : Sequelize.STRING, - allowNull: true, - } - ); - const p2 = queryInterface.changeColumn( - 'Certificate', - 'effectiveAmount', - { - type : Sequelize.STRING, - allowNull: true, - } - ); - const p3 = queryInterface.changeColumn( - 'Certificate', - 'height', - { - type : Sequelize.STRING, - allowNull: true, - } - ); - const p4 = queryInterface.changeColumn( - 'Certificate', - 'validAtHeight', - { - type : Sequelize.STRING, - allowNull: true, - } - ); - return Promise.all([p1, p2, p3, p4]); - }, -}; diff --git a/migrations/ChangeCertificateColumnTypes2.js b/migrations/ChangeCertificateColumnTypes2.js new file mode 100644 index 00000000..8fbcee79 --- /dev/null +++ b/migrations/ChangeCertificateColumnTypes2.js @@ -0,0 +1,42 @@ +module.exports = { + up: (queryInterface, Sequelize) => { + // logic for transforming into the new state + const p1 = queryInterface.changeColumn( + 'Certificate', + 'amount', + { + type : Sequelize.DECIMAL(19, 8), + allowNull: true, + } + ); + const p2 = queryInterface.changeColumn( + 'Certificate', + 'effectiveAmount', + { + type : Sequelize.DECIMAL(19, 8), + allowNull: true, + } + ); + return Promise.all([p1, p2]); + }, + down: (queryInterface, Sequelize) => { + // logic for reverting the changes + const p1 = queryInterface.changeColumn( + 'Certificate', + 'amount', + { + type : Sequelize.DOUBLE, + allowNull: true, + } + ); + const p2 = queryInterface.changeColumn( + 'Certificate', + 'effectiveAmount', + { + type : Sequelize.DOUBLE, + allowNull: true, + } + ); + return Promise.all([p1, p2]); + }, +}; diff --git a/migrations/ChangeEffectiveAmountColumnType.js b/migrations/ChangeClaimColumnTypes.js similarity index 53% rename from migrations/ChangeEffectiveAmountColumnType.js rename to migrations/ChangeClaimColumnTypes.js index 63f48e15..11c2bb52 100644 --- a/migrations/ChangeEffectiveAmountColumnType.js +++ b/migrations/ChangeClaimColumnTypes.js @@ -2,6 +2,34 @@ module.exports = { up: (queryInterface, Sequelize) => { // logic for transforming into the new state const p1 = queryInterface.changeColumn( + 'Claim', + 'amount', + { + type : Sequelize.DECIMAL(19, 8), + allowNull: true, + } + ); + const p2 = queryInterface.changeColumn( + 'Claim', + 'effectiveAmount', + { + type : Sequelize.DECIMAL(19, 8), + allowNull: true, + } + ); + return Promise.all([p1, p2]); + }, + down: (queryInterface, Sequelize) => { + // logic for reverting the changes + const p1 = queryInterface.changeColumn( + 'Claim', + 'amount', + { + type : Sequelize.DOUBLE, + allowNull: true, + } + ); + const p2 = queryInterface.changeColumn( 'Claim', 'effectiveAmount', { @@ -9,18 +37,6 @@ module.exports = { allowNull: true, } ); - return Promise.all([p1]); - }, - down: (queryInterface, Sequelize) => { - // logic for reverting the changes - const p1 = queryInterface.changeColumn( - 'Claim', - 'effectiveAmount', - { - type : Sequelize.STRING, - allowNull: true, - } - ); - return Promise.all([p1]); + return Promise.all([p1, p2]); }, }; diff --git a/migrations/ChangeHeightColumnType.js b/migrations/ChangeHeightColumnType.js deleted file mode 100644 index c6d32223..00000000 --- a/migrations/ChangeHeightColumnType.js +++ /dev/null @@ -1,26 +0,0 @@ -module.exports = { - up: (queryInterface, Sequelize) => { - // logic for transforming into the new state - const p1 = queryInterface.changeColumn( - 'Claim', - 'height', - { - type : Sequelize.INTEGER, - allowNull: true, - } - ); - return Promise.all([p1]); - }, - down: (queryInterface, Sequelize) => { - // logic for reverting the changes - const p1 = queryInterface.changeColumn( - 'Claim', - 'height', - { - type : Sequelize.STRING, - allowNull: true, - } - ); - return Promise.all([p1]); - }, -}; diff --git a/migrations/ChangeValidAtHeightColumnType.js b/migrations/ChangeValidAtHeightColumnType.js deleted file mode 100644 index 3f87aa4c..00000000 --- a/migrations/ChangeValidAtHeightColumnType.js +++ /dev/null @@ -1,26 +0,0 @@ -module.exports = { - up: (queryInterface, Sequelize) => { - // logic for transforming into the new state - const p1 = queryInterface.changeColumn( - 'Claim', - 'validAtHeight', - { - type : Sequelize.INTEGER, - allowNull: true, - } - ); - return Promise.all([p1]); - }, - down: (queryInterface, Sequelize) => { - // logic for reverting the changes - const p1 = queryInterface.changeColumn( - 'Claim', - 'validAtHeight', - { - type : Sequelize.STRING, - allowNull: true, - } - ); - return Promise.all([p1]); - }, -}; diff --git a/models/certificate.js b/models/certificate.js index 7fd1ca3a..59480a60 100644 --- a/models/certificate.js +++ b/models/certificate.js @@ -25,7 +25,7 @@ function sortResult (result, longId) { return shortId; } -module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, DOUBLE, Op }) => { +module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => { const Certificate = sequelize.define( 'Certificate', { @@ -34,7 +34,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D default: null, }, amount: { - type : DOUBLE, + type : DECIMAL(19, 8), default: null, }, claimId: { @@ -54,7 +54,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D default: null, }, effectiveAmount: { - type : DOUBLE, + type : DECIMAL(19, 8), default: null, }, hasSignature: { diff --git a/models/claim.js b/models/claim.js index 992a7158..04d6a4dd 100644 --- a/models/claim.js +++ b/models/claim.js @@ -25,7 +25,7 @@ function sortResult (result, longId) { return shortId; } -module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DOUBLE }) => { +module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => { const Claim = sequelize.define( 'Claim', { @@ -34,7 +34,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DOUBLE }) => { default: null, }, amount: { - type : DOUBLE, + type : DECIMAL(19, 8), default: null, }, claimId: { @@ -54,7 +54,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DOUBLE }) => { default: null, }, effectiveAmount: { - type : DOUBLE, + type : DECIMAL(19, 8), default: null, }, hasSignature: { diff --git a/models/index.js b/models/index.js index 83f277e9..4bc622fd 100644 --- a/models/index.js +++ b/models/index.js @@ -2,19 +2,20 @@ const fs = require('fs'); const path = require('path'); const Sequelize = require('sequelize'); const basename = path.basename(module.filename); -const config = require('../config/speechConfig.js'); -const db = {}; const logger = require('winston'); - +const config = require('../config/speechConfig.js'); const database = config.sql.database; const username = config.sql.username; const password = config.sql.password; +const db = {}; +// set sequelize options const sequelize = new Sequelize(database, username, password, { - host : 'localhost', - dialect: 'mysql', - logging: false, - pool : { + host : 'localhost', + dialect : 'mysql', + dialectOptions: {decimalNumbers: true}, // fix to ensure DECIMAL will not be stored as a string + logging : false, + pool : { max : 5, min : 0, idle : 10000, @@ -53,6 +54,7 @@ Object.keys(db).forEach(modelName => { db.sequelize = sequelize; db.Sequelize = Sequelize; +// add an 'upsert' method to the db object db.upsert = (Model, values, condition, tableName) => { return Model .findOne({ where: condition }) @@ -70,6 +72,7 @@ db.upsert = (Model, values, condition, tableName) => { }); }; +// add a 'getTrendingClaims' method to the db object db.getTrendingClaims = (startDate) => { return db.sequelize.query(`SELECT COUNT(*), File.* FROM Request LEFT JOIN File ON Request.FileId = File.id WHERE FileId IS NOT NULL AND nsfw != 1 AND trendingEligible = 1 AND Request.createdAt > "${startDate}" GROUP BY FileId ORDER BY COUNT(*) DESC LIMIT 25;`, { type: db.sequelize.QueryTypes.SELECT }); };