Skip to content

Commit 2d7ff9b

Browse files
authored
Fix unique (#2368)
* remove uniqueness,logging * remove noisy logging
1 parent 9d4d0ad commit 2d7ff9b

File tree

7 files changed

+42
-15
lines changed

7 files changed

+42
-15
lines changed

taco/internal/api/org_handler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ func (h *OrgHandler) CreateOrganization(c echo.Context) error {
9595
slog.Info("Creating organization",
9696
"name", req.Name,
9797
"displayName", req.DisplayName,
98+
"externalOrgID", req.ExternalOrgID,
9899
"createdBy", userIDStr,
99100
)
100101

taco/internal/query/types/models.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ func (rut *RuleUnitTag) BeforeCreate(tx *gorm.DB) error {
107107

108108
type Organization struct {
109109
ID string `gorm:"type:varchar(36);primaryKey"`
110-
Name string `gorm:"type:varchar(255);not null;uniqueIndex"` // Unique identifier (e.g., "acme") - used in CLI and paths
111-
DisplayName string `gorm:"type:varchar(255);not null"` // Friendly name (e.g., "Acme Corp") - shown in UI
112-
ExternalOrgID *string `gorm:"type:varchar(500);uniqueIndex"` // External org identifier (optional, nullable)
110+
Name string `gorm:"type:varchar(255);not null;index"` // Non-unique - multiple orgs can have same name (e.g., "Personal")
111+
DisplayName string `gorm:"type:varchar(255);not null"` // Friendly name (e.g., "Acme Corp") - shown in UI
112+
ExternalOrgID *string `gorm:"type:varchar(500);uniqueIndex"` // External org identifier (optional, nullable) - THIS is unique
113113
CreatedBy string `gorm:"type:varchar(255);not null"`
114114
CreatedAt time.Time
115115
UpdatedAt time.Time

taco/internal/repositories/org_repository.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,9 @@ func (r *orgRepository) Create(ctx context.Context, orgID, name, displayName, ex
5757
return nil, err
5858
}
5959

60-
// Check if org already exists by name (infrastructure logic)
61-
var existing types.Organization
62-
err := r.db.WithContext(ctx).Where(queryOrgByName, orgID).First(&existing).Error
63-
if err == nil {
64-
return nil, domain.ErrOrgExists
65-
}
66-
if !errors.Is(err, gorm.ErrRecordNotFound) {
67-
return nil, fmt.Errorf("failed to check existing org: %w", err)
68-
}
69-
60+
// Note: Name uniqueness is no longer enforced - multiple orgs can have the same name
61+
// Only external_org_id must be unique (enforced by database constraint)
62+
7063
// Check if external org ID already exists (if provided)
7164
if externalOrgID != "" {
7265
var existingExternal types.Organization
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Modify "organizations" table
2+
ALTER TABLE `organizations` DROP INDEX `idx_organizations_name`;
3+
-- Modify "organizations" table
4+
ALTER TABLE `organizations` ADD INDEX `idx_organizations_name` (`name`);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Drop index "idx_organizations_name" from table: "organizations"
2+
DROP INDEX "public"."idx_organizations_name";
3+
-- Create index "idx_organizations_name" to table: "organizations"
4+
CREATE INDEX "idx_organizations_name" ON "public"."organizations" ("name");
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
-- Disable the enforcement of foreign-keys constraints
2+
PRAGMA foreign_keys = off;
3+
-- Create "new_organizations" table
4+
CREATE TABLE `new_organizations` (
5+
`id` varchar NULL,
6+
`name` varchar NOT NULL,
7+
`display_name` varchar NOT NULL,
8+
`external_org_id` varchar NULL,
9+
`created_by` varchar NOT NULL,
10+
`created_at` datetime NULL,
11+
`updated_at` datetime NULL,
12+
PRIMARY KEY (`id`)
13+
);
14+
-- Copy rows from old table "organizations" to new temporary table "new_organizations"
15+
INSERT INTO `new_organizations` (`id`, `name`, `display_name`, `external_org_id`, `created_by`, `created_at`, `updated_at`) SELECT `id`, `name`, `display_name`, `external_org_id`, `created_by`, `created_at`, `updated_at` FROM `organizations`;
16+
-- Drop "organizations" table after copying rows
17+
DROP TABLE `organizations`;
18+
-- Rename temporary table "new_organizations" to "organizations"
19+
ALTER TABLE `new_organizations` RENAME TO `organizations`;
20+
-- Create index "idx_organizations_external_org_id" to table: "organizations"
21+
CREATE UNIQUE INDEX `idx_organizations_external_org_id` ON `organizations` (`external_org_id`);
22+
-- Create index "idx_organizations_name" to table: "organizations"
23+
CREATE INDEX `idx_organizations_name` ON `organizations` (`name`);
24+
-- Enable back the enforcement of foreign-keys constraints
25+
PRAGMA foreign_keys = on;

ui/src/routes/api/auth/workos/webhooks.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ export const Route = createFileRoute('/api/auth/workos/webhooks')({
7979
console.log(`Syncing organization ${orgName} to backend and statesman`);
8080
try {
8181
await syncOrgToBackend(invitation.organizationId!, orgName, null);
82-
await syncOrgToStatesman(invitation.organizationId!, orgName, personalOrgDisplayName, invitation.userId!, invitation.userEmail!);
82+
await syncOrgToStatesman(invitation.organizationId!, orgName, orgName, invitation.userId!, invitation.userEmail!);
8383
} catch (error) {
84-
console.error(`Error syncing user to backend:`, error);
84+
console.error(`Error syncing organization to backend:`, error);
8585
throw error;
8686
}
8787
}

0 commit comments

Comments
 (0)