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:
@@ -114,6 +114,24 @@ func (w *Writer) schemaToDBML(schema *models.Schema) string {
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// columnMarkerNote returns the note marking a column the database computes itself
|
||||
// (GENERATED ... STORED, or GENERATED ... AS IDENTITY), or "" for an ordinary column.
|
||||
// The DBML reader turns the note back into the model's Generated/Identity fields.
|
||||
func columnMarkerNote(column *models.Column) string {
|
||||
switch {
|
||||
case column.Generated:
|
||||
return fmt.Sprintf("GENERATED ALWAYS AS (%s) STORED", column.GenerationExpression)
|
||||
case column.Identity && column.IdentityGeneration != "":
|
||||
return fmt.Sprintf("GENERATED %s AS IDENTITY", strings.ToUpper(column.IdentityGeneration))
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// escapeNote escapes a value for use inside a single-quoted DBML note.
|
||||
func escapeNote(s string) string {
|
||||
return strings.ReplaceAll(strings.ReplaceAll(s, `\`, `\\`), `'`, `\'`)
|
||||
}
|
||||
|
||||
// tableToDBML converts a Table to DBML format string
|
||||
func (w *Writer) tableToDBML(t *models.Table) string {
|
||||
var sb strings.Builder
|
||||
@@ -146,6 +164,10 @@ func (w *Writer) tableToDBML(t *models.Table) string {
|
||||
}
|
||||
}
|
||||
|
||||
if marker := columnMarkerNote(column); marker != "" {
|
||||
attrs = append(attrs, fmt.Sprintf("note: '%s'", escapeNote(marker)))
|
||||
}
|
||||
|
||||
if len(attrs) > 0 {
|
||||
fmt.Fprintf(&sb, " [%s]", strings.Join(attrs, ", "))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user