Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25036d3
commit 7abde20
Showing
7 changed files
with
195 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
SqlWatch.Monitor/Project.SqlWatch.Database/Scripts/Pre-Deployment/SetDacVersion.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
-- Generated by MsBuild Step - DO NOT EDIT | ||
:setvar DacVersion " | ||
3.1.0.21269 | ||
3.1.0.23812 | ||
" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
SqlWatch.Monitor/Project.SqlWatch.Database/dbo/Tables/sqlwatch_config_baseline.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
CREATE TABLE [dbo].[sqlwatch_config_baseline] | ||
( | ||
baseline_id smallint not null identity(1,1), | ||
baseline_start datetime2(0) not null, | ||
baseline_end datetime2(0) not null, | ||
[is_default] bit not null, | ||
[comments] varchar(max) null, | ||
|
||
constraint pk_sqlwatch_config_baseline | ||
primary key clustered (baseline_id) | ||
|
||
) | ||
go | ||
|
||
CREATE UNIQUE INDEX idx_sqlwatch_config_baseline_default | ||
ON [dbo].[sqlwatch_config_baseline] ([is_default]) | ||
WHERE [is_default] = 1 | ||
GO | ||
|
||
CREATE UNIQUE INDEX idx_sqlwatch_config_baseline_dates | ||
ON [dbo].[sqlwatch_config_baseline] (baseline_start, baseline_end) | ||
GO | ||
|
||
create trigger trg_sqlwatch_config_baseline_meta_add | ||
on [dbo].[sqlwatch_config_baseline] | ||
for insert | ||
as | ||
begin | ||
declare @sql_instance varchar(32) = dbo.ufn_sqlwatch_get_servername(); | ||
|
||
insert into [dbo].[sqlwatch_meta_baseline] (sql_instance, baseline_id, baseline_start, baseline_end, [is_default], [comments], [date_updated]) | ||
select @sql_instance | ||
, inserted.baseline_id | ||
, inserted.baseline_start | ||
, inserted.baseline_end | ||
, inserted.is_default | ||
, inserted.comments | ||
, GETUTCDATE() | ||
from inserted | ||
|
||
--SQL Server does not support per-row triggers but we have to iterate through every inserted row here to load header based on baseline dates. | ||
--Whilst cusors in triggers are genearally bad approach, this will not be run very often as the baselines should never change often. | ||
|
||
declare @baseline_start datetime2(0), | ||
@baseline_end datetime2(0), | ||
@baseline_id smallint | ||
|
||
declare cur_insert cursor for | ||
|
||
select baseline_id, baseline_start, baseline_end | ||
from inserted | ||
|
||
open cur_insert | ||
fetch next from cur_insert | ||
into @baseline_id, @baseline_start, @baseline_end | ||
|
||
while @@FETCH_STATUS = 0 | ||
begin | ||
|
||
insert into [dbo].[sqlwatch_meta_snapshot_header_baseline] with (tablock) ( | ||
baseline_id | ||
, snapshot_time | ||
, sql_instance | ||
) | ||
select distinct @baseline_id, snapshot_time, @sql_instance | ||
from dbo.sqlwatch_logger_snapshot_header h | ||
where sql_instance = @sql_instance | ||
and snapshot_time between @baseline_start and @baseline_end | ||
|
||
fetch next from cur_insert | ||
into @baseline_id, @baseline_start, @baseline_end | ||
end | ||
|
||
close cur_insert | ||
deallocate cur_insert | ||
end | ||
go | ||
|
||
create trigger trg_sqlwatch_config_baseline_meta_remove | ||
on [dbo].[sqlwatch_config_baseline] | ||
for delete | ||
as | ||
begin | ||
declare @sql_instance varchar(32) = dbo.ufn_sqlwatch_get_servername(); | ||
|
||
delete m | ||
from [dbo].[sqlwatch_meta_baseline] m | ||
inner join deleted d | ||
on m.baseline_id = d.baseline_id | ||
and m.sql_instance = @sql_instance | ||
|
||
end | ||
go | ||
|
||
create trigger trg_sqlwatch_config_baseline_meta_update | ||
on [dbo].[sqlwatch_config_baseline] | ||
instead of update | ||
as | ||
begin | ||
|
||
--updating baseline dates will not be supported, only the default flag and comments | ||
--to update baseline dates we need to remove and recreate the baseline so we can ring fence new data set | ||
|
||
if exists ( | ||
select * | ||
from inserted i | ||
inner join deleted d | ||
on d.baseline_id = i.baseline_id | ||
and ( | ||
d.baseline_start <> i.baseline_start | ||
or d.baseline_end <> i.baseline_end | ||
) | ||
) | ||
begin | ||
raiserror('Changes to the baseline dates are not allowed. To modify baseline dates create new baseline and delete the old one.', 16,1); | ||
end | ||
else | ||
begin | ||
declare @sql_instance varchar(32) = dbo.ufn_sqlwatch_get_servername(); | ||
|
||
update m | ||
set is_default = i.is_default, | ||
comments = i.comments | ||
|
||
from [dbo].[sqlwatch_meta_baseline] m | ||
inner join inserted i | ||
on m.baseline_id = i.baseline_id | ||
and m.sql_instance = @sql_instance | ||
end | ||
end | ||
go |
27 changes: 27 additions & 0 deletions
27
SqlWatch.Monitor/Project.SqlWatch.Database/dbo/Tables/sqlwatch_meta_baseline.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
CREATE TABLE [dbo].[sqlwatch_meta_baseline] | ||
( | ||
[baseline_id] smallint not null, | ||
sql_instance varchar(32) not null, | ||
baseline_start datetime2(0) not null, | ||
baseline_end datetime2(0) not null, | ||
[is_default] bit not null, | ||
[comments] varchar(max) null, | ||
[date_updated] datetime not null, | ||
|
||
constraint pk_sqlwatch_meta_baseline primary key clustered ( | ||
[baseline_id], sql_instance | ||
), | ||
|
||
constraint fk_sqlwatch_meta_retention_sql_instance foreign key (sql_instance) | ||
references dbo.sqlwatch_config_sql_instance ([sql_instance]) on delete cascade | ||
) | ||
GO | ||
|
||
CREATE UNIQUE INDEX idx_sqlwatch_meta_retention_default | ||
ON [dbo].[sqlwatch_meta_baseline] (sql_instance, [is_default]) | ||
WHERE [is_default] = 1 | ||
GO | ||
|
||
CREATE UNIQUE INDEX idx_sqlwatch_meta_baseline_dates | ||
ON [dbo].[sqlwatch_meta_baseline] (baseline_start, baseline_end, sql_instance) | ||
GO |
18 changes: 18 additions & 0 deletions
18
...h.Monitor/Project.SqlWatch.Database/dbo/Tables/sqlwatch_meta_snapshot_header_baseline.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
CREATE TABLE [dbo].[sqlwatch_meta_snapshot_header_baseline] | ||
( | ||
[baseline_id] smallint not null, | ||
[snapshot_time] datetime2(0) not null, | ||
[sql_instance] varchar(32) not null, | ||
|
||
constraint pk_sqlwatch_meta_snapshot_header_baseline | ||
primary key clustered ([baseline_id], [snapshot_time], [sql_instance]), | ||
|
||
constraint fk_sqlwatch_meta_snapshot_header_baseline_meta | ||
foreign key ([baseline_id], [sql_instance]) | ||
references dbo.sqlwatch_meta_baseline ([baseline_id], [sql_instance]) on delete cascade | ||
) | ||
go | ||
|
||
create index idx_sqlwatch_meta_snapshot_header_baseline_1 | ||
on [dbo].[sqlwatch_meta_snapshot_header_baseline] ([snapshot_time]) | ||
go |