blob: 3055ac9863e49b156ceb1df613f1b2fb5ee26e91 (
plain)
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
|
DECLARE @base nvarchar(max),
@SQL nvarchar(max),
@id sysname;--
SET @base = 'ALTER TABLE /*_*/archive DROP CONSTRAINT ';--
SELECT @id = fk.name
FROM sys.foreign_keys fk
JOIN sys.foreign_key_columns fkc
ON fkc.constraint_object_id = fk.object_id
JOIN sys.columns c
ON c.column_id = fkc.parent_column_id
AND c.object_id = fkc.parent_object_id
WHERE
fk.parent_object_id = OBJECT_ID('/*_*/archive')
AND fk.referenced_object_id = OBJECT_ID('/*_*/revision')
AND c.name = 'ar_parent_id';--
SET @SQL = @base + @id;--
EXEC sp_executesql @SQL;--
-- while we're at it, let's fix up the other foreign key constraints on archive
-- as future patches touch constraints on other tables, they'll take the time to update constraint names there as well
SELECT @id = fk.name
FROM sys.foreign_keys fk
JOIN sys.foreign_key_columns fkc
ON fkc.constraint_object_id = fk.object_id
JOIN sys.columns c
ON c.column_id = fkc.parent_column_id
AND c.object_id = fkc.parent_object_id
WHERE
fk.parent_object_id = OBJECT_ID('/*_*/archive')
AND fk.referenced_object_id = OBJECT_ID('/*_*/mwuser')
AND c.name = 'ar_user';--
SET @SQL = @base + @id;--
EXEC sp_executesql @SQL;--
ALTER TABLE /*_*/archive ADD CONSTRAINT ar_user__user_id__fk FOREIGN KEY (ar_user) REFERENCES /*_*/mwuser(user_id);--
SELECT @id = fk.name
FROM sys.foreign_keys fk
JOIN sys.foreign_key_columns fkc
ON fkc.constraint_object_id = fk.object_id
JOIN sys.columns c
ON c.column_id = fkc.parent_column_id
AND c.object_id = fkc.parent_object_id
WHERE
fk.parent_object_id = OBJECT_ID('/*_*/archive')
AND fk.referenced_object_id = OBJECT_ID('/*_*/text')
AND c.name = 'ar_text_id';--
SET @SQL = @base + @id;--
EXEC sp_executesql @SQL;--
ALTER TABLE /*_*/archive ADD CONSTRAINT ar_text_id__old_id__fk FOREIGN KEY (ar_text_id) REFERENCES /*_*/text(old_id) ON DELETE CASCADE;
|