Skip to content

Instantly share code, notes, and snippets.

@akatakritos
Last active July 15, 2024 22:01
Show Gist options
  • Save akatakritos/0608eb16094f42b6ed0ea9b8563d370a to your computer and use it in GitHub Desktop.
Save akatakritos/0608eb16094f42b6ed0ea9b8563d370a to your computer and use it in GitHub Desktop.
SQL Server schema migration script for MassTransit's transactional outbox tables
-- https://github.com/MassTransit/MassTransit/discussions/4847
CREATE TABLE [InboxState] (
[Id] bigint NOT NULL IDENTITY,
[MessageId] uniqueidentifier NOT NULL,
[ConsumerId] uniqueidentifier NOT NULL,
[LockId] uniqueidentifier NOT NULL,
[RowVersion] rowversion NULL,
[Received] datetime2 NOT NULL,
[ReceiveCount] int NOT NULL,
[ExpirationTime] datetime2 NULL,
[Consumed] datetime2 NULL,
[Delivered] datetime2 NULL,
[LastSequenceNumber] bigint NULL,
CONSTRAINT [PK_InboxState] PRIMARY KEY ([Id]),
CONSTRAINT [AK_InboxState_MessageId_ConsumerId] UNIQUE ([MessageId], [ConsumerId])
);
CREATE TABLE [OutboxMessage] (
[SequenceNumber] bigint NOT NULL IDENTITY,
[EnqueueTime] datetime2 NULL,
[SentTime] datetime2 NOT NULL,
[Headers] nvarchar(max) NULL,
[Properties] nvarchar(max) NULL,
[InboxMessageId] uniqueidentifier NULL,
[InboxConsumerId] uniqueidentifier NULL,
[OutboxId] uniqueidentifier NULL,
[MessageId] uniqueidentifier NOT NULL,
[ContentType] nvarchar(256) NOT NULL,
[MessageType] nvarchar(max) NOT NULL,
[Body] nvarchar(max) NOT NULL,
[ConversationId] uniqueidentifier NULL,
[CorrelationId] uniqueidentifier NULL,
[InitiatorId] uniqueidentifier NULL,
[RequestId] uniqueidentifier NULL,
[SourceAddress] nvarchar(256) NULL,
[DestinationAddress] nvarchar(256) NULL,
[ResponseAddress] nvarchar(256) NULL,
[FaultAddress] nvarchar(256) NULL,
[ExpirationTime] datetime2 NULL,
CONSTRAINT [PK_OutboxMessage] PRIMARY KEY ([SequenceNumber])
);
CREATE TABLE [OutboxState] (
[OutboxId] uniqueidentifier NOT NULL,
[LockId] uniqueidentifier NOT NULL,
[RowVersion] rowversion NULL,
[Created] datetime2 NOT NULL,
[Delivered] datetime2 NULL,
[LastSequenceNumber] bigint NULL,
CONSTRAINT [PK_OutboxState] PRIMARY KEY ([OutboxId])
);
CREATE INDEX [IX_InboxState_Delivered] ON [InboxState] ([Delivered]);
CREATE INDEX [IX_OutboxMessage_EnqueueTime] ON [OutboxMessage] ([EnqueueTime]);
CREATE INDEX [IX_OutboxMessage_ExpirationTime] ON [OutboxMessage] ([ExpirationTime]);
CREATE UNIQUE INDEX [IX_OutboxMessage_InboxMessageId_InboxConsumerId_SequenceNumber] ON [OutboxMessage] ([InboxMessageId], [InboxConsumerId], [SequenceNumber]) WHERE [InboxMessageId] IS NOT NULL AND [InboxConsumerId] IS NOT NULL;
CREATE UNIQUE INDEX [IX_OutboxMessage_OutboxId_SequenceNumber] ON [OutboxMessage] ([OutboxId], [SequenceNumber]) WHERE [OutboxId] IS NOT NULL;
CREATE INDEX [IX_OutboxState_Created] ON [OutboxState] ([Created]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment