Skip to content

Commit

Permalink
refactor(memory-store): Reformat migrations using sql-formatter
Browse files Browse the repository at this point in the history
Signed-off-by: Diwank Singh Tomer <[email protected]>
  • Loading branch information
creatorrr committed Dec 14, 2024
1 parent 9c974e8 commit 85a4e8b
Show file tree
Hide file tree
Showing 23 changed files with 298 additions and 167 deletions.
12 changes: 11 additions & 1 deletion memory-store/migrations/000001_initial.down.sql
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
BEGIN;

-- Drop the update_updated_at_column function
DROP FUNCTION IF EXISTS update_updated_at_column();
DROP FUNCTION IF EXISTS update_updated_at_column ();

-- Drop misc extensions
DROP EXTENSION IF EXISTS "uuid-ossp" CASCADE;

DROP EXTENSION IF EXISTS citext CASCADE;

DROP EXTENSION IF EXISTS btree_gist CASCADE;

DROP EXTENSION IF EXISTS btree_gin CASCADE;

-- Drop timescale's pgai extensions
DROP EXTENSION IF EXISTS ai CASCADE;

DROP EXTENSION IF EXISTS vectorscale CASCADE;

DROP EXTENSION IF EXISTS vector CASCADE;

-- Drop timescaledb extensions
DROP EXTENSION IF EXISTS timescaledb_toolkit CASCADE;

DROP EXTENSION IF EXISTS timescaledb CASCADE;

COMMIT;
12 changes: 9 additions & 3 deletions memory-store/migrations/000001_initial.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,34 @@ BEGIN;

-- init timescaledb
CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;

CREATE EXTENSION IF NOT EXISTS timescaledb_toolkit CASCADE;

-- add timescale's pgai extension
CREATE EXTENSION IF NOT EXISTS vector CASCADE;

CREATE EXTENSION IF NOT EXISTS vectorscale CASCADE;

CREATE EXTENSION IF NOT EXISTS ai CASCADE;

-- add misc extensions (for indexing etc)
CREATE EXTENSION IF NOT EXISTS btree_gin CASCADE;

CREATE EXTENSION IF NOT EXISTS btree_gist CASCADE;

CREATE EXTENSION IF NOT EXISTS citext CASCADE;

CREATE EXTENSION IF NOT EXISTS "uuid-ossp" CASCADE;

-- Create function to update the updated_at timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
CREATE
OR REPLACE FUNCTION update_updated_at_column () RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$ language 'plpgsql';

COMMENT ON FUNCTION update_updated_at_column() IS 'Trigger function to automatically update updated_at timestamp';
COMMENT ON FUNCTION update_updated_at_column () IS 'Trigger function to automatically update updated_at timestamp';

