init.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package db
  2. import (
  3. "github.com/langgenius/dify-plugin-daemon/internal/db/mysql"
  4. "github.com/langgenius/dify-plugin-daemon/internal/db/pg"
  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. )
  9. func autoMigrate() error {
  10. err := DifyPluginDB.AutoMigrate(
  11. models.Plugin{},
  12. models.PluginInstallation{},
  13. models.PluginDeclaration{},
  14. models.Endpoint{},
  15. models.ServerlessRuntime{},
  16. models.ToolInstallation{},
  17. models.AIModelInstallation{},
  18. models.InstallTask{},
  19. models.TenantStorage{},
  20. models.AgentStrategyInstallation{},
  21. )
  22. if err != nil {
  23. return err
  24. }
  25. // check if "declaration" table exists in Plugin/ServerlessRuntime/ToolInstallation/AIModelInstallation/AgentStrategyInstallation
  26. // delete the column if exists
  27. ignoreDeclarationColumn := func(table string) error {
  28. if DifyPluginDB.Migrator().HasColumn(table, "declaration") {
  29. // remove NOT NULL constraint on declaration column
  30. if err := DifyPluginDB.Exec("ALTER TABLE " + table + " ALTER COLUMN declaration DROP NOT NULL").Error; err != nil {
  31. return err
  32. }
  33. }
  34. return nil
  35. }
  36. if err := ignoreDeclarationColumn("plugins"); err != nil {
  37. return err
  38. }
  39. if err := ignoreDeclarationColumn("serverless_runtimes"); err != nil {
  40. return err
  41. }
  42. if err := ignoreDeclarationColumn("tool_installations"); err != nil {
  43. return err
  44. }
  45. if err := ignoreDeclarationColumn("ai_model_installations"); err != nil {
  46. return err
  47. }
  48. if err := ignoreDeclarationColumn("agent_strategy_installations"); err != nil {
  49. return err
  50. }
  51. return nil
  52. }
  53. func Init(config *app.Config) {
  54. var err error
  55. if config.DBType == "postgresql" {
  56. DifyPluginDB, err = pg.InitPluginDB(
  57. config.DBHost,
  58. int(config.DBPort),
  59. config.DBDatabase,
  60. config.DBDefaultDatabase,
  61. config.DBUsername,
  62. config.DBPassword,
  63. config.DBSslMode,
  64. )
  65. } else if config.DBType == "mysql" {
  66. DifyPluginDB, err = mysql.InitPluginDB(
  67. config.DBHost,
  68. int(config.DBPort),
  69. config.DBDatabase,
  70. config.DBDefaultDatabase,
  71. config.DBUsername,
  72. config.DBPassword,
  73. config.DBSslMode,
  74. )
  75. } else {
  76. log.Panic("unsupported database type: %v", config.DBType)
  77. }
  78. if err != nil {
  79. log.Panic("failed to init dify plugin db: %v", err)
  80. }
  81. err = autoMigrate()
  82. if err != nil {
  83. log.Panic("failed to auto migrate: %v", err)
  84. }
  85. log.Info("dify plugin db initialized")
  86. }
  87. func Close() {
  88. db, err := DifyPluginDB.DB()
  89. if err != nil {
  90. log.Error("failed to close dify plugin db: %v", err)
  91. return
  92. }
  93. err = db.Close()
  94. if err != nil {
  95. log.Error("failed to close dify plugin db: %v", err)
  96. return
  97. }
  98. log.Info("dify plugin db closed")
  99. }