@@ -19,6 +19,7 @@ import {
1919 PgView ,
2020 uniqueKeyName ,
2121} from 'drizzle-orm/pg-core' ;
22+ import { parse as parsePostgresArray } from 'postgres-array' ;
2223import { CasingType } from 'src/cli/validations/common' ;
2324import { vectorOps } from 'src/extensions/vector' ;
2425import { withStyle } from '../cli/validations/outputs' ;
@@ -40,7 +41,6 @@ import type {
4041 UniqueConstraint ,
4142 View ,
4243} from '../serializer/pgSchema' ;
43- import { parse as parsePostgresArray } from 'postgres-array' ;
4444import { type DB , escapeSingleQuotes , isPgArrayType } from '../utils' ;
4545import { getColumnCasing , sqlToStr } from './utils' ;
4646
@@ -1942,21 +1942,25 @@ WHERE
19421942 */
19431943const formatArrayElement = ( element : any , dataType : string ) : string | null => {
19441944 if ( element === null ) return element ;
1945-
1945+
19461946 // Remove outer quotes from postgres-array parsed elements if present
19471947 // First trim spaces since postgres-array includes leading spaces in array elements
19481948 let cleanElement = typeof element === 'string' ? element . trim ( ) : element ;
1949- if ( typeof cleanElement === 'string' && ( ( cleanElement . startsWith ( "'" ) && cleanElement . endsWith ( "'" ) ) || ( cleanElement . startsWith ( '"' ) && cleanElement . endsWith ( '"' ) ) ) ) {
1949+ if (
1950+ typeof cleanElement === 'string'
1951+ && ( ( cleanElement . startsWith ( "'" ) && cleanElement . endsWith ( "'" ) )
1952+ || ( cleanElement . startsWith ( '"' ) && cleanElement . endsWith ( '"' ) ) )
1953+ ) {
19501954 cleanElement = cleanElement . slice ( 1 , - 1 ) ;
19511955 }
19521956
19531957 // remove [] from dataType if it exists
1954- if ( ! dataType . endsWith ( "[]" ) ) {
1958+ if ( ! dataType . endsWith ( '[]' ) ) {
19551959 throw new Error ( `array dataType ${ dataType } does not end with '[]'` ) ;
19561960 }
19571961
1958- const baseDataType = dataType . slice ( 0 , - "[]" . length ) ;
1959-
1962+ const baseDataType = dataType . slice ( 0 , - '[]' . length ) ;
1963+
19601964 if ( [ 'integer' , 'smallint' , 'bigint' , 'double precision' , 'real' ] . includes ( baseDataType ) ) {
19611965 return cleanElement ;
19621966 } else if ( dataType . startsWith ( 'timestamp' ) ) {
@@ -1968,7 +1972,7 @@ const formatArrayElement = (element: any, dataType: string): string | null => {
19681972 } else if ( [ 'json' , 'jsonb' ] . includes ( baseDataType ) ) {
19691973 // For JSON/JSONB arrays, cleanElement is already a JSON string
19701974 // We just need to ensure it's properly quoted
1971-
1975+
19721976 // First, try to parse it to validate it's valid JSON
19731977 const parsed = JSON . parse ( cleanElement ) ;
19741978 // Then stringify it back to ensure consistent formatting
@@ -1986,36 +1990,38 @@ const handleArrayDefault = (columnDefaultAsString: string, dataType: string): st
19861990 // Handle common simple cases
19871991 if ( columnDefaultAsString === '{}' || columnDefaultAsString === "'{}'" ) {
19881992 return "'{}'" ;
1989- } else if ( columnDefaultAsString === '{""}' || columnDefaultAsString === "'{\"\"}'" ) {
1990- return "'{\"\"}'" ;
1993+ } else if ( columnDefaultAsString === '{""}' || columnDefaultAsString === '\'{""}\'' ) {
1994+ return '\'{""}\'' ;
19911995 }
19921996
19931997 // Convert ARRAY constructor syntax to PostgreSQL bracket notation that postgres-array can parse
19941998 let normalizedArrayString = columnDefaultAsString ;
1995-
1999+
19962000 if ( columnDefaultAsString . startsWith ( 'ARRAY[' ) && columnDefaultAsString . endsWith ( ']' ) ) {
19972001 // Convert ARRAY['a'::text, 'b', 'c'::varchar] -> {'a', 'b', 'c'}
19982002 const content = columnDefaultAsString . slice ( 6 , - 1 ) ; // Remove 'ARRAY[' and ']'
1999-
2003+
20002004 // Remove type casting from individual elements (::text, ::varchar, etc.)
20012005 const cleanContent = content . replace ( / : : \w + / g, '' ) ;
20022006 normalizedArrayString = `{${ cleanContent } }` ;
20032007 }
2004-
2008+
20052009 // Handle various bracket notation formats to ensure compatibility with postgres-array
20062010 if ( normalizedArrayString . startsWith ( "'{" ) && normalizedArrayString . endsWith ( "}'" ) ) {
20072011 normalizedArrayString = normalizedArrayString . slice ( 1 , - 1 ) ; // Remove outer quotes
2008- } else if ( ! normalizedArrayString . startsWith ( "{" ) && ! normalizedArrayString . startsWith ( "'" ) && normalizedArrayString !== '{}' ) {
2012+ } else if (
2013+ ! normalizedArrayString . startsWith ( '{' ) && ! normalizedArrayString . startsWith ( "'" ) && normalizedArrayString !== '{}'
2014+ ) {
20092015 // Handle cases where array string doesn't have proper brackets
20102016 normalizedArrayString = `{${ normalizedArrayString } }` ;
20112017 }
20122018
20132019 // Use postgres-array library to parse the normalized string
20142020 const parsedArray = [ ...parsePostgresArray ( normalizedArrayString ) ] ;
2015-
2021+
20162022 // Format elements according to data type
20172023 const formattedElements = parsedArray . map ( ( element ) => formatArrayElement ( element , dataType ) ) ;
2018-
2024+
20192025 return `'{${ formattedElements . join ( ',' ) } }'` ;
20202026} ;
20212027
0 commit comments