Skip to content

Type Operations ​

Operation: createType ​

pgm.createType( type_name, values, type_options ) ​

IMPORTANT

Create a new data type - postgres docs Alias: addType

Arguments ​

NameTypeDescription
type_nameNamename of the new type
valuesarray[string] or objectpossible values for an enum type or names and types for a composite type
type_optionsobjectCheck below for available options

Options ​

CREATE TYPE itself takes no options - these are forwarded to the reverse operation (dropType) when the migration is rolled back.

OptionTypeDescription
ifExistsbooleandrops type only if it exists
cascadebooleandrops also dependent objects

Reverse Operation: dropType ​

pgm.dropType( type_name, drop_options ) ​

IMPORTANT

Drop a custom data type - postgres docs

Arguments ​

NameTypeDescription
type_nameNamename of the type to drop
drop_optionsobjectCheck below for available options

Options ​

OptionTypeDescription
ifExistsbooleandrops type only if it exists
cascadebooleandrops also dependent objects

Operation: alterType ​

pgm.renameType( type_name, new_type_name ) ​

IMPORTANT

Rename a data type - postgres docs

Arguments ​

NameTypeDescription
type_nameNamename of the type to rename
new_type_nameNamename of the new type

Renaming preserves the source schema, including during automatic reversal. See Renaming and schemas for destination-schema validation and supported PgLiteral identifiers.

To move a type between schemas, use SQL explicitly, for example pgm.sql('ALTER TYPE "old_schema"."my_type" SET SCHEMA "new_schema"'). Provide the corresponding SQL in your down migration to reverse that move.

Operation: alterType ​

pgm.addTypeAttribute( type_name, attribute_name, attribute_type, attribute_options ) ​

IMPORTANT

Add attribute to an existing data type - postgres docs

Arguments ​

NameTypeDescription
type_nameNamename of the type
attribute_namestringname of the attribute to add
attribute_typestringtype of the attribute to add
attribute_optionsobjectCheck below for available options

Options ​

ADD ATTRIBUTE itself takes no options - these are forwarded to the reverse operation (dropTypeAttribute) when the migration is rolled back.

OptionTypeDescription
ifExistsbooleanrollback drops the attribute only if it still exists

Example: automatic rollback ​

For an existing composite type, pass ifExists as the fourth argument to make automatic rollback tolerate an already removed attribute:

js
export const up = (pgm) => {
  pgm.addTypeAttribute('compfoo', 'f3', 'integer', { ifExists: true });
};

With no explicit down, rollback generates ALTER TYPE "compfoo" DROP ATTRIBUTE IF EXISTS "f3";.

Reverse Operation: dropTypeAttribute ​

pgm.dropTypeAttribute( type_name, attribute_name, options ) ​

IMPORTANT

Drop attribute from a data type - postgres docs

Arguments ​

NameTypeDescription
type_nameNamename of the type
attribute_namestringname of the attribute to drop
optionsobjectCheck below for available options

Options ​

OptionTypeDescription
ifExistsbooleandrops attribute only if it exists

Example: conditional removal ​

js
export const up = (pgm) => {
  pgm.dropTypeAttribute('compfoo', 'f3', { ifExists: true });
};

This generates ALTER TYPE "compfoo" DROP ATTRIBUTE IF EXISTS "f3";. ifExists suppresses an error for a missing attribute; the type must still exist.

Operation: alterType ​

pgm.setTypeAttribute( type_name, attribute_name, attribute_type ) ​

IMPORTANT

Set data type of an existing data attribute type - postgres docs

Arguments ​

NameTypeDescription
type_nameNamename of the type
attribute_namestringname of the attribute to set
attribute_typestringnew type of the attribute

Operation: alterType ​

pgm.addTypeValue( type_name, value, options ) ​

IMPORTANT

Add value to a list of enum data type - postgres docs

Arguments ​

NameTypeDescription
type_nameNamename of the type
valuestringvalue to add to list
optionsobjectCheck below for available options

Options ​

OptionTypeDescription
ifNotExistsbooleandefault false
beforestringvalue before which the new value should be add
afterstringvalue after which the new value should be add

Reverse Operation: dropTypeValue ​

pgm.renameTypeAttribute( type_name, attribute_name, new_attribute_name ) ​

IMPORTANT

Rename an attribute of data type - postgres docs

Arguments ​

NameTypeDescription
type_nameNamename of the type
attribute_namestringname of the attribute to rename
new_attribute_namestringnew name of the attribute

Operation: alterType ​

pgm.renameTypeValue( type_name, value, new_value ) ​

IMPORTANT

Rename a value of enum data type - postgres docs

Arguments ​

NameTypeDescription
type_nameNamename of the type
valuestringvalue to rename
new_valuestringnew value