bootstrap.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * Support functions for helping with Postgres tests
  3. */
  4. var _ = require('@sailshq/lodash');
  5. var MySQL = require('machinepack-mysql');
  6. var adapter = require('../../lib/adapter');
  7. var Support = module.exports = {};
  8. // Determine config (using env vars).
  9. Support.Config = (function(){
  10. var config = {
  11. schema: true,
  12. };
  13. // Try and build up a Waterline Adapter Tests URL if one isn't set.
  14. // (Not all automated test runners can be configured to automatically set these).
  15. // Docker sets various URL's that can be used to build up a URL for instance.
  16. if (process.env.WATERLINE_ADAPTER_TESTS_URL) {
  17. config.url = process.env.WATERLINE_ADAPTER_TESTS_URL;
  18. return config;
  19. }
  20. else {
  21. var host = process.env.MYSQL_PORT_3306_TCP_ADDR || process.env.WATERLINE_ADAPTER_TESTS_HOST || 'localhost';
  22. var port = process.env.WATERLINE_ADAPTER_TESTS_PORT || 3306;
  23. var user = process.env.MYSQL_ENV_MYSQL_USER || process.env.WATERLINE_ADAPTER_TESTS_USER || 'root';
  24. var password = process.env.MYSQL_ENV_MYSQL_PASSWORD || process.env.WATERLINE_ADAPTER_TESTS_PASSWORD || process.env.MYSQL_PWD || '';
  25. var database = process.env.MYSQL_ENV_MYSQL_DATABASE || process.env.WATERLINE_ADAPTER_TESTS_DATABASE || 'adapter_tests';
  26. config.url = 'mysql://' + user + ':' + password + '@' + host + ':' + port + '/' + database;
  27. return config;
  28. }
  29. })();
  30. // Fixture Model Def
  31. Support.Model = function model(name, def) {
  32. return {
  33. identity: name,
  34. tableName: name,
  35. datastore: 'test',
  36. primaryKey: 'id',
  37. definition: def || Support.Definition
  38. };
  39. };
  40. // Fixture Table Definition
  41. Support.Definition = {
  42. id: {
  43. type: 'number',
  44. columnName: 'id',
  45. autoMigrations: {
  46. columnType: 'integer',
  47. autoIncrement: true,
  48. unique: true
  49. }
  50. },
  51. fieldA: {
  52. type: 'string',
  53. columnName: 'fieldA',
  54. autoMigrations: {
  55. columnType: 'text'
  56. }
  57. },
  58. fieldB: {
  59. type: 'string',
  60. columnName: 'fieldB',
  61. autoMigrations: {
  62. columnType: 'text'
  63. }
  64. },
  65. fieldC: {
  66. type: 'ref',
  67. columnName: 'fieldC',
  68. autoMigrations: {
  69. columnType: 'mediumblob'
  70. }
  71. },
  72. fieldD: {
  73. type: 'ref',
  74. columnName: 'fieldD',
  75. autoMigrations: {
  76. columnType: 'datetime'
  77. }
  78. }
  79. };
  80. // Register and Define a Collection
  81. Support.Setup = function setup(tableName, cb) {
  82. var collection = Support.Model(tableName);
  83. var collections = {};
  84. collections[tableName] = collection;
  85. var connection = _.cloneDeep(Support.Config);
  86. connection.identity = 'test';
  87. // Setup a primaryKey for migrations
  88. collection.definition = _.cloneDeep(Support.Definition);
  89. // Build a schema to represent the underlying physical database structure
  90. var schema = {};
  91. _.each(collection.definition, function parseAttribute(attributeVal, attributeName) {
  92. var columnName = attributeVal.columnName || attributeName;
  93. // If the attribute doesn't have an `autoMigrations` key on it, ignore it.
  94. if (!_.has(attributeVal, 'autoMigrations')) {
  95. return;
  96. }
  97. schema[columnName] = attributeVal.autoMigrations;
  98. });
  99. // Set Primary Key flag on the primary key attribute
  100. var primaryKeyAttrName = collection.primaryKey;
  101. var primaryKey = collection.definition[primaryKeyAttrName];
  102. if (primaryKey) {
  103. var pkColumnName = primaryKey.columnName || primaryKeyAttrName;
  104. schema[pkColumnName].primaryKey = true;
  105. }
  106. adapter.registerDatastore(connection, collections, function registerCb(err) {
  107. if (err) {
  108. return cb(err);
  109. }
  110. adapter.define('test', tableName, schema, cb);
  111. });
  112. };
  113. // Just register a connection
  114. Support.registerConnection = function registerConnection(tableNames, cb) {
  115. var collections = {};
  116. _.each(tableNames, function processTable(name) {
  117. var collection = Support.Model(name);
  118. collections[name] = collection;
  119. });
  120. var connection = _.cloneDeep(Support.Config);
  121. connection.identity = 'test';
  122. adapter.registerDatastore(connection, collections, cb);
  123. };
  124. // Remove a table and destroy the manager
  125. Support.Teardown = function teardown(tableName, cb) {
  126. var manager = adapter.datastores[_.first(_.keys(adapter.datastores))].manager;
  127. MySQL.getConnection({
  128. manager: manager,
  129. meta: Support.Config
  130. }).exec(function getConnectionCb(err, report) {
  131. if (err) {
  132. return cb(err);
  133. }
  134. var query = 'DROP TABLE IF EXISTS `' + tableName + '`;';
  135. MySQL.sendNativeQuery({
  136. connection: report.connection,
  137. nativeQuery: query
  138. }).exec(function dropTableCb(err) {
  139. if (err) {
  140. return cb(err);
  141. }
  142. MySQL.releaseConnection({
  143. connection: report.connection
  144. }).exec(function releaseConnectionCb(err) {
  145. if (err) {
  146. return cb(err);
  147. }
  148. delete adapter.datastores[_.first(_.keys(adapter.datastores))];
  149. return cb();
  150. });
  151. });
  152. });
  153. };
  154. // Seed a record to use for testing
  155. Support.Seed = function seed(tableName, cb) {
  156. var manager = adapter.datastores[_.first(_.keys(adapter.datastores))].manager;
  157. MySQL.getConnection({
  158. manager: manager,
  159. meta: Support.Config
  160. }).exec(function getConnectionCb(err, report) {
  161. if (err) {
  162. return cb(err);
  163. }
  164. var query = [
  165. 'INSERT INTO `' + tableName + '` (`fieldA`, `fieldB`, `fieldC`, `fieldD`) ',
  166. 'values (\'foo\', \'bar\', null, null), (\'foo_2\', \'bAr_2\', $1, $2);'
  167. ].join('');
  168. MySQL.sendNativeQuery({
  169. connection: report.connection,
  170. nativeQuery: query,
  171. valuesToEscape: [new Buffer([1,2,3]), new Date('2001-06-15 12:00:00')]
  172. }).exec(function seedCb(err) {
  173. if (err) {
  174. return cb(err);
  175. }
  176. MySQL.releaseConnection({
  177. connection: report.connection
  178. }).exec(cb);
  179. });
  180. });
  181. };