Skip to content

Commit

Permalink
feat: improve SQLite performance (#1024)
Browse files Browse the repository at this point in the history
* refactor: Improve SQLite performance with connection pooling and retry logic

* feat: Add withTx and withTxRetry methods to SQLiteDatabase for handling database locks

* refactor: add Init command to all databases

* refactor: Improve transaction handling with retry and error management

* refactor: Remove panic/recover pattern in transaction handling

* refactor: Replace `errors.WithStack` with `fmt.Errorf` in transaction methods

* docs: Add docstrings to `withTx` and `withTxRetry` methods in SQLite database implementation

* feat: use new withTxRetry in SaveBookmarks

* feat: sqlite inmmediate transactions by default

* refactor: Split SQLiteDatabase into separate writer and reader dbbase instances

* refactor: Update Init method to configure both reader and writer database connections

* feat: use writer/reader sqlite databases

* refactor: Replace all read calls to use the `reader` database instance

* refactor: Replace errors.WithStack with fmt.Errorf and add nil checks

refactor: Replace errors.WithStack with fmt.Errorf and add proper error handling

fix: Handle potential database connection errors with improved error wrapping

refactor: Replace errors.WithStack with fmt.Errorf and improve error handling

refactor: Replace error handling with fmt.Errorf and proper nil checks

refactor: Replace errors.WithStack with fmt.Errorf and add nil error checks

refactor: Replace errors.WithStack with fmt.Errorf and add nil checks in sqlite.go

refactor: Replace errors.WithStack with fmt.Errorf and add nil checks

refactor: Replace errors.WithStack with fmt.Errorf and improve error handling

refactor: Replace remaining errors.WithStack with fmt.Errorf in sqlite.go

* refactor: Use withTxRetry for SetDatabaseSchemaVersion method

* fix: Simplify error handling in GetBookmark and GetAccount methods

* refactor: Remove duplicated non-nil error checks in sqlite.go

fix: duplicated non-nil checks

* tests: use testutil instead of a manual in memory sqlite db

* fix: openbsd sqlite connection
  • Loading branch information
fmartingr authored Jan 2, 2025
1 parent 98b6b3f commit c58c355
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 97 deletions.
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (c Config) SetDefaults(logger *logrus.Logger, portableMode bool) {

// Set default database url if not set
if c.Database.DBMS == "" && c.Database.URL == "" {
c.Database.URL = fmt.Sprintf("sqlite:///%s", filepath.Join(c.Storage.DataDir, "shiori.db"))
c.Database.URL = fmt.Sprintf("sqlite:///%s?_txlock=immediate", filepath.Join(c.Storage.DataDir, "shiori.db"))
}

c.Http.SetDefaults(logger)
Expand Down
3 changes: 3 additions & 0 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ type DB interface {
// DBx is the underlying sqlx.DB
DBx() *sqlx.DB

// Init initializes the database
Init(ctx context.Context) error

// Migrate runs migrations for this database
Migrate(ctx context.Context) error

Expand Down
5 changes: 5 additions & 0 deletions internal/database/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ func (db *MySQLDatabase) DBx() *sqlx.DB {
return db.DB
}

// Init initializes the database
func (db *MySQLDatabase) Init(ctx context.Context) error {
return nil
}

// Migrate runs migrations for this database engine
func (db *MySQLDatabase) Migrate(ctx context.Context) error {
if err := runMigrations(ctx, db, mysqlMigrations); err != nil {
Expand Down
5 changes: 5 additions & 0 deletions internal/database/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ func (db *PGDatabase) DBx() *sqlx.DB {
return db.DB
}

// Init initializes the database
func (db *PGDatabase) Init(ctx context.Context) error {
return nil
}

// Migrate runs migrations for this database engine
func (db *PGDatabase) Migrate(ctx context.Context) error {
if err := runMigrations(ctx, db, postgresMigrations); err != nil {
Expand Down
Loading

0 comments on commit c58c355

Please sign in to comment.