Skip to content

Commit 11e00ea

Browse files
authored
refactor: remove uuid-ossp dependency and update ID generation logic (#536)
* refactor: remove uuid-ossp dependency and update ID generation logic * fix: condition error
1 parent 0eb2cbe commit 11e00ea

File tree

3 files changed

+25
-30
lines changed

3 files changed

+25
-30
lines changed

internal/db/mysql/dialector.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,18 @@ type myMigrator struct {
3434
func (migrator myMigrator) FullDataTypeOf(field *schema.Field) clause.Expr {
3535
if field.DataType == "uuid" {
3636
field.DataType = "char(36)"
37-
if field.HasDefaultValue && field.DefaultValue == "uuid_generate_v4()" {
38-
field.HasDefaultValue = false
39-
field.DefaultValue = ""
40-
var fieldsWithDefault []*schema.Field
41-
for _, fieldWithDefault := range field.Schema.FieldsWithDefaultDBValue {
42-
if fieldWithDefault.DBName != "id" {
43-
fieldsWithDefault = append(fieldsWithDefault, fieldWithDefault)
44-
}
45-
}
46-
field.Schema.FieldsWithDefaultDBValue = fieldsWithDefault
47-
}
37+
// remove default value uuid_generate_v4(), see https://github.com/langgenius/dify-plugin-daemon/issues/469
38+
// if field.HasDefaultValue && field.DefaultValue == "uuid_generate_v4()" {
39+
// field.HasDefaultValue = false
40+
// field.DefaultValue = ""
41+
// var fieldsWithDefault []*schema.Field
42+
// for _, fieldWithDefault := range field.Schema.FieldsWithDefaultDBValue {
43+
// if fieldWithDefault.DBName != "id" {
44+
// fieldsWithDefault = append(fieldsWithDefault, fieldWithDefault)
45+
// }
46+
// }
47+
// field.Schema.FieldsWithDefaultDBValue = fieldsWithDefault
48+
// }
4849
} else if field.DataType == "text" {
4950
field.DataType = "longtext"
5051
}

internal/db/pg/pg.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,6 @@ func InitPluginDB(config *PGConfig) (*gorm.DB, error) {
6969
return nil, err
7070
}
7171

72-
// check if uuid-ossp extension exists
73-
rows, err := pgsqlDB.Query("SELECT 1 FROM pg_extension WHERE extname = 'uuid-ossp'")
74-
if err != nil {
75-
return nil, err
76-
}
77-
78-
if !rows.Next() {
79-
// create the uuid-ossp extension
80-
_, err = pgsqlDB.Exec("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\"")
81-
if err != nil {
82-
return nil, err
83-
}
84-
}
85-
8672
// configure connection pool
8773
pgsqlDB.SetMaxIdleConns(config.MaxIdleConns)
8874
pgsqlDB.SetMaxOpenConns(config.MaxOpenConns)

internal/types/models/base.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,22 @@ import (
88
)
99

1010
type Model struct {
11-
ID string `gorm:"column:id;primaryKey;type:uuid;default:uuid_generate_v4()" json:"id"`
11+
ID string `gorm:"column:id;primaryKey;type:uuid;" json:"id"`
1212
CreatedAt time.Time `json:"created_at"`
1313
UpdatedAt time.Time `json:"updated_at"`
1414
}
1515

16-
func (m *Model) BeforeCreate(tx *gorm.DB) (err error) {
17-
if tx.Dialector.Name() == "mysql" && m.ID == "" {
18-
m.ID = uuid.New().String()
16+
// BeforeCreate sets the ID field to a new UUID v7 value before creating a new record.
17+
// ISSUE: https://github.com/langgenius/dify-plugin-daemon/issues/469
18+
//
19+
// to support both pgsql17 and mysql, `generate_uuid_v4` was removed from the database side.
20+
// as it's not compatible to some of the DB versions.
21+
func (m *Model) BeforeCreate(tx *gorm.DB) error {
22+
uuid, err := uuid.NewV7()
23+
if err != nil {
24+
return err
1925
}
20-
return
26+
m.ID = uuid.String()
27+
28+
return nil
2129
}

0 commit comments

Comments
 (0)