tablecompiler.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _assign2 = require('lodash/assign');
  4. var _assign3 = _interopRequireDefault(_assign2);
  5. var _inherits = require('inherits');
  6. var _inherits2 = _interopRequireDefault(_inherits);
  7. var _tablecompiler = require('../../../schema/tablecompiler');
  8. var _tablecompiler2 = _interopRequireDefault(_tablecompiler);
  9. var _helpers = require('../../../helpers');
  10. var helpers = _interopRequireWildcard(_helpers);
  11. var _bluebird = require('bluebird');
  12. var _bluebird2 = _interopRequireDefault(_bluebird);
  13. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. // Table Compiler
  16. // ------
  17. /* eslint max-len:0 no-console:0*/
  18. // MySQL Table Builder & Compiler
  19. // -------
  20. function TableCompiler_MySQL() {
  21. _tablecompiler2.default.apply(this, arguments);
  22. }
  23. (0, _inherits2.default)(TableCompiler_MySQL, _tablecompiler2.default);
  24. (0, _assign3.default)(TableCompiler_MySQL.prototype, {
  25. createQuery: function createQuery(columns, ifNot) {
  26. var createStatement = ifNot ? 'create table if not exists ' : 'create table ';
  27. var client = this.client;
  28. var conn = {};
  29. var sql = createStatement + this.tableName() + ' (' + columns.sql.join(', ') + ')';
  30. // Check if the connection settings are set.
  31. if (client.connectionSettings) {
  32. conn = client.connectionSettings;
  33. }
  34. var charset = this.single.charset || conn.charset || '';
  35. var collation = this.single.collate || conn.collate || '';
  36. var engine = this.single.engine || '';
  37. // var conn = builder.client.connectionSettings;
  38. if (charset) sql += ' default character set ' + charset;
  39. if (collation) sql += ' collate ' + collation;
  40. if (engine) sql += ' engine = ' + engine;
  41. if (this.single.comment) {
  42. var comment = this.single.comment || '';
  43. if (comment.length > 60) helpers.warn('The max length for a table comment is 60 characters');
  44. sql += ' comment = \'' + comment + '\'';
  45. }
  46. this.pushQuery(sql);
  47. },
  48. addColumnsPrefix: 'add ',
  49. alterColumnsPrefix: 'modify ',
  50. dropColumnPrefix: 'drop ',
  51. // Compiles the comment on the table.
  52. comment: function comment(_comment) {
  53. this.pushQuery('alter table ' + this.tableName() + ' comment = \'' + _comment + '\'');
  54. },
  55. changeType: function changeType() {
  56. // alter table + table + ' modify ' + wrapped + '// type';
  57. },
  58. // Renames a column on the table.
  59. renameColumn: function renameColumn(from, to) {
  60. var compiler = this;
  61. var table = this.tableName();
  62. var wrapped = this.formatter.wrap(from) + ' ' + this.formatter.wrap(to);
  63. this.pushQuery({
  64. sql: 'show fields from ' + table + ' where field = ' + this.formatter.parameter(from),
  65. output: function output(resp) {
  66. var column = resp[0];
  67. var runner = this;
  68. return compiler.getFKRefs(runner).get(0).then(function (refs) {
  69. return _bluebird2.default.try(function () {
  70. if (!refs.length) {
  71. return;
  72. }
  73. return compiler.dropFKRefs(runner, refs);
  74. }).then(function () {
  75. var sql = 'alter table ' + table + ' change ' + wrapped + ' ' + column.Type;
  76. if (String(column.Null).toUpperCase() !== 'YES') {
  77. sql += ' NOT NULL';
  78. }
  79. if (column.Default !== void 0 && column.Default !== null) {
  80. sql += ' DEFAULT \'' + column.Default + '\'';
  81. }
  82. return runner.query({
  83. sql: sql
  84. });
  85. }).then(function () {
  86. if (!refs.length) {
  87. return;
  88. }
  89. return compiler.createFKRefs(runner, refs.map(function (ref) {
  90. if (ref.REFERENCED_COLUMN_NAME === from) {
  91. ref.REFERENCED_COLUMN_NAME = to;
  92. }
  93. if (ref.COLUMN_NAME === from) {
  94. ref.COLUMN_NAME = to;
  95. }
  96. return ref;
  97. }));
  98. });
  99. });
  100. }
  101. });
  102. },
  103. getFKRefs: function getFKRefs(runner) {
  104. var formatter = this.client.formatter();
  105. var sql = 'SELECT KCU.CONSTRAINT_NAME, KCU.TABLE_NAME, KCU.COLUMN_NAME, ' + ' KCU.REFERENCED_TABLE_NAME, KCU.REFERENCED_COLUMN_NAME, ' + ' RC.UPDATE_RULE, RC.DELETE_RULE ' + 'FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU ' + 'JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS RC ' + ' USING(CONSTRAINT_NAME)' + 'WHERE KCU.REFERENCED_TABLE_NAME = ' + formatter.parameter(this.tableNameRaw) + ' ' + ' AND KCU.CONSTRAINT_SCHEMA = ' + formatter.parameter(this.client.database()) + ' ' + ' AND RC.CONSTRAINT_SCHEMA = ' + formatter.parameter(this.client.database());
  106. return runner.query({
  107. sql: sql,
  108. bindings: formatter.bindings
  109. });
  110. },
  111. dropFKRefs: function dropFKRefs(runner, refs) {
  112. var formatter = this.client.formatter();
  113. return _bluebird2.default.all(refs.map(function (ref) {
  114. var constraintName = formatter.wrap(ref.CONSTRAINT_NAME);
  115. var tableName = formatter.wrap(ref.TABLE_NAME);
  116. return runner.query({
  117. sql: 'alter table ' + tableName + ' drop foreign key ' + constraintName
  118. });
  119. }));
  120. },
  121. createFKRefs: function createFKRefs(runner, refs) {
  122. var formatter = this.client.formatter();
  123. return _bluebird2.default.all(refs.map(function (ref) {
  124. var tableName = formatter.wrap(ref.TABLE_NAME);
  125. var keyName = formatter.wrap(ref.CONSTRAINT_NAME);
  126. var column = formatter.columnize(ref.COLUMN_NAME);
  127. var references = formatter.columnize(ref.REFERENCED_COLUMN_NAME);
  128. var inTable = formatter.wrap(ref.REFERENCED_TABLE_NAME);
  129. var onUpdate = ' ON UPDATE ' + ref.UPDATE_RULE;
  130. var onDelete = ' ON DELETE ' + ref.DELETE_RULE;
  131. return runner.query({
  132. sql: 'alter table ' + tableName + ' add constraint ' + keyName + ' ' + 'foreign key (' + column + ') references ' + inTable + ' (' + references + ')' + onUpdate + onDelete
  133. });
  134. }));
  135. },
  136. index: function index(columns, indexName) {
  137. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
  138. this.pushQuery('alter table ' + this.tableName() + ' add index ' + indexName + '(' + this.formatter.columnize(columns) + ')');
  139. },
  140. primary: function primary(columns, constraintName) {
  141. constraintName = constraintName ? this.formatter.wrap(constraintName) : this.formatter.wrap(this.tableNameRaw + '_pkey');
  142. this.pushQuery('alter table ' + this.tableName() + ' add primary key ' + constraintName + '(' + this.formatter.columnize(columns) + ')');
  143. },
  144. unique: function unique(columns, indexName) {
  145. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, columns);
  146. this.pushQuery('alter table ' + this.tableName() + ' add unique ' + indexName + '(' + this.formatter.columnize(columns) + ')');
  147. },
  148. // Compile a drop index command.
  149. dropIndex: function dropIndex(columns, indexName) {
  150. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
  151. this.pushQuery('alter table ' + this.tableName() + ' drop index ' + indexName);
  152. },
  153. // Compile a drop foreign key command.
  154. dropForeign: function dropForeign(columns, indexName) {
  155. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('foreign', this.tableNameRaw, columns);
  156. this.pushQuery('alter table ' + this.tableName() + ' drop foreign key ' + indexName);
  157. },
  158. // Compile a drop primary key command.
  159. dropPrimary: function dropPrimary() {
  160. this.pushQuery('alter table ' + this.tableName() + ' drop primary key');
  161. },
  162. // Compile a drop unique key command.
  163. dropUnique: function dropUnique(column, indexName) {
  164. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, column);
  165. this.pushQuery('alter table ' + this.tableName() + ' drop index ' + indexName);
  166. }
  167. });
  168. exports.default = TableCompiler_MySQL;
  169. module.exports = exports['default'];