-
Notifications
You must be signed in to change notification settings - Fork 0
/
V202409131653__v2.5.x_FixBaseCodeGenSPs.sql
192 lines (177 loc) · 5.94 KB
/
V202409131653__v2.5.x_FixBaseCodeGenSPs.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
-- Update this procedure to exclude virtual entities
DROP PROC IF EXISTS [${flyway:defaultSchema}].[spDeleteUnneededEntityFields]
GO
CREATE PROC [${flyway:defaultSchema}].[spDeleteUnneededEntityFields]
@ExcludedSchemaNames NVARCHAR(MAX)
AS
-- Get rid of any EntityFields that are NOT virtual and are not part of the underlying VIEW or TABLE - these are orphaned meta-data elements
-- where a field once existed but no longer does either it was renamed or removed from the table or view
IF OBJECT_ID('tempdb..#ef_spDeleteUnneededEntityFields') IS NOT NULL
DROP TABLE #ef_spDeleteUnneededEntityFields
IF OBJECT_ID('tempdb..#actual_spDeleteUnneededEntityFields') IS NOT NULL
DROP TABLE #actual_spDeleteUnneededEntityFields
-- put these two views into temp tables, for some SQL systems, this makes the join below WAY faster
SELECT
ef.*
INTO
#ef_spDeleteUnneededEntityFields
FROM
vwEntityFields ef
INNER JOIN
vwEntities e
ON
ef.EntityID = e.ID
-- Use LEFT JOIN with STRING_SPLIT to filter out excluded schemas
LEFT JOIN
STRING_SPLIT(@ExcludedSchemaNames, ',') AS excludedSchemas
ON
e.SchemaName = excludedSchemas.value
WHERE
e.VirtualEntity = 0 AND -- exclude virtual entities from this always
excludedSchemas.value IS NULL -- This ensures rows with matching SchemaName are excluded
SELECT * INTO #actual_spDeleteUnneededEntityFields FROM vwSQLColumnsAndEntityFields
-- first update the entity UpdatedAt so that our metadata timestamps are right
UPDATE [${flyway:defaultSchema}].Entity SET __mj_UpdatedAt=GETUTCDATE() WHERE ID IN
(
SELECT
ef.EntityID
FROM
#ef_spDeleteUnneededEntityFields ef
LEFT JOIN
#actual_spDeleteUnneededEntityFields actual
ON
ef.EntityID=actual.EntityID AND
ef.Name = actual.EntityFieldName
WHERE
actual.column_id IS NULL
)
-- now delete the entity fields themsevles
DELETE FROM [${flyway:defaultSchema}].EntityField WHERE ID IN
(
SELECT
ef.ID
FROM
#ef_spDeleteUnneededEntityFields ef
LEFT JOIN
#actual_spDeleteUnneededEntityFields actual
ON
ef.EntityID=actual.EntityID AND
ef.Name = actual.EntityFieldName
WHERE
actual.column_id IS NULL
)
-- clean up and get rid of our temp tables now
DROP TABLE #ef_spDeleteUnneededEntityFields
DROP TABLE #actual_spDeleteUnneededEntityFields
GO
---------------------------------------------------
-- update this proc to EXCLUDE virtual entities always
DROP PROC IF EXISTS [${flyway:defaultSchema}].[spUpdateExistingEntityFieldsFromSchema]
GO
CREATE PROC [${flyway:defaultSchema}].[spUpdateExistingEntityFieldsFromSchema]
@ExcludedSchemaNames NVARCHAR(MAX)
AS
BEGIN
-- Update Statement
UPDATE [${flyway:defaultSchema}].EntityField
SET
Description = IIF(ef.AutoUpdateDescription=1, CONVERT(NVARCHAR(MAX),fromSQL.Description), ef.Description),
Type = fromSQL.Type,
Length = fromSQL.Length,
Precision = fromSQL.Precision,
Scale = fromSQL.Scale,
AllowsNull = fromSQL.AllowsNull,
DefaultValue = fromSQL.DefaultValue,
AutoIncrement = fromSQL.AutoIncrement,
IsVirtual = fromSQL.IsVirtual,
Sequence = fromSQL.Sequence,
RelatedEntityID = re.ID,
RelatedEntityFieldName = fk.referenced_column,
IsPrimaryKey = CASE
WHEN pk.ColumnName IS NOT NULL THEN 1
ELSE 0
END,
IsUnique = CASE
WHEN pk.ColumnName IS NOT NULL THEN 1
ELSE
CASE
WHEN uk.ColumnName IS NOT NULL THEN 1
ELSE 0
END
END,
__mj_UpdatedAt = GETUTCDATE()
FROM
[${flyway:defaultSchema}].EntityField ef
INNER JOIN
vwSQLColumnsAndEntityFields fromSQL
ON
ef.EntityID = fromSQL.EntityID AND
ef.Name = fromSQL.FieldName
INNER JOIN
[${flyway:defaultSchema}].Entity e
ON
ef.EntityID = e.ID
LEFT OUTER JOIN
vwForeignKeys fk
ON
ef.Name = fk.[column] AND
e.BaseTable = fk.[table] AND
e.SchemaName = fk.[schema_name]
LEFT OUTER JOIN
[${flyway:defaultSchema}].Entity re -- Related Entity
ON
re.BaseTable = fk.referenced_table AND
re.SchemaName = fk.[referenced_schema]
LEFT OUTER JOIN
[${flyway:defaultSchema}].vwTablePrimaryKeys pk
ON
e.BaseTable = pk.TableName AND
ef.Name = pk.ColumnName AND
e.SchemaName = pk.SchemaName
LEFT OUTER JOIN
[${flyway:defaultSchema}].vwTableUniqueKeys uk
ON
e.BaseTable = uk.TableName AND
ef.Name = uk.ColumnName AND
e.SchemaName = uk.SchemaName
-- Use LEFT JOIN with STRING_SPLIT to filter out excluded schemas
LEFT JOIN
STRING_SPLIT(@ExcludedSchemaNames, ',') AS excludedSchemas
ON
e.SchemaName = excludedSchemas.value
WHERE
e.VirtualEntity = 0
AND
fromSQL.EntityFieldID IS NOT NULL -- only where we HAVE ALREADY CREATED EntityField records
AND
excludedSchemas.value IS NULL -- This ensures rows with matching SchemaName are excluded
END
GO
-- update this proc to always exclude virtual entities
DROP PROCEDURE IF EXISTS [${flyway:defaultSchema}].[spUpdateExistingEntitiesFromSchema];
GO
CREATE PROCEDURE [${flyway:defaultSchema}].spUpdateExistingEntitiesFromSchema
@ExcludedSchemaNames NVARCHAR(MAX)
AS
BEGIN
-- Update statement excluding rows with matching SchemaName
UPDATE
[${flyway:defaultSchema}].[Entity]
SET
Description = IIF(e.AutoUpdateDescription=1, CONVERT(NVARCHAR(MAX),fromSQL.EntityDescription), e.Description)
FROM
[${flyway:defaultSchema}].[Entity] e
INNER JOIN
[${flyway:defaultSchema}].[vwSQLTablesAndEntities] fromSQL
ON
e.ID = fromSQL.EntityID
-- Use LEFT JOIN with STRING_SPLIT to filter out excluded schemas
LEFT JOIN
STRING_SPLIT(@ExcludedSchemaNames, ',') AS excludedSchemas
ON
fromSQL.SchemaName = excludedSchemas.value
WHERE
e.VirtualEntity = 0 AND
excludedSchemas.value IS NULL; -- This ensures rows with matching SchemaName are excluded
END;
GO