tablecompiler.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _getIterator2 = require('babel-runtime/core-js/get-iterator');
  4. var _getIterator3 = _interopRequireDefault(_getIterator2);
  5. var _has2 = require('lodash/has');
  6. var _has3 = _interopRequireDefault(_has2);
  7. var _inherits = require('inherits');
  8. var _inherits2 = _interopRequireDefault(_inherits);
  9. var _tablecompiler = require('../../../schema/tablecompiler');
  10. var _tablecompiler2 = _interopRequireDefault(_tablecompiler);
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. /* eslint max-len: 0 */
  13. // PostgreSQL Table Builder & Compiler
  14. // -------
  15. function TableCompiler_PG() {
  16. _tablecompiler2.default.apply(this, arguments);
  17. }
  18. (0, _inherits2.default)(TableCompiler_PG, _tablecompiler2.default);
  19. // Compile a rename column command.
  20. TableCompiler_PG.prototype.renameColumn = function (from, to) {
  21. return this.pushQuery({
  22. sql: 'alter table ' + this.tableName() + ' rename ' + this.formatter.wrap(from) + ' to ' + this.formatter.wrap(to)
  23. });
  24. };
  25. TableCompiler_PG.prototype.compileAdd = function (builder) {
  26. var table = this.formatter.wrap(builder);
  27. var columns = this.prefixArray('add column', this.getColumns(builder));
  28. return this.pushQuery({
  29. sql: 'alter table ' + table + ' ' + columns.join(', ')
  30. });
  31. };
  32. // Adds the "create" query to the query sequence.
  33. TableCompiler_PG.prototype.createQuery = function (columns, ifNot) {
  34. var createStatement = ifNot ? 'create table if not exists ' : 'create table ';
  35. var sql = createStatement + this.tableName() + ' (' + columns.sql.join(', ') + ')';
  36. if (this.single.inherits) sql += ' inherits (' + this.formatter.wrap(this.single.inherits) + ')';
  37. this.pushQuery({
  38. sql: sql,
  39. bindings: columns.bindings
  40. });
  41. var hasComment = (0, _has3.default)(this.single, 'comment');
  42. if (hasComment) this.comment(this.single.comment);
  43. };
  44. TableCompiler_PG.prototype.addColumns = function (columns, prefix, colCompilers) {
  45. if (prefix === this.alterColumnsPrefix) {
  46. // alter columns
  47. for (var _iterator = colCompilers, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  48. var _ref;
  49. if (_isArray) {
  50. if (_i >= _iterator.length) break;
  51. _ref = _iterator[_i++];
  52. } else {
  53. _i = _iterator.next();
  54. if (_i.done) break;
  55. _ref = _i.value;
  56. }
  57. var col = _ref;
  58. var quotedTableName = this.tableName();
  59. var colName = col.getColumnName();
  60. var type = col.getColumnType();
  61. this.pushQuery({
  62. sql: 'alter table ' + quotedTableName + ' alter column "' + colName + '" drop default',
  63. bindings: []
  64. });
  65. this.pushQuery({
  66. sql: 'alter table ' + quotedTableName + ' alter column "' + colName + '" drop not null',
  67. bindings: []
  68. });
  69. this.pushQuery({
  70. sql: 'alter table ' + quotedTableName + ' alter column "' + colName + '" type ' + type + ' using ("' + colName + '"::' + type + ')',
  71. bindings: []
  72. });
  73. var defaultTo = col.modified['defaultTo'];
  74. if (defaultTo) {
  75. var modifier = col.defaultTo.apply(col, defaultTo);
  76. this.pushQuery({
  77. sql: 'alter table ' + quotedTableName + ' alter column "' + colName + '" set ' + modifier,
  78. bindings: []
  79. });
  80. }
  81. var nullable = col.modified['nullable'];
  82. if (nullable && nullable[0] === false) {
  83. this.pushQuery({
  84. sql: 'alter table ' + quotedTableName + ' alter column "' + colName + '" set not null',
  85. bindings: []
  86. });
  87. }
  88. }
  89. } else {
  90. // base class implementation for normal add
  91. _tablecompiler2.default.prototype.addColumns.call(this, columns, prefix);
  92. }
  93. };
  94. // Compiles the comment on the table.
  95. TableCompiler_PG.prototype.comment = function (comment) {
  96. this.pushQuery('comment on table ' + this.tableName() + ' is \'' + (this.single.comment || '') + '\'');
  97. };
  98. // Indexes:
  99. // -------
  100. TableCompiler_PG.prototype.primary = function (columns, constraintName) {
  101. constraintName = constraintName ? this.formatter.wrap(constraintName) : this.formatter.wrap(this.tableNameRaw + '_pkey');
  102. this.pushQuery('alter table ' + this.tableName() + ' add constraint ' + constraintName + ' primary key (' + this.formatter.columnize(columns) + ')');
  103. };
  104. TableCompiler_PG.prototype.unique = function (columns, indexName) {
  105. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, columns);
  106. this.pushQuery('alter table ' + this.tableName() + ' add constraint ' + indexName + ' unique (' + this.formatter.columnize(columns) + ')');
  107. };
  108. TableCompiler_PG.prototype.index = function (columns, indexName, indexType) {
  109. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
  110. this.pushQuery('create index ' + indexName + ' on ' + this.tableName() + (indexType && ' using ' + indexType || '') + ' (' + this.formatter.columnize(columns) + ')');
  111. };
  112. TableCompiler_PG.prototype.dropPrimary = function (constraintName) {
  113. constraintName = constraintName ? this.formatter.wrap(constraintName) : this.formatter.wrap(this.tableNameRaw + '_pkey');
  114. this.pushQuery('alter table ' + this.tableName() + ' drop constraint ' + constraintName);
  115. };
  116. TableCompiler_PG.prototype.dropIndex = function (columns, indexName) {
  117. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
  118. this.pushQuery('drop index ' + indexName);
  119. };
  120. TableCompiler_PG.prototype.dropUnique = function (columns, indexName) {
  121. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, columns);
  122. this.pushQuery('alter table ' + this.tableName() + ' drop constraint ' + indexName);
  123. };
  124. TableCompiler_PG.prototype.dropForeign = function (columns, indexName) {
  125. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('foreign', this.tableNameRaw, columns);
  126. this.pushQuery('alter table ' + this.tableName() + ' drop constraint ' + indexName);
  127. };
  128. exports.default = TableCompiler_PG;
  129. module.exports = exports['default'];