tablecompiler.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _getIterator2 = require('babel-runtime/core-js/get-iterator');
  4. var _getIterator3 = _interopRequireDefault(_getIterator2);
  5. var _typeof2 = require('babel-runtime/helpers/typeof');
  6. var _typeof3 = _interopRequireDefault(_typeof2);
  7. var _has2 = require('lodash/has');
  8. var _has3 = _interopRequireDefault(_has2);
  9. var _helpers = require('../../../helpers');
  10. var _inherits = require('inherits');
  11. var _inherits2 = _interopRequireDefault(_inherits);
  12. var _tablecompiler = require('../../postgres/schema/tablecompiler');
  13. var _tablecompiler2 = _interopRequireDefault(_tablecompiler);
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. function TableCompiler_Redshift() {
  16. _tablecompiler2.default.apply(this, arguments);
  17. } /* eslint max-len: 0 */
  18. // Redshift Table Builder & Compiler
  19. // -------
  20. (0, _inherits2.default)(TableCompiler_Redshift, _tablecompiler2.default);
  21. TableCompiler_Redshift.prototype.index = function (columns, indexName, indexType) {
  22. (0, _helpers.warn)('Redshift does not support the creation of indexes.');
  23. };
  24. TableCompiler_Redshift.prototype.dropIndex = function (columns, indexName) {
  25. (0, _helpers.warn)('Redshift does not support the deletion of indexes.');
  26. };
  27. // TODO: have to disable setting not null on columns that already exist...
  28. // Adds the "create" query to the query sequence.
  29. TableCompiler_Redshift.prototype.createQuery = function (columns, ifNot) {
  30. var createStatement = ifNot ? 'create table if not exists ' : 'create table ';
  31. var sql = createStatement + this.tableName() + ' (' + columns.sql.join(', ') + ')';
  32. if (this.single.inherits) sql += ' like (' + this.formatter.wrap(this.single.inherits) + ')';
  33. this.pushQuery({
  34. sql: sql,
  35. bindings: columns.bindings
  36. });
  37. var hasComment = (0, _has3.default)(this.single, 'comment');
  38. if (hasComment) this.comment(this.single.comment);
  39. };
  40. TableCompiler_Redshift.prototype.primary = function (columns, constraintName) {
  41. var self = this;
  42. constraintName = constraintName ? self.formatter.wrap(constraintName) : self.formatter.wrap(this.tableNameRaw + '_pkey');
  43. if (columns.constructor !== Array) {
  44. columns = [columns];
  45. }
  46. var thiscolumns = self.grouped.columns;
  47. if (thiscolumns) {
  48. var _loop = function _loop(i) {
  49. var exists = thiscolumns.find(function (tcb) {
  50. return tcb.grouping === "columns" && tcb.builder && tcb.builder._method === "add" && tcb.builder._args && tcb.builder._args.indexOf(columns[i]) > -1;
  51. });
  52. if (exists) {
  53. exists = exists.builder;
  54. }
  55. var nullable = !(exists && exists._modifiers && exists._modifiers["nullable"] && exists._modifiers["nullable"][0] === false);
  56. if (nullable) {
  57. if (exists) {
  58. return {
  59. v: (0, _helpers.warn)("Redshift does not allow primary keys to contain nullable columns.")
  60. };
  61. } else {
  62. return {
  63. v: (0, _helpers.warn)("Redshift does not allow primary keys to contain nonexistent columns.")
  64. };
  65. }
  66. }
  67. };
  68. for (var i = 0; i < columns.length; i++) {
  69. var _ret = _loop(i);
  70. if ((typeof _ret === 'undefined' ? 'undefined' : (0, _typeof3.default)(_ret)) === "object") return _ret.v;
  71. }
  72. }
  73. return self.pushQuery('alter table ' + self.tableName() + ' add constraint ' + constraintName + ' primary key (' + self.formatter.columnize(columns) + ')');
  74. };
  75. // Compiles column add. Redshift can only add one column per ALTER TABLE, so core addColumns doesn't work. #2545
  76. TableCompiler_Redshift.prototype.addColumns = function (columns, prefix, colCompilers) {
  77. if (prefix === this.alterColumnsPrefix) {
  78. _tablecompiler2.default.prototype.addColumns.call(this, columns, prefix, colCompilers);
  79. } else {
  80. prefix = prefix || this.addColumnsPrefix;
  81. colCompilers = colCompilers || this.getColumns();
  82. for (var _iterator = colCompilers, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  83. var _ref;
  84. if (_isArray) {
  85. if (_i >= _iterator.length) break;
  86. _ref = _iterator[_i++];
  87. } else {
  88. _i = _iterator.next();
  89. if (_i.done) break;
  90. _ref = _i.value;
  91. }
  92. var col = _ref;
  93. var quotedTableName = this.tableName();
  94. var colCompiled = col.compileColumn();
  95. this.pushQuery({
  96. sql: 'alter table ' + quotedTableName + ' ' + prefix + colCompiled,
  97. bindings: []
  98. });
  99. }
  100. }
  101. };
  102. exports.default = TableCompiler_Redshift;
  103. module.exports = exports['default'];