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
| Type | Description |
|---|---|
| WITH INOUT | Uses the source type's output function and target type's input function |
| WITH FUNCTION | Uses a custom function to perform the conversion |
| AS ASSIGNMENT | Cast is applied implicitly in assignment contexts |
| AS IMPLICIT | Cast 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 = varchartarget = composite.casttesttypewith = INOUT}cast {source = int4target = composite.casttesttypewith = function.int4_to_casttesttypeas = ASSIGNMENT}cast {source = texttarget = composite.casttesttypewith = function.text_to_casttesttypeas = IMPLICIT}
Generated Migrations
Atlas generates the appropriate CREATE CAST and DROP CAST statements:
-- Create cast with INOUTCREATE 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