mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 20:34:37 +03:00
Database connection pool refactor (#5274)
* Move optimise out of RunAllMigrations * Separate read and write database connections * Enforce readonly connection constraint * Fix migrations not using tx * #5155 - allow setting cache size from environment * Document new environment variable
This commit is contained in:
@@ -247,7 +247,7 @@ func (m *schema45Migrator) insertImage(data []byte, id int, destTable string, de
|
||||
func (m *schema45Migrator) dropTable(ctx context.Context, table string) error {
|
||||
if err := m.withTxn(ctx, func(tx *sqlx.Tx) error {
|
||||
logger.Debugf("Dropping %s", table)
|
||||
_, err := m.db.Exec(fmt.Sprintf("DROP TABLE `%s`", table))
|
||||
_, err := tx.Exec(fmt.Sprintf("DROP TABLE `%s`", table))
|
||||
return err
|
||||
}); err != nil {
|
||||
return err
|
||||
|
||||
@@ -52,7 +52,7 @@ func (m *schema48PreMigrator) validateScrapedItems(ctx context.Context) error {
|
||||
func (m *schema48PreMigrator) fixStudioNames(ctx context.Context) error {
|
||||
// First remove NULL names
|
||||
if err := m.withTxn(ctx, func(tx *sqlx.Tx) error {
|
||||
_, err := m.db.Exec("UPDATE studios SET name = 'NULL' WHERE name IS NULL")
|
||||
_, err := tx.Exec("UPDATE studios SET name = 'NULL' WHERE name IS NULL")
|
||||
return err
|
||||
}); err != nil {
|
||||
return err
|
||||
@@ -64,7 +64,7 @@ func (m *schema48PreMigrator) fixStudioNames(ctx context.Context) error {
|
||||
|
||||
// collect names
|
||||
if err := m.withTxn(ctx, func(tx *sqlx.Tx) error {
|
||||
rows, err := m.db.Query("SELECT id, name FROM studios ORDER BY name, id")
|
||||
rows, err := tx.Query("SELECT id, name FROM studios ORDER BY name, id")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -114,7 +114,7 @@ func (m *schema48PreMigrator) fixStudioNames(ctx context.Context) error {
|
||||
|
||||
var count int
|
||||
|
||||
row := m.db.QueryRowx("SELECT COUNT(*) FROM studios WHERE name = ?", newName)
|
||||
row := tx.QueryRowx("SELECT COUNT(*) FROM studios WHERE name = ?", newName)
|
||||
err := row.Scan(&count)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -131,7 +131,7 @@ func (m *schema48PreMigrator) fixStudioNames(ctx context.Context) error {
|
||||
}
|
||||
|
||||
logger.Infof("Renaming duplicate studio id %d to %s", id, newName)
|
||||
_, err := m.db.Exec("UPDATE studios SET name = ? WHERE id = ?", newName, id)
|
||||
_, err := tx.Exec("UPDATE studios SET name = ? WHERE id = ?", newName, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ func (m *schema60Migrator) migrate(ctx context.Context) error {
|
||||
if err := m.withTxn(ctx, func(tx *sqlx.Tx) error {
|
||||
query := "SELECT id, mode, find_filter, object_filter, ui_options FROM `saved_filters` WHERE `name` = ''"
|
||||
|
||||
rows, err := m.db.Query(query)
|
||||
rows, err := tx.Query(query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -98,7 +98,7 @@ func (m *schema60Migrator) migrate(ctx context.Context) error {
|
||||
|
||||
// remove the default filters from the database
|
||||
query = "DELETE FROM `saved_filters` WHERE `name` = ''"
|
||||
if _, err := m.db.Exec(query); err != nil {
|
||||
if _, err := tx.Exec(query); err != nil {
|
||||
return fmt.Errorf("deleting default filters: %w", err)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user