Skip to content

Commit f30b435

Browse files
committed
Generate foreign key constraint on CREATE TABLE; Add feature test
1 parent e5b9ea3 commit f30b435

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

index.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,21 @@ var Sqlite3Driver = Base.extend({
5656
callback(null);
5757
},
5858

59-
createColumnDef: function(name, spec, options) {
60-
name = '"' + name + '"';
59+
createColumnDef: function(name, spec, options, tableName) {
60+
var quotedName = '"' + name + '"';
6161
var dType = this.mapDataType(spec.type);
6262
var len = spec.length ? util.format('(%s)', spec.length) : '';
63-
var constraint = this.createColumnConstraint(spec, options);
63+
var constraint = this.createColumnConstraint(spec, options, tableName, name);
6464

6565
if(spec.type === type.INTEGER)
6666
len = '';
6767

6868
return { foreignKey: null,
69-
constraints: [name, dType, len, constraint].join(' ') };
69+
constraints: [quotedName, dType, len, constraint].join(' ') };
7070
},
7171

72-
createColumnConstraint: function(spec, options) {
72+
createColumnConstraint: function(spec, options, tableName, columnName) {
73+
var cb;
7374
var constraint = [];
7475
if (spec.primaryKey && options.emitPrimaryKey) {
7576
constraint.push('PRIMARY KEY');
@@ -95,6 +96,22 @@ var Sqlite3Driver = Base.extend({
9596
constraint.push(spec.defaultValue);
9697
}
9798

99+
if (spec.foreignKey) {
100+
constraint.push(`REFERENCES ${spec.foreignKey.table}(${spec.foreignKey.mapping})`)
101+
if( spec.foreignKey.rules ){
102+
Object.keys(spec.foreignKey.rules)
103+
.forEach( (rule) => {
104+
switch(rule){
105+
case 'onDelete': constraint.push(`ON DELETE ${spec.foreignKey.rules[rule]}`)
106+
break
107+
case 'onUpdate': constraint.push(`ON UPDATE ${spec.foreignKey.rules[rule]}`)
108+
break
109+
default: throw new Error('Unsupported foreign key action trigger: ' + rule )
110+
}
111+
})
112+
}
113+
}
114+
98115
return constraint.join(' ');
99116
},
100117

test/foreign_key_batch.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const dbmeta = require('db-meta')
22
const dataType = require('db-migrate-shared').dataType
33
const fs = require('fs')
4+
const assert = require('assert')
45

56
module.exports = (driver, config, internals) => ({
67
'columnForeignKeySpec': {
@@ -20,7 +21,7 @@ module.exports = (driver, config, internals) => ({
2021
primaryKey: true,
2122
autoIncrement: true
2223
},
23-
event_id: {
24+
event_type_id: {
2425
type: dataType.INTEGER,
2526
notNull: true,
2627
foreignKey: {
@@ -51,11 +52,25 @@ module.exports = (driver, config, internals) => ({
5152
if (err) {
5253
return this.callback(err);
5354
}
54-
meta.getColumns('event', this.callback);
55+
meta.getTables( (err, tables) => {
56+
if (err) {
57+
return this.callback(err)
58+
}
59+
this.callback( undefined, tables.find( (table) => table.getName() == "event" ) )
60+
})
5561
}.bind(this));
5662
},
57-
'that has foreign key column with the expected reference': function (err, columns) {
58-
console.log(err, columns)
63+
'that has foreign key column with the expected reference': function (err, table) {
64+
// console.log(err, table)
65+
const foreignKeyDefinition = table.meta
66+
.sql
67+
.match(/"event_type_id"[^,]+/)[0]
68+
.replace(/\s{2,}/g,'')
69+
70+
assert.equal(
71+
foreignKeyDefinition,
72+
'"event_type_id" INTEGERNOT NULL REFERENCES event_type(id) ON DELETE CASCADE'
73+
)
5974
/*var column = findByName(columns, 'id');
6075
assert.equal(column.getDataType(), 'INTEGER');
6176
assert.equal(column.isPrimaryKey(), true);

test/sqlite3_test.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ internals.migrationTable = 'migrations';
2424
vows.describe('sqlite3').addBatch({
2525
'createTable': {
2626
topic: function () {
27-
console.log("cc")
2827
driver.connect(config, internals, function (err, db) {
29-
console.log("cc_cc")
3028
db.createTable('event', {
3129
id: { type: dataType.INTEGER, primaryKey: true, autoIncrement: true, notNull: true },
3230
str: { type: dataType.STRING, unique: true },
@@ -40,7 +38,6 @@ vows.describe('sqlite3').addBatch({
4038
},
4139

4240
teardown: function (db) {
43-
console.log('tt')
4441
db.close(function (err) {
4542
fs.unlink(config.filename, this.callback);
4643
}.bind(this));

0 commit comments

Comments
 (0)