-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for creating tables and columns with comments (#224)
Update the **create table** and **add column** operations so that they support adding [Postgres comments](https://www.postgresql.org/docs/current/sql-comment.html): * **create table**: comments can be added to the table itself and to each of its columns. * **add column**: a comment can be added to the column. A **create table** migration that includes a comment on the table itself and on one of its columns looks like this: ```json { "name": "12_create_employees_table", "operations": [ { "create_table": { "name": "employees", "comment": "This is a comment for the employees table", "columns": [ { "name": "id", "type": "serial", "pk": true }, { "name": "role", "type": "varchar(255)", "comment": "This is a comment for the role column" } ] } } ] } ``` and an **add column** migration that includes a comment looks like this: ```json { "name": "30_add_column_simple_up", "operations": [ { "add_column": { "table": "people", "up": "'temporary-description'", "column": { "name": "description", "type": "varchar(255)", "nullable": false, "comment": "This is a comment for the description column" } } } ] } ``` This allows new tables and columns to be created with comments. Deletion and modification of comments should still be performed with a raw SQL migration. Until we see a use case that requires versioned modification/removal of comments, these operations are best performed directly on the base table with raw SQL.
- Loading branch information
1 parent
f1757f3
commit 61cc53a
Showing
11 changed files
with
218 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package migrations | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
|
||
"github.com/lib/pq" | ||
) | ||
|
||
func addCommentToColumn(ctx context.Context, conn *sql.DB, tableName, columnName, comment string) error { | ||
_, err := conn.ExecContext(ctx, fmt.Sprintf(`COMMENT ON COLUMN %s.%s IS %s`, | ||
pq.QuoteIdentifier(tableName), | ||
pq.QuoteIdentifier(columnName), | ||
pq.QuoteLiteral(comment))) | ||
|
||
return err | ||
} | ||
|
||
func addCommentToTable(ctx context.Context, conn *sql.DB, tableName, comment string) error { | ||
_, err := conn.ExecContext(ctx, fmt.Sprintf(`COMMENT ON TABLE %s IS %s`, | ||
pq.QuoteIdentifier(tableName), | ||
pq.QuoteLiteral(comment))) | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters