EXEC sp_configure 'CONTAINED DATABASE AUTHENTICATION', 1
GO
RECONFIGURE
GO

USE [master]
GO

CREATE DATABASE demo;
GO

ALTER DATABASE [demo] SET CONTAINMENT = PARTIAL
GO

USE [demo];
GO

CREATE USER demo_user WITH PASSWORD = 'pas$w0rd';
GO

-- a single table is used for all events in the cqrs system
CREATE TABLE events
(
    aggregate_type VARCHAR(256)                         NOT NULL,
    aggregate_id   VARCHAR(256)                         NOT NULL,
    sequence       bigint CHECK (sequence >= 0) NOT NULL,
    payload        TEXT         NOT NULL,
    metadata       TEXT         NOT NULL,
    PRIMARY KEY (aggregate_type, aggregate_id, sequence)
);

GO

GRANT SELECT ON OBJECT::dbo.events TO demo_user;
GRANT INSERT ON OBJECT::dbo.events TO demo_user;
GRANT UPDATE ON OBJECT::dbo.events TO demo_user;
GRANT DELETE ON OBJECT::dbo.events TO demo_user;
