Add document saved
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
-- Revert changes to letter_outgoing_recipients table
|
||||
ALTER TABLE letter_outgoing_recipients
|
||||
DROP COLUMN IF EXISTS user_id,
|
||||
DROP COLUMN IF EXISTS department_id,
|
||||
DROP COLUMN IF EXISTS status,
|
||||
DROP COLUMN IF EXISTS read_at,
|
||||
DROP COLUMN IF EXISTS flag,
|
||||
DROP COLUMN IF EXISTS is_archived;
|
||||
|
||||
-- Drop indexes
|
||||
DROP INDEX IF EXISTS idx_letter_outgoing_recipients_user;
|
||||
DROP INDEX IF EXISTS idx_letter_outgoing_recipients_department;
|
||||
DROP INDEX IF EXISTS idx_letter_outgoing_recipients_status;
|
||||
DROP INDEX IF EXISTS idx_letter_outgoing_recipients_archived;
|
||||
@@ -0,0 +1,14 @@
|
||||
-- Add missing fields to letter_outgoing_recipients table to match incoming letter structure
|
||||
ALTER TABLE letter_outgoing_recipients
|
||||
ADD COLUMN IF NOT EXISTS user_id UUID REFERENCES users(id),
|
||||
ADD COLUMN IF NOT EXISTS department_id UUID REFERENCES departments(id),
|
||||
ADD COLUMN IF NOT EXISTS status TEXT DEFAULT 'pending',
|
||||
ADD COLUMN IF NOT EXISTS read_at TIMESTAMP WITHOUT TIME ZONE,
|
||||
ADD COLUMN IF NOT EXISTS flag TEXT,
|
||||
ADD COLUMN IF NOT EXISTS is_archived BOOLEAN DEFAULT false;
|
||||
|
||||
-- Add indexes for better query performance
|
||||
CREATE INDEX IF NOT EXISTS idx_letter_outgoing_recipients_user ON letter_outgoing_recipients(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_letter_outgoing_recipients_department ON letter_outgoing_recipients(department_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_letter_outgoing_recipients_status ON letter_outgoing_recipients(status);
|
||||
CREATE INDEX IF NOT EXISTS idx_letter_outgoing_recipients_archived ON letter_outgoing_recipients(is_archived);
|
||||
@@ -0,0 +1,4 @@
|
||||
ALTER TABLE letter_outgoing_recipients
|
||||
ADD COLUMN IF NOT EXISTS recipient_name VARCHAR(255) NOT NULL,
|
||||
ADD COLUMN IF NOT EXISTS recipient_email VARCHAR(255) NOT NULL,
|
||||
ADD COLUMN IF NOT EXISTS recipient_position VARCHAR(255) NOT NULL;
|
||||
@@ -0,0 +1,4 @@
|
||||
ALTER TABLE letter_outgoing_recipients
|
||||
DROP COLUMN IF EXISTS recipient_name,
|
||||
DROP COLUMN IF EXISTS recipient_email,
|
||||
DROP COLUMN IF EXISTS recipient_position;
|
||||
@@ -0,0 +1,12 @@
|
||||
-- Drop triggers
|
||||
DROP TRIGGER IF EXISTS update_document_sessions_updated_at ON document_sessions;
|
||||
DROP TRIGGER IF EXISTS update_document_metadata_updated_at ON document_metadata;
|
||||
|
||||
-- Drop function
|
||||
DROP FUNCTION IF EXISTS update_updated_at_column();
|
||||
|
||||
-- Drop tables
|
||||
DROP TABLE IF EXISTS document_errors;
|
||||
DROP TABLE IF EXISTS document_metadata;
|
||||
DROP TABLE IF EXISTS document_versions;
|
||||
DROP TABLE IF EXISTS document_sessions;
|
||||
@@ -0,0 +1,91 @@
|
||||
-- Create document sessions table for OnlyOffice integration
|
||||
CREATE TABLE IF NOT EXISTS document_sessions (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
document_id UUID NOT NULL,
|
||||
document_key VARCHAR(255) UNIQUE NOT NULL,
|
||||
user_id UUID NOT NULL REFERENCES users(id),
|
||||
status INTEGER NOT NULL DEFAULT 0,
|
||||
is_locked BOOLEAN DEFAULT false,
|
||||
locked_by UUID REFERENCES users(id),
|
||||
locked_at TIMESTAMP WITHOUT TIME ZONE,
|
||||
last_saved_at TIMESTAMP WITHOUT TIME ZONE,
|
||||
version INTEGER NOT NULL DEFAULT 1,
|
||||
created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- Create indexes for document sessions
|
||||
CREATE INDEX idx_document_sessions_document ON document_sessions(document_id);
|
||||
CREATE INDEX idx_document_sessions_user ON document_sessions(user_id);
|
||||
CREATE INDEX idx_document_sessions_status ON document_sessions(status);
|
||||
CREATE INDEX idx_document_sessions_locked ON document_sessions(is_locked);
|
||||
|
||||
-- Create document versions table
|
||||
CREATE TABLE IF NOT EXISTS document_versions (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
document_id UUID NOT NULL,
|
||||
version INTEGER NOT NULL,
|
||||
file_url TEXT NOT NULL,
|
||||
file_size BIGINT NOT NULL,
|
||||
changes_url TEXT,
|
||||
saved_by UUID NOT NULL REFERENCES users(id),
|
||||
saved_at TIMESTAMP WITHOUT TIME ZONE NOT NULL,
|
||||
is_active BOOLEAN DEFAULT false,
|
||||
comments TEXT,
|
||||
created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- Create indexes for document versions
|
||||
CREATE INDEX idx_document_versions_document ON document_versions(document_id);
|
||||
CREATE INDEX idx_document_versions_active ON document_versions(is_active);
|
||||
CREATE UNIQUE INDEX idx_document_versions_active_unique ON document_versions(document_id, is_active) WHERE is_active = true;
|
||||
|
||||
-- Create document metadata table
|
||||
CREATE TABLE IF NOT EXISTS document_metadata (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
document_id UUID UNIQUE NOT NULL,
|
||||
document_type VARCHAR(50) NOT NULL,
|
||||
reference_id UUID NOT NULL,
|
||||
file_name TEXT NOT NULL,
|
||||
file_type VARCHAR(50) NOT NULL,
|
||||
file_size BIGINT NOT NULL,
|
||||
mime_type VARCHAR(255),
|
||||
created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- Create indexes for document metadata
|
||||
CREATE INDEX idx_document_metadata_document ON document_metadata(document_id);
|
||||
CREATE INDEX idx_document_metadata_type ON document_metadata(document_type);
|
||||
CREATE INDEX idx_document_metadata_reference ON document_metadata(reference_id);
|
||||
|
||||
-- Create document errors table for logging
|
||||
CREATE TABLE IF NOT EXISTS document_errors (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
document_id UUID NOT NULL,
|
||||
session_id UUID REFERENCES document_sessions(id),
|
||||
error_type VARCHAR(100) NOT NULL,
|
||||
error_msg TEXT NOT NULL,
|
||||
details JSONB,
|
||||
created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- Create indexes for document errors
|
||||
CREATE INDEX idx_document_errors_document ON document_errors(document_id);
|
||||
CREATE INDEX idx_document_errors_session ON document_errors(session_id);
|
||||
CREATE INDEX idx_document_errors_type ON document_errors(error_type);
|
||||
|
||||
-- Add trigger to update updated_at timestamp
|
||||
CREATE OR REPLACE FUNCTION update_updated_at_column()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
NEW.updated_at = CURRENT_TIMESTAMP;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ language 'plpgsql';
|
||||
|
||||
CREATE TRIGGER update_document_sessions_updated_at BEFORE UPDATE ON document_sessions
|
||||
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
|
||||
|
||||
CREATE TRIGGER update_document_metadata_updated_at BEFORE UPDATE ON document_metadata
|
||||
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
|
||||
@@ -0,0 +1,12 @@
|
||||
-- Drop performance indexes
|
||||
|
||||
DROP INDEX IF EXISTS idx_letters_outgoing_id_deleted;
|
||||
DROP INDEX IF EXISTS idx_users_id;
|
||||
DROP INDEX IF EXISTS idx_document_sessions_document_key;
|
||||
DROP INDEX IF EXISTS idx_document_metadata_document_id;
|
||||
DROP INDEX IF EXISTS idx_letter_outgoing_attachments_id;
|
||||
DROP INDEX IF EXISTS idx_letter_outgoing_attachments_letter_id;
|
||||
DROP INDEX IF EXISTS idx_letter_incoming_attachments_id;
|
||||
DROP INDEX IF EXISTS idx_letter_incoming_attachments_letter_id;
|
||||
DROP INDEX IF EXISTS idx_letter_outgoing_discussion_attachments_id;
|
||||
DROP INDEX IF EXISTS idx_letter_outgoing_discussion_attachments_discussion_id;
|
||||
@@ -0,0 +1,22 @@
|
||||
-- Add indexes to improve query performance
|
||||
|
||||
-- Index for letters_outgoing
|
||||
CREATE INDEX IF NOT EXISTS idx_letters_outgoing_id_deleted ON letters_outgoing(id, deleted_at);
|
||||
|
||||
-- Index for users table
|
||||
CREATE INDEX IF NOT EXISTS idx_users_id ON users(id);
|
||||
|
||||
-- Indexes for document sessions lookups
|
||||
CREATE INDEX IF NOT EXISTS idx_document_sessions_document_key ON document_sessions(document_key);
|
||||
CREATE INDEX IF NOT EXISTS idx_document_metadata_document_id ON document_metadata(document_id);
|
||||
|
||||
-- Additional indexes for letter attachments
|
||||
CREATE INDEX IF NOT EXISTS idx_letter_outgoing_attachments_id ON letter_outgoing_attachments(id);
|
||||
CREATE INDEX IF NOT EXISTS idx_letter_outgoing_attachments_letter_id ON letter_outgoing_attachments(letter_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_letter_incoming_attachments_id ON letter_incoming_attachments(id);
|
||||
CREATE INDEX IF NOT EXISTS idx_letter_incoming_attachments_letter_id ON letter_incoming_attachments(letter_id);
|
||||
|
||||
-- Index for discussion attachments
|
||||
CREATE INDEX IF NOT EXISTS idx_letter_outgoing_discussion_attachments_id ON letter_outgoing_discussion_attachments(id);
|
||||
CREATE INDEX IF NOT EXISTS idx_letter_outgoing_discussion_attachments_discussion_id ON letter_outgoing_discussion_attachments(discussion_id);
|
||||
@@ -0,0 +1,9 @@
|
||||
-- Revert document metadata VARCHAR constraints
|
||||
-- Note: This may fail if existing data exceeds 50 characters
|
||||
|
||||
ALTER TABLE document_metadata
|
||||
ALTER COLUMN document_type TYPE VARCHAR(50),
|
||||
ALTER COLUMN file_type TYPE VARCHAR(50);
|
||||
|
||||
ALTER TABLE document_metadata
|
||||
ALTER COLUMN mime_type TYPE VARCHAR(255);
|
||||
@@ -0,0 +1,10 @@
|
||||
-- Fix document metadata VARCHAR constraints
|
||||
-- Increase the size limit for document_type and file_type columns
|
||||
|
||||
ALTER TABLE document_metadata
|
||||
ALTER COLUMN document_type TYPE VARCHAR(255),
|
||||
ALTER COLUMN file_type TYPE VARCHAR(255);
|
||||
|
||||
-- Also ensure mime_type has sufficient length
|
||||
ALTER TABLE document_metadata
|
||||
ALTER COLUMN mime_type TYPE VARCHAR(255);
|
||||
Reference in New Issue
Block a user