BulkEncryptionDatabase.publish.sql 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. Deployment script for bulkencryption
  3. This code was generated by a tool.
  4. Changes to this file may cause incorrect behavior and will be lost if
  5. the code is regenerated.
  6. */
  7. GO
  8. SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;
  9. SET NUMERIC_ROUNDABORT OFF;
  10. GO
  11. :setvar DatabaseName "bulkencryption"
  12. :setvar DefaultFilePrefix "bulkencryption"
  13. :setvar DefaultDataPath ""
  14. :setvar DefaultLogPath ""
  15. GO
  16. :on error exit
  17. GO
  18. /*
  19. Detect SQLCMD mode and disable script execution if SQLCMD mode is not supported.
  20. To re-enable the script after enabling SQLCMD mode, execute the following:
  21. SET NOEXEC OFF;
  22. */
  23. :setvar __IsSqlCmdEnabled "True"
  24. GO
  25. IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True'
  26. BEGIN
  27. PRINT N'SQLCMD mode must be enabled to successfully execute this script.';
  28. SET NOEXEC ON;
  29. END
  30. GO
  31. PRINT N'Dropping unnamed constraint on [dbo].[Files]...';
  32. GO
  33. ALTER TABLE [dbo].[Files] DROP CONSTRAINT [DF__Files__Status__72C60C4A];
  34. GO
  35. PRINT N'Dropping unnamed constraint on [dbo].[Files]...';
  36. GO
  37. ALTER TABLE [dbo].[Files] DROP CONSTRAINT [FK__Files__OriginalF__73BA3083];
  38. GO
  39. PRINT N'Dropping unnamed constraint on [dbo].[Files]...';
  40. GO
  41. ALTER TABLE [dbo].[Files] DROP CONSTRAINT [FK__Files__LabelId__74AE54BC];
  42. GO
  43. /*
  44. The column [dbo].[Files].[FileServer] is being dropped, data loss could occur.
  45. */
  46. GO
  47. PRINT N'Starting rebuilding table [dbo].[Files]...';
  48. GO
  49. BEGIN TRANSACTION;
  50. SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
  51. SET XACT_ABORT ON;
  52. CREATE TABLE [dbo].[tmp_ms_xx_Files] (
  53. [Id] INT IDENTITY (1, 1) NOT NULL,
  54. [FilePath] NVARCHAR (1023) NOT NULL,
  55. [FileServerId] INT NULL,
  56. [Location] NVARCHAR (1023) NULL,
  57. [Status] INT DEFAULT 1 NOT NULL,
  58. [StartedWhen] DATETIME NULL,
  59. [CompletedWhen] DATETIME NULL,
  60. [Exception] NVARCHAR (MAX) NULL,
  61. [RetryCount] INT NULL,
  62. [InstanceId] INT NULL,
  63. [LabelId] INT NOT NULL,
  64. [NewFileName] NVARCHAR (1023) NULL,
  65. [NewFileSize] BIGINT NULL,
  66. [OriginalFileSize] BIGINT NULL,
  67. [PotentialBJLabel] BIT NULL,
  68. PRIMARY KEY CLUSTERED ([Id] ASC)
  69. );
  70. IF EXISTS (SELECT TOP 1 1
  71. FROM [dbo].[Files])
  72. BEGIN
  73. SET IDENTITY_INSERT [dbo].[tmp_ms_xx_Files] ON;
  74. INSERT INTO [dbo].[tmp_ms_xx_Files] ([Id], [FilePath], [Location], [Status], [StartedWhen], [CompletedWhen], [Exception], [RetryCount], [InstanceId], [LabelId], [NewFileName], [NewFileSize], [OriginalFileSize])
  75. SELECT [Id],
  76. [FilePath],
  77. [Location],
  78. [Status],
  79. [StartedWhen],
  80. [CompletedWhen],
  81. [Exception],
  82. [RetryCount],
  83. [InstanceId],
  84. [LabelId],
  85. [NewFileName],
  86. [NewFileSize],
  87. [OriginalFileSize]
  88. FROM [dbo].[Files]
  89. ORDER BY [Id] ASC;
  90. SET IDENTITY_INSERT [dbo].[tmp_ms_xx_Files] OFF;
  91. END
  92. DROP TABLE [dbo].[Files];
  93. EXECUTE sp_rename N'[dbo].[tmp_ms_xx_Files]', N'Files';
  94. COMMIT TRANSACTION;
  95. SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
  96. GO
  97. PRINT N'Altering [dbo].[ServersFileServers]...';
  98. GO
  99. ALTER TABLE [dbo].[ServersFileServers] DROP COLUMN [FileServer];
  100. GO
  101. ALTER TABLE [dbo].[ServersFileServers]
  102. ADD [FileServerId] INT NOT NULL;
  103. GO
  104. PRINT N'Starting rebuilding table [dbo].[GlobalConfig]...';
  105. GO
  106. BEGIN TRANSACTION;
  107. SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
  108. SET XACT_ABORT ON;
  109. CREATE TABLE [dbo].[tmp_ms_xx_GlobalConfig] (
  110. [Id] INT IDENTITY (1, 1) NOT NULL,
  111. [Key] NVARCHAR (1023) NOT NULL,
  112. [Value] NVARCHAR (MAX) NOT NULL,
  113. PRIMARY KEY CLUSTERED ([Id] ASC)
  114. );
  115. IF EXISTS (SELECT TOP 1 1
  116. FROM [dbo].[GlobalConfig])
  117. BEGIN
  118. SET IDENTITY_INSERT [dbo].[tmp_ms_xx_GlobalConfig] ON;
  119. INSERT INTO [dbo].[tmp_ms_xx_GlobalConfig] ([Id], [Key], [Value])
  120. SELECT [Id],
  121. [Key],
  122. [Value]
  123. FROM [dbo].[GlobalConfig]
  124. ORDER BY [Id] ASC;
  125. SET IDENTITY_INSERT [dbo].[tmp_ms_xx_GlobalConfig] OFF;
  126. END
  127. DROP TABLE [dbo].[GlobalConfig];
  128. EXECUTE sp_rename N'[dbo].[tmp_ms_xx_GlobalConfig]', N'GlobalConfig';
  129. COMMIT TRANSACTION;
  130. SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
  131. GO
  132. PRINT N'Creating [dbo].[FileServers]...';
  133. GO
  134. CREATE TABLE [dbo].[FileServers] (
  135. [Id] INT IDENTITY (1, 1) NOT NULL,
  136. [FileServer] NVARCHAR (1023) NOT NULL,
  137. [BJPCStartDate] DATETIME NULL,
  138. [BJPCEndDate] DATETIME NULL,
  139. PRIMARY KEY CLUSTERED ([Id] ASC)
  140. );
  141. GO
  142. PRINT N'Creating unnamed constraint on [dbo].[Servers]...';
  143. GO
  144. ALTER TABLE [dbo].[Servers]
  145. ADD DEFAULT 1 FOR [IsActive];
  146. GO
  147. PRINT N'Creating unnamed constraint on [dbo].[Files]...';
  148. GO
  149. ALTER TABLE [dbo].[Files] WITH NOCHECK
  150. ADD FOREIGN KEY ([InstanceId]) REFERENCES [dbo].[Instances] ([Id]);
  151. GO
  152. PRINT N'Creating unnamed constraint on [dbo].[Files]...';
  153. GO
  154. ALTER TABLE [dbo].[Files] WITH NOCHECK
  155. ADD FOREIGN KEY ([LabelId]) REFERENCES [dbo].[Labels] ([Id]);
  156. GO
  157. PRINT N'Creating unnamed constraint on [dbo].[Files]...';
  158. GO
  159. ALTER TABLE [dbo].[Files] WITH NOCHECK
  160. ADD FOREIGN KEY ([FileServerId]) REFERENCES [dbo].[FileServers] ([Id]);
  161. GO
  162. PRINT N'Creating unnamed constraint on [dbo].[ServersFileServers]...';
  163. GO
  164. ALTER TABLE [dbo].[ServersFileServers] WITH NOCHECK
  165. ADD FOREIGN KEY ([FileServerId]) REFERENCES [dbo].[FileServers] ([Id]);
  166. GO
  167. PRINT N'Altering [dbo].[SelectNextFileToProcess]...';
  168. GO
  169. ALTER PROCEDURE [dbo].[SelectNextFileToProcess]
  170. @serverId INT,
  171. @scriptInstanceId INT,
  172. @maxRetries INT,
  173. @mode NVARCHAR(50)
  174. AS
  175. BEGIN TRANSACTION
  176. -- Get the next row to process
  177. DECLARE @rowId INT = 0
  178. IF @mode = 'encrypt'
  179. BEGIN
  180. SELECT @rowId = Id
  181. FROM Files f
  182. WITH (TABLOCKX, HOLDLOCK)
  183. WHERE
  184. (f.[Status] = 1 OR f.[Status] = 4)
  185. AND f.[FileServerId] IN (SELECT [FileServerId] FROM [ServersFileServers] sfs WHERE sfs.ServerId = @serverId)
  186. AND (f.[RetryCount] IS NULL OR f.[RetryCount] < @maxRetries)
  187. ORDER BY f.[RetryCount] DESC, f.[Id] ASC
  188. END
  189. IF @mode = 'decrypt'
  190. BEGIN
  191. SELECT @rowId = Id
  192. FROM Files f
  193. WITH (TABLOCKX, HOLDLOCK)
  194. WHERE
  195. (f.[Status] = 3 OR f.[Status] = 5)
  196. AND f.[FileServerId] IN (SELECT [FileServerId] FROM [ServersFileServers] sfs WHERE sfs.ServerId = @serverId)
  197. AND (f.[RetryCount] IS NULL OR f.[RetryCount] < @maxRetries)
  198. ORDER BY f.[RetryCount] DESC, f.[Id] ASC
  199. END
  200. IF @rowId IS NOT NULL AND @rowId > 0
  201. BEGIN
  202. -- 'Lock' the row for processing by this script
  203. UPDATE Files
  204. SET [Status] = 2, [StartedWhen] = GETUTCDATE(), [InstanceId] = @scriptInstanceId
  205. WHERE [Id] = @rowId
  206. -- Get the row (with the label info + server status) to send back to the script
  207. SELECT f.*, l.[LabelGuid], l.[LabelName], s.IsActive, fs.FileServer, fs.BJPCStartDate, fs.BJPCEndDate
  208. FROM Files f
  209. LEFT JOIN Labels l on f.[LabelId] = l.Id
  210. LEFT JOIN Instances i on f.[InstanceId] = i.Id
  211. LEFT JOIN [Servers] s on i.ServerId = s.Id
  212. LEFT JOIN [FileServers] fs on fs.Id = f.FileServerId
  213. WHERE f.[Id] = @rowId
  214. END
  215. COMMIT
  216. GO
  217. PRINT N'Altering [dbo].[UpdateFileRow]...';
  218. GO
  219. ALTER PROCEDURE [dbo].[UpdateFileRow]
  220. @rowId INT,
  221. @exception NVARCHAR(MAX) = NULL,
  222. @retryCount INT = NULL,
  223. @status INT,
  224. @newfilename NVARCHAR(1024) = NULL,
  225. @newfilesize BIGINT = NULL,
  226. @originalfilesize BIGINT = NULL,
  227. @potentialBJLabel BIT = NULL
  228. AS
  229. -- Is this an error or are we good?
  230. -- DECLARE @status INT = 3
  231. -- IF @exception IS NOT NULL AND @exception != '' SET @status = 4
  232. -- Update the row
  233. UPDATE Files
  234. SET [Status] = @status, [CompletedWhen] = GETUTCDATE(), [Exception] = @exception, [RetryCount] = @retryCount, [NewFileName] = @newfilename, [NewFileSize] = @newfilesize, [OriginalFileSize] = @originalfilesize, [PotentialBJLabel] = @potentialBJLabel
  235. WHERE [Id] = @rowId
  236. -- Get the row to send back to the script
  237. SELECT * FROM Files f WHERE f.[Id] = @rowId
  238. GO
  239. /*
  240. Post-Deployment Script Template
  241. --------------------------------------------------------------------------------------
  242. This file contains SQL statements that will be appended to the build script.
  243. Use SQLCMD syntax to include a file in the post-deployment script.
  244. Example: :r .\myfile.sql
  245. Use SQLCMD syntax to reference a variable in the post-deployment script.
  246. Example: :setvar TableName MyTable
  247. SELECT * FROM [$(TableName)]
  248. --------------------------------------------------------------------------------------
  249. */
  250. /* STATUS VALUES
  251. 1 - Awaiting Processing
  252. 2 - In Process
  253. 3 - Complete
  254. 4 - Error
  255. 5 - Error rolling back (removing label)
  256. */
  257. -- GLOBAL CONFIG --
  258. SET IDENTITY_INSERT [GlobalConfig] ON
  259. INSERT INTO [GlobalConfig]
  260. (Id, [Key], [Value])
  261. VALUES
  262. (1, 'MaxRetries', '5'),
  263. (2, 'AADWebAppId', 'ABC-123'),
  264. (3, 'AADWebAppKey', 'XXX'),
  265. (4, 'AADNativeAppId', '123'),
  266. (5, 'AADToken', 'Token-1')
  267. SET IDENTITY_INSERT [GlobalConfig] OFF
  268. -- LABELS --
  269. SET IDENTITY_INSERT [Labels] ON
  270. INSERT INTO [Labels]
  271. (Id, LabelName, LabelGuid)
  272. VALUES
  273. (1, 'Public', '1234'),
  274. (2, 'Restricted External', '1234'),
  275. (3, 'Restricted Internal', '1234'),
  276. (4, 'Confidential', '1234'),
  277. (5, 'Secret', 'd9f23ae3-a239-45ea-bf23-f515f824c57b')
  278. SET IDENTITY_INSERT [Labels] OFF
  279. -- SERVERS --
  280. SET IDENTITY_INSERT [Servers] ON
  281. INSERT INTO [Servers]
  282. (Id, ServerName, StartTime, EndTime, NumberInstances, ServerComplete)
  283. VALUES
  284. (1, 'DAVROS', '09:00:00', '17:00:00', 1, NULL),
  285. (2, 'MININT-RDS9B7O', '09:00:00', '17:00:00', 2, NULL)
  286. SET IDENTITY_INSERT [Servers] OFF
  287. -- SERVERS - FILE SERVERS --
  288. INSERT INTO [ServersFileServers]
  289. (ServerId, FileServer)
  290. VALUES
  291. (1, 'X'),
  292. (1, 'Y'),
  293. (1, 'Z'),
  294. (2, 'A'),
  295. (2, 'B')
  296. -- FILES --
  297. INSERT INTO [Files]
  298. ([Status], [FileServer], [LabelId], [FilePath])
  299. VALUES
  300. (1, 'X', 5, 'X:\docs\ValidDoc1.docx'),
  301. (1, 'X', 5, 'X:\docs\ValidDoc2.docx'),
  302. (1, 'X', 5, 'X:\docs\ValidDoc3.docx'),
  303. (1, 'X', 5, 'X:\docs\ValidDoc4.docx'),
  304. (1, 'X', 5, 'X:\docs\ValidDoc5.docx'),
  305. (1, 'X', 5, 'X:\docs\ValidSpread1.xlsx'),
  306. (1, 'X', 5, 'X:\docs\ValidSpread2.xlsx'),
  307. (1, 'Y', 5, 'X:\docs\ValidSpread3.xlsx'),
  308. (1, 'Y', 5, 'X:\docs\ValidSpread4.xlsx'),
  309. (1, 'Y', 5, 'X:\docs\ValidSpread5.xlsx'),
  310. (1, 'Y', 5, 'X:\docs\OldDoc.doc'),
  311. (1, 'Y', 5, 'X:\docs\OldSpread.xls'),
  312. (1, 'Y', 5, 'X:\docs\CorruptedDoc.docx'),
  313. (1, 'Y', 5, 'X:\docs\CorruptedOldDoc.doc'),
  314. (1, 'X', 5, 'X:\docs\CorruptedSpread.xlsx'),
  315. (1, 'X', 5, 'X:\docs\CorruptedOldSpread.xls'),
  316. (1, 'X', 5, 'X:\docs\MissingDoc.docx'),
  317. (1, 'Z', 5, 'X:\docs\MisingSpread.docx'),
  318. (1, 'X', 5, 'X:\docs\AccessDeniedDoc.docx'),
  319. (1, 'X', 5, 'X:\docs\AccessDeniedSpread.xlsx'),
  320. (1, 'Z', 5, 'X:\docs\TextFile.txt'),
  321. (1, 'Z', 5, 'X:\docs\Image.jpg'),
  322. (1, 'A', 5, 'c:\test\1.docx'),
  323. (1, 'A', 5, 'c:\test\2.docx'),
  324. (1, 'A', 5, 'c:\test\3.docx'),
  325. (1, 'B', 5, 'c:\test\4.docx'),
  326. (1, 'B', 5, 'c:\test\5.docx'),
  327. (1, 'B', 5, 'c:\test\6.docx')
  328. -- STATUS CODES --
  329. INSERT INTO [StatusCodes] (Status) VALUES
  330. ('NotStarted'),('InProgress'),('SuccessfulEncrypt'),('EncryptError'),('FailedDecrypt'),('WillNotEncrypt'),('NotFound')
  331. GO
  332. GO