Skip to content

Commit 0d7e93c

Browse files
committed
Allow adding indexes with schema tool
1 parent 0510788 commit 0d7e93c

2 files changed

Lines changed: 25 additions & 15 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ Format of `schemaConfig.json`:
114114
"name": String,
115115
"type": String,
116116
"primaryKey": Boolean (Optional),
117+
"indexed": Boolean (Optional),
117118
"autoIncrement": Boolean (Optional)
118119
}
119120
]

schemaTool.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ function compareTableFieldAttributes(table, field, fieldAttributes) {
8484
var outputMessageList = [];
8585
var tempDescription = "Field \"" + field.name + "\" of table \"" + table.name + "\"";
8686
var tempAttributesAreCorrect = true;
87+
8788
var tempType = fieldAttributes.COLUMN_TYPE.toLowerCase();
8889
if (tempType.match(/^int\([0-9]+\)$/)) {
8990
tempType = "int";
@@ -94,17 +95,21 @@ function compareTableFieldAttributes(table, field, fieldAttributes) {
9495
outputMessageList.push(tempDescription + " has the wrong data type \"" + fieldAttributes.COLUMN_TYPE + "\". It should be \"" + field.type + "\".");
9596
tempAttributesAreCorrect = false;
9697
}
97-
var tempIsPrimaryKey = (fieldAttributes.COLUMN_KEY.toUpperCase() == "PRI")
98-
var tempShouldBePrimaryKey;
99-
if ("primaryKey" in field) {
100-
tempShouldBePrimaryKey = field.primaryKey;
98+
99+
let tempColumnKey = fieldAttributes.COLUMN_KEY.toUpperCase();
100+
var tempExpectedColumnKey;
101+
if ("primaryKey" in field && field.primaryKey) {
102+
tempExpectedColumnKey = "PRI"
103+
} else if ("indexed" in field && field.indexed) {
104+
tempExpectedColumnKey = "MUL";
101105
} else {
102-
tempShouldBePrimaryKey = false;
106+
tempExpectedColumnKey = "";
103107
}
104-
if (tempIsPrimaryKey != tempShouldBePrimaryKey) {
108+
if (tempColumnKey != tempExpectedColumnKey) {
105109
outputMessageList.push(tempDescription + " has the wrong COLUMN_KEY value.");
106110
tempAttributesAreCorrect = false;
107111
}
112+
108113
var tempIsAutoIncrement = (fieldAttributes.EXTRA.toLowerCase() == "auto_increment")
109114
var tempShouldBeAutoIncrement;
110115
if ("autoIncrement" in field) {
@@ -116,6 +121,7 @@ function compareTableFieldAttributes(table, field, fieldAttributes) {
116121
outputMessageList.push(tempDescription + " has the wrong EXTRA value.");
117122
tempAttributesAreCorrect = false;
118123
}
124+
119125
if (tempAttributesAreCorrect) {
120126
outputMessageList = [tempDescription + " exists and has the correct attributes."];
121127
}
@@ -152,21 +158,20 @@ function getFieldDefinition(field) {
152158

153159
function createTable(table, done) {
154160
var fieldDefinitionList = [];
155-
var primaryKeyField = null;
156161
var index = 0;
157162
while (index < table.fields.length) {
158163
var tempField = table.fields[index];
159164
var tempDefinition = getFieldDefinition(tempField);
160165
fieldDefinitionList.push(tempDefinition);
161-
if ("primaryKey" in tempField) {
162-
if (tempField.primaryKey) {
163-
primaryKeyField = tempField;
164-
}
165-
}
166166
index += 1;
167167
}
168-
if (primaryKeyField !== null) {
169-
fieldDefinitionList.push("PRIMARY KEY (" + primaryKeyField.name + ")");
168+
for (let field of table.fields) {
169+
if ("primaryKey" in field && field.primaryKey) {
170+
fieldDefinitionList.push(`PRIMARY KEY (${field.name})`);
171+
}
172+
if ("indexed" in field && field.indexed) {
173+
fieldDefinitionList.push(`INDEX (${field.name})`);
174+
}
170175
}
171176
connection.query(
172177
"CREATE TABLE " + databaseName + "." + table.name + " (" + fieldDefinitionList.join(", ") + ")",
@@ -184,8 +189,12 @@ function createTable(table, done) {
184189

185190
function addTableField(table, field, done) {
186191
var tempDefinition = getFieldDefinition(field);
192+
let tempStatement = `ALTER TABLE ${databaseName}.${table.name} ADD COLUMN ${tempDefinition}`;
193+
if ("indexed" in field && field.indexed) {
194+
tempStatement += `, ADD INDEX (${field.name})`;
195+
}
187196
connection.query(
188-
"ALTER TABLE " + databaseName + "." + table.name + " ADD COLUMN " + tempDefinition,
197+
tempStatement,
189198
[],
190199
function (error, results, fields) {
191200
if (error) {

0 commit comments

Comments
 (0)