From 2be91a2023f6ea7bdabb04d9888727b3a16bdded Mon Sep 17 00:00:00 2001 From: Vadim Voitenko Date: Sat, 7 Dec 2024 19:15:20 +0200 Subject: [PATCH] fix: fixed linter warns --- cmd/greenmask/cmd/restore/restore.go | 2 +- internal/db/postgres/restorers/base.go | 20 ++++++++-------- internal/db/postgres/restorers/table.go | 24 +++++++++---------- .../postgres/restorers/table_insert_format.go | 18 +++----------- 4 files changed, 26 insertions(+), 38 deletions(-) diff --git a/cmd/greenmask/cmd/restore/restore.go b/cmd/greenmask/cmd/restore/restore.go index 460db7e0..b263cb41 100644 --- a/cmd/greenmask/cmd/restore/restore.go +++ b/cmd/greenmask/cmd/restore/restore.go @@ -202,7 +202,7 @@ func init() { "disable-triggers", "enable-row-security", "if-exists", "no-comments", "no-data-for-failed-tables", "no-security-labels", "no-subscriptions", "no-table-access-method", "no-tablespaces", "section", "strict-names", "use-set-session-authorization", "inserts", "on-conflict-do-nothing", "restore-in-order", - "pgzip", "batch-size", "overriding-system-value", "disable-triggers", "superuser", "use-session-replication-role-replica", + "pgzip", "batch-size", "overriding-system-value", "superuser", "use-session-replication-role-replica", "host", "port", "username", } { diff --git a/internal/db/postgres/restorers/base.go b/internal/db/postgres/restorers/base.go index 1f3e30f2..f7bd7748 100644 --- a/internal/db/postgres/restorers/base.go +++ b/internal/db/postgres/restorers/base.go @@ -35,7 +35,7 @@ func (rb *restoreBase) DebugInfo() string { func (rb *restoreBase) setSessionReplicationRole(ctx context.Context, tx pgx.Tx) error { if err := rb.setSuperUser(ctx, tx); err != nil { - return fmt.Errorf("cannot set super user: %w", err) + return err } if rb.opt.UseSessionReplicationRoleReplica { _, err := tx.Exec(ctx, "SET session_replication_role = 'replica'") @@ -44,14 +44,14 @@ func (rb *restoreBase) setSessionReplicationRole(ctx context.Context, tx pgx.Tx) } } if err := rb.resetSuperUser(ctx, tx); err != nil { - return fmt.Errorf("cannot reset super user: %w", err) + return err } return nil } func (rb *restoreBase) resetSessionReplicationRole(ctx context.Context, tx pgx.Tx) error { if err := rb.setSuperUser(ctx, tx); err != nil { - return fmt.Errorf("cannot set super user: %w", err) + return err } if rb.opt.UseSessionReplicationRoleReplica { _, err := tx.Exec(ctx, "RESET session_replication_role") @@ -60,7 +60,7 @@ func (rb *restoreBase) resetSessionReplicationRole(ctx context.Context, tx pgx.T } } if err := rb.resetSuperUser(ctx, tx); err != nil { - return fmt.Errorf("cannot reset super user: %w", err) + return err } return nil } @@ -68,7 +68,7 @@ func (rb *restoreBase) resetSessionReplicationRole(ctx context.Context, tx pgx.T func (rb *restoreBase) disableTriggers(ctx context.Context, tx pgx.Tx) error { if rb.opt.DisableTriggers { if err := rb.setSuperUser(ctx, tx); err != nil { - return fmt.Errorf("cannot set super user: %w", err) + return err } _, err := tx.Exec( ctx, @@ -82,7 +82,7 @@ func (rb *restoreBase) disableTriggers(ctx context.Context, tx pgx.Tx) error { return err } if err := rb.resetSuperUser(ctx, tx); err != nil { - return fmt.Errorf("cannot reset super user: %w", err) + return err } } return nil @@ -91,7 +91,7 @@ func (rb *restoreBase) disableTriggers(ctx context.Context, tx pgx.Tx) error { func (rb *restoreBase) enableTriggers(ctx context.Context, tx pgx.Tx) error { if rb.opt.DisableTriggers { if err := rb.setSuperUser(ctx, tx); err != nil { - return fmt.Errorf("cannot set super user: %w", err) + return err } _, err := tx.Exec( ctx, @@ -105,7 +105,7 @@ func (rb *restoreBase) enableTriggers(ctx context.Context, tx pgx.Tx) error { return err } if err := rb.resetSuperUser(ctx, tx); err != nil { - return fmt.Errorf("cannot reset super user: %w", err) + return err } } return nil @@ -115,7 +115,7 @@ func (rb *restoreBase) setSuperUser(ctx context.Context, tx pgx.Tx) error { if rb.opt.SuperUser != "" { _, err := tx.Exec(ctx, fmt.Sprintf("SET ROLE %s", rb.opt.SuperUser)) if err != nil { - return err + return fmt.Errorf("cannot set superuser: %w", err) } } return nil @@ -125,7 +125,7 @@ func (rb *restoreBase) resetSuperUser(ctx context.Context, tx pgx.Tx) error { if rb.opt.SuperUser != "" { _, err := tx.Exec(ctx, "RESET ROLE") if err != nil { - return err + return fmt.Errorf("cannot reset superuser: %w", err) } } return nil diff --git a/internal/db/postgres/restorers/table.go b/internal/db/postgres/restorers/table.go index a1680893..4de41cf4 100644 --- a/internal/db/postgres/restorers/table.go +++ b/internal/db/postgres/restorers/table.go @@ -85,12 +85,7 @@ func (td *TableRestorer) Execute(ctx context.Context, conn *pgx.Conn) error { f := tx.Conn().PgConn().Frontend() if err = td.restoreCopy(ctx, f, r); err != nil { - if txErr := tx.Rollback(ctx); txErr != nil { - log.Warn(). - Err(txErr). - Str("objectName", td.DebugInfo()). - Msg("cannot rollback transaction") - } + rollbackTransaction(ctx, tx, td.entry) if td.opt.ExitOnError { return fmt.Errorf("unable to restore table: %w", err) } @@ -102,12 +97,7 @@ func (td *TableRestorer) Execute(ctx context.Context, conn *pgx.Conn) error { } if err := td.resetTx(ctx, tx); err != nil { - if txErr := tx.Rollback(ctx); txErr != nil { - log.Warn(). - Err(txErr). - Str("objectName", td.DebugInfo()). - Msg("cannot rollback transaction") - } + rollbackTransaction(ctx, tx, td.entry) if td.opt.ExitOnError { return fmt.Errorf("unable to reset transaction: %w", err) } @@ -281,6 +271,16 @@ func (td *TableRestorer) postStreamingHandle(ctx context.Context, f *pgproto3.Fr } } +func rollbackTransaction(ctx context.Context, tx pgx.Tx, e *toc.Entry) { + if err := tx.Rollback(ctx); err != nil { + log.Warn(). + Err(err). + Str("SchemaName", *e.Namespace). + Str("TableName", *e.Tag). + Msg("cannot rollback transaction") + } +} + // sendMessage - send a message to the PostgreSQL backend and flush a buffer func sendMessage(frontend *pgproto3.Frontend, msg pgproto3.FrontendMessage) error { frontend.Send(msg) diff --git a/internal/db/postgres/restorers/table_insert_format.go b/internal/db/postgres/restorers/table_insert_format.go index 1b1c124c..a81b750d 100644 --- a/internal/db/postgres/restorers/table_insert_format.go +++ b/internal/db/postgres/restorers/table_insert_format.go @@ -198,11 +198,7 @@ func (td *TableRestorerInsertFormat) insertData( } if err := td.setupTx(ctx, tx); err != nil { - if txErr := tx.Rollback(ctx); txErr != nil { - log.Warn(). - Err(txErr). - Msgf("cannot rollback transaction (restoring %s)", td.DebugInfo()) - } + rollbackTransaction(ctx, tx, td.entry) return fmt.Errorf("cannot setup transaction: %w", err) } @@ -211,20 +207,12 @@ func (td *TableRestorerInsertFormat) insertData( // in driver _, err = tx.Exec(ctx, td.query, getAllArguments(row)...) if err != nil { - if txErr := tx.Rollback(ctx); txErr != nil { - log.Warn(). - Err(txErr). - Msgf("cannot rollback transaction (restoring %s)", td.DebugInfo()) - } + rollbackTransaction(ctx, tx, td.entry) return err } if err := td.resetTx(ctx, tx); err != nil { - if txErr := tx.Rollback(ctx); txErr != nil { - log.Warn(). - Err(txErr). - Msgf("cannot rollback transaction (restoring %s)", td.DebugInfo()) - } + rollbackTransaction(ctx, tx, td.entry) return fmt.Errorf("cannot reset transaction: %w", err) }