@@ -217,6 +217,9 @@ func (r *Repository) Migrate(ctx context.Context) error {
217217 if err == nil {
218218 defer rows .Close ()
219219 hasIsRead := false
220+ hasFileCount := false
221+ hasTotalSize := false
222+
220223 for rows .Next () {
221224 var cid int
222225 var name , ctype string
@@ -225,7 +228,12 @@ func (r *Repository) Migrate(ctx context.Context) error {
225228 if err := rows .Scan (& cid , & name , & ctype , & notnull , & dflt_value , & pk ); err == nil {
226229 if name == "is_read" {
227230 hasIsRead = true
228- break
231+ }
232+ if name == "file_count" {
233+ hasFileCount = true
234+ }
235+ if name == "total_size" {
236+ hasTotalSize = true
229237 }
230238 }
231239 }
@@ -235,8 +243,22 @@ func (r *Repository) Migrate(ctx context.Context) error {
235243 _ , err = r .db .ExecContext (ctx , "ALTER TABLE messages ADD COLUMN is_read INTEGER DEFAULT 0" )
236244 if err != nil {
237245 log .Printf ("[Repo] Failed to add is_read column: %v" , err )
238- } else {
239- log .Println ("[Repo] is_read column added successfully." )
246+ }
247+ }
248+
249+ if ! hasFileCount {
250+ log .Println ("[Repo] Adding file_count column to messages table..." )
251+ _ , err = r .db .ExecContext (ctx , "ALTER TABLE messages ADD COLUMN file_count INTEGER DEFAULT 0" )
252+ if err != nil {
253+ log .Printf ("[Repo] Failed to add file_count column: %v" , err )
254+ }
255+ }
256+
257+ if ! hasTotalSize {
258+ log .Println ("[Repo] Adding total_size column to messages table..." )
259+ _ , err = r .db .ExecContext (ctx , "ALTER TABLE messages ADD COLUMN total_size INTEGER DEFAULT 0" )
260+ if err != nil {
261+ log .Printf ("[Repo] Failed to add total_size column: %v" , err )
240262 }
241263 }
242264 }
@@ -738,13 +760,15 @@ func (r *Repository) DeleteContact(ctx context.Context, id string) error {
738760func (r * Repository ) SaveMessage (ctx context.Context , msg * core.Message ) error {
739761 query := `
740762 INSERT INTO messages (id, chat_id, sender_id, content, content_type, status,
741- is_outgoing, reply_to_id, timestamp, created_at, updated_at)
742- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
763+ is_outgoing, reply_to_id, timestamp, created_at, updated_at, file_count, total_size )
764+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
743765 ON CONFLICT(id) DO UPDATE SET
744766 content = excluded.content,
745767 content_type = excluded.content_type,
746768 status = excluded.status,
747- updated_at = excluded.updated_at
769+ updated_at = excluded.updated_at,
770+ file_count = excluded.file_count,
771+ total_size = excluded.total_size
748772 `
749773
750774 now := time .Now ()
@@ -764,6 +788,7 @@ func (r *Repository) SaveMessage(ctx context.Context, msg *core.Message) error {
764788 _ , err := r .db .ExecContext (ctx , query ,
765789 msg .ID , msg .ChatID , msg .SenderID , content , msg .ContentType , msg .Status ,
766790 msg .IsOutgoing , msg .ReplyToID , msg .Timestamp , msg .CreatedAt , msg .UpdatedAt ,
791+ msg .FileCount , msg .TotalSize ,
767792 )
768793
769794 if err != nil {
@@ -806,7 +831,7 @@ func (r *Repository) SaveMessage(ctx context.Context, msg *core.Message) error {
806831func (r * Repository ) GetMessage (ctx context.Context , id string ) (* core.Message , error ) {
807832 query := `
808833 SELECT id, chat_id, sender_id, content, content_type, status,
809- is_outgoing, reply_to_id, timestamp, created_at, updated_at
834+ is_outgoing, reply_to_id, timestamp, created_at, updated_at, file_count, total_size
810835 FROM messages WHERE id = ?
811836 `
812837
@@ -832,6 +857,7 @@ func (r *Repository) scanMessage(row interface {
832857 err := row .Scan (
833858 & msg .ID , & msg .ChatID , & msg .SenderID , & msg .Content , & msg .ContentType , & msg .Status ,
834859 & msg .IsOutgoing , & msg .ReplyToID , & msg .Timestamp , & msg .CreatedAt , & msg .UpdatedAt ,
860+ & msg .FileCount , & msg .TotalSize ,
835861 )
836862 if err != nil {
837863 return nil , err
@@ -912,7 +938,7 @@ func (r *Repository) GetChatHistory(ctx context.Context, chatID string, limit, o
912938 query := `
913939 SELECT * FROM (
914940 SELECT id, chat_id, sender_id, content, content_type, status,
915- is_outgoing, reply_to_id, timestamp, created_at, updated_at
941+ is_outgoing, reply_to_id, timestamp, created_at, updated_at, file_count, total_size
916942 FROM messages
917943 WHERE chat_id = ?
918944 ORDER BY timestamp DESC
@@ -982,7 +1008,7 @@ func (r *Repository) UpdateMessageContent(ctx context.Context, id, newContent st
9821008func (r * Repository ) SearchMessages (ctx context.Context , chatID , queryStr string ) ([]* core.Message , error ) {
9831009 query := `
9841010 SELECT id, chat_id, sender_id, content, content_type, status,
985- is_outgoing, reply_to_id, timestamp, created_at, updated_at
1011+ is_outgoing, reply_to_id, timestamp, created_at, updated_at, file_count, total_size
9861012 FROM messages
9871013 WHERE chat_id = ? AND content LIKE ?
9881014 ORDER BY timestamp DESC
@@ -1001,6 +1027,7 @@ func (r *Repository) SearchMessages(ctx context.Context, chatID, queryStr string
10011027 err := rows .Scan (
10021028 & msg .ID , & msg .ChatID , & msg .SenderID , & msg .Content , & msg .ContentType , & msg .Status ,
10031029 & msg .IsOutgoing , & msg .ReplyToID , & msg .Timestamp , & msg .CreatedAt , & msg .UpdatedAt ,
1030+ & msg .FileCount , & msg .TotalSize ,
10041031 )
10051032 if err != nil {
10061033 return nil , fmt .Errorf ("failed to scan message: %w" , err )
0 commit comments