Skip to content

fix: mark a property generated if it is STORED or VIRTUAL #608

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

Merged
merged 1 commit into from
May 27, 2025
Merged
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
20 changes: 18 additions & 2 deletions lib/discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) : ''),
Expand All @@ -181,7 +188,16 @@ 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's GENERATED ALWAYS when using VIRTUAL or STORED, should we check for that?

Also could you please make the syntax consistent? i.e. for line 193:

 WHEN extra LIKE '%virtual%' THEN 1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aaqilniz, are you interested to continue to pursue with this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @dhmlau. I will update the PR soon and notify you for the review.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @dhmlau. I have just made a quick update. Can you please have a look at the PR?

when extra LIKE '%GENERATED ALWAYS%' AND extra LIKE '%VIRTUAL%' THEN 1
when extra LIKE '%GENERATED ALWAYS%' AND extra LIKE '%STORED%' THEN 1
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', {});
Expand Down
24 changes: 24 additions & 0 deletions test/mysql.discover.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,30 @@ 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 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() {
Expand Down
4 changes: 3 additions & 1 deletion test/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 */;

--
Expand Down