feat(models): add support for generated and identity columns
* extend Column struct with Generated, GenerationExpression, Identity, and IdentityGeneration fields * update queryColumns to handle new column properties * modify migration writer to avoid altering generated and identity columns * add tests to ensure correct handling of generated and identity columns
This commit is contained in:
@@ -243,7 +243,11 @@ func (r *Reader) queryColumns(schemaName string) (map[string]map[string]*models.
|
||||
c.numeric_scale,
|
||||
c.udt_name,
|
||||
pg_catalog.format_type(a.atttypid, a.atttypmod) as formatted_data_type,
|
||||
col_description((c.table_schema||'.'||c.table_name)::regclass, c.ordinal_position) as description
|
||||
col_description((c.table_schema||'.'||c.table_name)::regclass, c.ordinal_position) as description,
|
||||
c.is_generated,
|
||||
c.generation_expression,
|
||||
c.is_identity,
|
||||
c.identity_generation
|
||||
FROM information_schema.columns c
|
||||
JOIN pg_catalog.pg_namespace n
|
||||
ON n.nspname = c.table_schema
|
||||
@@ -268,17 +272,36 @@ func (r *Reader) queryColumns(schemaName string) (map[string]map[string]*models.
|
||||
columnsMap := make(map[string]map[string]*models.Column)
|
||||
|
||||
for rows.Next() {
|
||||
var schema, tableName, columnName, isNullable, dataType, udtName, formattedDataType string
|
||||
var schema, tableName, columnName, isNullable, dataType, udtName, formattedDataType, isGenerated, isIdentity string
|
||||
var ordinalPosition int
|
||||
var columnDefault, description *string
|
||||
var columnDefault, description, generationExpression, identityGeneration *string
|
||||
var charMaxLength, numPrecision, numScale *int
|
||||
|
||||
if err := rows.Scan(&schema, &tableName, &columnName, &ordinalPosition, &columnDefault, &isNullable, &dataType, &charMaxLength, &numPrecision, &numScale, &udtName, &formattedDataType, &description); err != nil {
|
||||
if err := rows.Scan(&schema, &tableName, &columnName, &ordinalPosition, &columnDefault, &isNullable, &dataType, &charMaxLength, &numPrecision, &numScale, &udtName, &formattedDataType, &description, &isGenerated, &generationExpression, &isIdentity, &identityGeneration); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
column := models.InitColumn(columnName, tableName, schema)
|
||||
|
||||
// GENERATED ALWAYS ... STORED columns are computed by Postgres and cannot
|
||||
// have their default altered/dropped like a regular column.
|
||||
column.Generated = isGenerated == "ALWAYS"
|
||||
if generationExpression != nil {
|
||||
column.GenerationExpression = *generationExpression
|
||||
}
|
||||
|
||||
// GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY columns are driven by an
|
||||
// internal sequence rather than a literal default (unlike serial columns,
|
||||
// they carry no pg_attrdef row at all), so they need the same DB-side
|
||||
// handling as generated columns even though the underlying mechanism differs.
|
||||
column.Identity = isIdentity == "YES"
|
||||
if identityGeneration != nil {
|
||||
column.IdentityGeneration = strings.ToUpper(strings.TrimSpace(*identityGeneration))
|
||||
}
|
||||
if column.Identity {
|
||||
column.AutoIncrement = true
|
||||
}
|
||||
|
||||
// Check if this is a serial type (has nextval default)
|
||||
hasNextval := false
|
||||
if columnDefault != nil {
|
||||
@@ -301,6 +324,24 @@ func (r *Reader) queryColumns(schemaName string) (map[string]map[string]*models.
|
||||
column.Description = *description
|
||||
}
|
||||
|
||||
if column.Generated {
|
||||
note := "GENERATED ALWAYS AS (" + column.GenerationExpression + ") STORED"
|
||||
if column.Description != "" {
|
||||
column.Description = column.Description + " " + note
|
||||
} else {
|
||||
column.Description = note
|
||||
}
|
||||
}
|
||||
|
||||
if column.Identity {
|
||||
note := "GENERATED " + column.IdentityGeneration + " AS IDENTITY"
|
||||
if column.Description != "" {
|
||||
column.Description = column.Description + " " + note
|
||||
} else {
|
||||
column.Description = note
|
||||
}
|
||||
}
|
||||
|
||||
if charMaxLength != nil {
|
||||
column.Length = *charMaxLength
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user