Return if column is generated or not (#198)

* Return if column is generated or not

Related to
https://github.com/strongloop/loopback-datasource-juggler/issues/899

* add pk for testgen table

* Fixe the double quotes around generated

as requested by @loay

* Fix commit linter

Block must not be padded by blank lines padded-blocks

* Code fixes to follow guide

* Should fix the tests failing

Test failing on the tableName (incorrect case)
This commit is contained in:
Christiaan Westerbeek 2017-03-09 00:56:27 +01:00 committed by Sakib Hasan
parent fa0e5b46c6
commit b67b57970f
3 changed files with 44 additions and 3 deletions

View File

@ -165,7 +165,8 @@ function mixinDiscovery(MySQL, mysql) {
' numeric_precision AS "dataPrecision",' +
' numeric_scale AS "dataScale",' +
' column_type AS "columnType",' +
' is_nullable = \'YES\' AS "nullable"' +
' is_nullable = \'YES\' AS "nullable",' +
' CASE WHEN extra LIKE \'%auto_increment%\' THEN 1 ELSE 0 END AS "generated"' +
' FROM information_schema.columns' +
' WHERE table_schema=' + mysql.escape(schema) +
(table ? ' AND table_name=' + mysql.escape(table) : ''),
@ -179,7 +180,8 @@ function mixinDiscovery(MySQL, mysql) {
' numeric_precision AS "dataPrecision",' +
' numeric_scale AS "dataScale",' +
' column_type AS "columnType",' +
' is_nullable = \'YES\' AS "nullable"' +
' is_nullable = \'YES\' AS "nullable",' +
' CASE WHEN extra LIKE \'%auto_increment%\' THEN 1 ELSE 0 END AS "generated"' +
' FROM information_schema.columns' +
(table ? ' WHERE table_name=' + mysql.escape(table) : ''),
'table_name, ordinal_position', {});

View File

@ -206,6 +206,31 @@ describe('Discover model foreign keys', function() {
});
});
describe('Discover model generated columns', function() {
it('should return an array of columns for STRONGLOOP.PRODUCT and none of them is generated', function(done) {
db.discoverModelProperties('product', function(err, models) {
if (err) return done(err);
models.forEach(function(model) {
assert(model.tableName === 'product');
assert(!model.generated, 'STRONGLOOP.PRODUCT table should not have generated (identity) columns');
});
done();
});
});
it('should return an array of columns for STRONGLOOP.TESTGEN and the first is generated', function(done) {
db.discoverModelProperties('testgen', function(err, models) {
if (err) return done(err);
models.forEach(function(model) {
assert(model.tableName === 'testgen');
if (model.columnName === 'ID') {
assert(model.generated, 'STRONGLOOP.TESTGEN.ID should be a generated (identity) column');
}
});
done();
});
});
});
describe('Discover LDL schema from a table', function() {
var schema;
before(function(done) {

View File

@ -86,7 +86,7 @@ DROP TABLE IF EXISTS `INVENTORY_VIEW`;
/*!50001 DROP VIEW IF EXISTS `INVENTORY_VIEW`*/;
SET @saved_cs_client = @@character_set_client;
SET character_set_client = utf8;
/*!50001 CREATE VIEW `INVENTORY_VIEW` AS SELECT
/*!50001 CREATE VIEW `INVENTORY_VIEW` AS SELECT
1 AS `ID`,
1 AS `PRODUCT_ID`,
1 AS `PRODUCT_NAME`,
@ -189,6 +189,20 @@ LOCK TABLES `RESERVATION` WRITE;
/*!40000 ALTER TABLE `RESERVATION` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `TESTGEN`
--
DROP TABLE IF EXISTS `TESTGEN`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `TESTGEN` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`NAME` varchar(64) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Current Database: `STRONGLOOP`
--