Skip to content

Commit

Permalink
Alter column comment (#292)
Browse files Browse the repository at this point in the history
* Ability to alter only comment on column

* test fix
  • Loading branch information
dolezel authored Jun 29, 2018
1 parent b3ee05a commit d88f694
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 21 deletions.
17 changes: 11 additions & 6 deletions lib/operations/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,17 @@ function alterColumn(tableName, columnName, options) {
actions.push("DROP NOT NULL");
}

const columnsStr = formatLines(actions, ` ALTER "${columnName}" `);
const columnCommentStr =
typeof columnComment !== "undefined"
? `\n${comment("TABLE ", columnName, columnComment)}`
: "";
return template`ALTER TABLE "${tableName}"\n${columnsStr};${columnCommentStr}`;
const queries = [];
if (actions.length > 0) {
const columnsStr = formatLines(actions, ` ALTER "${columnName}" `);
queries.push(template`ALTER TABLE "${tableName}"\n${columnsStr};`);
}
if (typeof columnComment !== "undefined") {
queries.push(
comment("COLUMN ", template`${tableName}"."${columnName}`, columnComment)
);
}
return queries.join("\n");
}

function renameTable(tableName, newName) {
Expand Down
13 changes: 6 additions & 7 deletions test/migrations/060_column_comment.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
exports.comment = "comment on column id";
const schema = "test";
exports.table = { schema: "test", name: "tcc" };

exports.up = pgm => {
pgm.createSchema("test");
pgm.createTable(
{ schema: "test", name: "tcc" },
{
id: { type: "id", comment: exports.comment }
}
);
pgm.createSchema(schema);
pgm.createTable(exports.table, {
id: { type: "id", comment: exports.comment }
});
};
20 changes: 12 additions & 8 deletions test/migrations/061_column_comment_test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
const column = require("./060_column_comment");
const {
comment,
table: { schema, name }
} = 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 = 'test' and c.table_name = 'tcc';`
`SELECT d.description
FROM pg_description d
join information_schema.columns c on (d.objsubid = c.ordinal_position)
join pg_class t ON (t.relname = c.table_name and t.relkind = 'r' and d.objoid = t.oid)
join pg_namespace n ON (t.relnamespace = n.oid and n.nspname = c.table_schema)
WHERE c.column_name = 'id' and c.table_schema = '${schema}' and c.table_name = '${name}';`
)
.then(
([{ comment }]) =>
comment === column.comment
? null
: reject(new Error("Comment not set"))
([{ description }]) =>
description === comment ? null : reject(new Error("Comment not set"))
)
.then(resolve)
);
Expand Down
15 changes: 15 additions & 0 deletions test/migrations/072_alter_column_comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
exports.comment = "This is my comment";
const schema = "comment_schema";
exports.table = { schema, name: "t" };

exports.up = pgm => {
pgm.createSchema(schema);
pgm.createTable(exports.table, { id: "id" });
pgm.alterColumn(exports.table, "id", { type: "text" });
pgm.alterColumn(exports.table, "id", { comment: exports.comment });
};

exports.down = pgm => {
pgm.dropTable(exports.table);
pgm.dropSchema(schema);
};
24 changes: 24 additions & 0 deletions test/migrations/073_alter_column_comment_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const {
table: { schema, name },
comment
} = require("./072_alter_column_comment");

exports.up = pgm =>
new Promise((resolve, reject) =>
pgm.db
.select(
`SELECT d.description
FROM pg_description d
join information_schema.columns c on (d.objsubid = c.ordinal_position)
join pg_class t ON (t.relname = c.table_name and t.relkind = 'r' and d.objoid = t.oid)
join pg_namespace n ON (t.relnamespace = n.oid and n.nspname = c.table_schema)
WHERE c.column_name = 'id' and c.table_schema = '${schema}' and c.table_name = '${name}';`
)
.then(
([{ description }]) =>
description === comment ? null : reject(new Error("Comment not set"))
)
.then(resolve)
);

exports.down = () => null;

0 comments on commit d88f694

Please sign in to comment.