dotnet-cqrs/Svrnty.CQRS.Events.PostgreSQL/Migrations/007_ProjectionCheckpoints.sql

50 lines
2.4 KiB
SQL

-- =====================================================
-- Migration 007: Projection Checkpoints
-- =====================================================
-- Creates the projection_checkpoints table for tracking
-- event sourcing projection progress and state.
--
-- Features:
-- - Composite primary key (projection_name, stream_name)
-- - Tracks last processed offset and events processed
-- - Stores error information for failed projections
-- - Indexes for efficient querying
-- =====================================================
-- Create projection_checkpoints table
CREATE TABLE IF NOT EXISTS projection_checkpoints (
projection_name TEXT NOT NULL,
stream_name TEXT NOT NULL,
last_processed_offset BIGINT NOT NULL DEFAULT -1,
last_updated TIMESTAMPTZ NOT NULL DEFAULT NOW(),
events_processed BIGINT NOT NULL DEFAULT 0,
last_error TEXT NULL,
last_error_at TIMESTAMPTZ NULL,
CONSTRAINT pk_projection_checkpoints PRIMARY KEY (projection_name, stream_name)
);
-- Create index on last_updated for querying recent checkpoints
CREATE INDEX IF NOT EXISTS ix_projection_checkpoints_last_updated
ON projection_checkpoints(last_updated DESC);
-- Create index on stream_name for querying by stream
CREATE INDEX IF NOT EXISTS ix_projection_checkpoints_stream_name
ON projection_checkpoints(stream_name);
-- Create index on projection_name for efficient lookups
CREATE INDEX IF NOT EXISTS ix_projection_checkpoints_projection_name
ON projection_checkpoints(projection_name);
-- Add comment to table
COMMENT ON TABLE projection_checkpoints IS 'Tracks event sourcing projection progress and state';
-- Add comments to columns
COMMENT ON COLUMN projection_checkpoints.projection_name IS 'Unique name of the projection';
COMMENT ON COLUMN projection_checkpoints.stream_name IS 'Name of the event stream being processed';
COMMENT ON COLUMN projection_checkpoints.last_processed_offset IS 'Offset of the last successfully processed event';
COMMENT ON COLUMN projection_checkpoints.last_updated IS 'Timestamp when checkpoint was last updated';
COMMENT ON COLUMN projection_checkpoints.events_processed IS 'Total number of events processed by this projection';
COMMENT ON COLUMN projection_checkpoints.last_error IS 'Error message from the last failed event processing attempt';
COMMENT ON COLUMN projection_checkpoints.last_error_at IS 'Timestamp when the last error occurred';