base
This commit is contained in:
32
sql/init.sql
32
sql/init.sql
@@ -13,9 +13,12 @@ CREATE TABLE IF NOT EXISTS files (
|
||||
modified_time TIMESTAMP WITH TIME ZONE,
|
||||
created_time TIMESTAMP WITH TIME ZONE,
|
||||
file_hash VARCHAR(64), -- SHA-256 hash
|
||||
checksum VARCHAR(64), -- Alias for file_hash (legacy compatibility)
|
||||
category VARCHAR(50),
|
||||
disk_label VARCHAR(50),
|
||||
last_verified TIMESTAMP WITH TIME ZONE,
|
||||
status VARCHAR(20) DEFAULT 'indexed',
|
||||
duplicate_of TEXT, -- Path to canonical file if this is a duplicate
|
||||
|
||||
-- Metadata
|
||||
metadata JSONB DEFAULT '{}',
|
||||
@@ -36,8 +39,13 @@ CREATE TABLE IF NOT EXISTS operations (
|
||||
target_path TEXT,
|
||||
status VARCHAR(20) NOT NULL,
|
||||
|
||||
-- Legacy compatibility fields
|
||||
executed INTEGER DEFAULT 0,
|
||||
verified INTEGER DEFAULT 0,
|
||||
error TEXT,
|
||||
|
||||
-- File reference
|
||||
file_id UUID REFERENCES files_bak(id) ON DELETE SET NULL,
|
||||
file_id UUID REFERENCES files(id) ON DELETE SET NULL,
|
||||
|
||||
-- Performance metrics
|
||||
duration_ms INTEGER,
|
||||
@@ -54,6 +62,7 @@ CREATE TABLE IF NOT EXISTS operations (
|
||||
-- Audit fields
|
||||
started_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
||||
completed_at TIMESTAMP WITH TIME ZONE,
|
||||
executed_at TIMESTAMP WITH TIME ZONE,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
@@ -89,14 +98,15 @@ CREATE TABLE IF NOT EXISTS migration_plans (
|
||||
);
|
||||
|
||||
-- Indexes for performance
|
||||
CREATE INDEX IF NOT EXISTS idx_files_path ON files_bak(path);
|
||||
CREATE INDEX IF NOT EXISTS idx_files_hash ON files_bak(file_hash);
|
||||
CREATE INDEX IF NOT EXISTS idx_files_disk ON files_bak(disk_label);
|
||||
CREATE INDEX IF NOT EXISTS idx_files_category ON files_bak(category);
|
||||
CREATE INDEX IF NOT EXISTS idx_files_path ON files(path);
|
||||
CREATE INDEX IF NOT EXISTS idx_files_hash ON files(file_hash);
|
||||
CREATE INDEX IF NOT EXISTS idx_files_disk ON files(disk_label);
|
||||
CREATE INDEX IF NOT EXISTS idx_files_category ON files(category);
|
||||
CREATE INDEX IF NOT EXISTS idx_files_status ON files(status);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_operations_status ON operations_bak(status);
|
||||
CREATE INDEX IF NOT EXISTS idx_operations_created ON operations_bak(created_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_operations_file_id ON operations_bak(file_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_operations_status ON operations(status);
|
||||
CREATE INDEX IF NOT EXISTS idx_operations_created ON operations(created_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_operations_file_id ON operations(file_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_dedup_canonical ON deduplication_store(canonical_path);
|
||||
|
||||
@@ -110,7 +120,7 @@ END;
|
||||
$$ language 'plpgsql';
|
||||
|
||||
-- Triggers for automatic updated_at
|
||||
CREATE TRIGGER update_files_updated_at BEFORE UPDATE ON files_bak
|
||||
CREATE TRIGGER update_files_updated_at BEFORE UPDATE ON files
|
||||
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
|
||||
|
||||
-- View for operational dashboard
|
||||
@@ -122,7 +132,7 @@ SELECT
|
||||
AVG(o.duration_ms) as avg_duration_ms,
|
||||
MIN(o.started_at) as earliest_operation,
|
||||
MAX(o.completed_at) as latest_operation
|
||||
FROM operations_bak o
|
||||
FROM operations o
|
||||
WHERE o.started_at > CURRENT_TIMESTAMP - INTERVAL '24 hours'
|
||||
GROUP BY o.status;
|
||||
|
||||
@@ -135,7 +145,7 @@ SELECT
|
||||
AVG(size) as avg_file_size,
|
||||
MIN(created_time) as oldest_file,
|
||||
MAX(modified_time) as newest_file
|
||||
FROM files_bak
|
||||
FROM files
|
||||
GROUP BY disk_label;
|
||||
|
||||
-- Insert default configuration
|
||||
|
||||
Reference in New Issue
Block a user