From 26608c89e2260cf1c1c8a0e4a345f84969b7bd23 Mon Sep 17 00:00:00 2001 From: Muhammad Aaqil Date: Sun, 14 Apr 2024 11:01:11 +0500 Subject: [PATCH] fix: misc Signed-off-by: Muhammad Aaqil --- lib/discovery.js | 9 +++++++++ test/mysql.discover.test.js | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/discovery.js b/lib/discovery.js index a9cf6c2c..70144dbb 100644 --- a/lib/discovery.js +++ b/lib/discovery.js @@ -298,6 +298,15 @@ function mixinDiscovery(MySQL, mysql) { return sql; }; + /** + * Build query to determine is strict mode + */ + + MySQL.prototype.buildQueryIsStrict = function() { + return 'SELECT @@GLOBAL.sql_mode LIKE \'%STRICT%\' AS globalStrictMode,' + + '@@SESSION.sql_mode LIKE \'%STRICT%\' AS sessionStrictMode;'; + }; + /** * Discover foreign keys that reference to the primary key of this table * @param {String} table The table name diff --git a/test/mysql.discover.test.js b/test/mysql.discover.test.js index 4fd1f290..01824c7b 100644 --- a/test/mysql.discover.test.js +++ b/test/mysql.discover.test.js @@ -6,6 +6,7 @@ 'use strict'; process.env.NODE_ENV = 'test'; const should = require('should'); +const async = require('async'); const assert = require('assert'); const DataSource = require('loopback-datasource-juggler').DataSource; @@ -542,3 +543,25 @@ describe('Discover and build models', function() { }); }); }); + +describe('Discover schema with strict mode on', function() { + let schema; + before(function(done) { + async.series([ + db.execute('SET GLOBAL sql_mode = \'STRICT_ALL_TABLES\';'), + db.discoverSchema('INVENTORY', {owner: 'STRONGLOOP'}, function(err, schema_) { + schema = schema_; + done(err); + }), + ]); + }); + it('should return an LDL schema for INVENTORY with strict mode on', function() { + assert.strictEqual(schema.name, 'Inventory'); + Object.keys(schema.properties).forEach(property => { + if (schema.properties.length) { + assert.strictEqual(property.jsonSchema.maxLength, property.length); + } + }); + async.series([db.execute('SET GLOBAL sql_mode = \'\';')]); + }); +});