--- system-status

CREATE TABLE system_statuses
(
    id         BIGINT PRIMARY KEY IDENTITY(1, 1),
    user_id    BIGINT NOT NULL REFERENCES users (id),
    [type]     NVARCHAR(255) NOT NULL,
    [active]   BIT NOT NULL,
    started_at DATETIMEOFFSET DEFAULT SYSDATETIMEOFFSET(),
    ended_at   DATETIMEOFFSET
);

CREATE UNIQUE INDEX system_statuses_type_active_uniq
    ON system_statuses (user_id, [type], [active]) WHERE [active] = 1;

CREATE INDEX system_statuses_started_ix ON system_statuses (started_at);
CREATE INDEX system_statuses_user_started_in ON system_statuses (user_id, started_at);