43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package tenant
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gocql/gocql"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// Delete deletes a tenant from all tables
|
|
// Uses batched writes to maintain consistency across denormalized tables
|
|
// Note: Consider implementing soft delete (status = 'deleted') instead
|
|
func (r *repository) Delete(ctx context.Context, id string) error {
|
|
// First, get the tenant to retrieve the slug and status
|
|
// (needed to delete from tenants_by_slug and tenants_by_status tables)
|
|
tenant, err := r.GetByID(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Create batch for atomic delete
|
|
batch := r.session.NewBatch(gocql.LoggedBatch)
|
|
|
|
// Delete from tenants_by_id table
|
|
batch.Query(`DELETE FROM tenants_by_id WHERE id = ?`, id)
|
|
|
|
// Delete from tenants_by_slug table
|
|
batch.Query(`DELETE FROM tenants_by_slug WHERE slug = ?`, tenant.Slug)
|
|
|
|
// Delete from tenants_by_status table
|
|
batch.Query(`DELETE FROM tenants_by_status WHERE status = ? AND id = ?`,
|
|
string(tenant.Status), id)
|
|
|
|
// Execute batch
|
|
if err := r.session.ExecuteBatch(batch); err != nil {
|
|
r.logger.Error("failed to delete tenant",
|
|
zap.String("tenant_id", id),
|
|
zap.Error(err))
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|