aboutsummaryrefslogtreecommitdiffstats
path: root/internal
diff options
context:
space:
mode:
Diffstat (limited to 'internal')
-rw-r--r--internal/output/console.go12
-rw-r--r--internal/repositories/migration_db_repo.go53
-rw-r--r--internal/repositories/migration_fs_repo.go4
3 files changed, 48 insertions, 21 deletions
diff --git a/internal/output/console.go b/internal/output/console.go
index d2946ed..50534dd 100644
--- a/internal/output/console.go
+++ b/internal/output/console.go
@@ -18,12 +18,12 @@ func NewConsoleOutputter() ConsoleOutputter {
func (c ConsoleOutputter) Output(message string) error {
_, err := fmt.Fprintln(c.stdout, message)
- return err
+ return fmt.Errorf("unable to print message %s: %w", message, err)
}
func (c ConsoleOutputter) Error(message string) error {
_, err := fmt.Fprintln(c.stderr, message)
- return err
+ return fmt.Errorf("unable to print message %s: %w", message, err)
}
func (c ConsoleOutputter) Table(headers []string, rows [][]string) error {
@@ -32,13 +32,13 @@ func (c ConsoleOutputter) Table(headers []string, rows [][]string) error {
for _, header := range headers {
_, err := fmt.Fprintf(w, "%s\t", header)
if err != nil {
- return err
+ return fmt.Errorf("unable to print table header %s: %w", header, err)
}
}
if len(headers) > 0 {
_, err := fmt.Fprint(w, "\n")
if err != nil {
- return err
+ return fmt.Errorf("unable to print newline after table header: %w", err)
}
}
@@ -46,12 +46,12 @@ func (c ConsoleOutputter) Table(headers []string, rows [][]string) error {
for _, cell := range row {
_, err := fmt.Fprintf(w, "%s\t", cell)
if err != nil {
- return err
+ return fmt.Errorf("unable to print table cell %s: %w", cell, err)
}
}
_, err := fmt.Fprint(w, "\n")
if err != nil {
- return err
+ return fmt.Errorf("unable to print newline for table row %s: %w", row, err)
}
}
diff --git a/internal/repositories/migration_db_repo.go b/internal/repositories/migration_db_repo.go
index 42cf0e8..93b9c73 100644
--- a/internal/repositories/migration_db_repo.go
+++ b/internal/repositories/migration_db_repo.go
@@ -3,6 +3,7 @@ package repositories
import (
"database/sql"
"errors"
+ "fmt"
"strings"
"github.com/eugenetriguba/bolt/internal/models"
@@ -91,19 +92,22 @@ func (mr *MigrationDBRepo) Apply(
sqlParser := sqlparse.NewSqlParser(strings.NewReader(upgradeScript))
execOptions, err := sqlParser.Parse()
if err != nil {
- return err
+ return fmt.Errorf("unable to parse sql file for upgrade script: %w", err)
}
if execOptions.UseTransaction {
tx, err := mr.db.Begin()
if err != nil {
- return err
+ return fmt.Errorf(
+ "unable to start transaction during upgrade script execution: %w",
+ err,
+ )
}
defer tx.Rollback()
_, err = tx.Exec(upgradeScript)
if err != nil {
- return err
+ return fmt.Errorf("unable to execute upgrade script: %w", err)
}
_, err = tx.Exec(
@@ -111,16 +115,22 @@ func (mr *MigrationDBRepo) Apply(
migration.Version,
)
if err != nil {
- return err
+ return fmt.Errorf(
+ "unable to insert applied migration to bolt_migrations table: %w",
+ err,
+ )
}
err = tx.Commit()
if err != nil {
- return err
+ return fmt.Errorf(
+ "unable to commit transaction during upgrade script execution: %w",
+ err,
+ )
}
} else {
_, err = mr.db.Exec(upgradeScript)
if err != nil {
- return err
+ return fmt.Errorf("unable to execute upgrade script: %w", err)
}
_, err = mr.db.Exec(
@@ -128,7 +138,10 @@ func (mr *MigrationDBRepo) Apply(
migration.Version,
)
if err != nil {
- return err
+ return fmt.Errorf(
+ "unable to insert applied migration to bolt_migrations table: %w",
+ err,
+ )
}
}
@@ -146,19 +159,22 @@ func (mr *MigrationDBRepo) Revert(
sqlParser := sqlparse.NewSqlParser(strings.NewReader(downgradeScript))
execOptions, err := sqlParser.Parse()
if err != nil {
- return err
+ return fmt.Errorf("unable to parse sql file for downgrade script: %w", err)
}
if execOptions.UseTransaction {
tx, err := mr.db.Begin()
if err != nil {
- return err
+ return fmt.Errorf(
+ "unable to start transaction during downgrade script execution: %w",
+ err,
+ )
}
defer tx.Rollback()
_, err = tx.Exec(downgradeScript)
if err != nil {
- return err
+ return fmt.Errorf("unable to execute downgrade script: %w", err)
}
_, err = tx.Exec(
@@ -166,17 +182,23 @@ func (mr *MigrationDBRepo) Revert(
migration.Version,
)
if err != nil {
- return err
+ return fmt.Errorf(
+ "unable to remove reverted migration from bolt_migrations table: %w",
+ err,
+ )
}
err = tx.Commit()
if err != nil {
- return err
+ return fmt.Errorf(
+ "unable to commit transaction during downgrade script execution: %w",
+ err,
+ )
}
} else {
_, err = mr.db.Exec(downgradeScript)
if err != nil {
- return err
+ return fmt.Errorf("unable to execute downgrade script: %w", err)
}
_, err = mr.db.Exec(
@@ -184,7 +206,10 @@ func (mr *MigrationDBRepo) Revert(
migration.Version,
)
if err != nil {
- return err
+ return fmt.Errorf(
+ "unable to remove reverted migration from bolt_migrations table: %w",
+ err,
+ )
}
}
diff --git a/internal/repositories/migration_fs_repo.go b/internal/repositories/migration_fs_repo.go
index 65afe5f..c541006 100644
--- a/internal/repositories/migration_fs_repo.go
+++ b/internal/repositories/migration_fs_repo.go
@@ -27,7 +27,9 @@ type MigrationFsRepo struct {
migrationsDirPath string
}
-func NewMigrationFsRepo(migrationsConfig *configloader.MigrationsConfig) (*MigrationFsRepo, error) {
+func NewMigrationFsRepo(
+ migrationsConfig *configloader.MigrationsConfig,
+) (*MigrationFsRepo, error) {
fileInfo, err := os.Stat(migrationsConfig.DirectoryPath)
if errors.Is(err, os.ErrNotExist) {
err = os.MkdirAll(migrationsConfig.DirectoryPath, 0755)