Skip to content

Commit

Permalink
feat: add brain_type column
Browse files Browse the repository at this point in the history
  • Loading branch information
mamadoudicko committed Nov 7, 2023
1 parent 5feaecf commit 844ec0f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
7 changes: 7 additions & 0 deletions backend/models/databases/supabase/brains.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from enum import Enum
from typing import Optional
from uuid import UUID

Expand All @@ -9,6 +10,11 @@
logger = get_logger(__name__)


class BrainType(str, Enum):
DOC = "doc"
API = "api"


class CreateBrainProperties(BaseModel):
name: Optional[str] = "Default brain"
description: Optional[str] = "This is a description"
Expand All @@ -18,6 +24,7 @@ class CreateBrainProperties(BaseModel):
max_tokens: Optional[int] = 256
openai_api_key: Optional[str] = None
prompt_id: Optional[UUID] = None
brain_type: Optional[BrainType] = BrainType.DOC

def dict(self, *args, **kwargs):
brain_dict = super().dict(*args, **kwargs)
Expand Down
34 changes: 34 additions & 0 deletions scripts/20231106110000_add_field_brain_type_to_brain_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-- Check if the ENUM type 'brain_type' already exists
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'brain_type_enum') THEN
-- Create the ENUM type 'brain_type' if it doesn't exist
CREATE TYPE brain_type_enum AS ENUM ('doc', 'api');
END IF;
END $$;

-- Add a column 'brain_type' to the 'brains' table using the 'brain_type' ENUM type
BEGIN;

-- Add a column 'brain_type' to the 'brains' table as the 'brain_type' ENUM type with a default value 'doc'
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM information_schema.columns
WHERE table_name = 'brains'
AND column_name = 'brain_type'
) THEN
ALTER TABLE brains ADD COLUMN brain_type brain_type_enum DEFAULT 'doc';
END IF;
END $$;

-- Insert a migration record if it doesn't exist
INSERT INTO migrations (name)
SELECT '20231106110000_add_field_brain_type_to_brain_table'
WHERE NOT EXISTS (
SELECT 1 FROM migrations WHERE name = '20231106110000_add_field_brain_type_to_brain_table'
);

-- Commit the changes
COMMIT;
15 changes: 12 additions & 3 deletions scripts/tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ CREATE TABLE IF NOT EXISTS prompts (
status VARCHAR(255) DEFAULT 'private'
);

DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'brain_type_enum') THEN
-- Create the ENUM type 'brain_type' if it doesn't exist
CREATE TYPE brain_type_enum AS ENUM ('doc', 'api');
END IF;
END $$;

--- Create brains table
CREATE TABLE IF NOT EXISTS brains (
brain_id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
Expand All @@ -137,7 +145,8 @@ CREATE TABLE IF NOT EXISTS brains (
temperature FLOAT,
openai_api_key TEXT,
prompt_id UUID REFERENCES prompts(id),
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
brain_type brain_type_enum DEFAULT 'doc'
);


Expand Down Expand Up @@ -390,9 +399,9 @@ CREATE POLICY "Access Quivr Storage 1jccrwz_2" ON storage.objects FOR UPDATE TO
CREATE POLICY "Access Quivr Storage 1jccrwz_3" ON storage.objects FOR DELETE TO anon USING (bucket_id = 'quivr');

INSERT INTO migrations (name)
SELECT '2023110607100000_add_api_brain_config_table'
SELECT '20231106110000_add_field_brain_type_to_brain_table'
WHERE NOT EXISTS (
SELECT 1 FROM migrations WHERE name = '2023110607100000_add_api_brain_config_table'
SELECT 1 FROM migrations WHERE name = '20231106110000_add_field_brain_type_to_brain_table'
);


0 comments on commit 844ec0f

Please sign in to comment.