Skip to content

Commit

Permalink
fix: fixed linter warns
Browse files Browse the repository at this point in the history
  • Loading branch information
wwoytenko committed Dec 7, 2024
1 parent 1d07342 commit 2be91a2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 38 deletions.
2 changes: 1 addition & 1 deletion cmd/greenmask/cmd/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
} {
Expand Down
20 changes: 10 additions & 10 deletions internal/db/postgres/restorers/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'")
Expand All @@ -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")
Expand All @@ -60,15 +60,15 @@ 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
}

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,
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
24 changes: 12 additions & 12 deletions internal/db/postgres/restorers/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 3 additions & 15 deletions internal/db/postgres/restorers/table_insert_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down

0 comments on commit 2be91a2

Please sign in to comment.