feat(type_mapper): add support for serial types and auto-increment tags
All checks were successful
CI / Build (push) Successful in -25m39s
Integration Tests / Integration Tests (push) Successful in -25m15s
CI / Test (1.24) (push) Successful in -24m39s
CI / Test (1.25) (push) Successful in -24m24s
CI / Lint (push) Successful in -25m9s
Release / Build and Release (push) Successful in -25m21s
All checks were successful
CI / Build (push) Successful in -25m39s
Integration Tests / Integration Tests (push) Successful in -25m15s
CI / Test (1.24) (push) Successful in -24m39s
CI / Test (1.25) (push) Successful in -24m24s
CI / Lint (push) Successful in -25m9s
Release / Build and Release (push) Successful in -25m21s
This commit is contained in:
@@ -62,6 +62,17 @@ func (tm *TypeMapper) isSimpleType(sqlType string) bool {
|
|||||||
return simpleTypes[sqlType]
|
return simpleTypes[sqlType]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isSerialType checks if a SQL type is a serial type (auto-incrementing)
|
||||||
|
func (tm *TypeMapper) isSerialType(sqlType string) bool {
|
||||||
|
baseType := tm.extractBaseType(sqlType)
|
||||||
|
serialTypes := map[string]bool{
|
||||||
|
"serial": true,
|
||||||
|
"bigserial": true,
|
||||||
|
"smallserial": true,
|
||||||
|
}
|
||||||
|
return serialTypes[baseType]
|
||||||
|
}
|
||||||
|
|
||||||
// baseGoType returns the base Go type for a SQL type (not null, simple types only)
|
// baseGoType returns the base Go type for a SQL type (not null, simple types only)
|
||||||
func (tm *TypeMapper) baseGoType(sqlType string) string {
|
func (tm *TypeMapper) baseGoType(sqlType string) string {
|
||||||
typeMap := map[string]string{
|
typeMap := map[string]string{
|
||||||
@@ -190,6 +201,11 @@ func (tm *TypeMapper) BuildBunTag(column *models.Column, table *models.Table) st
|
|||||||
parts = append(parts, "pk")
|
parts = append(parts, "pk")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Auto increment (for serial types or explicit auto_increment)
|
||||||
|
if column.AutoIncrement || tm.isSerialType(column.Type) {
|
||||||
|
parts = append(parts, "autoincrement")
|
||||||
|
}
|
||||||
|
|
||||||
// Default value
|
// Default value
|
||||||
if column.Default != nil {
|
if column.Default != nil {
|
||||||
// Sanitize default value to remove backticks
|
// Sanitize default value to remove backticks
|
||||||
|
|||||||
@@ -90,8 +90,8 @@ func TestWriter_WriteTable(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Verify Bun-specific elements
|
// Verify Bun-specific elements
|
||||||
if !strings.Contains(generated, "bun:\"id,type:bigint,pk,") {
|
if !strings.Contains(generated, "bun:\"id,type:bigint,pk,autoincrement,") {
|
||||||
t.Errorf("Missing Bun-style primary key tag")
|
t.Errorf("Missing Bun-style primary key tag with autoincrement")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -624,6 +624,37 @@ func TestTypeMapper_BuildBunTag(t *testing.T) {
|
|||||||
},
|
},
|
||||||
want: []string{"status,", "type:text,", "default:active,"},
|
want: []string{"status,", "type:text,", "default:active,"},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "auto increment with AutoIncrement flag",
|
||||||
|
column: &models.Column{
|
||||||
|
Name: "id",
|
||||||
|
Type: "bigint",
|
||||||
|
NotNull: true,
|
||||||
|
IsPrimaryKey: true,
|
||||||
|
AutoIncrement: true,
|
||||||
|
},
|
||||||
|
want: []string{"id,", "type:bigint,", "pk,", "autoincrement,"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "serial type (auto-increment)",
|
||||||
|
column: &models.Column{
|
||||||
|
Name: "id",
|
||||||
|
Type: "serial",
|
||||||
|
NotNull: true,
|
||||||
|
IsPrimaryKey: true,
|
||||||
|
},
|
||||||
|
want: []string{"id,", "type:serial,", "pk,", "autoincrement,"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "bigserial type (auto-increment)",
|
||||||
|
column: &models.Column{
|
||||||
|
Name: "id",
|
||||||
|
Type: "bigserial",
|
||||||
|
NotNull: true,
|
||||||
|
IsPrimaryKey: true,
|
||||||
|
},
|
||||||
|
want: []string{"id,", "type:bigserial,", "pk,", "autoincrement,"},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
|||||||
Reference in New Issue
Block a user