Refactor database schema to use UUIDs for primary keys and foreign keys#2233
Refactor database schema to use UUIDs for primary keys and foreign keys#2233Thushani-Jayasekera wants to merge 1 commit into
Conversation
Thushani-Jayasekera
commented
Jun 21, 2026
- Updated the schema for organizations, projects, applications, artifacts, rest_apis, subscription_plans, subscriptions, gateways, gateway_custom_policies, gateway_custom_policy_usages, gateway_tokens, deployments, deployment_status, and association_mappings tables to replace VARCHAR(40) with UUID for uuid and related foreign key fields.
- Added new indexes for improved query performance on various tables, including api_keys, gateway_custom_policy_usages, and events.
- Updated the schema for organizations, projects, applications, artifacts, rest_apis, subscription_plans, subscriptions, gateways, gateway_custom_policies, gateway_custom_policy_usages, gateway_tokens, deployments, deployment_status, and association_mappings tables to replace VARCHAR(40) with UUID for uuid and related foreign key fields. - Added new indexes for improved query performance on various tables, including api_keys, gateway_custom_policy_usages, and events.
📝 WalkthroughDatabase Schema Refactoring to UUID IdentifiersThis pull request refactors the database schema to use UUID types for primary keys and foreign key references across the platform. The changes are applied consistently across PostgreSQL, SQLite, and generic SQL schema files. UUID MigrationThe refactoring converts VARCHAR(40) identifier columns to native UUID types across 14 core tables:
The Performance ImprovementsNew indexes are added to optimize common query patterns:
Referential IntegrityA foreign key constraint is added to the The WalkthroughThe PostgreSQL schema ( 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
platform-api/src/internal/database/schema.postgres.sql (1)
469-486:⚠️ Potential issue | 🔴 CriticalAlign gateway ID column types in schema.
gateway_states.gateway_idandevents.gateway_idareTEXT, but the foreign key at line 473 referencesgateways(uuid), which isUUID. PostgreSQL enforces type compatibility for foreign keys; this mismatch will prevent schema creation. Change both columns toUUID:
- Line 470:
gateway_id UUID PRIMARY KEY- Line 477:
gateway_id UUID NOT NULLProposed fix
CREATE TABLE IF NOT EXISTS gateway_states ( - gateway_id TEXT PRIMARY KEY, + gateway_id UUID PRIMARY KEY, version_id TEXT NOT NULL DEFAULT '', updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (gateway_id) REFERENCES gateways(uuid) ON DELETE CASCADE ); CREATE TABLE IF NOT EXISTS events ( - gateway_id TEXT NOT NULL, + gateway_id UUID NOT NULL, processed_timestamp TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, originated_timestamp TIMESTAMPTZ NOT NULL, entity_type TEXT NOT NULL,🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@platform-api/src/internal/database/schema.postgres.sql` around lines 469 - 486, The gateway_id columns in both the gateway_states and events tables are defined as TEXT type, but the foreign key constraint in gateway_states references gateways(uuid) which is a UUID type, causing a type mismatch that will prevent schema creation. PostgreSQL requires foreign key columns to have compatible types with their referenced columns. Change the gateway_id column in the gateway_states table from TEXT PRIMARY KEY to UUID PRIMARY KEY, and change the gateway_id column in the events table from TEXT NOT NULL to UUID NOT NULL to align with the UUID type in the gateways table and ensure proper foreign key relationships.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@platform-api/src/internal/database/schema.postgres.sql`:
- Around line 469-486: The gateway_id columns in both the gateway_states and
events tables are defined as TEXT type, but the foreign key constraint in
gateway_states references gateways(uuid) which is a UUID type, causing a type
mismatch that will prevent schema creation. PostgreSQL requires foreign key
columns to have compatible types with their referenced columns. Change the
gateway_id column in the gateway_states table from TEXT PRIMARY KEY to UUID
PRIMARY KEY, and change the gateway_id column in the events table from TEXT NOT
NULL to UUID NOT NULL to align with the UUID type in the gateways table and
ensure proper foreign key relationships.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: fb3b5d7d-e096-4c95-bd5e-0c70aef5eaf8
📒 Files selected for processing (3)
platform-api/src/internal/database/schema.postgres.sqlplatform-api/src/internal/database/schema.sqlplatform-api/src/internal/database/schema.sqlite.sql