aboutsummaryrefslogtreecommitdiff
path: root/mediaapi
diff options
context:
space:
mode:
authorNeil Alexander <neilalexander@users.noreply.github.com>2020-07-21 15:48:21 +0100
committerGitHub <noreply@github.com>2020-07-21 15:48:21 +0100
commitb6bc132485ec4d6b37815929f6a4e73e5a062d3b (patch)
treedddea5f8e9fdbd25905822971a908218eb6697a6 /mediaapi
parent1d72ce8b7ab759555503df37af666529749b489c (diff)
Use TransactionWriter in other component SQLite (#1209)
* Use TransactionWriter on other component SQLites * Fix sync API tests * Fix panic in media API * Fix a couple of transactions * Fix wrong query, add some logging output * Add debug logging into StoreEvent * Adjust InsertRoomNID * Update logging
Diffstat (limited to 'mediaapi')
-rw-r--r--mediaapi/storage/sqlite3/media_repository_table.go33
1 files changed, 21 insertions, 12 deletions
diff --git a/mediaapi/storage/sqlite3/media_repository_table.go b/mediaapi/storage/sqlite3/media_repository_table.go
index 8e2e6236..f53f164d 100644
--- a/mediaapi/storage/sqlite3/media_repository_table.go
+++ b/mediaapi/storage/sqlite3/media_repository_table.go
@@ -20,6 +20,7 @@ import (
"database/sql"
"time"
+ "github.com/matrix-org/dendrite/internal/sqlutil"
"github.com/matrix-org/dendrite/mediaapi/types"
"github.com/matrix-org/gomatrixserverlib"
)
@@ -60,11 +61,16 @@ SELECT content_type, file_size_bytes, creation_ts, upload_name, base64hash, user
`
type mediaStatements struct {
+ db *sql.DB
+ writer *sqlutil.TransactionWriter
insertMediaStmt *sql.Stmt
selectMediaStmt *sql.Stmt
}
func (s *mediaStatements) prepare(db *sql.DB) (err error) {
+ s.db = db
+ s.writer = sqlutil.NewTransactionWriter()
+
_, err = db.Exec(mediaSchema)
if err != nil {
return
@@ -80,18 +86,21 @@ func (s *mediaStatements) insertMedia(
ctx context.Context, mediaMetadata *types.MediaMetadata,
) error {
mediaMetadata.CreationTimestamp = types.UnixMs(time.Now().UnixNano() / 1000000)
- _, err := s.insertMediaStmt.ExecContext(
- ctx,
- mediaMetadata.MediaID,
- mediaMetadata.Origin,
- mediaMetadata.ContentType,
- mediaMetadata.FileSizeBytes,
- mediaMetadata.CreationTimestamp,
- mediaMetadata.UploadName,
- mediaMetadata.Base64Hash,
- mediaMetadata.UserID,
- )
- return err
+ return s.writer.Do(s.db, nil, func(txn *sql.Tx) error {
+ stmt := sqlutil.TxStmt(txn, s.insertMediaStmt)
+ _, err := stmt.ExecContext(
+ ctx,
+ mediaMetadata.MediaID,
+ mediaMetadata.Origin,
+ mediaMetadata.ContentType,
+ mediaMetadata.FileSizeBytes,
+ mediaMetadata.CreationTimestamp,
+ mediaMetadata.UploadName,
+ mediaMetadata.Base64Hash,
+ mediaMetadata.UserID,
+ )
+ return err
+ })
}
func (s *mediaStatements) selectMedia(