summaryrefslogtreecommitdiff
path: root/internal/db
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2021-09-11 13:19:06 +0200
committerLibravatar GitHub <noreply@github.com>2021-09-11 13:19:06 +0200
commit9dc2255a8fab8ef0bc4b9f417c6131e4c468cb9c (patch)
treeae528bf14a3475bbea264ff26e5ffded3dfadf8a /internal/db
parentTest both dbs (#205) (diff)
downloadgotosocial-9dc2255a8fab8ef0bc4b9f417c6131e4c468cb9c.tar.xz
kim is a reply guy (#208)
* bun debug * bun trace logging hooks * more tests * fix up some stuffffff * drop the frontend cache until a proper fix is made * go fmt
Diffstat (limited to 'internal/db')
-rw-r--r--internal/db/basic.go4
-rw-r--r--internal/db/bundb/account.go14
-rw-r--r--internal/db/bundb/basic.go10
-rw-r--r--internal/db/bundb/basic_test.go34
-rw-r--r--internal/db/bundb/bundb.go9
-rw-r--r--internal/db/bundb/trace.go53
-rw-r--r--internal/db/db.go4
7 files changed, 68 insertions, 60 deletions
diff --git a/internal/db/basic.go b/internal/db/basic.go
index d94c98e45..44000ef24 100644
--- a/internal/db/basic.go
+++ b/internal/db/basic.go
@@ -66,10 +66,6 @@ type Basic interface {
// The given interface i will be set to the result of the query, whatever it is. Use a pointer or a slice.
UpdateByPrimaryKey(ctx context.Context, i interface{}) Error
- // UpdateOneByPrimaryKey sets one column of interface, with the given key, to the given value.
- // It uses the primary key of interface i to decide which row to update. This is usually the `id`.
- UpdateOneByPrimaryKey(ctx context.Context, key string, value interface{}, i interface{}) Error
-
// UpdateWhere updates column key of interface i with the given value, where the given parameters apply.
UpdateWhere(ctx context.Context, where []Where, key string, value interface{}, i interface{}) Error
diff --git a/internal/db/bundb/account.go b/internal/db/bundb/account.go
index 32a70f7cd..745e41567 100644
--- a/internal/db/bundb/account.go
+++ b/internal/db/bundb/account.go
@@ -22,7 +22,6 @@ import (
"context"
"errors"
"fmt"
- "strings"
"time"
"github.com/superseriousbusiness/gotosocial/internal/cache"
@@ -103,16 +102,15 @@ func (a *accountDB) getAccount(ctx context.Context, cacheGet func() (*gtsmodel.A
}
func (a *accountDB) UpdateAccount(ctx context.Context, account *gtsmodel.Account) (*gtsmodel.Account, db.Error) {
- if strings.TrimSpace(account.ID) == "" {
- // TODO: we should not need this check here
- return nil, errors.New("account had no ID")
- }
-
- // Update the account's last-used
+ // Update the account's last-updated
account.UpdatedAt = time.Now()
// Update the account model in the DB
- _, err := a.conn.NewUpdate().Model(account).WherePK().Exec(ctx)
+ _, err := a.conn.
+ NewUpdate().
+ Model(account).
+ WherePK().
+ Exec(ctx)
if err != nil {
return nil, a.conn.ProcessError(err)
}
diff --git a/internal/db/bundb/basic.go b/internal/db/bundb/basic.go
index 1e7880379..e5a1fbaf9 100644
--- a/internal/db/bundb/basic.go
+++ b/internal/db/bundb/basic.go
@@ -105,16 +105,6 @@ func (b *basicDB) UpdateByPrimaryKey(ctx context.Context, i interface{}) db.Erro
return b.conn.ProcessError(err)
}
-func (b *basicDB) UpdateOneByPrimaryKey(ctx context.Context, key string, value interface{}, i interface{}) db.Error {
- q := b.conn.NewUpdate().
- Model(i).
- Set("? = ?", bun.Safe(key), value).
- WherePK()
-
- _, err := q.Exec(ctx)
- return b.conn.ProcessError(err)
-}
-
func (b *basicDB) UpdateWhere(ctx context.Context, where []db.Where, key string, value interface{}, i interface{}) db.Error {
q := b.conn.NewUpdate().Model(i)
diff --git a/internal/db/bundb/basic_test.go b/internal/db/bundb/basic_test.go
index acdfb6640..e5f7e159a 100644
--- a/internal/db/bundb/basic_test.go
+++ b/internal/db/bundb/basic_test.go
@@ -64,40 +64,6 @@ func (suite *BasicTestSuite) TestGetAllNotNull() {
}
}
-func (suite *BasicTestSuite) TestUpdateOneByPrimaryKeySetEmpty() {
- testAccount := suite.testAccounts["local_account_1"]
-
- // try removing the note from zork
- err := suite.db.UpdateOneByPrimaryKey(context.Background(), "note", "", testAccount)
- suite.NoError(err)
-
- // get zork out of the database
- dbAccount, err := suite.db.GetAccountByID(context.Background(), testAccount.ID)
- suite.NoError(err)
- suite.NotNil(dbAccount)
-
- // note should be empty now
- suite.Empty(dbAccount.Note)
-}
-
-func (suite *BasicTestSuite) TestUpdateOneByPrimaryKeySetValue() {
- testAccount := suite.testAccounts["local_account_1"]
-
- note := "this is my new note :)"
-
- // try updating the note on zork
- err := suite.db.UpdateOneByPrimaryKey(context.Background(), "note", note, testAccount)
- suite.NoError(err)
-
- // get zork out of the database
- dbAccount, err := suite.db.GetAccountByID(context.Background(), testAccount.ID)
- suite.NoError(err)
- suite.NotNil(dbAccount)
-
- // note should be set now
- suite.Equal(note, dbAccount.Note)
-}
-
func TestBasicTestSuite(t *testing.T) {
suite.Run(t, new(BasicTestSuite))
}
diff --git a/internal/db/bundb/bundb.go b/internal/db/bundb/bundb.go
index 7ddcab5c7..400535da7 100644
--- a/internal/db/bundb/bundb.go
+++ b/internal/db/bundb/bundb.go
@@ -147,6 +147,11 @@ func NewBunDBService(ctx context.Context, c *config.Config, log *logrus.Logger)
return nil, fmt.Errorf("database type %s not supported for bundb", strings.ToLower(c.DBConfig.Type))
}
+ if log.Level >= logrus.TraceLevel {
+ // add a hook to just log queries and the time they take
+ conn.DB.AddQueryHook(newDebugQueryHook(log))
+ }
+
// actually *begin* the connection so that we can tell if the db is there and listening
if err := conn.Ping(); err != nil {
return nil, fmt.Errorf("db connection error: %s", err)
@@ -402,7 +407,7 @@ func (ps *bunDBService) MentionStringsToMentions(ctx context.Context, targetAcco
return menchies, nil
}
-func (ps *bunDBService) TagStringsToTags(ctx context.Context, tags []string, originAccountID string, statusID string) ([]*gtsmodel.Tag, error) {
+func (ps *bunDBService) TagStringsToTags(ctx context.Context, tags []string, originAccountID string) ([]*gtsmodel.Tag, error) {
newTags := []*gtsmodel.Tag{}
for _, t := range tags {
tag := &gtsmodel.Tag{}
@@ -438,7 +443,7 @@ func (ps *bunDBService) TagStringsToTags(ctx context.Context, tags []string, ori
return newTags, nil
}
-func (ps *bunDBService) EmojiStringsToEmojis(ctx context.Context, emojis []string, originAccountID string, statusID string) ([]*gtsmodel.Emoji, error) {
+func (ps *bunDBService) EmojiStringsToEmojis(ctx context.Context, emojis []string) ([]*gtsmodel.Emoji, error) {
newEmojis := []*gtsmodel.Emoji{}
for _, e := range emojis {
emoji := &gtsmodel.Emoji{}
diff --git a/internal/db/bundb/trace.go b/internal/db/bundb/trace.go
new file mode 100644
index 000000000..e62e8c01f
--- /dev/null
+++ b/internal/db/bundb/trace.go
@@ -0,0 +1,53 @@
+/*
+ GoToSocial
+ Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+package bundb
+
+import (
+ "context"
+ "time"
+
+ "github.com/sirupsen/logrus"
+ "github.com/uptrace/bun"
+)
+
+func newDebugQueryHook(log *logrus.Logger) bun.QueryHook {
+ return &debugQueryHook{
+ log: log,
+ }
+}
+
+// debugQueryHook implements bun.QueryHook
+type debugQueryHook struct {
+ log *logrus.Logger
+}
+
+func (q *debugQueryHook) BeforeQuery(ctx context.Context, event *bun.QueryEvent) context.Context {
+ // do nothing
+ return ctx
+}
+
+// AfterQuery logs the time taken to query, the operation (select, update, etc), and the query itself as translated by bun.
+func (q *debugQueryHook) AfterQuery(ctx context.Context, event *bun.QueryEvent) {
+ dur := time.Since(event.StartTime).Round(time.Microsecond)
+ l := q.log.WithFields(logrus.Fields{
+ "queryTime": dur,
+ "operation": event.Operation(),
+ })
+ l.Trace(event.Query)
+}
diff --git a/internal/db/db.go b/internal/db/db.go
index ec94fcfe7..9a93322cb 100644
--- a/internal/db/db.go
+++ b/internal/db/db.go
@@ -64,7 +64,7 @@ type DB interface {
//
// Note: this func doesn't/shouldn't do any manipulation of the tags in the DB, it's just for checking
// if they exist in the db already, and conveniently returning them, or creating new tag structs.
- TagStringsToTags(ctx context.Context, tags []string, originAccountID string, statusID string) ([]*gtsmodel.Tag, error)
+ TagStringsToTags(ctx context.Context, tags []string, originAccountID string) ([]*gtsmodel.Tag, error)
// EmojiStringsToEmojis takes a slice of deduplicated, lowercase emojis in the form ":emojiname:", which have been
// used in a status. It takes the id of the account that wrote the status, and the id of the status itself, and then
@@ -72,5 +72,5 @@ type DB interface {
//
// Note: this func doesn't/shouldn't do any manipulation of the emoji in the DB, it's just for checking
// if they exist in the db and conveniently returning them if they do.
- EmojiStringsToEmojis(ctx context.Context, emojis []string, originAccountID string, statusID string) ([]*gtsmodel.Emoji, error)
+ EmojiStringsToEmojis(ctx context.Context, emojis []string) ([]*gtsmodel.Emoji, error)
}