MCPcopy Create free account
hub / github.com/Preloading/TwitterAPIBridge / InitDB

Function InitDB

db_controller/db_controller.go:102–137  ·  view source on GitHub ↗
(_cfg config.Config)

Source from the content-addressed store, hash-verified

100)
101
102func InitDB(_cfg config.Config) {
103 cfg = _cfg
104 // Ensure the directory exists
105 if cfg.DatabaseType == "sqlite" {
106 dbDir := filepath.Dir(cfg.DatabasePath)
107 if err := os.MkdirAll(dbDir, os.ModePerm); err != nil {
108 panic("failed to create database directory")
109 }
110 }
111
112 // Initialize the database connection
113 var err error
114 switch cfg.DatabaseType {
115 case "sqlite":
116 db, err = gorm.Open(sqlite.Open(cfg.DatabasePath), &gorm.Config{})
117 case "mysql":
118 db, err = gorm.Open(mysql.Open(cfg.DatabasePath), &gorm.Config{})
119 case "postgres":
120 db, err = gorm.Open(postgres.Open(cfg.DatabasePath), &gorm.Config{})
121 default:
122 panic("unsupported database type")
123 }
124
125 if err != nil {
126 panic("failed to connect database")
127 }
128
129 // Auto-migrate the schema
130 db.AutoMigrate(&Token{})
131 db.AutoMigrate(&MessageContext{})
132 db.AutoMigrate(&TwitterIDs{})
133 db.AutoMigrate(&AnalyticData{})
134 db.AutoMigrate(&ShortLink{})
135
136 StartPeriodicAnalyticsWriter(time.Minute)
137}
138
139// StoreToken stores an encrypted access token and refresh token in the database.
140// It returns the UUID of the stored token or an error if the operation fails.

Callers 1

mainFunction · 0.92

Calls 1

Tested by

no test coverage detected