Skip to content

Commit

Permalink
fix: memory delete tenant
Browse files Browse the repository at this point in the history
  • Loading branch information
ufuk committed Aug 27, 2024
1 parent 7325a66 commit 4f92e50
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions internal/storage/memory/tenantWriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package memory
import (
"context"
"errors"
"fmt"
"time"

"github.com/Permify/permify/internal/storage"
Expand Down Expand Up @@ -43,47 +44,47 @@ func (w *TenantWriter) CreateTenant(_ context.Context, id, name string) (result
func (w *TenantWriter) DeleteTenant(_ context.Context, tenantID string) (result *base.Tenant, err error) {
txn := w.database.DB.Txn(true)
defer txn.Abort()
var raw interface{}
raw, err = txn.First(constants.TenantsTable, "id", tenantID)
if err != nil {
return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
}

_, err = txn.First(constants.AttributesTable, "tenant_id", tenantID)
if err != nil {
if _, deleteErr := txn.DeleteAll(constants.AttributesTable, "tenant_id", tenantID); deleteErr != nil {
return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
}
return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
// Define a slice of tables to delete associated records
tables := []string{
constants.AttributesTable,
constants.BundlesTable,
constants.RelationTuplesTable,
constants.SchemaDefinitionsTable,
}

_, err = txn.First(constants.BundlesTable, "tenant_id", tenantID)
if err != nil {
if _, deleteErr := txn.DeleteAll(constants.BundlesTable, "tenant_id", tenantID); deleteErr != nil {
return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
// Iterate through each table and delete records associated with the tenant
totalDeleted := 0
for _, table := range tables {
if _, err := txn.First(table, "tenant_id", tenantID); err == nil {
numDeleted, deleteErr := txn.DeleteAll(table, "tenant_id", tenantID)
if deleteErr != nil {
return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
}
totalDeleted += numDeleted
}
return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
}

_, err = txn.First(constants.RelationTuplesTable, "tenant_id", tenantID)
// Retrieve the tenant first
raw, err := txn.First(constants.TenantsTable, "id", tenantID)
if err != nil {
if _, deleteErr := txn.DeleteAll(constants.RelationTuplesTable, "tenant_id", tenantID); deleteErr != nil {
if totalDeleted > 0 {
raw = storage.Tenant{
ID: tenantID,
Name: fmt.Sprintf("Affected rows: %d", totalDeleted),
CreatedAt: time.Now(),
}
} else {
return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
}
return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
}

_, err = txn.First(constants.SchemaDefinitionsTable, "tenant_id", tenantID)
if err != nil {
if _, deleteErr := txn.DeleteAll(constants.SchemaDefinitionsTable, "tenant_id", tenantID); deleteErr != nil {
} else {
// Finally, delete the tenant record
if _, err = txn.DeleteAll(constants.TenantsTable, "id", tenantID); err != nil {
return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
}
return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
}

if _, err = txn.DeleteAll(constants.TenantsTable, "id", tenantID); err != nil {
return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
}
txn.Commit()
return raw.(storage.Tenant).ToTenant(), nil

}

0 comments on commit 4f92e50

Please sign in to comment.