From 7f5c02fee394981d5bd526e130a3c466880e62d7 Mon Sep 17 00:00:00 2001 From: Muhammad Aaqil Date: Sun, 1 Sep 2024 12:30:23 +0500 Subject: [PATCH] fix: mark a property generated if it is STORED or VIRTUAL Signed-off-by: Muhammad Aaqil --- lib/discovery.js | 18 ++++++++++++++++-- test/mysql.discover.test.js | 36 ++++++++++++++++++++++++++++++++++++ test/schema.sql | 4 +++- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/lib/discovery.js b/lib/discovery.js index 6c53216bf..60a9de1e7 100644 --- a/lib/discovery.js +++ b/lib/discovery.js @@ -166,7 +166,14 @@ function mixinDiscovery(MySQL, mysql) { ' numeric_scale AS "dataScale",' + ' column_type AS "columnType",' + ' is_nullable = \'YES\' AS "nullable",' + - ' CASE WHEN extra LIKE \'%auto_increment%\' THEN 1 ELSE 0 END AS "generated"' + + ` + case + when extra like '%virtual%' then 1 + when extra like '%stored%' then 1 + 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) : ''), @@ -181,7 +188,14 @@ function mixinDiscovery(MySQL, mysql) { ' numeric_scale AS "dataScale",' + ' column_type AS "columnType",' + ' is_nullable = \'YES\' AS "nullable",' + - ' CASE WHEN extra LIKE \'%auto_increment%\' THEN 1 ELSE 0 END AS "generated"' + + ` + case + when extra like '%virtual%' then 1 + when extra like '%stored%' then 1 + 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', {}); diff --git a/test/mysql.discover.test.js b/test/mysql.discover.test.js index 9da8060ef..323aab250 100644 --- a/test/mysql.discover.test.js +++ b/test/mysql.discover.test.js @@ -251,6 +251,42 @@ describe('Discover model generated columns', function() { done(); }); }); + it('should mark STORED column as generated', function(done) { + db.discoverModelProperties('testgen', function(err, models) { + if (err) return done(err); + models.forEach(function(model) { + assert(model.tableName.toLowerCase() === 'testgen'); + if (model.columnName === 'TOKEN') { + assert(model.generated, 'STRONGLOOP.TESTGEN.TOKEN should be a generated (identity) column'); + } + }); + done(); + }); + }); + it('should mark STORED column as generated', function(done) { + db.discoverModelProperties('testgen', function(err, models) { + if (err) return done(err); + models.forEach(function(model) { + assert(model.tableName.toLowerCase() === 'testgen'); + if (model.columnName === 'TOKEN') { + assert(model.generated, 'STRONGLOOP.TESTGEN.TOKEN should be a generated (identity) column'); + } + }); + done(); + }); + }); + it('should mark VIRTUAL column as generated', function(done) { + db.discoverModelProperties('testgen', function(err, models) { + if (err) return done(err); + models.forEach(function(model) { + assert(model.tableName.toLowerCase() === 'testgen'); + if (model.columnName === 'VIRTUAL_TOKEN') { + assert(model.generated, 'STRONGLOOP.TESTGEN.VIRTUAL_TOKEN should be a generated (identity) column'); + } + }); + done(); + }); + }); }); describe('Discover LDL schema from a table', function() { diff --git a/test/schema.sql b/test/schema.sql index f80ff03ed..d0fa2d66a 100644 --- a/test/schema.sql +++ b/test/schema.sql @@ -217,8 +217,10 @@ DROP TABLE IF EXISTS `TESTGEN`; CREATE TABLE `TESTGEN` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `NAME` varchar(64) DEFAULT NULL, + `TOKEN` CHAR(32) GENERATED ALWAYS AS (MD5(NAME)) STORED, + `VIRTUAL_TOKEN` CHAR(32) GENERATED ALWAYS AS (MD5(NAME)) VIRTUAL, PRIMARY KEY (`ID`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; +) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; --