init.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package db
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/langgenius/dify-plugin-daemon/internal/types/app"
  6. "github.com/langgenius/dify-plugin-daemon/internal/types/models"
  7. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  8. "gorm.io/driver/postgres"
  9. "gorm.io/gorm"
  10. )
  11. func initDifyPluginDB(host string, port int, db_name string, default_db_name string, user string, pass string, sslmode string) error {
  12. // first try to connect to target database
  13. dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s", host, port, user, pass, db_name, sslmode)
  14. db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
  15. if err != nil {
  16. // if connection fails, try to create database
  17. dsn = fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s", host, port, user, pass, default_db_name, sslmode)
  18. db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
  19. if err != nil {
  20. return err
  21. }
  22. pgsqlDB, err := db.DB()
  23. if err != nil {
  24. return err
  25. }
  26. defer pgsqlDB.Close()
  27. // check if the db exists
  28. rows, err := pgsqlDB.Query(fmt.Sprintf("SELECT 1 FROM pg_database WHERE datname = '%s'", db_name))
  29. if err != nil {
  30. return err
  31. }
  32. if !rows.Next() {
  33. // create database
  34. _, err = pgsqlDB.Exec(fmt.Sprintf("CREATE DATABASE %s", db_name))
  35. if err != nil {
  36. return err
  37. }
  38. }
  39. // connect to the new db
  40. dsn = fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s", host, port, user, pass, db_name, sslmode)
  41. db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
  42. if err != nil {
  43. return err
  44. }
  45. }
  46. pgsqlDB, err := db.DB()
  47. if err != nil {
  48. return err
  49. }
  50. // check if uuid-ossp extension exists
  51. rows, err := pgsqlDB.Query("SELECT 1 FROM pg_extension WHERE extname = 'uuid-ossp'")
  52. if err != nil {
  53. return err
  54. }
  55. if !rows.Next() {
  56. // create the uuid-ossp extension
  57. _, err = pgsqlDB.Exec("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\"")
  58. if err != nil {
  59. return err
  60. }
  61. }
  62. pgsqlDB.SetConnMaxIdleTime(time.Minute * 1)
  63. DifyPluginDB = db
  64. return nil
  65. }
  66. func autoMigrate() error {
  67. err := DifyPluginDB.AutoMigrate(
  68. models.Plugin{},
  69. models.PluginInstallation{},
  70. models.PluginDeclaration{},
  71. models.Endpoint{},
  72. models.ServerlessRuntime{},
  73. models.ToolInstallation{},
  74. models.AIModelInstallation{},
  75. models.InstallTask{},
  76. models.TenantStorage{},
  77. models.AgentStrategyInstallation{},
  78. )
  79. if err != nil {
  80. return err
  81. }
  82. // check if "declaration" table exists in Plugin/ServerlessRuntime/ToolInstallation/AIModelInstallation/AgentStrategyInstallation
  83. // delete the column if exists
  84. ignoreDeclarationColumn := func(table string) error {
  85. if DifyPluginDB.Migrator().HasColumn(table, "declaration") {
  86. // remove NOT NULL constraint on declaration column
  87. if err := DifyPluginDB.Exec("ALTER TABLE " + table + " ALTER COLUMN declaration DROP NOT NULL").Error; err != nil {
  88. return err
  89. }
  90. }
  91. return nil
  92. }
  93. if err := ignoreDeclarationColumn("plugins"); err != nil {
  94. return err
  95. }
  96. if err := ignoreDeclarationColumn("serverless_runtimes"); err != nil {
  97. return err
  98. }
  99. if err := ignoreDeclarationColumn("tool_installations"); err != nil {
  100. return err
  101. }
  102. if err := ignoreDeclarationColumn("ai_model_installations"); err != nil {
  103. return err
  104. }
  105. if err := ignoreDeclarationColumn("agent_strategy_installations"); err != nil {
  106. return err
  107. }
  108. return nil
  109. }
  110. func Init(config *app.Config) {
  111. err := initDifyPluginDB(
  112. config.DBHost,
  113. int(config.DBPort),
  114. config.DBDatabase,
  115. "postgres",
  116. config.DBUsername,
  117. config.DBPassword,
  118. config.DBSslMode,
  119. )
  120. if err != nil {
  121. log.Panic("failed to init dify plugin db: %v", err)
  122. }
  123. err = autoMigrate()
  124. if err != nil {
  125. log.Panic("failed to auto migrate: %v", err)
  126. }
  127. log.Info("dify plugin db initialized")
  128. }
  129. func Close() {
  130. db, err := DifyPluginDB.DB()
  131. if err != nil {
  132. log.Error("failed to close dify plugin db: %v", err)
  133. return
  134. }
  135. err = db.Close()
  136. if err != nil {
  137. log.Error("failed to close dify plugin db: %v", err)
  138. return
  139. }
  140. log.Info("dify plugin db closed")
  141. }