added migration to change DOUBLE to DECIMAL

This commit is contained in:
bill bittner 2017-11-08 07:25:21 -08:00
parent 37315c6c66
commit 9dc4b2f151
9 changed files with 87 additions and 178 deletions

View file

@ -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]);
},
};

View file

@ -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]);
},
};

View file

@ -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]);
},
};

View file

@ -2,6 +2,34 @@ module.exports = {
up: (queryInterface, Sequelize) => { up: (queryInterface, Sequelize) => {
// logic for transforming into the new state // logic for transforming into the new state
const p1 = queryInterface.changeColumn( 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', 'Claim',
'effectiveAmount', 'effectiveAmount',
{ {
@ -9,18 +37,6 @@ module.exports = {
allowNull: true, allowNull: true,
} }
); );
return Promise.all([p1]); return Promise.all([p1, p2]);
},
down: (queryInterface, Sequelize) => {
// logic for reverting the changes
const p1 = queryInterface.changeColumn(
'Claim',
'effectiveAmount',
{
type : Sequelize.STRING,
allowNull: true,
}
);
return Promise.all([p1]);
}, },
}; };

View file

@ -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]);
},
};

View file

@ -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]);
},
};

View file

@ -25,7 +25,7 @@ function sortResult (result, longId) {
return shortId; 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( const Certificate = sequelize.define(
'Certificate', 'Certificate',
{ {
@ -34,7 +34,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D
default: null, default: null,
}, },
amount: { amount: {
type : DOUBLE, type : DECIMAL(19, 8),
default: null, default: null,
}, },
claimId: { claimId: {
@ -54,7 +54,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D
default: null, default: null,
}, },
effectiveAmount: { effectiveAmount: {
type : DOUBLE, type : DECIMAL(19, 8),
default: null, default: null,
}, },
hasSignature: { hasSignature: {

View file

@ -25,7 +25,7 @@ function sortResult (result, longId) {
return shortId; return shortId;
} }
module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DOUBLE }) => { module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => {
const Claim = sequelize.define( const Claim = sequelize.define(
'Claim', 'Claim',
{ {
@ -34,7 +34,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DOUBLE }) => {
default: null, default: null,
}, },
amount: { amount: {
type : DOUBLE, type : DECIMAL(19, 8),
default: null, default: null,
}, },
claimId: { claimId: {
@ -54,7 +54,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DOUBLE }) => {
default: null, default: null,
}, },
effectiveAmount: { effectiveAmount: {
type : DOUBLE, type : DECIMAL(19, 8),
default: null, default: null,
}, },
hasSignature: { hasSignature: {

View file

@ -2,18 +2,19 @@ const fs = require('fs');
const path = require('path'); const path = require('path');
const Sequelize = require('sequelize'); const Sequelize = require('sequelize');
const basename = path.basename(module.filename); const basename = path.basename(module.filename);
const config = require('../config/speechConfig.js');
const db = {};
const logger = require('winston'); const logger = require('winston');
const config = require('../config/speechConfig.js');
const database = config.sql.database; const database = config.sql.database;
const username = config.sql.username; const username = config.sql.username;
const password = config.sql.password; const password = config.sql.password;
const db = {};
// set sequelize options
const sequelize = new Sequelize(database, username, password, { const sequelize = new Sequelize(database, username, password, {
host : 'localhost', host : 'localhost',
dialect: 'mysql', dialect : 'mysql',
logging: false, dialectOptions: {decimalNumbers: true}, // fix to ensure DECIMAL will not be stored as a string
logging : false,
pool : { pool : {
max : 5, max : 5,
min : 0, min : 0,
@ -53,6 +54,7 @@ Object.keys(db).forEach(modelName => {
db.sequelize = sequelize; db.sequelize = sequelize;
db.Sequelize = Sequelize; db.Sequelize = Sequelize;
// add an 'upsert' method to the db object
db.upsert = (Model, values, condition, tableName) => { db.upsert = (Model, values, condition, tableName) => {
return Model return Model
.findOne({ where: condition }) .findOne({ where: condition })
@ -70,6 +72,7 @@ db.upsert = (Model, values, condition, tableName) => {
}); });
}; };
// add a 'getTrendingClaims' method to the db object
db.getTrendingClaims = (startDate) => { 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 }); 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 });
}; };