Skip to content

Commit 390f34a

Browse files
fix: Fix api keys migration
1 parent e805f6a commit 390f34a

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

memory-store/migrations/000042_add_api_keys.up.sql

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ CREATE TABLE IF NOT EXISTS api_keys (
1111
metadata JSONB DEFAULT '{}'::jsonb,
1212
deleted_at TIMESTAMPTZ DEFAULT NULL,
1313
CONSTRAINT pk_api_keys PRIMARY KEY (developer_id, api_key_id),
14-
CONSTRAINT uq_api_keys_unique UNIQUE(developer_id, name) WHERE deleted_at IS NULL,
1514
CONSTRAINT ct_api_keys_metadata_is_object CHECK (jsonb_typeof(metadata) = 'object'),
1615
CONSTRAINT ct_api_keys_name_valid_identifier CHECK (name ~ '^[a-zA-Z][a-zA-Z0-9_]*$'),
1716
CONSTRAINT fk_api_keys_developer FOREIGN KEY (developer_id) REFERENCES developers(developer_id)
1817
);
1918

19+
-- Create partial unique index instead of constraint with WHERE clause
20+
CREATE UNIQUE INDEX IF NOT EXISTS uq_api_keys_developer_id_name
21+
ON api_keys (developer_id, name)
22+
WHERE deleted_at IS NULL;
23+
2024
-- Create trigger function for updated_at
2125
CREATE OR REPLACE FUNCTION update_api_keys_timestamp()
2226
RETURNS TRIGGER AS $$
@@ -67,4 +71,4 @@ $$ LANGUAGE plpgsql SECURITY DEFINER;
6771
-- Add comment to table
6872
COMMENT ON TABLE api_keys IS 'Stores API keys with encryption for developers';
6973

70-
COMMIT;
74+
COMMIT;

0 commit comments

Comments
 (0)