-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathBaseSchema.sql
279 lines (246 loc) · 7.31 KB
/
BaseSchema.sql
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
-- NOTE: This script is to configure database and apply base schema required for the Schema migration tool
-- Style guide: please see: https://github.com/ktaranov/sqlserver-kit/blob/master/SQL%20Server%20Name%20Convention%20and%20T-SQL%20Programming%20Style.md
/*************************************************************
Configure database
**************************************************************/
-- Enable RCSI
-- ROLLBACK IMMEDIATE will cut off live connections and roll back any open transactions immediately
IF ((SELECT is_read_committed_snapshot_on FROM sys.databases WHERE database_id = DB_ID()) = 0) BEGIN
ALTER DATABASE CURRENT SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE
END
-- Avoid blocking queries when statistics need to be rebuilt
IF ((SELECT is_auto_update_stats_async_on FROM sys.databases WHERE database_id = DB_ID()) = 0) BEGIN
ALTER DATABASE CURRENT SET AUTO_UPDATE_STATISTICS_ASYNC ON
END
-- Use ANSI behavior for null values
IF ((SELECT is_ansi_nulls_on FROM sys.databases WHERE database_id = DB_ID()) = 0) BEGIN
ALTER DATABASE CURRENT SET ANSI_NULLS ON
END
GO
/*************************************************************
Schema Version
**************************************************************/
IF NOT EXISTS (SELECT * FROM sys.objects WHERE name = 'SchemaVersion' and type = 'U')
CREATE TABLE dbo.SchemaVersion
(
Version int PRIMARY KEY,
Status varchar(10)
)
GO
--
-- STORED PROCEDURE
-- SelectCurrentSchemaVersion
--
-- DESCRIPTION
-- Selects the current completed schema version
--
-- RETURNS
-- The current version as a result set
--
IF EXISTS (SELECT * FROM sys.objects WHERE NAME='SelectCurrentSchemaVersion' and type = 'P')
DROP PROCEDURE SelectCurrentSchemaVersion
GO
CREATE PROCEDURE dbo.SelectCurrentSchemaVersion
AS
BEGIN
SET NOCOUNT ON
SELECT MAX(Version)
FROM SchemaVersion
WHERE Status = 'complete' OR Status = 'completed'
END
GO
--
-- STORED PROCEDURE
-- UpsertSchemaVersion
--
-- DESCRIPTION
-- Creates or updates a new schema version entry
--
-- PARAMETERS
-- @version
-- * The version number
-- @status
-- * The status of the version
--
IF EXISTS (SELECT * FROM sys.objects WHERE NAME='UpsertSchemaVersion' and type = 'P')
DROP PROCEDURE UpsertSchemaVersion
GO
CREATE PROCEDURE dbo.UpsertSchemaVersion
@version int,
@status varchar(10)
AS
SET NOCOUNT ON
IF EXISTS(SELECT *
FROM dbo.SchemaVersion
WHERE Version = @version)
BEGIN
UPDATE dbo.SchemaVersion
SET Status = @status
WHERE Version = @version
END
ELSE
BEGIN
INSERT INTO dbo.SchemaVersion
(Version, Status)
VALUES
(@version, @status)
END
GO
/*************************************************************
Instance Schema
**************************************************************/
IF NOT EXISTS (SELECT * FROM sys.objects WHERE name = 'InstanceSchema' and type = 'U')
BEGIN
CREATE TABLE dbo.InstanceSchema
(
Name varchar(64) COLLATE Latin1_General_100_CS_AS NOT NULL,
CurrentVersion int NOT NULL,
MaxVersion int NOT NULL,
MinVersion int NOT NULL,
Timeout datetime2(0) NOT NULL
)
CREATE UNIQUE CLUSTERED INDEX IXC_InstanceSchema ON dbo.InstanceSchema
(
Name
)
CREATE NONCLUSTERED INDEX IX_InstanceSchema_Timeout ON dbo.InstanceSchema
(
Timeout
)
END
GO
--
-- STORED PROCEDURE
-- Gets schema information given its instance name.
--
-- DESCRIPTION
-- Retrieves the instance schema record from the InstanceSchema table that has the matching name.
--
-- PARAMETERS
-- @name
-- * The unique name for a particular instance
--
-- RETURN VALUE
-- The matching record.
--
IF EXISTS (SELECT * FROM sys.objects WHERE NAME='GetInstanceSchemaByName' and type = 'P')
DROP PROCEDURE GetInstanceSchemaByName
GO
CREATE PROCEDURE dbo.GetInstanceSchemaByName
@name varchar(64)
AS
SET NOCOUNT ON
SELECT CurrentVersion, MaxVersion, MinVersion, Timeout
FROM dbo.InstanceSchema
WHERE Name = @name
GO
--
-- STORED PROCEDURE
-- Update an instance schema.
--
-- DESCRIPTION
-- Modifies an existing record in the InstanceSchema table.
--
-- PARAMETERS
-- @name
-- * The unique name for a particular instance
-- @maxVersion
-- * The maximum supported schema version for the given instance
-- @minVersion
-- * The minimum supported schema version for the given instance
-- @addMinutesOnTimeout
-- * The minutes to add
--
IF EXISTS (SELECT * FROM sys.objects WHERE NAME='UpsertInstanceSchema' and type = 'P')
DROP PROCEDURE UpsertInstanceSchema
GO
CREATE PROCEDURE dbo.UpsertInstanceSchema
@name varchar(64),
@maxVersion int,
@minVersion int,
@addMinutesOnTimeout int
AS
SET NOCOUNT ON
DECLARE @timeout datetime2(0) = DATEADD(minute, @addMinutesOnTimeout, SYSUTCDATETIME())
DECLARE @currentVersion int = (SELECT COALESCE(MAX(Version), 0)
FROM dbo.SchemaVersion
WHERE Status = 'completed' OR Status = 'complete' AND Version <= @maxVersion)
IF EXISTS(SELECT *
FROM dbo.InstanceSchema
WHERE Name = @name)
BEGIN
UPDATE dbo.InstanceSchema
SET CurrentVersion = @currentVersion, MaxVersion = @maxVersion, Timeout = @timeout
WHERE Name = @name
SELECT @currentVersion
END
ELSE
BEGIN
INSERT INTO dbo.InstanceSchema
(Name, CurrentVersion, MaxVersion, MinVersion, Timeout)
VALUES
(@name, @currentVersion, @maxVersion, @minVersion, @timeout)
SELECT @currentVersion
END
GO
--
-- STORED PROCEDURE
-- Delete instance schema information.
--
-- DESCRIPTION
-- Delete all the expired records in the InstanceSchema table.
--
IF EXISTS (SELECT * FROM sys.objects WHERE NAME='DeleteInstanceSchema' and type = 'P')
DROP PROCEDURE DeleteInstanceSchema
GO
CREATE PROCEDURE dbo.DeleteInstanceSchema
AS
SET NOCOUNT ON
DELETE FROM dbo.InstanceSchema
WHERE Timeout < SYSUTCDATETIME()
GO
--
-- STORED PROCEDURE
-- SelectCompatibleSchemaVersions
--
-- DESCRIPTION
-- Selects the compatible schema versions
--
-- RETURNS
-- The maximum and minimum compatible versions
--
IF EXISTS (SELECT * FROM sys.objects WHERE NAME='SelectCompatibleSchemaVersions' and type = 'P')
DROP PROCEDURE SelectCompatibleSchemaVersions
GO
CREATE PROCEDURE dbo.SelectCompatibleSchemaVersions
AS
BEGIN
SET NOCOUNT ON
SELECT MAX(MinVersion), MIN(MaxVersion)
FROM dbo.InstanceSchema
WHERE Timeout > SYSUTCDATETIME()
END
GO
--
-- STORED PROCEDURE
-- SelectCurrentVersionsInformation
--
-- DESCRIPTION
-- Selects the current schema versions information
--
-- RETURNS
-- The current versions, status and server names using that version
--
IF EXISTS (SELECT * FROM sys.objects WHERE NAME='SelectCurrentVersionsInformation' and type = 'P')
DROP PROCEDURE SelectCurrentVersionsInformation
GO
CREATE PROCEDURE dbo.SelectCurrentVersionsInformation
AS
BEGIN
SET NOCOUNT ON
SELECT SV.Version, SV.Status, STRING_AGG(SCH.NAME, ',')
FROM dbo.SchemaVersion AS SV LEFT OUTER JOIN dbo.InstanceSchema AS SCH
ON SV.Version = SCH.CurrentVersion
GROUP BY Version, Status
END
GO