-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfiguration_v01-to-v1_Migration.sql
3079 lines (2644 loc) · 117 KB
/
Configuration_v01-to-v1_Migration.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
USE [Config2]
-- Migration Config DB Script
-- vars
:setvar CortexDBUser "sotonlab\ctx_cerberusdb"
:setvar DatabaseFilePath "C:\Cortex Databases\"
:setvar DatabaseLogPath "C:\Cortex Databases\"
:setvar Distribution_DataPath "C:\Cortex Databases\distribution"
:setvar Distribution_LogPath "C:\Cortex Databases\distribution"
:setvar InstanceName "v-ctxappdb19"
:setvar MachineName "v-ctxappdb19"
:setvar REPL_Admin_User "sotonlab\ctx_sql_admin"
:setvar REPL_Working_Directory "C:\Cortex Databases\"
:setvar ResilientInstanceName "v-ctxappdb20"
:setvar DatabaseName "Config2"
:setvar DefaultFilePrefix "Config2"
:setvar DefaultDataPath ""
:setvar DefaultLogPath ""
GO
:on error exit
GO
/*
Detect SQLCMD mode and disable script execution if SQLCMD mode is not supported.
To re-enable the script after enabling SQLCMD mode, execute the following:
SET NOEXEC OFF;
*/
:setvar __IsSqlCmdEnabled "True"
GO
IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True'
BEGIN
PRINT N'SQLCMD mode must be enabled to successfully execute this script.';
SET NOEXEC ON;
END
/*
Rename old database tables
*/
PRINT 'Starting old database tables renaming...'
GO
PRINT '***Renaming Config_Values to Config_Values-v1...'
exec sp_rename 'Config_Values','Config_Values-v1'
GO
PRINT '***Renaming Config_Parameters to Config_Parameters-v1...'
exec sp_rename 'Config_Parameters','Config_Parameters-v1'
GO
PRINT '***Renaming Config_Mapping to Config_Mapping-v1...'
exec sp_rename 'Config_Mapping','Config_Mapping-v1'
GO
PRINT '***Renaming Cust_Area_Mapped to Cust_Area_Mapped-v1...'
exec sp_rename 'Cust_Area_Mapped','Cust_Area_Mapped-v1'
GO
PRINT '***Renaming Areas to Areas-v1...'
exec sp_rename 'Areas','Areas-v1'
GO
PRINT '***Renaming Customers to Customers-v1...'
exec sp_rename 'Customers','Customers-v1'
GO
PRINT '***Renaming Environments to Environments-v1...'
exec sp_rename 'Environments','Environments-v1'
GO
PRINT '***Renaming DeleteTransactions to DeleteTransactions-v1...'
exec sp_rename 'DeleteTransactions','DeleteTransactions-v1'
/*
Deploy new database tables
*/
PRINT 'Creating Cortex Configuration v2 database objects...'
/****** Object: Table [dbo].[Areas] Script Date: 23/05/2019 10:55:58 ******/
PRINT '***Creating table [dbo].[Areas]...'
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Areas](
[ID] [int] NOT NULL,
[Area] [varchar](50) NOT NULL,
[Description] [varchar](500) NULL,
CONSTRAINT [PK_Areas-v2] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [Area_Unique] UNIQUE NONCLUSTERED
(
[Area] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Customers] Script Date: 23/05/2019 10:55:58 ******/
PRINT '***Creating Table [dbo].[Customers]...'
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Customers](
[ID] [int] NOT NULL,
[Customer] [varchar](50) NOT NULL,
[Description] [varchar](500) NULL,
CONSTRAINT [PK_Customers-v2] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [Cust_Unique] UNIQUE NONCLUSTERED
(
[Customer] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Environments] Script Date: 23/05/2019 10:55:58 ******/
PRINT '***Creating Table [dbo].[Environments]...'
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Environments](
[ID] [int] NOT NULL,
[Environment] [varchar](50) NOT NULL,
[Description] [varchar](500) NULL,
CONSTRAINT [PK_Environments-v2] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [Env_Unique] UNIQUE NONCLUSTERED
(
[Environment] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
PRINT '***Creating Table [dbo].[Param_Name]...'
/****** Object: Table [dbo].[Param_Name] Script Date: 23/05/2019 10:55:58 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Param_Name](
[ID] [int] NOT NULL,
[Param_Name] [varchar](50) NOT NULL,
[Description] [varchar](500) NULL,
CONSTRAINT [PK_Param_Name-v2] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [Prm_Unique] UNIQUE NONCLUSTERED
(
[Param_Name] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
PRINT '***Creating Table [dbo].[Param_Values]...'
/****** Object: Table [dbo].[Param_Values] Script Date: 23/05/2019 10:55:58 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Param_Values](
[ID] [int] NOT NULL,
[AreaID] [int] NOT NULL,
[CustomerID] [int] NULL,
[EnvironmentID] [int] NULL,
[Param_NameID] [int] NOT NULL,
[Param_Value] [varchar](max) NOT NULL,
[Description] [varchar](500) NULL,
CONSTRAINT [PK_Param_Values-v2] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [unique_prm] UNIQUE NONCLUSTERED
(
[AreaID] ASC,
[CustomerID] ASC,
[EnvironmentID] ASC,
[Param_NameID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
PRINT '***Creating View [dbo].[CGF_All_Permutations]...'
/****** Object: View [dbo].[CGF_All_Permutations] Script Date: 23/05/2019 10:55:58 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[CGF_All_Permutations] AS
SELECT T.Area
, T.Customer
, T.Environment
, T.Param_Name
, PV.ID
, PV.Param_Value
FROM
(SELECT A.ID AS AreaID
, A.Area
, C.ID AS CustomerID
, C.Customer
, E.ID AS EnvironmentID
, E.Environment
, PN.ID AS ParamID
, PN.Param_Name
FROM Param_Name PN
CROSS JOIN Areas A
CROSS JOIN Customers C
CROSS JOIN Environments E
UNION
SELECT A.ID AS AreaID
, A.Area
, C.ID AS CustomerID
, C.Customer
, NULL AS EnvironmentID
, NULL Environment
, PN.ID AS ParamID
, PN.Param_Name
FROM Param_Name PN
CROSS JOIN Areas A
CROSS JOIN Customers C
UNION
SELECT A.ID AS AreaID
, A.Area
, NULL AS CustomerID
, NULL AS Customer
, E.ID AS EnvironmentID
, E.Environment
, PN.ID AS ParamID
, PN.Param_Name
FROM Param_Name PN
CROSS JOIN Areas A
CROSS JOIN Environments E
UNION
SELECT A.ID AS AreaID
, A.Area
, NULL AS CustomerID
, NULL AS Customer
, NULL AS EnvironmentID
, NULL AS Environment
, PN.ID AS ParamID
, PN.Param_Name
FROM Param_Name PN
CROSS JOIN Areas A) T
LEFT OUTER JOIN Param_Values PV
ON ISNULL(PV.Param_NameID,-1) = ISNULL(T.ParamID,-1)
AND ISNULL(PV.AreaID,-1) = ISNULL(T.AreaID,-1)
AND ISNULL(PV.CustomerID,-1) = ISNULL(T.CustomerID,-1)
AND ISNULL(PV.EnvironmentID,-1) = ISNULL(T.EnvironmentID,-1)
GO
/****** Object: Table [dbo].[DeleteTransactions] Script Date: 23/05/2019 10:55:58 ******/
PRINT '***Creating Table [dbo].[DeleteTransactions]...'
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[DeleteTransactions](
[ID] [int] NOT NULL,
[DeletedTable] [varchar](30) NOT NULL,
[DeletedValue] [varchar](max) NOT NULL,
[Timestamp] [datetime] NOT NULL,
[RestoreQuery] [varchar](max) NOT NULL,
CONSTRAINT [PK_DeleteTransactions-v2] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
-- Added this - needs to be tested - JL
CREATE TRIGGER [dbo].[TRG_InsteadOfInsert_DeleteTransactions]
ON [dbo].[DeleteTransactions]
INSTEAD OF INSERT NOT FOR REPLICATION
AS
BEGIN
SET NOCOUNT ON;
DECLARE @DelID int = (SELECT dbo.udf_GetSequenceNumber('DeleteTransactions','ID'));
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO DeleteTranDeleteTransactions(ID
,DeletedTable
,DeletedValue
,Timestamp
,RestoreQuery)
SELECT @DelID
,inserted.DeletedTable
,inserted.DeletedValue
,inserted.Timestamp
,inserted.RestoreQuery
FROM inserted
UPDATE SYS_Sequence
SET SequenceNumber = @DelID + 1
WHERE TableName = 'DeleteTransactions'
AND ColumnName = 'ID'
COMMIT TRANSACTION
END TRY
BEGIN CATCH
PRINT ERROR_MESSAGE()
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION
END CATCH
END
GO
ALTER TABLE [dbo].[Param_Values] WITH CHECK ADD CONSTRAINT [FK_Param_Values_Areas] FOREIGN KEY([AreaID])
REFERENCES [dbo].[Areas] ([ID])
GO
ALTER TABLE [dbo].[Param_Values] CHECK CONSTRAINT [FK_Param_Values_Areas]
GO
ALTER TABLE [dbo].[Param_Values] WITH CHECK ADD CONSTRAINT [FK_Param_Values_Customers] FOREIGN KEY([CustomerID])
REFERENCES [dbo].[Customers] ([ID])
GO
ALTER TABLE [dbo].[Param_Values] CHECK CONSTRAINT [FK_Param_Values_Customers]
GO
ALTER TABLE [dbo].[Param_Values] WITH CHECK ADD CONSTRAINT [FK_Param_Values_Environments] FOREIGN KEY([EnvironmentID])
REFERENCES [dbo].[Environments] ([ID])
GO
ALTER TABLE [dbo].[Param_Values] CHECK CONSTRAINT [FK_Param_Values_Environments]
GO
ALTER TABLE [dbo].[Param_Values] WITH CHECK ADD CONSTRAINT [FK_Param_Values_Param_Name] FOREIGN KEY([Param_NameID])
REFERENCES [dbo].[Param_Name] ([ID])
GO
ALTER TABLE [dbo].[Param_Values] CHECK CONSTRAINT [FK_Param_Values_Param_Name]
GO
/****** Object: StoredProcedure [dbo].[usp_InsertUpdate_ParamValues] Script Date: 23/05/2019 10:55:58 ******/
PRINT '***Creating StoredProcedure [dbo].[usp_InsertUpdate_ParamValues]...'
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Pedro Nogueira
-- Create date: 21/04/2019
-- Description: Insert/Update parameters into ParamValues table
-- =============================================
CREATE PROCEDURE [dbo].[usp_InsertUpdate_ParamValues]
-- Add the parameters for the stored procedure here
@i_sAreaName varchar(50),
@i_sParamName varchar(50),
@i_sCustomerName varchar(50) = NULL,
@i_sEnvironmentName varchar(50) = NULL,
@i_sParamValue varchar(max)
AS
BEGIN
DECLARE @nAreaID INT = (SELECT ID
FROM Areas
WHERE Area = @i_sAreaName)
, @nCustomerID INT = (SELECT ID
FROM Customers
WHERE Customer = @i_sCustomerName)
, @nEnvironmentID INT = (SELECT ID
FROM Environments
WHERE Environment = @i_sEnvironmentName)
, @nParamID INT = (SELECT ID
FROM Param_Name
WHERE Param_Name = @i_sParamName)
IF NOT EXISTS(SELECT 1
FROM Param_Values
WHERE AreaID = @nAreaID
AND Param_NameID = @nParamID
AND ISNULL(EnvironmentID,-1) = ISNULL(@nEnvironmentID,-1)
AND ISNULL(CustomerID,-1) = ISNULL(@nCustomerID,-1))
BEGIN
INSERT INTO [dbo].[Param_Values]
([AreaID]
,[CustomerID]
,[EnvironmentID]
,[Param_nameID]
,[Param_Value])
VALUES
(@nAreaID
, @nCustomerID
, @nEnvironmentID
, @nParamID
, @i_sParamValue)
END
ELSE
BEGIN
UPDATE [dbo].[Param_Values]
SET Param_Value = @i_sParamValue
WHERE AreaID = @nAreaID
AND Param_nameID = @nParamID
AND ISNULL(EnvironmentID,-1) = ISNULL(@nEnvironmentID,-1)
AND ISNULL(CustomerID,-1) = ISNULL(@nCustomerID,-1)
END
END
-- JL: Add required v1.1 objects
GO
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;
SET NUMERIC_ROUNDABORT OFF;
GO
IF EXISTS (SELECT srv.name
FROM sys.servers srv
WHERE srv.server_id != 0
AND srv.name = N'REPL-$(ResilientInstanceName)')
EXEC sp_dropserver 'REPL-$(ResilientInstanceName)','droplogins'
EXEC master.dbo.sp_addlinkedserver @server = N'REPL-$(ResilientInstanceName)', @srvproduct='', @provider='SQLOLEDB', @datasrc=N'$(ResilientInstanceName)'
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'REPL-$(ResilientInstanceName)',@useself=N'True',@locallogin=NULL,@rmtuser=NULL,@rmtpassword=NULL
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'collation compatible', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'data access', @optvalue=N'true'
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'dist', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'pub', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'rpc', @optvalue=N'true'
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'rpc out', @optvalue=N'true'
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'sub', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'connect timeout', @optvalue=N'0'
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'collation name', @optvalue=null
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'lazy schema validation', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'query timeout', @optvalue=N'0'
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'use remote collation', @optvalue=N'true'
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'remote proc transaction promotion', @optvalue=N'true'
GO
PRINT N'Creating [dbo].[SYS_Sequence]...';
GO
CREATE TABLE [dbo].[SYS_Sequence] (
[TableName] VARCHAR (100) NOT NULL,
[ColumnName] VARCHAR (100) NOT NULL,
[SequenceNumber] BIGINT NOT NULL,
CONSTRAINT [PK_SYS_Sequence] PRIMARY KEY CLUSTERED ([TableName] ASC)
);
GO
PRINT N'Creating [dbo].[SYS_SETTINGS]...';
GO
CREATE TABLE [dbo].[SYS_SETTINGS] (
[KEY] VARCHAR (32) NOT NULL,
[VALUE] VARCHAR (MAX) NOT NULL,
[DESCRIPTION] VARCHAR (MAX) NULL,
CONSTRAINT [PK_SYS_SETTINGS] PRIMARY KEY CLUSTERED ([KEY] ASC)
);
GO
PRINT N'Creating [dbo].[udf_Get_Parameter_Value]...';
GO
CREATE FUNCTION [dbo].[udf_Get_Parameter_Value](@pi_Param_Name varchar(200))
RETURNS varchar(200) AS
BEGIN
declare @return_value varchar(200)
select @return_value = [VALUE]
from SYS_SETTINGS
where [KEY] = @pi_Param_Name
return @return_value
END
GO
PRINT N'Creating [dbo].[udf_GetSequenceNumber]...';
GO
CREATE FUNCTION [dbo].[udf_GetSequenceNumber]
(
@i_sTableName VARCHAR(100)
, @i_sColumnName VARCHAR(100)
)
RETURNS BIGINT
AS
BEGIN
DECLARE @nSequenceNumber BIGINT
SELECT @nSequenceNumber = [SequenceNumber]
FROM SYS_Sequence
WHERE [TableName] = @i_sTableName
AND [ColumnName] = @i_sColumnName
RETURN @nSequenceNumber
END
GO
GO
PRINT N'Creating [dbo].[CFG_View]...';
GO
CREATE VIEW [dbo].[CFG_View]
AS
SELECT TOP 100 PERCENT A.Area,
C.Customer,
E.Environment,
PN.Param_name,
PV.Description,
PV.Param_value
FROM dbo.param_values AS PV
INNER JOIN dbo.param_name AS PN
ON PV.param_nameid = PN.id
INNER JOIN dbo.areas AS A
ON PV.areaid = A.id
LEFT OUTER JOIN dbo.environments AS E
ON PV.environmentid = E.id
LEFT OUTER JOIN dbo.customers AS C
ON PV.customerid = C.id
ORDER BY A.Area,
C.Customer,
E.Environment,
PN.Param_name
GO
PRINT N'Creating [dbo].[usp_Get_Job_Status]...';
GO
CREATE PROCEDURE [dbo].[usp_Get_Job_Status]
@job_name SYSNAME
AS
SET NOCOUNT ON
DECLARE @job_id UNIQUEIDENTIFIER
DECLARE @is_idle INT
declare @status int
--Check name exists
IF NOT EXISTS(SELECT 1
FROM msdb.dbo.sysjobs (nolock)
where name=@job_name)
BEGIN
RAISERROR('The job name specified does not exist',16,1)
RETURN 1
END
--Temp table to store results of xp_sqlagent_enum_jobs
create table #xp_results (job_id UNIQUEIDENTIFIER NOT NULL,
last_run_date INT NOT NULL,
last_run_time INT NOT NULL,
next_run_date INT NOT NULL,
next_run_time INT NOT NULL,
next_run_schedule_id INT NOT NULL,
requested_to_run INT NOT NULL,
request_source INT NOT NULL,
request_source_id sysname COLLATE database_default NULL,
running INT NOT NULL,
current_step INT NOT NULL,
current_retry_attempt INT NOT NULL,
job_state INT NOT NULL)
--Get the Job ID
EXECUTE msdb.dbo.sp_verify_job_identifiers '@job_name', '@job_id', @job_name OUTPUT, @job_id OUTPUT
--Get Job Details
INSERT INTO #xp_results
EXECUTE master.dbo.xp_sqlagent_enum_jobs 1, 'sa', @job_id -- SQLCMD?
--Get Job Status
/*
1 Executing.
2 Waiting for thread.
3 Between retries.
4 Idle.
5 Suspended.
7 Performing completion actions.
*/
SELECT @status=job_state
FROM #xp_results
DROP TABLE #xp_results
return @status
GO
PRINT N'Creating [dbo].[usp_REPL_Add_Article]...';
GO
-- =================================================================================
-- Author: Donna-Marie Dimmer
-- Create Date: 19th October 2011
-- Description: This procedure adds an article to a Publication and has replaced
-- the original procedures usp_REPL_Add_Merge_Article and
-- usp_REPL_TXN_Add_Article.
-- =================================================================================
-- Calls: None
-- Called By: usp_REPL_Create_Publication_Live_Transactional
-- usp_REPL_Create_Publication_CFG_Transactional
-- usp_REPL_Create_Publication_CFG_Merge
-- usp_REPL_Recreate_Publication_Live_Transactional
-- =================================================================================
-- Update Author:Chris Jenkins
-- Update Date: 3rd September 2012
-- Description: 1) Added code for a new Partitioning publication. This replicates
-- the partitioning call on the remote site rather than performing
-- all changes on the live site and replicating the changes. This
-- involves a large number of deletes and takes a lot of resources
-- unnecessarily in live environments.
-- 2) Tidied and used new SQL 2008 functionality where applicable.
-- 3) Updated the merge schema options to avoid incompatible property
-- warnings on install.
-- =================================================================================
CREATE PROCEDURE [dbo].[usp_REPL_Add_Article]
@i_sPubName sysname
, @i_sPubType varchar(15)
, @i_sArticleName sysname
, @i_sArticleType varchar(50)
AS
DECLARE @nSchemaOption binary(8)
, @sType varchar(16)
, @sPreCreationCmd varchar(16)
SET NOCOUNT ON
IF @i_sPubType = 'Transactional'
BEGIN
IF @i_sArticleType = 'table'
BEGIN
SET @nSchemaOption = 0x0000000048045DDD
SET @sType = 'logbased'
SET @sPreCreationCmd = 'delete'
END
ELSE
BEGIN
SET @nSchemaOption = 0x0000000008000001
SET @sPreCreationCmd = 'none'
IF (@i_sArticleType = 'procedure')
BEGIN
SET @sType = 'proc exec'
END
ELSE
BEGIN
SET @sType = @i_sArticleType + ' schema only'
END
END
-- Add the article
EXEC sp_addarticle @publication = @i_sPubName
, @article = @i_sArticleName
, @source_owner = N'dbo'
, @source_object = @i_sArticleName
, @destination_table = @i_sArticleName
, @type = @sType
, @description = null
, @pre_creation_cmd = @sPreCreationCmd
, @schema_option = @nSchemaOption
, @status = 16
, @vertical_partition = N'false'
, @filter = null
, @sync_object = null
END
ELSE --(@i_sPubType = 'Merge')
BEGIN
SET @sPreCreationCmd = 'DROP'
IF @i_sArticleType = 'table'
BEGIN
SET @nSchemaOption = 0x000000B20C034FD1
SET @sType = 'table'
END
ELSE
BEGIN
SET @nSchemaOption = 0x0000000008000001
SET @sType = @i_sArticleType + ' schema only'
END
-- Add the merge articles for the retreived function
EXEC sp_addmergearticle @publication = @i_sPubName
, @article = @i_sArticleName
, @source_owner = N'dbo'
, @source_object = @i_sArticleName
, @type = @sType
, @description = null
, @pre_creation_cmd = @sPreCreationCmd
, @creation_script = null
, @schema_option = @nSchemaOption
, @destination_owner = N'dbo'
, @force_reinit_subscription = 1
, @column_tracking = N'false'
, @vertical_partition = N'false'
, @verify_resolver_signature = 1
, @allow_interactive_resolver = N'false'
, @fast_multicol_updateproc = N'true'
, @subset_filterclause = null
, @destination_object = @i_sArticleName
, @check_permissions = 0
, @subscriber_upload_options = 0
, @force_invalidate_snapshot = 1
, @delete_tracking = N'true'
, @compensate_for_errors = N'false'
, @stream_blob_columns = N'false'
, @partition_options = 0
END
GO
PRINT N'Creating [dbo].[usp_REPL_Create_LogReader_Job]...';
GO
-- =================================================================================
-- Author: Donna-Marie Dimmer
-- Create Date: 27th August 2010
-- Description: This procedure Creates the Log Reader Job for a Transactional
-- Publication
-- =================================================================================
-- Calls: None.
-- Called By: usp_REPL_Setup_Replication.
-- =================================================================================
-- Update Author:Donna-Marie Dimmer
-- Update Date: 20th October 2011
-- Description: Renamed the procedure
-- =================================================================================
CREATE PROCEDURE [dbo].[usp_REPL_Create_LogReader_Job]
@i_sJobName sysname
, @i_sPublisher sysname
, @i_sDistributor sysname
AS
set nocount on
-- get replication details
declare @sReplUser varchar(100)
, @sSchedName varchar(100)
, @nJobID binary(16)
, @nReturnCode int
, @nAgentId int
, @sCommand nvarchar(max)
set @sReplUser = dbo.udf_Get_Parameter_Value('REPL_Admin_User')
set @sSchedName = 'Replication agent schedule - ' + @i_sJobName
set @nAgentId = ident_current('distribution..MSlogReader_agents') + 1
set @nReturnCode = 0
if @nAgentId is null
set @nAgentId = 1
-- create the REPL-Merge category if it doesn't exist
if not exists (SELECT 1
FROM msdb.dbo.syscategories
WHERE name = N'REPL-LogReader')
exec msdb.dbo.sp_add_category @name = N'REPL-LogReader'
-- Delete the job with the same name (if it exists)
SELECT @nJobID = job_id
FROM msdb.dbo.sysjobs
WHERE name = @i_sJobName
if @nJobID is not null begin
-- Delete the [local] job
exec msdb.dbo.sp_delete_job @job_name = @i_sJobName
select @nJobID = NULL
end
-- Add the job
exec @nReturnCode = msdb.dbo.sp_add_job @job_id = @nJobID OUTPUT
, @job_name = @i_sJobName
, @owner_login_name = @sReplUser
, @description = N'No description available.'
, @category_name = N'REPL-LogReader'
, @enabled = 1
, @notify_level_email = 0
, @notify_level_page = 0
, @notify_level_netsend = 0
, @notify_level_eventlog = 0
, @delete_level = 0
IF (@@ERROR <> 0 OR @nReturnCode <> 0) GOTO QuitWithRollback
-- Add the job steps
set @sCommand = 'sp_MSadd_LogReader_history @perfmon_increment = 0, ' +
'@agent_id = ' + convert(varchar, @nAgentId) + ', ' +
'@runstatus = 1, @comments = ''Starting agent.'''
exec @nReturnCode = msdb.dbo.sp_add_jobstep @job_id = @nJobID
, @step_id = 1
, @step_name = N'Distribution Agent startup message.'
, @command = @sCommand
, @database_name = N'distribution'
, @server = @i_sPublisher
, @subsystem = N'TSQL'
, @cmdexec_success_code = 0
, @flags = 0
, @retry_attempts = 0
, @retry_interval = 0
, @on_success_step_id = 0
, @on_success_action = 3
, @on_fail_step_id = 0
, @on_fail_action = 3
IF (@@ERROR <> 0 OR @nReturnCode <> 0) GOTO QuitWithRollback
set @sCommand = '-Publisher [' + @i_sPublisher + '] ' +
'-PublisherDB [$(DatabaseName)] ' +
'-Distributor [' + @i_sDistributor + '] ' +
'-DistributorSecurityMode 1 ' +
'-Continuous'
exec @nReturnCode = msdb.dbo.sp_add_jobstep @job_id = @nJobID
, @step_id = 2
, @step_name = N'Run agent.'
, @command = @sCommand
, @database_name = N'distribution'
, @server = @i_sPublisher
, @subsystem = N'LogReader'
, @cmdexec_success_code = 0
, @flags = 0
, @retry_attempts = 10
, @retry_interval = 1
, @on_success_step_id = 0
, @on_success_action = 1
, @on_fail_step_id = 0
, @on_fail_action = 3
IF (@@ERROR <> 0 OR @nReturnCode <> 0) GOTO QuitWithRollback
set @sCommand = 'sp_MSdetect_nonlogged_shutdown @subsystem = ''LogReader'', ' +
'@agent_id = ' + convert(varchar, @nAgentId)
exec @nReturnCode = msdb.dbo.sp_add_jobstep @job_id = @nJobID
, @step_id = 3
, @step_name = N'Detect nonlogged agent shutdown.'
, @command = @sCommand
, @database_name = N'distribution'
, @server = @i_sPublisher
, @subsystem = N'TSQL'
, @cmdexec_success_code = 0
, @flags = 0
, @retry_attempts = 0
, @retry_interval = 0
, @on_success_step_id = 0
, @on_success_action = 2
, @on_fail_step_id = 0
, @on_fail_action = 2
IF (@@ERROR <> 0 OR @nReturnCode <> 0) GOTO QuitWithRollback
exec @nReturnCode = msdb.dbo.sp_update_job @job_id = @nJobID
, @start_step_id = 1
IF (@@ERROR <> 0 OR @nReturnCode <> 0) GOTO QuitWithRollback
-- Add the job schedules (job is to run every 5 minutes!)
EXECUTE @nReturnCode = msdb.dbo.sp_add_schedule @schedule_name = @sSchedName
, @enabled = 1
, @freq_type = 64
, @active_start_date = 20000101
, @active_start_time = 0
, @freq_interval = 0
, @freq_subday_type = 0
, @freq_subday_interval = 0
, @freq_relative_interval = 0
, @freq_recurrence_factor = 0
, @active_end_date = 99991231
, @active_end_time = 235900
IF (@@ERROR <> 0 OR @nReturnCode <> 0) GOTO QuitWithRollback
exec msdb.dbo.sp_attach_schedule @job_name = @i_sJobName,
@schedule_name = @sSchedName
-- Add the Target Servers
EXECUTE @nReturnCode = msdb.dbo.sp_add_jobserver @job_id = @nJobID
, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @nReturnCode <> 0) GOTO QuitWithRollback
GOTO EndSave
QuitWithRollback:
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
GO
PRINT N'Creating [dbo].[usp_REPL_Create_QueueReader_Job]...';
GO
-- =================================================================================
-- Author: Donna-Marie Dimmer
-- Create Date: 27th August 2010
-- Description: This procedure Creates the Queue Reader Job for a Transactional
-- Publication
-- =================================================================================
-- Calls: none
-- Called By: usp_REPL_Setup_Replication.
-- =================================================================================
-- Update Author:Donna-Marie Dimmer
-- Update Date: 20th October 2011
-- Description: Renamed the procedure
-- =================================================================================
CREATE PROCEDURE [dbo].[usp_REPL_Create_QueueReader_Job]
@i_sJobName sysname
, @i_sPublisher sysname
, @i_sDistributor sysname
AS
set nocount on
-- get replication details
declare @sReplUser varchar(100)
, @sSchedName varchar(100)
, @nAgentId int
, @nJobID binary(16)
, @nReturnCode int
, @sCommand nvarchar(max)
set @sReplUser = dbo.udf_Get_Parameter_Value('REPL_Admin_User')
set @sSchedName = 'Replication agent schedule - ' + @i_sJobName
set @nAgentId = ident_current('distribution..MSqReader_agents') + 1
set @nReturnCode = 0
if @nAgentId is null
set @nAgentId = 1
-- create the REPL-Merge category if it doesn't exist
if not exists (SELECT 1
FROM msdb.dbo.syscategories
WHERE name = N'REPL-QueueReader')
exec msdb.dbo.sp_add_category @name = N'REPL-QueueReader'
-- Delete the job with the same name (if it exists)
SELECT @nJobID = job_id
FROM msdb.dbo.sysjobs
WHERE name = @i_sJobName
if @nJobID is not null
begin
-- Delete the [local] job
exec msdb.dbo.sp_delete_job @job_name = @i_sJobName
select @nJobID = NULL
end
-- Add the job
exec @nReturnCode = msdb.dbo.sp_add_job @job_id = @nJobID OUTPUT
, @job_name = @i_sJobName
, @owner_login_name = @sReplUser
, @description = N'Reads queues for Queued updating subscriptions.'
, @category_name = N'REPL-QueueReader'
, @enabled = 1
, @notify_level_email = 0
, @notify_level_page = 0
, @notify_level_netsend = 0
, @notify_level_eventlog = 0
, @delete_level = 0
IF (@@ERROR <> 0 OR @nReturnCode <> 0) GOTO QuitWithRollback
-- Add the job steps
set @sCommand = 'sp_MSadd_qReader_history @perfmon_increment = 0, ' +
'@agent_id = ' + convert(varchar, @nAgentId) + ', ' +
'@runstatus = 1, @comments = ''Starting agent.'''
exec @nReturnCode = msdb.dbo.sp_add_jobstep @job_id = @nJobID
, @step_id = 1
, @step_name = N'Distribution Agent startup message.'
, @command = @sCommand
, @database_name = N'distribution'
, @server = @i_sPublisher
, @subsystem = N'TSQL'
, @cmdexec_success_code = 0