From 04287f6a77c9115b7b23f7d438803c35472daa6c Mon Sep 17 00:00:00 2001 From: Matthew Dickinson Date: Mon, 19 Sep 2016 19:11:47 -0400 Subject: [PATCH] Added test to validate foreign key handling --- test/mysql.autoupdate.test.js | 146 ++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) diff --git a/test/mysql.autoupdate.test.js b/test/mysql.autoupdate.test.js index 3975fee..07be0fe 100644 --- a/test/mysql.autoupdate.test.js +++ b/test/mysql.autoupdate.test.js @@ -228,6 +228,152 @@ describe('MySQL connector', function() { }); }); + it('should auto migrate/update foreign keys in tables', function(done) { + var customer2_schema = + { + 'name': 'CustomerTest2', + 'options': { + 'idInjection': false, + 'mysql': { + 'schema': 'myapp_test', + 'table': 'customer_test2', + }, + }, + 'properties': { + 'id': { + 'type': 'String', + 'length': 20, + 'id': 1, + }, + 'name': { + 'type': 'String', + 'required': false, + 'length': 40, + }, + 'email': { + 'type': 'String', + 'required': true, + 'length': 40, + }, + 'age': { + 'type': 'Number', + 'required': false, + }, + }, + }; + + var schema_v1 = + { + 'name': 'OrderTest', + 'options': { + 'idInjection': false, + 'mysql': { + 'schema': 'myapp_test', + 'table': 'order_test', + }, + 'foreignKeys': { + 'fk_ordertest_customerId': { + 'name': 'fk_ordertest_customerId', + 'entity': 'CustomerTest2', + 'entityKey': 'id', + 'foreignKey': 'customerId', + }, + }, + }, + 'properties': { + 'id': { + 'type': 'String', + 'length': 20, + 'id': 1, + }, + 'customerId': { + 'type': 'String', + 'length': 20, + 'id': 1, + }, + 'description': { + 'type': 'String', + 'required': false, + 'length': 40, + }, + }, + }; + + var schema_v2 = + { + 'name': 'OrderTest', + 'options': { + 'idInjection': false, + 'mysql': { + 'schema': 'myapp_test', + 'table': 'order_test', + }, + }, + 'properties': { + 'id': { + 'type': 'String', + 'length': 20, + 'id': 1, + }, + 'customerId': { + 'type': 'String', + 'length': 20, + 'id': 1, + }, + 'description': { + 'type': 'String', + 'required': false, + 'length': 40, + }, + }, + }; + + var foreignKeySelect = + 'SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME,REFERENCED_COLUMN_NAME ' + + 'FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE ' + + 'WHERE REFERENCED_TABLE_SCHEMA = "myapp_test" ' + + 'AND REFERENCED_TABLE_NAME = "customer_test2"'; + + ds.createModel(customer2_schema.name, customer2_schema.properties, customer2_schema.options); + ds.createModel(schema_v1.name, schema_v1.properties, schema_v1.options); + + ds.automigrate(function() { + ds.autoupdate(function() { //foreign keys won't be created on table create + ds.discoverModelProperties('order_test', function(err, props) { + assert.equal(props.length, 3); + + ds.connector.execute(foreignKeySelect, function(err, foreignKeys) { + if (err) return done (err); + assert(foreignKeys); + assert(foreignKeys.length.should.be.equal(1)); + assert.equal(foreignKeys[0].TABLE_NAME, 'order_test'); + assert.equal(foreignKeys[0].COLUMN_NAME, 'customerId'); + assert.equal(foreignKeys[0].CONSTRAINT_NAME, 'fk_ordertest_customerId'); + assert.equal(foreignKeys[0].REFERENCED_COLUMN_NAME, 'id'); + + ds.createModel(schema_v2.name, schema_v2.properties, schema_v2.options); + ds.autoupdate(function(err, result) { + if (err) return done (err); + ds.discoverModelProperties('order_test', function(err, props) { + if (err) return done (err); + + assert.equal(props.length, 3); + + ds.connector.execute(foreignKeySelect, function(err, updatedForeignKeys) { + if (err) return done (err); + assert(updatedForeignKeys); + assert(updatedForeignKeys.length.should.be.equal(0)); + + done(err, result); + }); + }); + }); + }); + }); + }); + }); + }); + function setupAltColNameData() { var schema = { name: 'ColRenameTest',