Skip to content

Commit f705836

Browse files
committed
feat: fetch index info into the model
Signed-off-by: Muhammad Aaqil <[email protected]>
1 parent 6af3b7c commit f705836

File tree

3 files changed

+102
-70
lines changed

3 files changed

+102
-70
lines changed

lib/discovery.js

+70-70
Original file line numberDiff line numberDiff line change
@@ -158,89 +158,89 @@ function mixinDiscovery(MySQL, mysql) {
158158
let sql = null;
159159
if (schema) {
160160
sql = paginateSQL(
161-
`SELECT
162-
c.table_schema AS "owner",
163-
c.table_name AS "tableName",
164-
c.column_name AS "columnName",
165-
c.data_type AS "dataType",
166-
c.character_maximum_length AS "dataLength",
167-
c.numeric_precision AS "dataPrecision",
168-
c.numeric_scale AS "dataScale",
169-
c.column_type AS "columnType",
170-
c.is_nullable = 'YES' AS "nullable",
171-
CASE WHEN c.extra LIKE '%auto_increment%' THEN 1 ELSE 0 END AS "generated",
172-
s.index_name AS "indexName",
173-
s.non_unique AS "nonUnique",
174-
s.seq_in_index AS "seqInIndex",
175-
s.cardinality AS "cardinality",
176-
s.index_type AS "indexType",
177-
CASE WHEN fk.column_name IS NOT NULL THEN 1 ELSE 0 END AS "isForeignKey" -- Flag for foreign key
178-
FROM
179-
information_schema.columns c
180-
LEFT JOIN
181-
information_schema.statistics s
182-
ON
183-
c.table_schema = s.table_schema
184-
AND c.table_name = s.table_name
185-
AND c.column_name = s.column_name
186-
LEFT JOIN
187-
information_schema.KEY_COLUMN_USAGE fk
188-
ON
189-
c.table_schema = fk.table_schema
190-
AND c.table_name = fk.table_name
191-
AND c.column_name = fk.column_name
192-
AND fk.referenced_table_name IS NOT NULL -- Ensure it's a foreign key
193-
WHERE
194-
c.table_schema = ${mysql.escape(schema)}
195-
${table ? ' AND c.table_name = ' + mysql.escape(table) : ''}
161+
`SELECT
162+
cols.table_schema AS "owner",
163+
cols.table_name AS "tableName",
164+
cols.column_name AS "columnName",
165+
cols.data_type AS "dataType",
166+
cols.character_maximum_length AS "dataLength",
167+
cols.numeric_precision AS "dataPrecision",
168+
cols.numeric_scale AS "dataScale",
169+
cols.column_type AS "columnType",
170+
cols.is_nullable = 'YES' AS "nullable",
171+
CASE WHEN cols.extra LIKE '%auto_increment%' THEN 1 ELSE 0 END AS "generated",
172+
indexes.index_name AS "indexName",
173+
indexes.non_unique AS "nonUnique",
174+
indexes.seq_in_index AS "seqInIndex",
175+
indexes.cardinality AS "cardinality",
176+
indexes.index_type AS "indexType",
177+
CASE WHEN fk.column_name IS NOT NULL THEN 1 ELSE 0 END AS "isForeignKey"
178+
FROM
179+
information_schema.columns cols
180+
LEFT JOIN
181+
information_schema.statistics indexes
182+
ON
183+
cols.table_schema = indexes.table_schema
184+
AND cols.table_name = indexes.table_name
185+
AND cols.column_name = indexes.column_name
186+
LEFT JOIN
187+
information_schema.KEY_COLUMN_USAGE fk
188+
ON
189+
cols.table_schema = fk.table_schema
190+
AND cols.table_name = fk.table_name
191+
AND cols.column_name = fk.column_name
192+
AND fk.referenced_table_name IS NOT NULL
193+
WHERE
194+
cols.table_schema = ${mysql.escape(schema)}
195+
${table ? ' AND cols.table_name = ' + mysql.escape(table) : ''}
196196
`,
197-
'c.table_name, c.ordinal_position',
197+
'cols.table_name, cols.ordinal_position',
198198
{},
199199
);
200200
} else {
201201
sql = paginateSQL(
202-
`SELECT
203-
columns.table_schema AS "owner",
204-
columns.table_name AS "tableName",
205-
columns.column_name AS "columnName",
206-
columns.data_type AS "dataType",
207-
columns.character_maximum_length AS "dataLength",
208-
columns.numeric_precision AS "dataPrecision",
209-
columns.numeric_scale AS "dataScale",
210-
columns.column_type AS "columnType",
211-
columns.is_nullable = 'YES' AS "nullable",
212-
CASE WHEN columns.extra LIKE '%auto_increment%' THEN 1 ELSE 0 END AS "generated",
202+
`SELECT
203+
cols.table_schema AS "owner",
204+
cols.table_name AS "tableName",
205+
cols.column_name AS "columnName",
206+
cols.data_type AS "dataType",
207+
cols.character_maximum_length AS "dataLength",
208+
cols.numeric_precision AS "dataPrecision",
209+
cols.numeric_scale AS "dataScale",
210+
cols.column_type AS "columnType",
211+
cols.is_nullable = 'YES' AS "nullable",
212+
CASE WHEN cols.extra LIKE '%auto_increment%' THEN 1 ELSE 0 END AS "generated",
213213
indexes.index_name AS "indexName",
214214
indexes.seq_in_index AS "indexColumnOrder",
215215
indexes.non_unique AS "nonUnique",
216-
indexes.cardinality AS "cardinality", -- Cardinality of the index
217-
indexes.index_type AS "indexType", -- Type of the index
218-
CASE WHEN fk.column_name IS NOT NULL THEN 1 ELSE 0 END AS "isForeignKey" -- Flag for foreign key
219-
FROM
220-
information_schema.columns AS columns
221-
LEFT JOIN
222-
information_schema.statistics AS indexes
223-
ON
224-
columns.table_schema = indexes.table_schema
225-
AND columns.table_name = indexes.table_name
226-
AND columns.column_name = indexes.column_name
227-
LEFT JOIN
228-
information_schema.KEY_COLUMN_USAGE AS fk
229-
ON
230-
columns.table_schema = fk.table_schema
231-
AND columns.table_name = fk.table_name
232-
AND columns.column_name = fk.column_name
233-
AND fk.referenced_table_name IS NOT NULL -- Ensure it's a foreign key
234-
WHERE
235-
columns.table_schema = ${mysql.escape(schema)}
236-
${table ? ' AND columns.table_name = ' + mysql.escape(table) : ''}
216+
indexes.cardinality AS "cardinality",
217+
indexes.index_type AS "indexType",
218+
CASE WHEN fk.column_name IS NOT NULL THEN 1 ELSE 0 END AS "isForeignKey"
219+
FROM
220+
information_schema.columns AS cols
221+
LEFT JOIN
222+
information_schema.statistics AS indexes
223+
ON
224+
cols.table_schema = indexes.table_schema
225+
AND cols.table_name = indexes.table_name
226+
AND cols.column_name = indexes.column_name
227+
LEFT JOIN
228+
information_schema.KEY_COLUMN_USAGE AS fk
229+
ON
230+
cols.table_schema = fk.table_schema
231+
AND cols.table_name = fk.table_name
232+
AND cols.column_name = fk.column_name
233+
AND fk.referenced_table_name IS NOT NULL
234+
WHERE
235+
cols.table_schema = ${mysql.escape(schema)}
236+
${table ? ' AND cols.table_name = ' + mysql.escape(table) : ''}
237237
`,
238-
'columns.table_name, columns.ordinal_position',
238+
'cols.table_name, cols.ordinal_position',
239239
{},
240240
);
241241
}
242242
if (options.orderBy) {
243-
sql += ' ORDER BY ' + options.orderBy;
243+
sql += ' ORDER BY ' + 'cols.' + options.orderBy;
244244
}
245245
return sql;
246246
};

