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

Renaming of enum values #293

Merged
merged 9 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions docs/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,15 @@
- `type_name` _[string]_ - name of the type
- `attribute_name` _[string]_ - name of the attribute to rename
- `new_attribute_name` _[string]_ - new name of the attribute

---

### `pgm.renameTypeValue( type_name, value, new_value )`

> Rename a value of enum data type - [postgres docs](https://www.postgresql.org/docs/current/static/sql-altertype.html)

**Arguments:**

- `type_name` _[string]_ - name of the type
- `value` _[string]_ - value to rename
- `new_value` _[string]_ - new value
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Definitions by: Bradley Ayers <https://github.com/bradleyayers>
// Definitions by: Sam Grönblom <https://github.com/sgronblo>
// Definitions by: Jan Doležel <https://github.com/dolezel>
// Definitions by: Christopher Quadflieg <https://github.com/Shinigami92>

interface ValueArray extends Array<Value> {}

Expand Down Expand Up @@ -391,6 +392,7 @@ export interface MigrationBuilder {
setTypeAttribute(typeName: Name, attributeName: string, attributeType: Type): void
addTypeValue(typeName: Name, value: Value, options?: { ifNotExists?: boolean, before?: string, after?: string }): void
renameTypeAttribute(typeName: Name, attributeName: string, newAttributeName: string): void
renameTypeValue(typeName: Name, value: string, newValue: string): void

// Roles
createRole(roleName: Name, roleOptions?: RoleOptions): void
Expand Down
1 change: 1 addition & 0 deletions lib/migration-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ module.exports = class MigrationBuilder {
this.addType = this.createType;
this.renameType = wrap(types.renameType);
this.renameTypeAttribute = wrap(types.renameTypeAttribute);
this.renameTypeValue = wrap(types.renameTypeValue);
this.addTypeAttribute = wrap(types.addTypeAttribute(typeShorthands));
this.dropTypeAttribute = wrap(types.dropTypeAttribute);
this.setTypeAttribute = wrap(types.setTypeAttribute(typeShorthands));
Expand Down
11 changes: 11 additions & 0 deletions lib/operations/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,18 @@ function renameTypeAttribute(typeName, attributeName, newAttributeName) {
const undoRenameTypeAttribute = (typeName, attributeName, newAttributeName) =>
renameTypeAttribute(typeName, newAttributeName, attributeName);

function renameTypeValue(typeName, value, newValue) {
const valueStr = escapeValue(value);
const newValueStr = escapeValue(newValue);
return template`ALTER TYPE "${typeName}" RENAME VALUE ${valueStr} TO ${newValueStr};`;
}

const undoRenameTypeValue = (typeName, value, newValue) =>
renameTypeValue(typeName, newValue, value);

renameType.reverse = undoRename;
renameTypeAttribute.reverse = undoRenameTypeAttribute;
renameTypeValue.reverse = undoRenameTypeValue;

module.exports = {
createType,
Expand All @@ -84,5 +94,6 @@ module.exports = {
dropTypeAttribute,
setTypeAttribute,
renameTypeAttribute,
renameTypeValue,
addTypeValue
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Postgresql database migration management tool for node.js",
"author": "Theo Ephraim",
"contributors": [
"Jan Doležel <[email protected]> (http://www.eithel.net/)"
"Jan Doležel <[email protected]> (http://www.eithel.net/)",
"Christopher Quadflieg <[email protected]>"
],
"bin": {
"node-pg-migrate": "bin/node-pg-migrate"
Expand Down
4 changes: 4 additions & 0 deletions test/migrations/074_rename_type_value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exports.up = pgm => {
pgm.createType("list2", ["a", "d", "c"]);
pgm.renameTypeValue("list2", "d", "b");
};