Back to changelog
New
2 minute read

PostgreSQL Cast Support

Atlas now supports managing type casts in PostgreSQL, including creation with INOUT, custom functions, and assignment casts.

Atlas now supports managing CAST definitions in PostgreSQL, allowing you to define type conversions as part of your schema. This is essential for custom types that need to be converted from standard types like varchar or integer.

Supported Cast Types

TypeDescription
WITH INOUTUses the source type's output function and target type's input function
WITH FUNCTIONUses a custom function to perform the conversion
AS ASSIGNMENTCast is applied implicitly in assignment contexts
AS IMPLICITCast is applied implicitly in any context

HCL Schema Syntax

Define casts directly in your HCL schema. Use with = INOUT for INOUT casts, or reference a function with with = function.name. Use as to specify cast context:

cast {
source = varchar
target = composite.casttesttype
with = INOUT
}
cast {
source = int4
target = composite.casttesttype
with = function.int4_to_casttesttype
as = ASSIGNMENT
}
cast {
source = text
target = composite.casttesttype
with = function.text_to_casttesttype
as = IMPLICIT
}

Generated Migrations

Atlas generates the appropriate CREATE CAST and DROP CAST statements:

-- Create cast with INOUT
CREATE CAST (varchar AS "public"."casttesttype") WITH INOUT;
-- Create cast with function (AS ASSIGNMENT)
CREATE CAST (int4 AS "public"."casttesttype")
WITH FUNCTION "public"."int4_to_casttesttype"(int4) AS ASSIGNMENT;
-- Create cast with function (AS IMPLICIT)
CREATE CAST (text AS "public"."casttesttype")
WITH FUNCTION "public"."text_to_casttesttype"(text) AS IMPLICIT;

Key Features

  • Schema Inspection: Atlas reads cast definitions from pg_cast system catalog
  • Diff Detection: Changes to cast definitions are automatically detected and generate appropriate migrations
  • Function References: Casts can reference custom conversion functions defined in your schema
  • HCL & SQL Parity: Define casts in HCL or SQL with consistent behavior
featurepostgrescasttypes