COMMIT;
11 changes: 8 additions & 3 deletions memory-store/migrations/000002_developers.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ BEGIN;
-- Create developers table
CREATE TABLE IF NOT EXISTS developers (
developer_id UUID NOT NULL,
email TEXT NOT NULL CONSTRAINT ct_developers_email_format CHECK (email ~* '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$'),
active BOOLEAN NOT NULL DEFAULT true,
email TEXT NOT NULL CONSTRAINT ct_developers_email_format CHECK (
email ~* '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$'
),
active BOOLEAN NOT NULL DEFAULT TRUE,
tags TEXT[] DEFAULT ARRAY[]::TEXT[],
settings JSONB NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
Expand All @@ -23,7 +25,9 @@ CREATE INDEX IF NOT EXISTS idx_developers_email ON developers (email);
CREATE INDEX IF NOT EXISTS idx_developers_tags ON developers USING GIN (tags);

-- Create partial index for active developers
CREATE INDEX IF NOT EXISTS idx_developers_active ON developers (developer_id) WHERE active = true;
CREATE INDEX IF NOT EXISTS idx_developers_active ON developers (developer_id)
WHERE
active = TRUE;

-- Create trigger to automatically update updated_at
DO $$
Expand All @@ -39,4 +43,5 @@ $$;

-- Add comment to table
COMMENT ON TABLE developers IS 'Stores developer information including their settings and tags';

COMMIT;
4 changes: 3 additions & 1 deletion memory-store/migrations/000003_users.down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ DROP TRIGGER IF EXISTS update_users_updated_at ON users;

-- Drop indexes
DROP INDEX IF EXISTS users_metadata_gin_idx;

DROP INDEX IF EXISTS users_developer_id_idx;

DROP INDEX IF EXISTS users_id_sorted_idx;

-- Drop foreign key constraint
ALTER TABLE IF EXISTS users
DROP CONSTRAINT IF EXISTS users_developer_id_fkey;
DROP CONSTRAINT IF EXISTS users_developer_id_fkey;

-- Finally drop the table
DROP TABLE IF EXISTS users;
Expand Down
1 change: 1 addition & 0 deletions memory-store/migrations/000003_users.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ END $$;

-- Add comment to table (comments are idempotent by default)
COMMENT ON TABLE users IS 'Stores user information linked to developers';

COMMIT;
2 changes: 2 additions & 0 deletions memory-store/migrations/000004_agents.down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ DROP TRIGGER IF EXISTS trg_agents_updated_at ON agents;

-- Drop indexes
DROP INDEX IF EXISTS idx_agents_metadata;

DROP INDEX IF EXISTS idx_agents_developer;

DROP INDEX IF EXISTS idx_agents_id_sorted;

-- Drop table (this will automatically drop associated constraints)
Expand Down
36 changes: 24 additions & 12 deletions memory-store/migrations/000004_agents.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,31 @@ BEGIN;

-- Drop existing objects if they exist
DROP TRIGGER IF EXISTS trg_agents_updated_at ON agents;

DROP INDEX IF EXISTS idx_agents_metadata;

DROP INDEX IF EXISTS idx_agents_developer;

DROP INDEX IF EXISTS idx_agents_id_sorted;

DROP TABLE IF EXISTS agents;

-- Create agents table
CREATE TABLE IF NOT EXISTS agents (
developer_id UUID NOT NULL,
agent_id UUID NOT NULL,
canonical_name citext NOT NULL CONSTRAINT ct_agents_canonical_name_length CHECK (length(canonical_name) >= 1 AND length(canonical_name) <= 255),
name TEXT NOT NULL CONSTRAINT ct_agents_name_length CHECK (length(name) >= 1 AND length(name) <= 255),
about TEXT CONSTRAINT ct_agents_about_length CHECK (about IS NULL OR length(about) <= 1000),
canonical_name citext NOT NULL CONSTRAINT ct_agents_canonical_name_length CHECK (
length(canonical_name) >= 1
AND length(canonical_name) <= 255
),
name TEXT NOT NULL CONSTRAINT ct_agents_name_length CHECK (
length(name) >= 1
AND length(name) <= 255
),
about TEXT CONSTRAINT ct_agents_about_length CHECK (
about IS NULL
OR length(about) <= 1000
),
instructions TEXT[] DEFAULT ARRAY[]::TEXT[],
model TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
Expand All @@ -29,23 +42,22 @@ CREATE TABLE IF NOT EXISTS agents (
CREATE INDEX IF NOT EXISTS idx_agents_id_sorted ON agents (agent_id DESC);

-- Create foreign key constraint and index on developer_id
ALTER TABLE agents
DROP CONSTRAINT IF EXISTS fk_agents_developer,
ADD CONSTRAINT fk_agents_developer
FOREIGN KEY (developer_id)
REFERENCES developers(developer_id);
ALTER TABLE agents
DROP CONSTRAINT IF EXISTS fk_agents_developer,
ADD CONSTRAINT fk_agents_developer FOREIGN KEY (developer_id) REFERENCES developers (developer_id);

CREATE INDEX IF NOT EXISTS idx_agents_developer ON agents (developer_id);

-- Create a GIN index on the entire metadata column
CREATE INDEX IF NOT EXISTS idx_agents_metadata ON agents USING GIN (metadata);

-- Create trigger to automatically update updated_at
CREATE OR REPLACE TRIGGER trg_agents_updated_at
BEFORE UPDATE ON agents
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
CREATE
OR REPLACE TRIGGER trg_agents_updated_at BEFORE
UPDATE ON agents FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column ();

-- Add comment to table
COMMENT ON TABLE agents IS 'Stores AI agent configurations and metadata for developers';

COMMIT;
1 change: 1 addition & 0 deletions memory-store/migrations/000005_files.down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ DROP TABLE IF EXISTS user_files;

-- Drop files table and its dependencies
DROP TRIGGER IF EXISTS trg_files_updated_at ON files;

DROP TABLE IF EXISTS files;

COMMIT;
23 changes: 16 additions & 7 deletions memory-store/migrations/000005_files.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@ BEGIN;
CREATE TABLE IF NOT EXISTS files (
developer_id UUID NOT NULL,
file_id UUID NOT NULL,
name TEXT NOT NULL CONSTRAINT ct_files_name_length CHECK (length(name) >= 1 AND length(name) <= 255),
description TEXT DEFAULT NULL CONSTRAINT ct_files_description_length CHECK (description IS NULL OR length(description) <= 1000),
mime_type TEXT DEFAULT NULL CONSTRAINT ct_files_mime_type_length CHECK (mime_type IS NULL OR length(mime_type) <= 127),
name TEXT NOT NULL CONSTRAINT ct_files_name_length CHECK (
length(name) >= 1
AND length(name) <= 255
),
description TEXT DEFAULT NULL CONSTRAINT ct_files_description_length CHECK (
description IS NULL
OR length(description) <= 1000
),
mime_type TEXT DEFAULT NULL CONSTRAINT ct_files_mime_type_length CHECK (
mime_type IS NULL
OR length(mime_type) <= 127
),
size BIGINT NOT NULL CONSTRAINT ct_files_size_positive CHECK (size > 0),
hash BYTEA NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
Expand Down Expand Up @@ -53,8 +62,8 @@ CREATE TABLE IF NOT EXISTS user_files (
user_id UUID NOT NULL,
file_id UUID NOT NULL,
CONSTRAINT pk_user_files PRIMARY KEY (developer_id, user_id, file_id),
CONSTRAINT fk_user_files_user FOREIGN KEY (developer_id, user_id) REFERENCES users(developer_id, user_id),
CONSTRAINT fk_user_files_file FOREIGN KEY (developer_id, file_id) REFERENCES files(developer_id, file_id)
CONSTRAINT fk_user_files_user FOREIGN KEY (developer_id, user_id) REFERENCES users (developer_id, user_id),
CONSTRAINT fk_user_files_file FOREIGN KEY (developer_id, file_id) REFERENCES files (developer_id, file_id)
);

-- Create index if it doesn't exist
Expand All @@ -66,8 +75,8 @@ CREATE TABLE IF NOT EXISTS agent_files (
agent_id UUID NOT NULL,
file_id UUID NOT NULL,
CONSTRAINT pk_agent_files PRIMARY KEY (developer_id, agent_id, file_id),
CONSTRAINT fk_agent_files_agent FOREIGN KEY (developer_id, agent_id) REFERENCES agents(developer_id, agent_id),
CONSTRAINT fk_agent_files_file FOREIGN KEY (developer_id, file_id) REFERENCES files(developer_id, file_id)
CONSTRAINT fk_agent_files_agent FOREIGN KEY (developer_id, agent_id) REFERENCES agents (developer_id, agent_id),
CONSTRAINT fk_agent_files_file FOREIGN KEY (developer_id, file_id) REFERENCES files (developer_id, file_id)
);

-- Create index if it doesn't exist
Expand Down
18 changes: 15 additions & 3 deletions memory-store/migrations/000006_docs.down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,40 @@ BEGIN;

-- Drop indexes
DROP INDEX IF EXISTS idx_docs_content_trgm;

DROP INDEX IF EXISTS idx_docs_title_trgm;

DROP INDEX IF EXISTS idx_docs_search_tsv;

DROP INDEX IF EXISTS idx_docs_metadata;

DROP INDEX IF EXISTS idx_agent_docs_agent;

DROP INDEX IF EXISTS idx_user_docs_user;

DROP INDEX IF EXISTS idx_docs_developer;

DROP INDEX IF EXISTS idx_docs_id_sorted;

-- Drop triggers
DROP TRIGGER IF EXISTS trg_docs_search_tsv ON docs;

DROP TRIGGER IF EXISTS trg_docs_updated_at ON docs;

-- Drop the constraint that depends on is_valid_language function
ALTER TABLE IF EXISTS docs DROP CONSTRAINT IF EXISTS ct_docs_valid_language;
ALTER TABLE IF EXISTS docs
DROP CONSTRAINT IF EXISTS ct_docs_valid_language;

-- Drop functions
DROP FUNCTION IF EXISTS docs_update_search_tsv();
DROP FUNCTION IF EXISTS is_valid_language(text);
DROP FUNCTION IF EXISTS docs_update_search_tsv ();

DROP FUNCTION IF EXISTS is_valid_language (text);

-- Drop tables (in correct order due to foreign key constraints)
DROP TABLE IF EXISTS agent_docs;

DROP TABLE IF EXISTS user_docs;

DROP TABLE IF EXISTS docs;

COMMIT;
Loading

0 comments on commit 85a4e8b

Please sign in to comment.