不要怂,就是干,撸起袖子干!

Commit 7cc86a49 by Jack

Fixed regex for foreign key query. Added on_update/on_delete keys and testing for them.

1 parent 4181d346
...@@ -123,11 +123,17 @@ module.exports = (function() { ...@@ -123,11 +123,17 @@ module.exports = (function() {
result = []; result = [];
rows.forEach(function(row) { rows.forEach(function(row) {
var defParts; var defParts;
if (row.condef !== undefined && (defParts = row.condef.match(/FOREIGN KEY \((.+){1}\) REFERENCES (.+){1}\((.+){1}\)/))) { if (row.condef !== undefined && (defParts = row.condef.match(/FOREIGN KEY \((.+)\) REFERENCES (.+)\((.+)\)( ON (UPDATE|DELETE) (CASCADE|RESTRICT))?( ON (UPDATE|DELETE) (CASCADE|RESTRICT))?/))) {
row.id = row.constraint_name; row.id = row.constraint_name;
row.table = defParts[2]; row.table = defParts[2];
row.from = defParts[1]; row.from = defParts[1];
row.to = defParts[3]; row.to = defParts[3];
var i;
for (i=5;i<=8;i+=3) {
if (/(UPDATE|DELETE)/.test(defParts[i])) {
row['on_'+defParts[i].toLowerCase()] = defParts[i+1];
}
}
} }
result.push(row); result.push(row);
}); });
...@@ -265,7 +271,7 @@ module.exports = (function() { ...@@ -265,7 +271,7 @@ module.exports = (function() {
}; };
Query.prototype.isForeignKeysQuery = function() { Query.prototype.isForeignKeysQuery = function() {
return this.sql.match(/SELECT conname as constraint_name, pg_catalog\.pg_get_constraintdef(r\.oid, true) as condef FROM pg_catalog\.pg_constraint r WHERE r\.conrelid = (SELECT oid FROM pg_class WHERE relname = '.*' LIMIT 1) AND r\.contype = 'f' ORDER BY 1;/g); return /SELECT conname as constraint_name, pg_catalog\.pg_get_constraintdef\(r\.oid, true\) as condef FROM pg_catalog\.pg_constraint r WHERE r\.conrelid = \(SELECT oid FROM pg_class WHERE relname = '.*' LIMIT 1\) AND r\.contype = 'f' ORDER BY 1;/.test(this.sql);
}; };
Query.prototype.getInsertIdField = function() { Query.prototype.getInsertIdField = function() {
......
...@@ -265,11 +265,24 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () { ...@@ -265,11 +265,24 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
primaryKey: true, primaryKey: true,
autoincrement: true autoincrement: true
}, },
admin: {
type: DataTypes.INTEGER,
references: 'users',
referenceKey: 'id',
},
operator: {
type: DataTypes.INTEGER,
references: 'users',
referenceKey: 'id',
onUpdate: 'cascade',
},
owner: { owner: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
references: 'users', references: 'users',
referenceKey: 'id', referenceKey: 'id',
} onUpdate: 'cascade',
onDelete: 'restrict'
},
}) })
}) })
}) })
...@@ -277,10 +290,14 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () { ...@@ -277,10 +290,14 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
it('should get a list of foreign keys for the table', function (done) { it('should get a list of foreign keys for the table', function (done) {
this.sequelize.query(this.queryInterface.QueryGenerator.getForeignKeysQuery('hosts', this.sequelize.config.database)).complete(function(err, fks) { this.sequelize.query(this.queryInterface.QueryGenerator.getForeignKeysQuery('hosts', this.sequelize.config.database)).complete(function(err, fks) {
expect(err).to.be.null expect(err).to.be.null
expect(fks).to.have.length(1) expect(fks).to.have.length(3)
keys = Object.keys(fks[0]); keys = Object.keys(fks[0])
keys2 = Object.keys(fks[1])
keys3 = Object.keys(fks[2])
if (dialect === "postgres" || dialect === "postgres-native") { if (dialect === "postgres" || dialect === "postgres-native") {
expect(keys).to.have.length(5) expect(keys).to.have.length(6)
expect(keys2).to.have.length(7)
expect(keys3).to.have.length(8)
} else if (dialect === "sqlite") { } else if (dialect === "sqlite") {
expect(keys).to.have.length(8) expect(keys).to.have.length(8)
} else if (dialect === "mysql") { } else if (dialect === "mysql") {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!