-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.sql
261 lines (239 loc) · 7.27 KB
/
test.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
PRINT('======== BEGIN SETUP ========');
PRINT('');
PRINT('SETUP: Switching to [destination_db]');
USE [destination_db];
-- BEGIN CREATE FUNCTIONS --
PRINT('SETUP: Creating look-up functions');
PRINT('');
GO
CREATE FUNCTION dbo.get_individual_individual_id_by_orig_agent_id(@value AS INT)
RETURNS UNIQUEIDENTIFIER
BEGIN
DECLARE @ret UNIQUEIDENTIFIER;
SELECT @ret = individual_id
FROM [individual]
WHERE [orig_agent_id] = @value;
RETURN @ret;
END;
GO
-- END CREATE FUNCTIONS --
PRINT('SETUP: Executing Setup Commands');
PRINT('');
PRINT('========= END SETUP =========');
PRINT('');
PRINT('===================================================');
PRINT('=!!!!! TEST MODE: TRANSACTION IS ROLLED BACK !!!!!=');
PRINT('===================================================');
PRINT('');
BEGIN TRANSACTION;
-- BEGIN CLEAN UP SQL --
PRINT('======== BEGIN CLEANUP ========');
PRINT('');
-- PRINT('INFO: Deleting all records in [destination_db].[dbo].[user_roles] where orig_user_id IS NOT NULL');
-- DELETE FROM [destination_db].[dbo].[user_roles] WHERE orig_user_id IS NOT NULL;
-- PRINT('');
-- PRINT('INFO: Deleting all records in [destination_db].[dbo].[users] where orig_id IS NOT NULL');
-- DELETE FROM [destination_db].[dbo].[users] WHERE orig_id IS NOT NULL;
-- PRINT('');
PRINT('========= END CLEANUP =========');
PRINT('');
-- END CLEAN UP SQL --
-- BEGIN INSERT-SELECT SQL --
PRINT('======== BEGIN PASS 1 of 2 ========');
PRINT('');
PRINT('==== Merging [destination_db].[dbo].[users] with [source_db].[dbo].[etl_Users] =====');
PRINT('INFO: migrating NON-DUPLICATE users');
MERGE [destination_db].[dbo].[users] AS D
USING [source_db].[dbo].[etl_Users] AS S
ON D.[orig_id] = S.[UserID]
WHEN MATCHED THEN
UPDATE SET
[orig_id] = S.[UserID],
[orig_admin] = S.[Admin],
[user_id] = S.[Username],
[password] = NULL,
[user_nm] = RTRIM(LTRIM(COALESCE(S.[FirstName], '') + ' ' + COALESCE(S.[LastName], ''))),
[attr_bits] = CASE WHEN S.[IsActive] = 1 THEN 0 ELSE 1 END,
[email_address] = S.[EMailAddress],
[user_guid] = dbo.get_individual_individual_id_by_orig_agent_id(S.[AgentID]),
[first_name] = S.[FirstName],
[last_name] = S.[LastName],
[date_created] = S.[RegistrationDate],
[pin_code] = S.[PinCode]
WHEN NOT MATCHED THEN
INSERT (
[orig_id],
[orig_admin],
[user_id],
[password],
[user_nm],
[attr_bits],
[email_address],
[user_guid],
[first_name],
[last_name],
[date_created],
[pin_code]
) VALUES (
S.[UserID],
S.[Admin],
S.[Username],
NULL,
RTRIM(LTRIM(COALESCE(S.[FirstName], '') + ' ' + COALESCE(S.[LastName], ''))),
CASE WHEN S.[IsActive] = 1 THEN 0 ELSE 1 END,
S.[EMailAddress],
dbo.get_individual_individual_id_by_orig_agent_id(S.[AgentID]),
S.[FirstName],
S.[LastName],
S.[RegistrationDate],
S.[PinCode]
)
OUTPUT $action,
Inserted.[orig_id],
Inserted.[orig_admin],
Inserted.[user_id],
Inserted.[password],
Inserted.[user_nm],
Inserted.[attr_bits],
Inserted.[email_address],
Inserted.[user_guid],
Inserted.[first_name],
Inserted.[last_name],
Inserted.[date_created],
Inserted.[pin_code];
PRINT('');
PRINT('==== Merging [destination_db].[dbo].[user_roles] with [source_db].[dbo].[etl_Users] =====');
PRINT('INFO: defaulted role for imported to Users');
MERGE [destination_db].[dbo].[user_roles] AS D
USING [source_db].[dbo].[etl_Users] AS S
ON D.[orig_user_id] = S.[UserID]
WHEN MATCHED THEN
UPDATE SET
[orig_user_id] = S.[UserID],
[user_id] = S.[Username],
[role_nm] = 'Users'
WHEN NOT MATCHED THEN
INSERT (
[orig_user_id],
[user_id],
[role_nm]
) VALUES (
S.[UserID],
S.[Username],
'Users'
)
OUTPUT $action,
Inserted.[orig_user_id],
Inserted.[user_id],
Inserted.[role_nm];
PRINT('');
PRINT('======= END OF PASS 1 of 2 ========');
PRINT('');
PRINT('======== BEGIN PASS 2 of 2 ========');
PRINT('');
PRINT('==== Merging [destination_db].[dbo].[users] with [source_db].[dbo].[etl_Users] =====');
PRINT('INFO: migrating NON-DUPLICATE users; and non-netquarry users by');
MERGE [destination_db].[dbo].[users] AS D
USING [source_db].[dbo].[etl_Users] AS S
ON D.[orig_id] = S.[UserID]
WHEN MATCHED THEN
UPDATE SET
[orig_id] = S.[UserID],
[orig_admin] = S.[Admin],
[user_id] = S.[Username],
[password] = NULL,
[user_nm] = RTRIM(LTRIM(COALESCE(S.[FirstName], '') + ' ' + COALESCE(S.[LastName], ''))),
[attr_bits] = CASE WHEN S.[IsActive] = 1 THEN 0 ELSE 1 END,
[email_address] = S.[EMailAddress],
[user_guid] = dbo.get_individual_individual_id_by_orig_agent_id(S.[AgentID]),
[first_name] = S.[FirstName],
[last_name] = S.[LastName],
[date_created] = S.[RegistrationDate],
[pin_code] = S.[PinCode]
WHEN NOT MATCHED THEN
INSERT (
[orig_id],
[orig_admin],
[user_id],
[password],
[user_nm],
[attr_bits],
[email_address],
[user_guid],
[first_name],
[last_name],
[date_created],
[pin_code]
) VALUES (
S.[UserID],
S.[Admin],
S.[Username],
NULL,
RTRIM(LTRIM(COALESCE(S.[FirstName], '') + ' ' + COALESCE(S.[LastName], ''))),
CASE WHEN S.[IsActive] = 1 THEN 0 ELSE 1 END,
S.[EMailAddress],
dbo.get_individual_individual_id_by_orig_agent_id(S.[AgentID]),
S.[FirstName],
S.[LastName],
S.[RegistrationDate],
S.[PinCode]
)
OUTPUT $action,
Inserted.[orig_id],
Inserted.[orig_admin],
Inserted.[user_id],
Inserted.[password],
Inserted.[user_nm],
Inserted.[attr_bits],
Inserted.[email_address],
Inserted.[user_guid],
Inserted.[first_name],
Inserted.[last_name],
Inserted.[date_created],
Inserted.[pin_code];
PRINT('');
PRINT('==== Merging [destination_db].[dbo].[user_roles] with [source_db].[dbo].[etl_Users] =====');
PRINT('INFO: defaulted role for imported to Users');
MERGE [destination_db].[dbo].[user_roles] AS D
USING [source_db].[dbo].[etl_Users] AS S
ON D.[orig_user_id] = S.[UserID]
WHEN MATCHED THEN
UPDATE SET
[orig_user_id] = S.[UserID],
[user_id] = S.[Username],
[role_nm] = 'Users'
WHEN NOT MATCHED THEN
INSERT (
[orig_user_id],
[user_id],
[role_nm]
) VALUES (
S.[UserID],
S.[Username],
'Users'
)
OUTPUT $action,
Inserted.[orig_user_id],
Inserted.[user_id],
Inserted.[role_nm];
PRINT('');
PRINT('======= END OF PASS 2 of 2 ========');
PRINT('');
-- END INSERT-SELECT SQL --
PRINT('===================================================');
PRINT('=!!!!!! TEST MODE: ROLLING BACK TRANSACTION !!!!!!=');
PRINT('===================================================');
rollback;
PRINT('');
PRINT('======== BEGIN TEARDOWN ========');
PRINT('');
GO
-- BEGIN DROP FUNCTIONS --
PRINT('TEARDOWN: Dropping Look-up Functions');
PRINT('');
GO
DROP FUNCTION dbo.get_individual_individual_id_by_orig_agent_id;
-- END DROP FUNCTIONS --
PRINT('TEARDOWN: Executing Teardown Commands');
PRINT('');
PRINT('========= END TEARDOWN =========');