diff options
| author | 2021-10-11 05:37:33 -0700 | |
|---|---|---|
| committer | 2021-10-11 14:37:33 +0200 | |
| commit | 083099a9575f8b2fac22c1d4a51a9dd0e2201243 (patch) | |
| tree | d1787aa544679c433f797d2313ce532250fe574f /internal/db/bundb | |
| parent | Handle forwarded messages (#273) (diff) | |
| download | gotosocial-083099a9575f8b2fac22c1d4a51a9dd0e2201243.tar.xz | |
reference global logrus (#274)
* reference logrus' global logger instead of passing and storing a logger reference everywhere
* always directly use global logrus logger instead of referencing an instance
* test suites should also directly use the global logrus logger
* rename gin logging function to clarify that it's middleware
* correct comments which erroneously referenced removed logger parameter
* setting log level for tests now uses logrus' exported type instead of the string value, to guarantee error isn't possible
Diffstat (limited to 'internal/db/bundb')
| -rw-r--r-- | internal/db/bundb/admin.go | 13 | ||||
| -rw-r--r-- | internal/db/bundb/basic.go | 3 | ||||
| -rw-r--r-- | internal/db/bundb/bundb.go | 24 | ||||
| -rw-r--r-- | internal/db/bundb/bundb_test.go | 4 | ||||
| -rw-r--r-- | internal/db/bundb/conn.go | 5 | ||||
| -rw-r--r-- | internal/db/bundb/instance.go | 3 | ||||
| -rw-r--r-- | internal/db/bundb/trace.go | 10 | 
7 files changed, 29 insertions, 33 deletions
| diff --git a/internal/db/bundb/admin.go b/internal/db/bundb/admin.go index a2028f8f0..79ef4d142 100644 --- a/internal/db/bundb/admin.go +++ b/internal/db/bundb/admin.go @@ -24,6 +24,7 @@ import (  	"crypto/rsa"  	"database/sql"  	"fmt" +	"github.com/sirupsen/logrus"  	"net"  	"net/mail"  	"strings" @@ -86,7 +87,7 @@ func (a *adminDB) IsEmailAvailable(ctx context.Context, email string) (bool, db.  func (a *adminDB) NewSignup(ctx context.Context, username string, reason string, requireApproval bool, email string, password string, signUpIP net.IP, locale string, appID string, emailVerified bool, admin bool) (*gtsmodel.User, db.Error) {  	key, err := rsa.GenerateKey(rand.Reader, 2048)  	if err != nil { -		a.conn.log.Errorf("error creating new rsa key: %s", err) +		logrus.Errorf("error creating new rsa key: %s", err)  		return nil, err  	} @@ -183,7 +184,7 @@ func (a *adminDB) CreateInstanceAccount(ctx context.Context) db.Error {  		WhereGroup(" AND ", whereEmptyOrNull("domain"))  	count, err := existsQ.Count(ctx)  	if err != nil && count == 1 { -		a.conn.log.Infof("instance account %s already exists", username) +		logrus.Infof("instance account %s already exists", username)  		return nil  	} else if err != sql.ErrNoRows {  		return a.conn.ProcessError(err) @@ -191,7 +192,7 @@ func (a *adminDB) CreateInstanceAccount(ctx context.Context) db.Error {  	key, err := rsa.GenerateKey(rand.Reader, 2048)  	if err != nil { -		a.conn.log.Errorf("error creating new rsa key: %s", err) +		logrus.Errorf("error creating new rsa key: %s", err)  		return err  	} @@ -226,7 +227,7 @@ func (a *adminDB) CreateInstanceAccount(ctx context.Context) db.Error {  		return a.conn.ProcessError(err)  	} -	a.conn.log.Infof("instance account %s CREATED with id %s", username, acct.ID) +	logrus.Infof("instance account %s CREATED with id %s", username, acct.ID)  	return nil  } @@ -244,7 +245,7 @@ func (a *adminDB) CreateInstanceInstance(ctx context.Context) db.Error {  		return err  	}  	if exists { -		a.conn.log.Infof("instance entry already exists") +		logrus.Infof("instance entry already exists")  		return nil  	} @@ -269,6 +270,6 @@ func (a *adminDB) CreateInstanceInstance(ctx context.Context) db.Error {  		return a.conn.ProcessError(err)  	} -	a.conn.log.Infof("created instance instance %s with id %s", domain, i.ID) +	logrus.Infof("created instance instance %s with id %s", domain, i.ID)  	return nil  } diff --git a/internal/db/bundb/basic.go b/internal/db/bundb/basic.go index e5a1fbaf9..464d40e3e 100644 --- a/internal/db/bundb/basic.go +++ b/internal/db/bundb/basic.go @@ -21,6 +21,7 @@ package bundb  import (  	"context"  	"errors" +	"github.com/sirupsen/logrus"  	"github.com/superseriousbusiness/gotosocial/internal/config"  	"github.com/superseriousbusiness/gotosocial/internal/db" @@ -165,6 +166,6 @@ func (b *basicDB) IsHealthy(ctx context.Context) db.Error {  }  func (b *basicDB) Stop(ctx context.Context) db.Error { -	b.conn.log.Info("closing db connection") +	logrus.Info("closing db connection")  	return b.conn.Close()  } diff --git a/internal/db/bundb/bundb.go b/internal/db/bundb/bundb.go index e6ebe5d88..5bcab5430 100644 --- a/internal/db/bundb/bundb.go +++ b/internal/db/bundb/bundb.go @@ -78,8 +78,8 @@ type bunDBService struct {  	conn   *DBConn  } -func doMigration(ctx context.Context, db *bun.DB, log *logrus.Logger) error { -	l := log.WithField("func", "doMigration") +func doMigration(ctx context.Context, db *bun.DB) error { +	l := logrus.WithField("func", "doMigration")  	migrator := migrate.NewMigrator(db, migrations.Migrations) @@ -106,7 +106,7 @@ func doMigration(ctx context.Context, db *bun.DB, log *logrus.Logger) error {  // NewBunDBService returns a bunDB derived from the provided config, which implements the go-fed DB interface.  // Under the hood, it uses https://github.com/uptrace/bun to create and maintain a database connection. -func NewBunDBService(ctx context.Context, c *config.Config, log *logrus.Logger) (db.DB, error) { +func NewBunDBService(ctx context.Context, c *config.Config) (db.DB, error) {  	var sqldb *sql.DB  	var conn *DBConn @@ -120,7 +120,7 @@ func NewBunDBService(ctx context.Context, c *config.Config, log *logrus.Logger)  		}  		sqldb = stdlib.OpenDB(*opts)  		tweakConnectionValues(sqldb) -		conn = WrapDBConn(bun.NewDB(sqldb, pgdialect.New()), log) +		conn = WrapDBConn(bun.NewDB(sqldb, pgdialect.New()))  	case dbTypeSqlite:  		// SQLITE @@ -138,10 +138,10 @@ func NewBunDBService(ctx context.Context, c *config.Config, log *logrus.Logger)  			return nil, fmt.Errorf("could not open sqlite db: %s", err)  		}  		tweakConnectionValues(sqldb) -		conn = WrapDBConn(bun.NewDB(sqldb, sqlitedialect.New()), log) +		conn = WrapDBConn(bun.NewDB(sqldb, sqlitedialect.New()))  		if c.DBConfig.Address == "file::memory:?cache=shared" { -			log.Warn("sqlite in-memory database should only be used for debugging") +			logrus.Warn("sqlite in-memory database should only be used for debugging")  			// don't close connections on disconnect -- otherwise  			// the SQLite database will be deleted when there @@ -152,23 +152,23 @@ 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 { +	if logrus.GetLevel() >= logrus.TraceLevel {  		// add a hook to just log queries and the time they take -		conn.DB.AddQueryHook(newDebugQueryHook(log)) +		conn.DB.AddQueryHook(newDebugQueryHook())  	}  	// 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)  	} -	log.Info("connected to database") +	logrus.Info("connected to database")  	for _, t := range registerTables {  		// https://bun.uptrace.dev/orm/many-to-many-relation/  		conn.RegisterModel(t)  	} -	if err := doMigration(ctx, conn.DB, log); err != nil { +	if err := doMigration(ctx, conn.DB); err != nil {  		return nil, fmt.Errorf("db migration error: %s", err)  	} @@ -398,7 +398,7 @@ func (ps *bunDBService) MentionStringsToMentions(ctx context.Context, targetAcco  		if err != nil {  			if err == sql.ErrNoRows {  				// no result found for this username/domain so just don't include it as a mencho and carry on about our business -				ps.conn.log.Debugf("no account found with username '%s' and domain '%s', skipping it", username, domain) +				logrus.Debugf("no account found with username '%s' and domain '%s', skipping it", username, domain)  				continue  			}  			// a serious error has happened so bail @@ -464,7 +464,7 @@ func (ps *bunDBService) EmojiStringsToEmojis(ctx context.Context, emojis []strin  		if err != nil {  			if err == sql.ErrNoRows {  				// no result found for this username/domain so just don't include it as an emoji and carry on about our business -				ps.conn.log.Debugf("no emoji found with shortcode %s, skipping it", e) +				logrus.Debugf("no emoji found with shortcode %s, skipping it", e)  				continue  			}  			// a serious error has happened so bail diff --git a/internal/db/bundb/bundb_test.go b/internal/db/bundb/bundb_test.go index 3fa74530d..d4655a253 100644 --- a/internal/db/bundb/bundb_test.go +++ b/internal/db/bundb/bundb_test.go @@ -19,7 +19,6 @@  package bundb_test  import ( -	"github.com/sirupsen/logrus"  	"github.com/stretchr/testify/suite"  	"github.com/superseriousbusiness/gotosocial/internal/config"  	"github.com/superseriousbusiness/gotosocial/internal/db" @@ -32,7 +31,6 @@ type BunDBStandardTestSuite struct {  	suite.Suite  	config *config.Config  	db     db.DB -	log    *logrus.Logger  	// standard suite models  	testTokens       map[string]*gtsmodel.Token @@ -61,7 +59,7 @@ func (suite *BunDBStandardTestSuite) SetupSuite() {  func (suite *BunDBStandardTestSuite) SetupTest() {  	suite.config = testrig.NewTestConfig()  	suite.db = testrig.NewTestDB() -	suite.log = testrig.NewTestLog() +	testrig.InitTestLog()  	testrig.StandardDBSetup(suite.db, suite.testAccounts)  } diff --git a/internal/db/bundb/conn.go b/internal/db/bundb/conn.go index aeb1b5db0..3b5a3ac92 100644 --- a/internal/db/bundb/conn.go +++ b/internal/db/bundb/conn.go @@ -4,7 +4,6 @@ import (  	"context"  	"database/sql" -	"github.com/sirupsen/logrus"  	"github.com/superseriousbusiness/gotosocial/internal/db"  	"github.com/uptrace/bun"  	"github.com/uptrace/bun/dialect" @@ -15,12 +14,11 @@ type DBConn struct {  	// TODO: move *Config here, no need to be in each struct type  	errProc func(error) db.Error // errProc is the SQL-type specific error processor -	log     *logrus.Logger       // log is the logger passed with this DBConn  	*bun.DB                      // DB is the underlying bun.DB connection  }  // WrapDBConn @TODO -func WrapDBConn(dbConn *bun.DB, log *logrus.Logger) *DBConn { +func WrapDBConn(dbConn *bun.DB) *DBConn {  	var errProc func(error) db.Error  	switch dbConn.Dialect().Name() {  	case dialect.PG: @@ -32,7 +30,6 @@ func WrapDBConn(dbConn *bun.DB, log *logrus.Logger) *DBConn {  	}  	return &DBConn{  		errProc: errProc, -		log:     log,  		DB:      dbConn,  	}  } diff --git a/internal/db/bundb/instance.go b/internal/db/bundb/instance.go index 4e26fc7c4..ea507ad43 100644 --- a/internal/db/bundb/instance.go +++ b/internal/db/bundb/instance.go @@ -20,6 +20,7 @@ package bundb  import (  	"context" +	"github.com/sirupsen/logrus"  	"github.com/superseriousbusiness/gotosocial/internal/config"  	"github.com/superseriousbusiness/gotosocial/internal/db" @@ -98,7 +99,7 @@ func (i *instanceDB) CountInstanceDomains(ctx context.Context, domain string) (i  }  func (i *instanceDB) GetInstanceAccounts(ctx context.Context, domain string, maxID string, limit int) ([]*gtsmodel.Account, db.Error) { -	i.conn.log.Debug("GetAccountsForInstance") +	logrus.Debug("GetAccountsForInstance")  	accounts := []*gtsmodel.Account{} diff --git a/internal/db/bundb/trace.go b/internal/db/bundb/trace.go index 82b50c058..b13adf1bf 100644 --- a/internal/db/bundb/trace.go +++ b/internal/db/bundb/trace.go @@ -27,26 +27,24 @@ import (  	"github.com/uptrace/bun"  ) -func newDebugQueryHook(log *logrus.Logger) bun.QueryHook { +func newDebugQueryHook() 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 { +func (q *debugQueryHook) BeforeQuery(ctx context.Context, _ *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) { +func (q *debugQueryHook) AfterQuery(_ context.Context, event *bun.QueryEvent) {  	dur := time.Since(event.StartTime).Round(time.Microsecond) -	l := q.log.WithFields(logrus.Fields{ +	l := logrus.WithFields(logrus.Fields{  		"duration":  dur,  		"operation": event.Operation(),  	}) | 
