1 | /* this script is supposed to be executed after the plain DB is generated by the linq-to-sql schema */
|
---|
2 |
|
---|
3 | /* use these DROP commands if you want to run this query a second time on the same db */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | ALTER TABLE dbo.AssignedResources ALTER COLUMN AssignedRessourcesId DROP ROWGUIDCOL;
|
---|
7 | ALTER TABLE dbo.Job ALTER COLUMN JobId DROP ROWGUIDCOL;
|
---|
8 | ALTER TABLE dbo.SlaveGroup_Resource ALTER COLUMN SlaveGroup_RessourceId DROP ROWGUIDCOL;
|
---|
9 | ALTER TABLE dbo.Plugin ALTER COLUMN PluginId DROP ROWGUIDCOL;
|
---|
10 | ALTER TABLE dbo.RequiredPlugins ALTER COLUMN RequiredPluginId DROP ROWGUIDCOL;
|
---|
11 | ALTER TABLE dbo.Resource ALTER COLUMN ResourceId DROP ROWGUIDCOL;
|
---|
12 | ALTER TABLE dbo.UptimeCalendar ALTER COLUMN UptimeCalendarId DROP ROWGUIDCOL;
|
---|
13 | ALTER TABLE dbo.HiveExperiment ALTER COLUMN HiveExperimentId DROP ROWGUIDCOL;
|
---|
14 |
|
---|
15 | ALTER TABLE dbo.AssignedResources DROP CONSTRAINT [DF_AssignedResources_AssignedRessourcesId];
|
---|
16 | ALTER TABLE dbo.Job DROP CONSTRAINT [DF_Job_JobId];
|
---|
17 | ALTER TABLE dbo.SlaveGroup_Resource DROP CONSTRAINT [DF_SlaveGroup_ResourceSlaveGroup_RessourceId];
|
---|
18 | ALTER TABLE dbo.Plugin DROP CONSTRAINT [DF_Plugin_PluginId];
|
---|
19 | ALTER TABLE dbo.RequiredPlugins DROP CONSTRAINT [DF_RequiredPlugins_RequiredPluginId];
|
---|
20 | ALTER TABLE dbo.Resource DROP CONSTRAINT [DF_Resource_ResourceId];
|
---|
21 | ALTER TABLE dbo.UptimeCalendar DROP CONSTRAINT [DF_UptimeCalendar_UptimeCalendarId];
|
---|
22 | ALTER TABLE dbo.HiveExperiment DROP CONSTRAINT [DF_HiveExperiment_HiveExperimentId];
|
---|
23 | */
|
---|
24 |
|
---|
25 | ALTER TABLE dbo.AssignedResources ALTER COLUMN AssignedRessourcesId ADD ROWGUIDCOL;
|
---|
26 | ALTER TABLE dbo.AssignedResources WITH NOCHECK ADD CONSTRAINT [DF_AssignedResources_AssignedRessourcesId] DEFAULT (newid()) FOR AssignedRessourcesId;
|
---|
27 | ALTER TABLE [dbo].[AssignedResources] DROP CONSTRAINT [Job_AssignedResource]
|
---|
28 | ALTER TABLE [dbo].[AssignedResources] WITH CHECK ADD CONSTRAINT [Job_AssignedResource] FOREIGN KEY([JobId])
|
---|
29 | REFERENCES [dbo].[Job] ([JobId])
|
---|
30 | ON UPDATE CASCADE
|
---|
31 | ON DELETE CASCADE
|
---|
32 | GO
|
---|
33 | ALTER TABLE [dbo].[AssignedResources] DROP CONSTRAINT [Resource_AssignedResource]
|
---|
34 | ALTER TABLE [dbo].[AssignedResources] WITH CHECK ADD CONSTRAINT [Resource_AssignedResource] FOREIGN KEY([ResourceId])
|
---|
35 | REFERENCES [dbo].[Resource] ([ResourceId])
|
---|
36 | ON UPDATE CASCADE
|
---|
37 | ON DELETE CASCADE
|
---|
38 | GO
|
---|
39 |
|
---|
40 | ALTER TABLE dbo.Job ALTER COLUMN JobId ADD ROWGUIDCOL;
|
---|
41 | ALTER TABLE dbo.Job WITH NOCHECK ADD CONSTRAINT [DF_Job_JobId] DEFAULT (newid()) FOR JobId;
|
---|
42 | GO
|
---|
43 |
|
---|
44 | ALTER TABLE [dbo].[StateLog] DROP CONSTRAINT [Job_StateLog]
|
---|
45 | ALTER TABLE [dbo].[StateLog] WITH CHECK ADD CONSTRAINT [Job_StateLog] FOREIGN KEY([JobId])
|
---|
46 | REFERENCES [dbo].[Job] ([JobId])
|
---|
47 | ON UPDATE CASCADE
|
---|
48 | ON DELETE CASCADE
|
---|
49 | GO
|
---|
50 |
|
---|
51 | ALTER TABLE dbo.Plugin ALTER COLUMN PluginId ADD ROWGUIDCOL;
|
---|
52 | ALTER TABLE dbo.Plugin WITH NOCHECK ADD CONSTRAINT [DF_Plugin_PluginId] DEFAULT (newid()) FOR PluginId;
|
---|
53 |
|
---|
54 | ALTER TABLE dbo.PluginData ALTER COLUMN PluginDataId ADD ROWGUIDCOL;
|
---|
55 | ALTER TABLE dbo.PluginData WITH NOCHECK ADD CONSTRAINT [DF_PluginData_PluginDataId] DEFAULT (newid()) FOR PluginDataId;
|
---|
56 |
|
---|
57 | ALTER TABLE dbo.RequiredPlugins ALTER COLUMN RequiredPluginId ADD ROWGUIDCOL;
|
---|
58 | ALTER TABLE dbo.RequiredPlugins WITH NOCHECK ADD CONSTRAINT [DF_RequiredPlugins_RequiredPluginId] DEFAULT (newid()) FOR RequiredPluginId;
|
---|
59 |
|
---|
60 | ALTER TABLE [dbo].[RequiredPlugins] DROP CONSTRAINT [Job_RequiredPlugin]
|
---|
61 | ALTER TABLE [dbo].[RequiredPlugins] WITH CHECK ADD CONSTRAINT [Job_RequiredPlugin] FOREIGN KEY([JobId])
|
---|
62 | REFERENCES [dbo].[Job] ([JobId])
|
---|
63 | ON UPDATE CASCADE
|
---|
64 | ON DELETE CASCADE
|
---|
65 | GO
|
---|
66 |
|
---|
67 | ALTER TABLE [dbo].[RequiredPlugins] DROP CONSTRAINT [Plugin_RequiredPlugin]
|
---|
68 | ALTER TABLE [dbo].[RequiredPlugins] WITH CHECK ADD CONSTRAINT [Plugin_RequiredPlugin] FOREIGN KEY([PluginId])
|
---|
69 | REFERENCES [dbo].[Plugin] ([PluginId])
|
---|
70 | ON UPDATE CASCADE
|
---|
71 | ON DELETE CASCADE
|
---|
72 | GO
|
---|
73 |
|
---|
74 | ALTER TABLE dbo.Resource ALTER COLUMN ResourceId ADD ROWGUIDCOL;
|
---|
75 | ALTER TABLE dbo.Resource WITH NOCHECK ADD CONSTRAINT [DF_Resource_ResourceId] DEFAULT (newid()) FOR ResourceId;
|
---|
76 |
|
---|
77 | ALTER TABLE dbo.UptimeCalendar ALTER COLUMN UptimeCalendarId ADD ROWGUIDCOL;
|
---|
78 | ALTER TABLE dbo.UptimeCalendar WITH NOCHECK ADD CONSTRAINT [DF_UptimeCalendar_UptimeCalendarId] DEFAULT (newid()) FOR UptimeCalendarId;
|
---|
79 |
|
---|
80 | ALTER TABLE dbo.HiveExperiment ALTER COLUMN HiveExperimentId ADD ROWGUIDCOL;
|
---|
81 | ALTER TABLE dbo.HiveExperiment WITH NOCHECK ADD CONSTRAINT [DF_HiveExperiment_HiveExperimentId] DEFAULT (newid()) FOR HiveExperimentId;
|
---|
82 |
|
---|
83 | ALTER TABLE dbo.StateLog ALTER COLUMN StateLogId ADD ROWGUIDCOL;
|
---|
84 | ALTER TABLE dbo.StateLog WITH NOCHECK ADD CONSTRAINT [DF_StateLog_StateLogId] DEFAULT (newid()) FOR StateLogId;
|
---|
85 |
|
---|
86 | ALTER TABLE [dbo].[HiveExperimentPermission] DROP CONSTRAINT [HiveExperiment_HiveExperimentPermission]
|
---|
87 | ALTER TABLE [dbo].[HiveExperimentPermission] WITH CHECK ADD CONSTRAINT [HiveExperiment_HiveExperimentPermission] FOREIGN KEY([HiveExperimentId])
|
---|
88 | REFERENCES [dbo].[HiveExperiment] ([HiveExperimentId])
|
---|
89 | ON UPDATE CASCADE
|
---|
90 | ON DELETE CASCADE
|
---|
91 | GO
|
---|
92 |
|
---|
93 | /* create indices */
|
---|
94 | CREATE INDEX Index_RequiredPlugins_JobId ON RequiredPlugins(JobId);
|
---|
95 |
|
---|
96 | /* triggers */
|
---|
97 | SET ANSI_NULLS ON
|
---|
98 | GO
|
---|
99 | SET QUOTED_IDENTIFIER ON
|
---|
100 | GO
|
---|
101 | -- =============================================
|
---|
102 | -- Author: cneumuel
|
---|
103 | -- Create date: 11.11.2010
|
---|
104 | -- Description: Deletes the root-job of the experiment when experiment is deleted
|
---|
105 | -- =============================================
|
---|
106 | CREATE TRIGGER [dbo].[tr_HiveExperimentDeleteCascade] ON [dbo].[HiveExperiment] FOR DELETE AS
|
---|
107 | SET NOCOUNT ON
|
---|
108 | DELETE Job FROM deleted, Job WHERE Job.JobId = deleted.RootJobId
|
---|
109 | GO
|
---|
110 |
|
---|
111 | SET ANSI_NULLS ON
|
---|
112 | GO
|
---|
113 | SET QUOTED_IDENTIFIER ON
|
---|
114 | GO
|
---|
115 | -- =============================================
|
---|
116 | -- Author: cneumuel
|
---|
117 | -- Create date: 11.11.2010
|
---|
118 | -- 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/)
|
---|
119 | -- =============================================
|
---|
120 | CREATE TRIGGER [dbo].[tr_JobDeleteCascade] ON [dbo].[Job] INSTEAD OF DELETE AS
|
---|
121 | BEGIN
|
---|
122 | CREATE TABLE #Table(
|
---|
123 | JobId uniqueidentifier
|
---|
124 | )
|
---|
125 | INSERT INTO #Table (JobId)
|
---|
126 | SELECT JobId FROM deleted
|
---|
127 |
|
---|
128 | DECLARE @c INT
|
---|
129 | SET @c = 0
|
---|
130 |
|
---|
131 | WHILE @c <> (SELECT COUNT(JobId) FROM #Table) BEGIN
|
---|
132 | SELECT @c = COUNT(JobId) FROM #Table
|
---|
133 |
|
---|
134 | INSERT INTO #Table (JobId)
|
---|
135 | SELECT Job.JobId
|
---|
136 | FROM Job
|
---|
137 | LEFT OUTER JOIN #Table ON Job.JobId = #Table.JobId
|
---|
138 | WHERE Job.ParentJobId IN (SELECT JobId FROM #Table)
|
---|
139 | AND #Table.JobId IS NULL
|
---|
140 | END
|
---|
141 |
|
---|
142 | DELETE JobData FROM JobData INNER JOIN #Table ON JobData.JobId = #Table.JobId
|
---|
143 | DELETE Job FROM Job INNER JOIN #Table ON Job.JobId = #Table.JobId
|
---|
144 | END
|
---|