| Conditions | 8 |
| Total Lines | 73 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | package cmd |
||
| 70 | func repairDatastore() func(cmd *cobra.Command, args []string) error { |
||
| 71 | return func(cmd *cobra.Command, args []string) error { |
||
| 72 | // Get database URI and engine |
||
| 73 | databaseURI, _ := cmd.Flags().GetString(repairDatabaseURI) |
||
| 74 | databaseEngine, _ := cmd.Flags().GetString(repairDatabaseEngine) |
||
| 75 | |||
| 76 | // Validate database engine |
||
| 77 | if databaseEngine != "postgres" { |
||
| 78 | return fmt.Errorf("only postgres database engine is supported for repair") |
||
| 79 | } |
||
| 80 | |||
| 81 | // Create PostgreSQL instance |
||
| 82 | pg, err := postgres.New(databaseURI) |
||
| 83 | if err != nil { |
||
| 84 | return fmt.Errorf("failed to create PostgreSQL instance: %w", err) |
||
|
|
|||
| 85 | } |
||
| 86 | defer pg.Close() |
||
| 87 | |||
| 88 | // Parse flags directly from cobra |
||
| 89 | batchSize, _ := cmd.Flags().GetInt(repairBatchSize) |
||
| 90 | retries, _ := cmd.Flags().GetInt(repairRetries) |
||
| 91 | dryRun, _ := cmd.Flags().GetBool(repairDryRun) |
||
| 92 | verbose, _ := cmd.Flags().GetBool(repairVerbose) |
||
| 93 | |||
| 94 | // Create repair configuration |
||
| 95 | config := &postgres.RepairConfig{ |
||
| 96 | BatchSize: batchSize, |
||
| 97 | MaxRetries: retries, |
||
| 98 | RetryDelay: 100, |
||
| 99 | DryRun: dryRun, |
||
| 100 | Verbose: verbose, |
||
| 101 | } |
||
| 102 | |||
| 103 | // Perform repair |
||
| 104 | ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute) |
||
| 105 | defer cancel() |
||
| 106 | |||
| 107 | slog.InfoContext(ctx, "Starting PostgreSQL XID counter repair", |
||
| 108 | slog.String("database_uri", maskURL(databaseURI)), |
||
| 109 | slog.Int("batch_size", config.BatchSize), |
||
| 110 | slog.Bool("dry_run", config.DryRun), |
||
| 111 | slog.Int("max_retries", config.MaxRetries)) |
||
| 112 | |||
| 113 | start := time.Now() |
||
| 114 | result, err := pg.Repair(ctx, config) |
||
| 115 | duration := time.Since(start) |
||
| 116 | |||
| 117 | if err != nil { |
||
| 118 | return fmt.Errorf("repair failed: %w", err) |
||
| 119 | } |
||
| 120 | |||
| 121 | // Print results |
||
| 122 | slog.InfoContext(ctx, "Repair completed successfully", |
||
| 123 | slog.Duration("duration", duration), |
||
| 124 | slog.Int("created_tx_id_fixed", result.CreatedTxIdFixed), |
||
| 125 | slog.Int("errors", len(result.Errors))) |
||
| 126 | |||
| 127 | if len(result.Errors) > 0 { |
||
| 128 | slog.WarnContext(ctx, "Errors encountered during repair") |
||
| 129 | for i, err := range result.Errors { |
||
| 130 | slog.WarnContext(ctx, "Repair error", |
||
| 131 | slog.Int("error_index", i+1), |
||
| 132 | slog.String("error", err.Error())) |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | if result.CreatedTxIdFixed > 0 { |
||
| 137 | slog.InfoContext(ctx, "XID counter repair completed successfully! Advanced XID counter to prevent wraparound issues.") |
||
| 138 | } else { |
||
| 139 | slog.InfoContext(ctx, "No XID counter repair needed. PostgreSQL XID counter is already properly positioned.") |
||
| 140 | } |
||
| 141 | |||
| 142 | return nil |
||
| 143 | } |
||
| 153 |