Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing setting comments on columns #228

Merged
merged 4 commits into from
Mar 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fixing setting comments on columns
  • Loading branch information
dolezel committed Mar 23, 2018
commit 6899b8a3962eb6752212d24f08c90b68016ed54b
4 changes: 2 additions & 2 deletions lib/operations/tables.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from 'lodash';
import { escapeValue, template, quote, applyType, applyTypeAdapters, comment } from '../utils';
import { escapeValue, template, quote, applyType, applyTypeAdapters, comment, schemalize } from '../utils';

const formatLines = (lines, replace, separator = ',') =>
lines
Expand Down Expand Up @@ -61,7 +61,7 @@ const parseColumns = (tableName, columns, extendingTypeShorthands = {}) => {
}

const comments = _.chain(columnsWithOptions)
.map((options, columnName) => typeof options.comment !== 'undefined' && comment('COLUMN', `${tableName}"."${columnName}`, options.comment))
.map((options, columnName) => typeof options.comment !== 'undefined' && comment('COLUMN', `${schemalize(tableName)}"."${columnName}`, options.comment))
.filter()
.value();

Expand Down
17 changes: 7 additions & 10 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,13 @@ export const applyTypeAdapters = type => (typeAdapters[type] ? typeAdapters[type

export const applyType = (type, extendingTypeShorthands = {}) => {
const typeShorthands = { ...defaultTypeShorthands, ...extendingTypeShorthands };
let options = type;
if (typeof type === 'string') {
options = typeShorthands[type] // eslint-disable-line no-param-reassign
? typeShorthands[type]
: { type };
}

options.type = applyTypeAdapters(options.type); // eslint-disable-line no-param-reassign

return options;
const options = typeof type === 'string' ? { type } : type;
const ext = typeShorthands[options.type] || { type: options.type };
return {
...ext,
...options,
type: applyTypeAdapters(ext.type),
};
};

const formatParam = typeShorthands => (param) => {
Expand Down
7 changes: 7 additions & 0 deletions test/migrations/060_column_comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
exports.comment = 'comment on column id';

exports.up = (pgm) => {
pgm.createTable({ schema: 'circle_test', name: 'tcc' }, {
id: { type: 'id', comment: exports.comment },
});
};
16 changes: 16 additions & 0 deletions test/migrations/061_column_comment_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const column = require('./060_column_comment');

exports.up = pgm =>
new Promise((resolve, reject) =>
pgm.db.select(`SELECT d.description as "comment"
FROM pg_description d join information_schema.columns c on (d.objsubid = c.ordinal_position)
WHERE c.table_schema = 'circle_test' and c.table_name = 'tcc';`)
.then(([{ comment }]) => (
comment === column.comment
? null
: reject(new Error('Comment not set'))
))
.then(resolve)
);

exports.down = () => null;