test/mysql.discover.test.js

+17
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,23 @@ describe('Discover model primary keys', function() {
199199
});
200200
});
201201

202+
describe('Discover user model with index', function() {
203+
it('should return user with index', function(done) {
204+
db.discoverModelProperties('user', function(err, models) {
205+
if (err) {
206+
console.error(err);
207+
done(err);
208+
} else {
209+
models.forEach(function(m) {
210+
assert(m.tableName.toLowerCase() === 'user');
211+
assert(m.properties.email.index);
212+
});
213+
done(null, models);
214+
}
215+
});
216+
});
217+
});
218+
202219
describe('Discover model foreign keys', function() {
203220
it('should return an array of foreign keys for INVENTORY', function(done) {
204221
db.discoverForeignKeys('INVENTORY', function(err, models) {

test/schema.sql

+15
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,21 @@ LOCK TABLES `RESERVATION` WRITE;
208208
/*!40000 ALTER TABLE `RESERVATION` ENABLE KEYS */;
209209
UNLOCK TABLES;
210210

211+
DROP TABLE IF EXISTS `USER`;
212+
/*!40101 SET @saved_cs_client = @@character_set_client */;
213+
/*!40101 SET character_set_client = utf8 */;
214+
215+
CREATE TABLE `USER` (
216+
`ID` VARCHAR(20) NOT NULL,
217+
`NAME` VARCHAR(100) NOT NULL,
218+
`EMAIL` VARCHAR(255) NOT NULL,
219+
`PASSWORD` VARCHAR(255) NOT NULL,
220+
`CREATED_AT` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
221+
`UPDATED_AT` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
222+
PRIMARY KEY (`ID`),
223+
UNIQUE KEY `USER_EMAIL_UNIQUE` (`EMAIL`)
224+
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
225+
211226
--
212227
-- Table structure for table `TESTGEN`
213228
--

0 commit comments

Comments
 (0)