MCPcopy
hub / github.com/sqldef/sqldef / buildPostgresConstraintName

Function buildPostgresConstraintName

schema/normalize.go:183–213  ·  view source on GitHub ↗

BuildPostgresConstraintName generates a constraint name following PostgreSQL's naming convention. It automatically truncates names to 63 characters (NAMEDATALEN - 1) using PostgreSQL's algorithm: - If column > 28 chars: reduce column to 28 first, then apply remaining overflow to table - If column ==

(tableName, columnName, suffix string)

Source from the content-addressed store, hash-verified

181// - If column < 28 chars: truncate table
182// In summary: when column <= 28, always truncate the table first
183func buildPostgresConstraintName(tableName, columnName, suffix string) string {
184 fullName := fmt.Sprintf("%s_%s_%s", tableName, columnName, suffix)
185 if len(fullName) <= 63 {
186 return fullName
187 }
188
189 overflow := len(fullName) - 63
190 tableLen := len(tableName)
191 columnLen := len(columnName)
192
193 tableRemove := 0
194 columnRemove := 0
195
196 if columnLen > 28 {
197 // Column exceeds 28: reduce to 28 first, then put remaining overflow on table
198 columnRemove = overflow
199 if columnRemove > columnLen-28 {
200 // Column can only be reduced to 28, put the rest on table
201 tableRemove = columnRemove - (columnLen - 28)
202 columnRemove = columnLen - 28
203 }
204 } else {
205 // Column <= 28: always truncate table
206 tableRemove = overflow
207 }
208
209 truncatedTable := tableName[:tableLen-tableRemove]
210 truncatedColumn := columnName[:columnLen-columnRemove]
211
212 return fmt.Sprintf("%s_%s_%s", truncatedTable, truncatedColumn, suffix)
213}
214
215// buildPostgresConstraintNameIdent builds a PostgreSQL auto-generated constraint name
216// and returns it as an Ident with quote information inferred from case.

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected