1 | USE [HeuristicLab.Hive-3.3]
|
---|
2 | /* this script is supposed to be executed after the plain DB is generated by the linq-to-sql schema */
|
---|
3 | /* adds default values */
|
---|
4 | /* creates delete and update cascades */
|
---|
5 | /* creates indices */
|
---|
6 | /* creates views */
|
---|
7 | /* creates triggers */
|
---|
8 |
|
---|
9 |
|
---|
10 | ALTER TABLE [dbo].[AssignedResources] DROP CONSTRAINT [Task_AssignedResource]
|
---|
11 | ALTER TABLE [dbo].[AssignedResources] WITH CHECK ADD CONSTRAINT [Task_AssignedResource] FOREIGN KEY([TaskId])
|
---|
12 | REFERENCES [dbo].[Task] ([TaskId])
|
---|
13 | ON UPDATE CASCADE
|
---|
14 | ON DELETE CASCADE
|
---|
15 | GO
|
---|
16 | ALTER TABLE [dbo].[AssignedResources] DROP CONSTRAINT [Resource_AssignedResource]
|
---|
17 | ALTER TABLE [dbo].[AssignedResources] WITH CHECK ADD CONSTRAINT [Resource_AssignedResource] FOREIGN KEY([ResourceId])
|
---|
18 | REFERENCES [dbo].[Resource] ([ResourceId])
|
---|
19 | ON UPDATE CASCADE
|
---|
20 | ON DELETE CASCADE
|
---|
21 | GO
|
---|
22 |
|
---|
23 | ALTER TABLE dbo.Task ALTER COLUMN TaskId ADD ROWGUIDCOL;
|
---|
24 | ALTER TABLE dbo.Task WITH NOCHECK ADD CONSTRAINT [DF_Task_TaskId] DEFAULT (NEWSEQUENTIALID()) FOR TaskId;
|
---|
25 | GO
|
---|
26 |
|
---|
27 | ALTER TABLE [dbo].[StateLog] DROP CONSTRAINT [Task_StateLog]
|
---|
28 | ALTER TABLE [dbo].[StateLog] WITH CHECK ADD CONSTRAINT [Task_StateLog] FOREIGN KEY([TaskId])
|
---|
29 | REFERENCES [dbo].[Task] ([TaskId])
|
---|
30 | ON UPDATE CASCADE
|
---|
31 | ON DELETE CASCADE
|
---|
32 | GO
|
---|
33 | ALTER TABLE [dbo].[StateLog] DROP CONSTRAINT [Resource_StateLog]
|
---|
34 | ALTER TABLE [dbo].[StateLog] WITH CHECK ADD CONSTRAINT [Resource_StateLog] FOREIGN KEY([SlaveId])
|
---|
35 | REFERENCES [dbo].[Resource] ([ResourceId])
|
---|
36 | ON UPDATE CASCADE
|
---|
37 | ON DELETE SET NULL
|
---|
38 | GO
|
---|
39 |
|
---|
40 | ALTER TABLE dbo.Plugin ALTER COLUMN PluginId ADD ROWGUIDCOL;
|
---|
41 | ALTER TABLE dbo.Plugin WITH NOCHECK ADD CONSTRAINT [DF_Plugin_PluginId] DEFAULT (NEWSEQUENTIALID()) FOR PluginId;
|
---|
42 |
|
---|
43 | ALTER TABLE dbo.PluginData WITH NOCHECK ADD CONSTRAINT [DF_PluginData_PluginDataId] DEFAULT (NEWSEQUENTIALID()) FOR PluginDataId;
|
---|
44 |
|
---|
45 | ALTER TABLE [dbo].[PluginData] DROP CONSTRAINT [Plugin_PluginData]
|
---|
46 | ALTER TABLE [dbo].[PluginData] WITH CHECK ADD CONSTRAINT [Plugin_PluginData] FOREIGN KEY([PluginId])
|
---|
47 | REFERENCES [dbo].[Plugin] ([PluginId])
|
---|
48 | ON UPDATE CASCADE
|
---|
49 | ON DELETE CASCADE
|
---|
50 | GO
|
---|
51 |
|
---|
52 | ALTER TABLE dbo.RequiredPlugins ALTER COLUMN RequiredPluginId ADD ROWGUIDCOL;
|
---|
53 | ALTER TABLE dbo.RequiredPlugins WITH NOCHECK ADD CONSTRAINT [DF_RequiredPlugins_RequiredPluginId] DEFAULT (NEWSEQUENTIALID()) FOR RequiredPluginId;
|
---|
54 |
|
---|
55 | ALTER TABLE [dbo].[RequiredPlugins] DROP CONSTRAINT [Task_RequiredPlugin]
|
---|
56 | ALTER TABLE [dbo].[RequiredPlugins] WITH CHECK ADD CONSTRAINT [Task_RequiredPlugin] FOREIGN KEY([TaskId])
|
---|
57 | REFERENCES [dbo].[Task] ([TaskId])
|
---|
58 | ON UPDATE CASCADE
|
---|
59 | ON DELETE CASCADE
|
---|
60 | GO
|
---|
61 |
|
---|
62 | ALTER TABLE [dbo].[RequiredPlugins] DROP CONSTRAINT [Plugin_RequiredPlugin]
|
---|
63 | ALTER TABLE [dbo].[RequiredPlugins] WITH CHECK ADD CONSTRAINT [Plugin_RequiredPlugin] FOREIGN KEY([PluginId])
|
---|
64 | REFERENCES [dbo].[Plugin] ([PluginId])
|
---|
65 | ON UPDATE CASCADE
|
---|
66 | ON DELETE CASCADE
|
---|
67 | GO
|
---|
68 |
|
---|
69 | ALTER TABLE dbo.Resource ALTER COLUMN ResourceId ADD ROWGUIDCOL;
|
---|
70 | ALTER TABLE dbo.Resource WITH NOCHECK ADD CONSTRAINT [DF_Resource_ResourceId] DEFAULT (NEWSEQUENTIALID()) FOR ResourceId;
|
---|
71 |
|
---|
72 | ALTER TABLE dbo.Downtime ALTER COLUMN DowntimeId ADD ROWGUIDCOL;
|
---|
73 | ALTER TABLE dbo.Downtime WITH NOCHECK ADD CONSTRAINT [DF_Downtime_DowntimeId] DEFAULT (NEWSEQUENTIALID()) FOR DowntimeId;
|
---|
74 |
|
---|
75 | ALTER TABLE dbo.Job ALTER COLUMN JobId ADD ROWGUIDCOL;
|
---|
76 | ALTER TABLE dbo.Job WITH NOCHECK ADD CONSTRAINT [DF_Job_JobId] DEFAULT (NEWSEQUENTIALID()) FOR JobId;
|
---|
77 |
|
---|
78 | ALTER TABLE dbo.StateLog ALTER COLUMN StateLogId ADD ROWGUIDCOL;
|
---|
79 | ALTER TABLE dbo.StateLog WITH NOCHECK ADD CONSTRAINT [DF_StateLog_StateLogId] DEFAULT (NEWSEQUENTIALID()) FOR StateLogId;
|
---|
80 |
|
---|
81 | ALTER TABLE [dbo].[JobPermission] DROP CONSTRAINT [Job_JobPermission]
|
---|
82 | ALTER TABLE [dbo].[JobPermission] WITH CHECK ADD CONSTRAINT [Job_JobPermission] FOREIGN KEY([JobId])
|
---|
83 | REFERENCES [dbo].[Job] ([JobId])
|
---|
84 | ON UPDATE CASCADE
|
---|
85 | ON DELETE CASCADE
|
---|
86 | GO
|
---|
87 |
|
---|
88 | ALTER TABLE [dbo].[ResourcePermission] DROP CONSTRAINT [Resource_ResourcePermission]
|
---|
89 | ALTER TABLE [dbo].[ResourcePermission] WITH CHECK ADD CONSTRAINT [Resource_ResourcePermission] FOREIGN KEY([ResourceId])
|
---|
90 | REFERENCES [dbo].[Resource] ([ResourceId])
|
---|
91 | ON UPDATE CASCADE
|
---|
92 | ON DELETE CASCADE
|
---|
93 | GO
|
---|
94 |
|
---|
95 | /* create indices */
|
---|
96 | CREATE INDEX Index_RequiredPlugins_TaskId ON RequiredPlugins(TaskId);
|
---|
97 | GO
|
---|
98 |
|
---|
99 | /* views */
|
---|
100 | -- =============================================
|
---|
101 | -- Author: cneumuel
|
---|
102 | -- Description: Returns the first StateLog entry for each job
|
---|
103 | -- =============================================
|
---|
104 | CREATE VIEW [dbo].[view_FirstState]
|
---|
105 | AS
|
---|
106 | SELECT sl.TaskId, sl.DateTime, sl.State
|
---|
107 | FROM dbo.StateLog AS sl INNER JOIN
|
---|
108 | (SELECT TaskId, MIN(DateTime) AS DateTime
|
---|
109 | FROM dbo.StateLog
|
---|
110 | GROUP BY TaskId) AS minDate ON sl.DateTime = minDate.DateTime AND sl.TaskId = minDate.TaskId
|
---|
111 |
|
---|
112 | GO
|
---|
113 |
|
---|
114 | -- =============================================
|
---|
115 | -- Author: cneumuel
|
---|
116 | -- Description: Returns the last StateLog entry for each job
|
---|
117 | -- =============================================
|
---|
118 | CREATE VIEW [dbo].[view_LastState]
|
---|
119 | AS
|
---|
120 | SELECT sl.TaskId, sl.DateTime, sl.State
|
---|
121 | FROM dbo.StateLog AS sl INNER JOIN
|
---|
122 | (SELECT TaskId, MAX(DateTime) AS DateTime
|
---|
123 | FROM dbo.StateLog
|
---|
124 | GROUP BY TaskId) AS minDate ON sl.DateTime = minDate.DateTime AND sl.TaskId = minDate.TaskId
|
---|
125 | GO
|
---|
126 |
|
---|
127 | /****** Object: Trigger [dbo].[tr_JobDeleteCascade] Script Date: 04/19/2011 16:31:53 ******/
|
---|
128 | SET ANSI_NULLS ON
|
---|
129 | GO
|
---|
130 | SET QUOTED_IDENTIFIER ON
|
---|
131 | GO
|
---|
132 | -- =============================================
|
---|
133 | -- Author: cneumuel
|
---|
134 | -- Create date: 19.04.2011
|
---|
135 | -- Description: (1) Writes the execution times of deleted jobs into DeletedJobStats to ensure correct statistics
|
---|
136 | -- (2) Deletes all associated jobs. This cannot be done with cascading delete,
|
---|
137 | -- because the job table defines a INSTEAD OF DELETE trigger itself, which
|
---|
138 | -- is not compatible with cascading deletes.
|
---|
139 | -- =============================================
|
---|
140 | CREATE TRIGGER [dbo].[tr_JobDeleteCascade] ON [dbo].[Job] INSTEAD OF DELETE AS
|
---|
141 | BEGIN
|
---|
142 | DELETE Task FROM deleted, Task WHERE deleted.JobId = Task.JobId
|
---|
143 | DELETE Job FROM deleted, Job WHERE deleted.JobId = Job.JobId
|
---|
144 | END
|
---|
145 | GO
|
---|
146 |
|
---|
147 | -- =============================================
|
---|
148 | -- Author: cneumuel
|
---|
149 | -- Create date: 11.11.2010
|
---|
150 | -- Description: Recursively deletes all child-jobs of a job when it is deleted. (Source: http://devio.wordpress.com/2008/05/23/recursive-delete-in-sql-server/)
|
---|
151 | -- =============================================DeletedJobStatistics
|
---|
152 | CREATE TRIGGER [dbo].[tr_TaskDeleteCascade] ON [dbo].[Task] INSTEAD OF DELETE AS
|
---|
153 | BEGIN
|
---|
154 | SELECT
|
---|
155 | he.OwnerUserId AS UserId,
|
---|
156 | ROUND(SUM(j.ExecutionTimeMs) / 1000, 0) AS ExecutionTimeS,
|
---|
157 | ROUND(ISNULL(SUM(CASE ls.State WHEN 'Finished' THEN j.ExecutionTimeMs END), 0) / 1000, 0) AS ExecutionTimeSFinishedJobs,
|
---|
158 | ISNULL(SUM(CASE ls.State WHEN 'Finished' THEN DATEDIFF(s, fs.DateTime, ls.DateTime) ELSE 0 END), 0) AS StartToEndTimeS
|
---|
159 | FROM
|
---|
160 | deleted j,
|
---|
161 | Job he,
|
---|
162 | view_FirstState fs,
|
---|
163 | view_LastState ls
|
---|
164 | WHERE
|
---|
165 | he.JobId = j.JobId AND
|
---|
166 | fs.TaskId = j.TaskId AND
|
---|
167 | ls.TaskId = j.TaskId
|
---|
168 | GROUP BY he.OwnerUserId
|
---|
169 |
|
---|
170 | -- recursively delete jobs
|
---|
171 | CREATE TABLE #Table(
|
---|
172 | TaskId uniqueidentifier
|
---|
173 | )
|
---|
174 | INSERT INTO #Table (TaskId)
|
---|
175 | SELECT TaskId FROM deleted
|
---|
176 |
|
---|
177 | DECLARE @c INT
|
---|
178 | SET @c = 0
|
---|
179 |
|
---|
180 | WHILE @c <> (SELECT COUNT(TaskId) FROM #Table) BEGIN
|
---|
181 | SELECT @c = COUNT(TaskId) FROM #Table
|
---|
182 |
|
---|
183 | INSERT INTO #Table (TaskId)
|
---|
184 | SELECT Task.TaskId
|
---|
185 | FROM Task
|
---|
186 | LEFT OUTER JOIN #Table ON Task.TaskId = #Table.TaskId
|
---|
187 | WHERE Task.ParentTaskId IN (SELECT TaskId FROM #Table)
|
---|
188 | AND #Table.TaskId IS NULL
|
---|
189 | END
|
---|
190 |
|
---|
191 | DELETE TaskData FROM TaskData INNER JOIN #Table ON TaskData.TaskId = #Table.TaskId
|
---|
192 | DELETE Task FROM Task INNER JOIN #Table ON Task.TaskId = #Table.TaskId
|
---|
193 | END
|
---|
194 | GO
|
---|
195 |
|
---|
196 | -- ============================================================
|
---|
197 | -- Description: Create indices to speed up execution of queries
|
---|
198 | -- ============================================================
|
---|
199 |
|
---|
200 | -- speed up joins between Job and Task
|
---|
201 | CREATE NONCLUSTERED INDEX [TaskJobIdIndex]
|
---|
202 | ON [dbo].[Task] ([JobId])
|
---|
203 | INCLUDE ([TaskId],[TaskState],[ExecutionTimeMs],[LastHeartbeat],[ParentTaskId],[Priority],[CoresNeeded],[MemoryNeeded],[IsParentTask],[FinishWhenChildJobsFinished],[Command],[IsPrivileged])
|
---|
204 | GO
|
---|
205 |
|
---|
206 | -- this is an index to speed up the GetWaitingTasks() method
|
---|
207 | CREATE NONCLUSTERED INDEX [TaskGetWaitingTasksIndex]
|
---|
208 | ON [dbo].[Task] ([TaskState],[IsParentTask],[FinishWhenChildJobsFinished],[CoresNeeded],[MemoryNeeded])
|
---|
209 | INCLUDE ([TaskId],[ExecutionTimeMs],[LastHeartbeat],[ParentTaskId],[Priority],[Command],[JobId],[IsPrivileged])
|
---|
210 | GO
|
---|