Conditions | 12 |
Total Lines | 70 |
Code Lines | 43 |
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:
Complex classes like postgres.*TenantWriter.DeleteTenant often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | package postgres |
||
67 | func (w *TenantWriter) DeleteTenant(ctx context.Context, tenantID string) (err error) { |
||
68 | ctx, span := internal.Tracer.Start(ctx, "tenant-writer.delete-tenant") |
||
69 | defer span.End() |
||
70 | |||
71 | slog.DebugContext(ctx, "deleting tenant", slog.Any("tenant_id", tenantID)) |
||
72 | |||
73 | tx, err := w.database.WritePool.Begin(ctx) |
||
74 | if err != nil { |
||
75 | return utils.HandleError(ctx, span, err, base.ErrorCode_ERROR_CODE_EXECUTION) |
||
76 | } |
||
77 | defer tx.Rollback(ctx) |
||
78 | |||
79 | // Check if tenant exists first |
||
80 | var exists bool |
||
81 | err = tx.QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM "+TenantsTable+" WHERE id = $1)", tenantID).Scan(&exists) |
||
82 | if err != nil { |
||
83 | return utils.HandleError(ctx, span, err, base.ErrorCode_ERROR_CODE_EXECUTION) |
||
84 | } |
||
85 | if !exists { |
||
86 | return utils.HandleError(ctx, span, errors.New("tenant not found"), base.ErrorCode_ERROR_CODE_NOT_FOUND) |
||
87 | } |
||
88 | |||
89 | // Prepare batch operations for deleting tenant-related records from multiple tables |
||
90 | tables := []string{BundlesTable, RelationTuplesTable, AttributesTable, SchemaDefinitionTable, TransactionsTable} |
||
91 | batch := &pgx.Batch{} |
||
92 | for _, table := range tables { |
||
93 | query := fmt.Sprintf(utils.DeleteAllByTenantTemplate, table) |
||
|
|||
94 | batch.Queue(query, tenantID) |
||
95 | } |
||
96 | batch.Queue(utils.DeleteTenantTemplate, tenantID) |
||
97 | |||
98 | // Execute the batch of delete queries |
||
99 | br := tx.SendBatch(ctx, batch) |
||
100 | |||
101 | for range tables { |
||
102 | _, err := br.Exec() |
||
103 | if err != nil { |
||
104 | originalErr := err |
||
105 | closeErr := br.Close() |
||
106 | if closeErr != nil { |
||
107 | return closeErr |
||
108 | } |
||
109 | // Don't commit on error, let defer tx.Rollback() handle it |
||
110 | return utils.HandleError(ctx, span, originalErr, base.ErrorCode_ERROR_CODE_EXECUTION) |
||
111 | } |
||
112 | } |
||
113 | |||
114 | // Execute the tenant deletion |
||
115 | _, err = br.Exec() |
||
116 | if err != nil { |
||
117 | originalErr := err |
||
118 | closeErr := br.Close() |
||
119 | if closeErr != nil { |
||
120 | return closeErr |
||
121 | } |
||
122 | // Don't commit on error, let defer tx.Rollback() handle it |
||
123 | return utils.HandleError(ctx, span, originalErr, base.ErrorCode_ERROR_CODE_EXECUTION) |
||
124 | } |
||
125 | |||
126 | err = br.Close() |
||
127 | if err != nil { |
||
128 | return utils.HandleError(ctx, span, err, base.ErrorCode_ERROR_CODE_EXECUTION) |
||
129 | } |
||
130 | |||
131 | err = tx.Commit(ctx) |
||
132 | if err != nil { |
||
133 | return utils.HandleError(ctx, span, err, base.ErrorCode_ERROR_CODE_EXECUTION) |
||
134 | } |
||
135 | |||
136 | return nil |
||
137 | } |
||
138 |