forked from daniel-miller/timeline
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate Database.sql
More file actions
173 lines (130 loc) · 4.51 KB
/
Create Database.sql
File metadata and controls
173 lines (130 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use master;
go
-- Drop the table and its backup history.
exec msdb.dbo.sp_delete_database_backuphistory @database_name = N'Timeline'
go
alter database [Timeline] set single_user with rollback immediate
go
drop database if exists Timeline;
go
-- Create a new database named "Timeline".
create database Timeline;
go
use Timeline;
go
-- Store commands, events, aggregates, and snapshots in a schema named "logs".
create schema logs;
go
create table logs.[Command]
(
AggregateIdentifier uniqueidentifier not null
, ExpectedVersion int null
, IdentityTenant uniqueidentifier not null
, IdentityUser uniqueidentifier not null
, CommandClass varchar(200) not null
, CommandType varchar(100) not null
, CommandData nvarchar(max) not null
, CommandIdentifier uniqueidentifier not null primary key
, SendStatus varchar(20) null
, SendError varchar(max) null
, SendScheduled datetimeoffset(7) null
, SendStarted datetimeoffset(7) null
, SendCompleted datetimeoffset(7) null
, SendCancelled datetimeoffset(7) null
);
create table logs.[Event]
(
AggregateIdentifier uniqueidentifier not null
, AggregateVersion int not null
, IdentityTenant uniqueidentifier not null
, IdentityUser uniqueidentifier not null
, EventTime datetimeoffset(7) not null
, EventClass varchar(200) not null
, EventType varchar(100) not null
, [EventData] nvarchar(max) not null
, constraint PK_Event
primary key clustered (
AggregateIdentifier asc
, AggregateVersion asc
)
);
create table logs.[Aggregate]
(
AggregateIdentifier uniqueidentifier not null
, AggregateType varchar(100) not null
, AggregateClass varchar(200) not null
, AggregateExpires datetimeoffset(7) null
, TenantIdentifier uniqueidentifier not null
, constraint PK_Aggregate
primary key clustered (AggregateIdentifier asc)
);
create table logs.[Snapshot]
(
AggregateIdentifier uniqueidentifier not null
, AggregateVersion int not null
, AggregateState nvarchar(max) not null
, constraint PK_Snapshot
primary key clustered (AggregateIdentifier)
);
go
-- Store projections in a schema named "queries".
create schema queries;
go
create table queries.UserSummary
(
UserIdentifier uniqueidentifier not null primary key,
LoginName varchar(100) not null,
LoginPassword varchar(100) not null,
UserRegistrationStatus varchar(10) not null
)
go
create table queries.PersonSummary
(
TenantIdentifier uniqueidentifier not null,
UserIdentifier uniqueidentifier null,
PersonIdentifier uniqueidentifier not null primary key,
PersonName varchar(100) null,
PersonRegistered datetimeoffset not null,
OpenAccountCount int not null,
TotalAccountBalance money null
)
go
create table queries.AccountSummary
(
TenantIdentifier uniqueidentifier not null,
AccountIdentifier uniqueidentifier not null primary key,
AccountCode varchar(100) null,
AccountStatus varchar(10) null,
AccountBalance money not null,
OwnerIdentifier uniqueidentifier not null,
OwnerName varchar(100) null
)
go
create table queries.TransferSummary
(
TenantIdentifier uniqueidentifier not null,
TransferIdentifier uniqueidentifier not null primary key,
TransferAmount money not null,
TransferStatus varchar(10) null,
TransferActivity varchar(20) null,
FromAccountIdentifier uniqueidentifier not null,
FromAccountOwner varchar(100) null,
ToAccountIdentifier uniqueidentifier not null,
ToAccountOwner varchar(100) null
)
go
-- Create a stored procedure to look after denormalizing projection data. For example, an event might contain an
-- identifier for a person, and not contain the person's name. If we want the person's name in a projection query
-- then we have to chase that down after the data is inserted or updated in the query table.
create proc queries.Denormalize
as
begin
-- Ensure the name of the account owner is current.
update queries.AccountSummary set OwnerName = PersonName
from queries.PersonSummary where OwnerIdentifier = PersonIdentifier;
update queries.TransferSummary set FromAccountOwner = PersonName
from queries.PersonSummary where FromAccountIdentifier = PersonIdentifier;
update queries.TransferSummary set ToAccountOwner = PersonName
from queries.PersonSummary where ToAccountIdentifier = PersonIdentifier;
end;
go