From c82ba4c089372f9713c1a5279e23c056bcb0eb5f Mon Sep 17 00:00:00 2001 From: Jim Laurie Date: Mon, 4 Jun 2018 18:22:21 +0200 Subject: [PATCH] Fix decimal on pg an mysql fix #1273 --- packages/strapi-bookshelf/lib/index.js | 2 +- packages/strapi-knex/lib/index.js | 31 +++++++++++++++----------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/packages/strapi-bookshelf/lib/index.js b/packages/strapi-bookshelf/lib/index.js index ea100344b1..53c3143adc 100755 --- a/packages/strapi-bookshelf/lib/index.js +++ b/packages/strapi-bookshelf/lib/index.js @@ -361,7 +361,7 @@ module.exports = function(strapi) { type = definition.client === 'pg' ? 'double precision' : 'double'; break; case 'decimal': - type = 'decimal'; + type = 'decimal(10,2)'; break; case 'date': case 'time': diff --git a/packages/strapi-knex/lib/index.js b/packages/strapi-knex/lib/index.js index 6f96e320f4..6d859a425e 100755 --- a/packages/strapi-knex/lib/index.js +++ b/packages/strapi-knex/lib/index.js @@ -78,8 +78,9 @@ module.exports = strapi => { // Make sure the client is installed in the application // `node_modules` directory. + let client; try { - require(path.resolve(strapi.config.appPath, 'node_modules', connection.settings.client)); + client = require(path.resolve(strapi.config.appPath, 'node_modules', connection.settings.client)); } catch (err) { strapi.log.error('The client `' + connection.settings.client + '` is not installed.'); strapi.log.error('You can install it with `$ npm install ' + connection.settings.client + ' --save`.'); @@ -105,18 +106,22 @@ module.exports = strapi => { migrations: _.get(connection.options, 'migrations') }, strapi.config.hook.settings.knex); - if (options.client === 'pg' && _.isString(_.get(options.connection, 'schema'))) { - options.pool = { - min: _.get(connection.options, 'pool.min') || 0, - max: _.get(connection.options, 'pool.max') || 10, - afterCreate: (conn, cb) => { - conn.query(`SET SESSION SCHEMA '${options.connection.schema}';`, (err) => { - cb(err, conn); - }); - } - }; - } else { - delete options.connection.schema; + if (options.client === 'pg') { + client.types.setTypeParser(1700, 'text', parseFloat); + + if (_.isString(_.get(options.connection, 'schema'))) { + options.pool = { + min: _.get(connection.options, 'pool.min') || 0, + max: _.get(connection.options, 'pool.max') || 10, + afterCreate: (conn, cb) => { + conn.query(`SET SESSION SCHEMA '${options.connection.schema}';`, (err) => { + cb(err, conn); + }); + } + }; + } else { + delete options.connection.schema; + } } if (options.client === 'mysql') {