quivr/scripts/20231203173900_new_api_key_format.sql
Stan Girard 31c1802084
feat(api-keys): added customization (#1802)
# Description

Please include a summary of the changes and the related issue. Please
also include relevant motivation and context.

## Checklist before requesting a review

Please delete options that are not relevant.

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented hard-to-understand areas
- [ ] I have ideally added tests that prove my fix is effective or that
my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged

## Screenshots (if appropriate):
2023-12-03 23:32:37 +01:00

34 lines
1.2 KiB
SQL

DO $$
BEGIN
-- Add 'name' column if it does not exist
IF NOT EXISTS (SELECT FROM pg_attribute WHERE attrelid = 'api_keys'::regclass AND attname = 'name') THEN
ALTER TABLE api_keys ADD COLUMN name TEXT DEFAULT 'API_KEY';
END IF;
-- Add 'days' column if it does not exist
IF NOT EXISTS (SELECT FROM pg_attribute WHERE attrelid = 'api_keys'::regclass AND attname = 'days') THEN
ALTER TABLE api_keys ADD COLUMN days INT DEFAULT 30;
END IF;
-- Add 'only_chat' column if it does not exist
IF NOT EXISTS (SELECT FROM pg_attribute WHERE attrelid = 'api_keys'::regclass AND attname = 'only_chat') THEN
ALTER TABLE api_keys ADD COLUMN only_chat BOOLEAN DEFAULT false;
END IF;
-- Optionally, update default values for existing rows if necessary
-- UPDATE api_keys SET name = 'API_KEY' WHERE name IS NULL;
-- UPDATE api_keys SET days = 30 WHERE days IS NULL;
-- UPDATE api_keys SET only_chat = false WHERE only_chat IS NULL;
END $$;
-- Update migrations table
INSERT INTO migrations (name)
SELECT '20231203173900_new_api_key_format'
WHERE NOT EXISTS (
SELECT 1 FROM migrations WHERE name = '20231203173900_new_api_key_format'
);
COMMIT;