feat: mark generated and identity columns across writers and DBML
- bun: scanonly+generated for GENERATED columns; scanonly+identity for ALWAYS identity (PK gets identity only) - gorm: <-:false+generated / identity, same primary key rule - dbml: write GENERATED/IDENTITY column notes and read them back into the model - pgsql: emit generation/identity clauses in migration create-table and add-column, no DEFAULT - readmes updated
This commit is contained in:
@@ -77,6 +77,10 @@ type ColumnData struct {
|
||||
Type string
|
||||
Default string
|
||||
NotNull bool
|
||||
// Generated is the expression of a GENERATED ALWAYS AS (...) STORED column.
|
||||
Generated string
|
||||
// Identity is "ALWAYS" or "BY DEFAULT" for a GENERATED ... AS IDENTITY column.
|
||||
Identity string
|
||||
}
|
||||
|
||||
// AddColumnData contains data for add column template
|
||||
@@ -87,6 +91,10 @@ type AddColumnData struct {
|
||||
ColumnType string
|
||||
Default string
|
||||
NotNull bool
|
||||
// Generated is the expression of a GENERATED ALWAYS AS (...) STORED column.
|
||||
Generated string
|
||||
// Identity is "ALWAYS" or "BY DEFAULT" for a GENERATED ... AS IDENTITY column.
|
||||
Identity string
|
||||
}
|
||||
|
||||
// AlterColumnTypeWithCheckData contains data for the guarded alter column
|
||||
@@ -520,6 +528,19 @@ func (te *TemplateExecutor) ExecuteCreatePrimaryKeyWithAutoGenCheck(data CreateP
|
||||
|
||||
// Helper functions to build template data from models
|
||||
|
||||
// columnGenerationClauses returns the generation expression of a GENERATED ... STORED
|
||||
// column and the generation mode of an identity column ("" for each that does not apply).
|
||||
// Such columns cannot carry a DEFAULT.
|
||||
func columnGenerationClauses(col *models.Column) (generated, identity string) {
|
||||
if col.Generated && col.GenerationExpression != "" {
|
||||
return col.GenerationExpression, ""
|
||||
}
|
||||
if col.Identity && col.IdentityGeneration != "" {
|
||||
return "", strings.ToUpper(col.IdentityGeneration)
|
||||
}
|
||||
return "", ""
|
||||
}
|
||||
|
||||
// BuildCreateTableData builds CreateTableData from a models.Table
|
||||
func BuildCreateTableData(schemaName string, table *models.Table) CreateTableData {
|
||||
columns := make([]ColumnData, 0, len(table.Columns))
|
||||
@@ -532,7 +553,8 @@ func BuildCreateTableData(schemaName string, table *models.Table) CreateTableDat
|
||||
Type: col.Type,
|
||||
NotNull: col.NotNull,
|
||||
}
|
||||
if col.Default != nil {
|
||||
colData.Generated, colData.Identity = columnGenerationClauses(col)
|
||||
if col.Default != nil && colData.Generated == "" && colData.Identity == "" {
|
||||
if value, ok := col.Default.(string); ok {
|
||||
colData.Default = writers.QuoteDefaultValue(stripBackticks(value), col.Type)
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user