-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Execute base schema script via tool (#76)
* Code changes * Adds fhir/dicom script separately * Merge master * Updated sqlServer dependency * Code refactoring * Removes base command * Adds baseSchema to current command * Removed base command name * Addresses feedback * Fixes the available version retry logic * Avoid re-executing baseSchema
- Loading branch information
Showing
12 changed files
with
361 additions
and
585 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
276 changes: 1 addition & 275 deletions
276
test/Microsoft.Health.SqlServer.Web/Features/Schema/Migrations/1.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,285 +1,11 @@ | ||
-- NOTE: This script DROPS AND RECREATES all database objects. | ||
-- Style guide: please see: https://github.com/ktaranov/sqlserver-kit/blob/master/SQL%20Server%20Name%20Convention%20and%20T-SQL%20Programming%20Style.md | ||
|
||
|
||
/************************************************************* | ||
Drop existing objects | ||
**************************************************************/ | ||
|
||
DECLARE @sql nvarchar(max) ='' | ||
|
||
SELECT @sql = @sql + 'DROP PROCEDURE ' + name + '; ' | ||
FROM sys.procedures | ||
|
||
SELECT @sql = @sql + 'DROP TABLE ' + name + '; ' | ||
FROM sys.tables | ||
|
||
SELECT @sql = @sql + 'DROP TYPE ' + name + '; ' | ||
FROM sys.table_types | ||
|
||
SELECT @sql = @sql + 'DROP SEQUENCE ' + name + '; ' | ||
FROM sys.sequences | ||
|
||
EXEC(@sql) | ||
|
||
GO | ||
-- Style guide: please see: https://github.com/ktaranov/sqlserver-kit/blob/master/SQL%20Server%20Name%20Convention%20and%20T-SQL%20Programming%20Style.md | ||
|
||
/************************************************************* | ||
Configure database | ||
**************************************************************/ | ||
|
||
-- Enable RCSI | ||
IF ((SELECT is_read_committed_snapshot_on FROM sys.databases WHERE database_id = DB_ID()) = 0) BEGIN | ||
ALTER DATABASE CURRENT SET READ_COMMITTED_SNAPSHOT ON | ||
END | ||
|
||
-- Avoid blocking queries when statistics need to be rebuilt | ||
IF ((SELECT is_auto_update_stats_async_on FROM sys.databases WHERE database_id = DB_ID()) = 0) BEGIN | ||
ALTER DATABASE CURRENT SET AUTO_UPDATE_STATISTICS_ASYNC ON | ||
END | ||
|
||
-- Use ANSI behavior for null values | ||
IF ((SELECT is_ansi_nulls_on FROM sys.databases WHERE database_id = DB_ID()) = 0) BEGIN | ||
ALTER DATABASE CURRENT SET ANSI_NULLS ON | ||
END | ||
|
||
GO | ||
|
||
/************************************************************* | ||
Schema bootstrap | ||
**************************************************************/ | ||
/************************************************************* | ||
Schema Version | ||
**************************************************************/ | ||
CREATE TABLE dbo.SchemaVersion | ||
( | ||
Version int PRIMARY KEY, | ||
Status varchar(10) | ||
) | ||
|
||
INSERT INTO dbo.SchemaVersion | ||
VALUES | ||
(1, 'started') | ||
|
||
GO | ||
|
||
-- | ||
-- STORED PROCEDURE | ||
-- UpsertSchemaVersion | ||
-- | ||
-- DESCRIPTION | ||
-- Creates or updates a new schema version entry | ||
-- | ||
-- PARAMETERS | ||
-- @version | ||
-- * The version number | ||
-- @status | ||
-- * The status of the version | ||
-- | ||
CREATE PROCEDURE dbo.UpsertSchemaVersion | ||
@version int, | ||
@status varchar(10) | ||
AS | ||
SET NOCOUNT ON | ||
|
||
IF EXISTS(SELECT * | ||
FROM dbo.SchemaVersion | ||
WHERE Version = @version) | ||
BEGIN | ||
UPDATE dbo.SchemaVersion | ||
SET Status = @status | ||
WHERE Version = @version | ||
END | ||
ELSE | ||
BEGIN | ||
INSERT INTO dbo.SchemaVersion | ||
(Version, Status) | ||
VALUES | ||
(@version, @status) | ||
END | ||
GO | ||
|
||
/************************************************************* | ||
Instance Schema | ||
**************************************************************/ | ||
CREATE TABLE dbo.InstanceSchema | ||
( | ||
Name varchar(64) COLLATE Latin1_General_100_CS_AS NOT NULL, | ||
CurrentVersion int NOT NULL, | ||
MaxVersion int NOT NULL, | ||
MinVersion int NOT NULL, | ||
Timeout datetime2(0) NOT NULL | ||
) | ||
|
||
CREATE UNIQUE CLUSTERED INDEX IXC_InstanceSchema | ||
ON dbo.InstanceSchema | ||
( | ||
Name | ||
) | ||
|
||
CREATE NONCLUSTERED INDEX IX_InstanceSchema_Timeout | ||
ON dbo.InstanceSchema | ||
( | ||
Timeout | ||
) | ||
|
||
GO | ||
|
||
-- | ||
-- STORED PROCEDURE | ||
-- Gets schema information given its instance name. | ||
-- | ||
-- DESCRIPTION | ||
-- Retrieves the instance schema record from the InstanceSchema table that has the matching name. | ||
-- | ||
-- PARAMETERS | ||
-- @name | ||
-- * The unique name for a particular instance | ||
-- | ||
-- RETURN VALUE | ||
-- The matching record. | ||
-- | ||
CREATE PROCEDURE dbo.GetInstanceSchemaByName | ||
@name varchar(64) | ||
AS | ||
SET NOCOUNT ON | ||
|
||
SELECT CurrentVersion, MaxVersion, MinVersion, Timeout | ||
FROM dbo.InstanceSchema | ||
WHERE Name = @name | ||
GO | ||
|
||
-- | ||
-- STORED PROCEDURE | ||
-- Update an instance schema. | ||
-- | ||
-- DESCRIPTION | ||
-- Modifies an existing record in the InstanceSchema table. | ||
-- | ||
-- PARAMETERS | ||
-- @name | ||
-- * The unique name for a particular instance | ||
-- @maxVersion | ||
-- * The maximum supported schema version for the given instance | ||
-- @minVersion | ||
-- * The minimum supported schema version for the given instance | ||
-- @addMinutesOnTimeout | ||
-- * The minutes to add | ||
-- | ||
CREATE PROCEDURE dbo.UpsertInstanceSchema | ||
@name varchar(64), | ||
@maxVersion int, | ||
@minVersion int, | ||
@addMinutesOnTimeout int | ||
|
||
AS | ||
SET NOCOUNT ON | ||
|
||
DECLARE @timeout datetime2(0) = DATEADD(minute, @addMinutesOnTimeout, SYSUTCDATETIME()) | ||
DECLARE @currentVersion int = (SELECT COALESCE(MAX(Version), 0) | ||
FROM dbo.SchemaVersion | ||
WHERE Status = 'completed' OR Status = 'complete' AND Version <= @maxVersion) | ||
IF EXISTS(SELECT * | ||
FROM dbo.InstanceSchema | ||
WHERE Name = @name) | ||
BEGIN | ||
UPDATE dbo.InstanceSchema | ||
SET CurrentVersion = @currentVersion, MaxVersion = @maxVersion, Timeout = @timeout | ||
WHERE Name = @name | ||
|
||
SELECT @currentVersion | ||
END | ||
ELSE | ||
BEGIN | ||
INSERT INTO dbo.InstanceSchema | ||
(Name, CurrentVersion, MaxVersion, MinVersion, Timeout) | ||
VALUES | ||
(@name, @currentVersion, @maxVersion, @minVersion, @timeout) | ||
|
||
SELECT @currentVersion | ||
END | ||
GO | ||
|
||
-- | ||
-- STORED PROCEDURE | ||
-- Delete instance schema information. | ||
-- | ||
-- DESCRIPTION | ||
-- Delete all the expired records in the InstanceSchema table. | ||
-- | ||
CREATE PROCEDURE dbo.DeleteInstanceSchema | ||
|
||
AS | ||
SET NOCOUNT ON | ||
|
||
DELETE FROM dbo.InstanceSchema | ||
WHERE Timeout < SYSUTCDATETIME() | ||
|
||
GO | ||
|
||
-- | ||
-- STORED PROCEDURE | ||
-- SelectCompatibleSchemaVersions | ||
-- | ||
-- DESCRIPTION | ||
-- Selects the compatible schema versions | ||
-- | ||
-- RETURNS | ||
-- The maximum and minimum compatible versions | ||
-- | ||
CREATE PROCEDURE dbo.SelectCompatibleSchemaVersions | ||
|
||
AS | ||
BEGIN | ||
SET NOCOUNT ON | ||
|
||
SELECT MAX(MinVersion), MIN(MaxVersion) | ||
FROM dbo.InstanceSchema | ||
WHERE Timeout > SYSUTCDATETIME() | ||
END | ||
GO | ||
|
||
-- | ||
-- STORED PROCEDURE | ||
-- SelectCurrentVersionsInformation | ||
-- | ||
-- DESCRIPTION | ||
-- Selects the current schema versions information | ||
-- | ||
-- RETURNS | ||
-- The current versions, status and server names using that version | ||
-- | ||
CREATE PROCEDURE dbo.SelectCurrentVersionsInformation | ||
|
||
AS | ||
BEGIN | ||
SET NOCOUNT ON | ||
|
||
SELECT SV.Version, SV.Status, STRING_AGG(SCH.NAME, ',') | ||
FROM dbo.SchemaVersion AS SV LEFT OUTER JOIN dbo.InstanceSchema AS SCH | ||
ON SV.Version = SCH.CurrentVersion | ||
GROUP BY Version, Status | ||
|
||
END | ||
GO | ||
|
||
-- | ||
-- STORED PROCEDURE | ||
-- SelectCurrentSchemaVersion | ||
-- | ||
-- DESCRIPTION | ||
-- Selects the current completed schema version | ||
-- | ||
-- RETURNS | ||
-- The current version as a result set | ||
-- | ||
CREATE PROCEDURE dbo.SelectCurrentSchemaVersion | ||
AS | ||
BEGIN | ||
SET NOCOUNT ON | ||
|
||
SELECT MAX(Version) | ||
FROM SchemaVersion | ||
WHERE Status = 'completed' | ||
END | ||
GO |
Oops, something went wrong.