set up for deployment
This commit is contained in:
32
backend/scripts/archive/init.sql
Normal file
32
backend/scripts/archive/init.sql
Normal file
@@ -0,0 +1,32 @@
|
||||
-- Initialize database schema for realtime collaboration
|
||||
|
||||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
||||
|
||||
CREATE TABLE IF NOT EXISTS documents (
|
||||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||
name VARCHAR(255) NOT NULL,
|
||||
type VARCHAR(50) NOT NULL CHECK (type IN ('editor', 'kanban')),
|
||||
yjs_state BYTEA,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_documents_type ON documents(type);
|
||||
CREATE INDEX idx_documents_created_at ON documents(created_at DESC);
|
||||
|
||||
-- Optional: Table for storing incremental updates (for history)
|
||||
CREATE TABLE IF NOT EXISTS document_updates (
|
||||
id SERIAL PRIMARY KEY,
|
||||
document_id UUID NOT NULL REFERENCES documents(id) ON DELETE CASCADE,
|
||||
update BYTEA NOT NULL,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_updates_document_id ON document_updates(document_id);
|
||||
CREATE INDEX idx_updates_created_at ON document_updates(created_at DESC);
|
||||
|
||||
-- Insert some sample documents for testing
|
||||
INSERT INTO documents (id, name, type) VALUES
|
||||
('00000000-0000-0000-0000-000000000001', 'Welcome Document', 'editor'),
|
||||
('00000000-0000-0000-0000-000000000002', 'Project Kanban', 'kanban')
|
||||
ON CONFLICT DO NOTHING;
|
||||
38
backend/scripts/archive/migration_add_versions.sql
Normal file
38
backend/scripts/archive/migration_add_versions.sql
Normal file
@@ -0,0 +1,38 @@
|
||||
-- Migration: Add document version history support
|
||||
-- Run: psql -U postgres collaboration < backend/scripts/migration_add_versions.sql
|
||||
|
||||
CREATE TABLE IF NOT EXISTS document_versions (
|
||||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||
document_id UUID NOT NULL REFERENCES documents(id) ON DELETE CASCADE,
|
||||
yjs_snapshot BYTEA NOT NULL,
|
||||
text_preview TEXT,
|
||||
version_number INTEGER NOT NULL,
|
||||
created_by UUID REFERENCES users(id) ON DELETE SET NULL,
|
||||
version_label TEXT,
|
||||
is_auto_generated BOOLEAN DEFAULT true,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
|
||||
CONSTRAINT unique_document_version UNIQUE(document_id, version_number)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_document_versions_document_id ON document_versions(document_id, created_at DESC);
|
||||
CREATE INDEX idx_document_versions_created_by ON document_versions(created_by);
|
||||
|
||||
-- Add version tracking to documents table
|
||||
ALTER TABLE documents ADD COLUMN IF NOT EXISTS version_count INTEGER DEFAULT 0;
|
||||
ALTER TABLE documents ADD COLUMN IF NOT EXISTS last_snapshot_at TIMESTAMPTZ;
|
||||
|
||||
-- Function to get next version number
|
||||
CREATE OR REPLACE FUNCTION get_next_version_number(p_document_id UUID)
|
||||
RETURNS INTEGER AS $$
|
||||
DECLARE
|
||||
next_version INTEGER;
|
||||
BEGIN
|
||||
SELECT COALESCE(MAX(version_number), 0) + 1
|
||||
INTO next_version
|
||||
FROM document_versions
|
||||
WHERE document_id = p_document_id;
|
||||
|
||||
RETURN next_version;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
Reference in New Issue
Block a user