Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add query builder to determine strict mode #569

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions test/mysql.discover.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = \'\';')]);
});
});