fix email issues, add company,project,todos
This commit is contained in:
31
src/db/migrations/add_company_link_and_reminders.sql
Normal file
31
src/db/migrations/add_company_link_and_reminders.sql
Normal file
@@ -0,0 +1,31 @@
|
||||
-- Add company_id to contacts table
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_name='contacts' AND column_name='company_id'
|
||||
) THEN
|
||||
ALTER TABLE contacts ADD COLUMN company_id UUID REFERENCES companies(id) ON DELETE SET NULL;
|
||||
CREATE INDEX idx_contacts_company_id ON contacts(company_id);
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- Add reminder fields to notes table
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_name='notes' AND column_name='reminder_date'
|
||||
) THEN
|
||||
ALTER TABLE notes ADD COLUMN reminder_date TIMESTAMP;
|
||||
CREATE INDEX idx_notes_reminder_date ON notes(reminder_date) WHERE reminder_date IS NOT NULL;
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_name='notes' AND column_name='reminder_sent'
|
||||
) THEN
|
||||
ALTER TABLE notes ADD COLUMN reminder_sent BOOLEAN NOT NULL DEFAULT false;
|
||||
CREATE INDEX idx_notes_reminder_pending ON notes(reminder_date, reminder_sent) WHERE reminder_date IS NOT NULL AND reminder_sent = false;
|
||||
END IF;
|
||||
END $$;
|
||||
107
src/db/migrations/add_crm_tables.sql
Normal file
107
src/db/migrations/add_crm_tables.sql
Normal file
@@ -0,0 +1,107 @@
|
||||
-- Add new enum types
|
||||
DO $$ BEGIN
|
||||
CREATE TYPE project_status AS ENUM('active', 'completed', 'on_hold', 'cancelled');
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
|
||||
DO $$ BEGIN
|
||||
CREATE TYPE todo_status AS ENUM('pending', 'in_progress', 'completed', 'cancelled');
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
|
||||
DO $$ BEGIN
|
||||
CREATE TYPE todo_priority AS ENUM('low', 'medium', 'high', 'urgent');
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
|
||||
-- Create companies table
|
||||
CREATE TABLE IF NOT EXISTS companies (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
name TEXT NOT NULL,
|
||||
description TEXT,
|
||||
address TEXT,
|
||||
city TEXT,
|
||||
country TEXT,
|
||||
phone TEXT,
|
||||
email TEXT,
|
||||
website TEXT,
|
||||
created_by UUID REFERENCES users(id) ON DELETE SET NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Create projects table
|
||||
CREATE TABLE IF NOT EXISTS projects (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
name TEXT NOT NULL,
|
||||
description TEXT,
|
||||
company_id UUID REFERENCES companies(id) ON DELETE CASCADE,
|
||||
status project_status NOT NULL DEFAULT 'active',
|
||||
start_date TIMESTAMP,
|
||||
end_date TIMESTAMP,
|
||||
created_by UUID REFERENCES users(id) ON DELETE SET NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Create todos table
|
||||
CREATE TABLE IF NOT EXISTS todos (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
title TEXT NOT NULL,
|
||||
description TEXT,
|
||||
project_id UUID REFERENCES projects(id) ON DELETE CASCADE,
|
||||
company_id UUID REFERENCES companies(id) ON DELETE CASCADE,
|
||||
assigned_to UUID REFERENCES users(id) ON DELETE SET NULL,
|
||||
status todo_status NOT NULL DEFAULT 'pending',
|
||||
priority todo_priority NOT NULL DEFAULT 'medium',
|
||||
due_date TIMESTAMP,
|
||||
completed_at TIMESTAMP,
|
||||
created_by UUID REFERENCES users(id) ON DELETE SET NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Create notes table
|
||||
CREATE TABLE IF NOT EXISTS notes (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
title TEXT,
|
||||
content TEXT NOT NULL,
|
||||
company_id UUID REFERENCES companies(id) ON DELETE CASCADE,
|
||||
project_id UUID REFERENCES projects(id) ON DELETE CASCADE,
|
||||
todo_id UUID REFERENCES todos(id) ON DELETE CASCADE,
|
||||
contact_id UUID REFERENCES contacts(id) ON DELETE CASCADE,
|
||||
created_by UUID REFERENCES users(id) ON DELETE SET NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Add project_id to timesheets table if not exists
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_name='timesheets' AND column_name='project_id'
|
||||
) THEN
|
||||
ALTER TABLE timesheets ADD COLUMN project_id UUID REFERENCES projects(id) ON DELETE SET NULL;
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- Create indexes for better query performance
|
||||
CREATE INDEX IF NOT EXISTS idx_companies_created_at ON companies(created_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_projects_company_id ON projects(company_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_projects_status ON projects(status);
|
||||
CREATE INDEX IF NOT EXISTS idx_projects_created_at ON projects(created_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_todos_project_id ON todos(project_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_todos_company_id ON todos(company_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_todos_assigned_to ON todos(assigned_to);
|
||||
CREATE INDEX IF NOT EXISTS idx_todos_status ON todos(status);
|
||||
CREATE INDEX IF NOT EXISTS idx_todos_created_at ON todos(created_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_notes_company_id ON notes(company_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_notes_project_id ON notes(project_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_notes_todo_id ON notes(todo_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_notes_contact_id ON notes(contact_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_notes_created_at ON notes(created_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_timesheets_project_id ON timesheets(project_id);
|
||||
21
src/db/migrations/add_project_users.sql
Normal file
21
src/db/migrations/add_project_users.sql
Normal file
@@ -0,0 +1,21 @@
|
||||
-- Migration: Add project_users junction table for project team management
|
||||
-- Created: 2025-11-21
|
||||
-- Description: Allows many-to-many relationship between projects and users
|
||||
|
||||
-- Create project_users junction table
|
||||
CREATE TABLE IF NOT EXISTS project_users (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
project_id UUID NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
role TEXT,
|
||||
added_by UUID REFERENCES users(id) ON DELETE SET NULL,
|
||||
added_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
CONSTRAINT project_user_unique UNIQUE(project_id, user_id)
|
||||
);
|
||||
|
||||
-- Create indexes for better query performance
|
||||
CREATE INDEX IF NOT EXISTS idx_project_users_project_id ON project_users(project_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_project_users_user_id ON project_users(user_id);
|
||||
|
||||
-- Add comment
|
||||
COMMENT ON TABLE project_users IS 'Junction table for many-to-many relationship between projects and users (project team members)';
|
||||
Reference in New Issue
Block a user