narasiahli-be/migrations/001_create_chat_history_tab...

29 lines
1.0 KiB
Go

package migrations
import (
"narasi-ahli-be/app/database"
"narasi-ahli-be/app/database/entity"
)
// CreateChatHistoryTables creates the chat history tables
func CreateChatHistoryTables(db *database.Database) error {
// Auto-migrate the new entities
err := db.DB.AutoMigrate(
&entity.ChatSessions{},
&entity.ChatMessagesNew{},
)
if err != nil {
return err
}
// Create indexes manually if needed
db.DB.Exec("CREATE INDEX IF NOT EXISTS idx_chat_sessions_user_id ON chat_sessions (user_id)")
db.DB.Exec("CREATE INDEX IF NOT EXISTS idx_chat_sessions_agent_id ON chat_sessions (agent_id)")
db.DB.Exec("CREATE INDEX IF NOT EXISTS idx_chat_sessions_session_id ON chat_sessions (session_id)")
db.DB.Exec("CREATE INDEX IF NOT EXISTS idx_chat_sessions_created_at ON chat_sessions (created_at)")
db.DB.Exec("CREATE INDEX IF NOT EXISTS idx_chat_messages_new_session_id ON chat_messages_new (session_id)")
db.DB.Exec("CREATE INDEX IF NOT EXISTS idx_chat_messages_new_created_at ON chat_messages_new (created_at)")
return nil
}