Skip to content

Commit

Permalink
Feat: ORV2-1865 Add Feature Flag DB Migration (#1111)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikataot authored Jan 24, 2024
1 parent ec6325a commit 1747ee2
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 0 deletions.
30 changes: 30 additions & 0 deletions database/mssql/scripts/versions/revert/sample-ddl-revert.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- This is a typical revert script
-- Each migration script in the versions folder should also include a revert script

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET NOCOUNT ON
GO

SET XACT_ABORT ON

-- Sample of rolling back a Table creation wrapped in a TRANSACTION
--BEGIN TRY
-- BEGIN TRANSACTION
-- DROP TABLE [dbo].[ORBC_NEW_TABLE]
-- COMMIT
--END TRY

--BEGIN CATCH
-- IF @@TRANCOUNT > 0
-- ROLLBACK;
-- THROW
--END CATCH

-- Also remember to rollback the version number from your migration!
DECLARE @VersionDescription VARCHAR(255)
SET @VersionDescription = '*** Enter description of DB change here ***'

INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [RELEASE_DATE]) VALUES (/*<<REPLACE VERSION NUMBER HERE>>*/, @VersionDescription, getutcdate())
25 changes: 25 additions & 0 deletions database/mssql/scripts/versions/revert/v_14_ddl_revert.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET NOCOUNT ON
GO

SET XACT_ABORT ON

BEGIN TRY
BEGIN TRANSACTION
DROP TABLE [dbo].[ORBC_FEATURE_FLAG]
COMMIT
END TRY

BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK;
THROW
END CATCH

DECLARE @VersionDescription VARCHAR(255)
SET @VersionDescription = 'Reverting initial creation of entities for feature flags.'

INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [RELEASE_DATE]) VALUES (13, @VersionDescription, getutcdate())
43 changes: 43 additions & 0 deletions database/mssql/scripts/versions/sample-ddl.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,53 @@
-- This is a typical DDL migration script
-- Note that a revert script is ALSO needed, so be sure to check out
-- revert/sample-ddl.sql

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET NOCOUNT ON
GO

-- Here is what a typical create table looks like
-- Note that the APP_CREATE_*, APP_LAST_*, CONCURRENCY and DB_CREATE / DB_LAST columns
-- are all mandatory along with the columns you're creating
-- CREATE TABLE [dbo].[ORBC_NEW_TABLE](
-- [NEW_TABLE_ID] [int] IDENTITY(1,1) NOT NULL,
-- [NEW_TABLE_COL_1] [nvarchar](50) NOT NULL,
-- [NEW_TABLE_COL_2] [nvarchar](50) NOT NULL,
-- [APP_CREATE_TIMESTAMP] [datetime2](7) DEFAULT (getutcdate()),
-- [APP_CREATE_USERID] [nvarchar](30) DEFAULT (user_name()),
-- [APP_CREATE_USER_GUID] [char](32) NULL,
-- [APP_CREATE_USER_DIRECTORY] [nvarchar](30) DEFAULT (user_name()),
-- [APP_LAST_UPDATE_TIMESTAMP] [datetime2](7) DEFAULT (getutcdate()),
-- [APP_LAST_UPDATE_USERID] [nvarchar](30) DEFAULT (user_name()),
-- [APP_LAST_UPDATE_USER_GUID] [char](32) NULL,
-- [APP_LAST_UPDATE_USER_DIRECTORY] [nvarchar](30) DEFAULT (user_name()),
-- [CONCURRENCY_CONTROL_NUMBER] [int] NULL,
-- [DB_CREATE_USERID] [varchar](63) NULL,
-- [DB_CREATE_TIMESTAMP] [datetime2](7) NULL,
-- [DB_LAST_UPDATE_USERID] [varchar](63) NULL,
-- [DB_LAST_UPDATE_TIMESTAMP] [datetime2](7) NULL,
-- CONSTRAINT [ORBC_FEATURE_FLAG_PK] PRIMARY KEY CLUSTERED
--(
-- [NEW_TABLE_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]
--GO

-- Include extended descriptions of each new column created
-- along with the descriptions of the standard columns
--EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Unique auto-generated surrogate primary key' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'ORBC_NEW_TABLE', @level2type=N'COLUMN',@level2name=N'NEW_TABLE_ID'
--EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'The column1 description' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1Name=N'ORBC_NEW_TABLE', @level2type=N'COLUMN',@level2name=N'NEW_TABLE_COL_1'
--EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'The column2 description' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1Name=N'ORBC_NEW_TABLE', @level2type=N'COLUMN',@level2name=N'NEW_TABLE_COL_2'
--EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Application code is responsible for retrieving the row and then incrementing the value of the CONCURRENCY_CONTROL_NUMBER column by one prior to issuing an update. If this is done then the update will succeed, provided that the row was not updated by any other transactions in the period between the read and the update operations.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1Name=N'ORBC_FEATURE_FLAG', @level2type=N'COLUMN',@level2name=N'CONCURRENCY_CONTROL_NUMBER'
--EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'The user or proxy account that created or last updated the record.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1Name=N'ORBC_NEW_TABLE', @level2type=N'COLUMN',@level2name=N'DB_LAST_UPDATE_USERID'
--EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'The date and time the record was created.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1Name=N'ORBC_NEW_TABLE', @level2type=N'COLUMN',@level2name=N'DB_CREATE_TIMESTAMP'
--EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'The user or proxy account that created the record.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1Name=N'ORBC_NEW_TABLE', @level2type=N'COLUMN',@level2name=N'DB_CREATE_USERID'
--EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'The date and time the record was created or last updated.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1Name=N'ORBC_NEW_TABLE', @level2type=N'COLUMN',@level2name=N'DB_LAST_UPDATE_TIMESTAMP'
--GO

-- Include all SQL to upgrade database version here.
-- Include updates to lookup table data that should go into prod, but do not include any test data.
-- Replace the version description below to describe the change(s) made
Expand Down
45 changes: 45 additions & 0 deletions database/mssql/scripts/versions/v_14_ddl.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET NOCOUNT ON
GO

CREATE TABLE [dbo].[ORBC_FEATURE_FLAG](
[FEATURE_ID] [int] IDENTITY(1,1) NOT NULL,
[FEATURE_KEY] [nvarchar](50) NOT NULL,
[FEATURE_VALUE] [nvarchar](50) NOT NULL CHECK (FEATURE_VALUE IN ('ENABLED','DISABLED')),
[APP_CREATE_TIMESTAMP] [datetime2](7) DEFAULT (getutcdate()),
[APP_CREATE_USERID] [nvarchar](30) DEFAULT (user_name()),
[APP_CREATE_USER_GUID] [char](32) NULL,
[APP_CREATE_USER_DIRECTORY] [nvarchar](30) DEFAULT (user_name()),
[APP_LAST_UPDATE_TIMESTAMP] [datetime2](7) DEFAULT (getutcdate()),
[APP_LAST_UPDATE_USERID] [nvarchar](30) DEFAULT (user_name()),
[APP_LAST_UPDATE_USER_GUID] [char](32) NULL,
[APP_LAST_UPDATE_USER_DIRECTORY] [nvarchar](30) DEFAULT (user_name()),
[CONCURRENCY_CONTROL_NUMBER] [int] NULL,
[DB_CREATE_USERID] [varchar](63) NULL,
[DB_CREATE_TIMESTAMP] [datetime2](7) NULL,
[DB_LAST_UPDATE_USERID] [varchar](63) NULL,
[DB_LAST_UPDATE_TIMESTAMP] [datetime2](7) NULL,
CONSTRAINT [ORBC_FEATURE_FLAG_PK] PRIMARY KEY CLUSTERED
(
[FEATURE_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]
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Unique auto-generated surrogate primary key' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'ORBC_FEATURE_FLAG', @level2type=N'COLUMN',@level2name=N'FEATURE_ID'
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'The feature flag' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1Name=N'ORBC_FEATURE_FLAG', @level2type=N'COLUMN',@level2name=N'FEATURE_KEY'
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'The feature value - ENABLED/DISABLED.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1Name=N'ORBC_FEATURE_FLAG', @level2type=N'COLUMN',@level2name=N'FEATURE_VALUE'
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Application code is responsible for retrieving the row and then incrementing the value of the CONCURRENCY_CONTROL_NUMBER column by one prior to issuing an update. If this is done then the update will succeed, provided that the row was not updated by any other transactions in the period between the read and the update operations.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1Name=N'ORBC_FEATURE_FLAG', @level2type=N'COLUMN',@level2name=N'CONCURRENCY_CONTROL_NUMBER'
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'The user or proxy account that created or last updated the record.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1Name=N'ORBC_FEATURE_FLAG', @level2type=N'COLUMN',@level2name=N'DB_LAST_UPDATE_USERID'
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'The date and time the record was created.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1Name=N'ORBC_FEATURE_FLAG', @level2type=N'COLUMN',@level2name=N'DB_CREATE_TIMESTAMP'
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'The user or proxy account that created the record.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1Name=N'ORBC_FEATURE_FLAG', @level2type=N'COLUMN',@level2name=N'DB_CREATE_USERID'
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'The date and time the record was created or last updated.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1Name=N'ORBC_FEATURE_FLAG', @level2type=N'COLUMN',@level2name=N'DB_LAST_UPDATE_TIMESTAMP'
GO

DECLARE @VersionDescription VARCHAR(255)
SET @VersionDescription = 'Initial creation of entities for feature flag'

INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [UPDATE_SCRIPT], [REVERT_SCRIPT], [RELEASE_DATE]) VALUES (14, @VersionDescription, '$(UPDATE_SCRIPT)', '$(REVERT_SCRIPT)', getutcdate())
5 changes: 5 additions & 0 deletions database/mssql/test/versions/v_14_1_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SET NOCOUNT ON
IF OBJECT_ID('[$(DB_NAME)].[dbo].[ORBC_FEATURE_FLAG]', 'U') IS NOT NULL
SELECT 1
ELSE
SELECT 0
18 changes: 18 additions & 0 deletions database/mssql/test/versions/v_14_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

# Retrieve arguments
source ${SCRIPT_DIR}/utility/getopt.sh
USAGE="-u USER -p PASS -s SERVER -d DATABASE"
parse_options "${USAGE}" ${@}

# All database tests for database version 14 are run from this shell script.
# TESTS_DIR variable set by the calling test-runner script.

# Test 14.1 - verify that the tps migration tables exist
TEST_14_1_RESULT=$(/opt/mssql-tools/bin/sqlcmd -U ${USER} -P "${PASS}" -S ${SERVER} -v DB_NAME=${DATABASE} -h -1 -i ${TESTS_DIR}/v_14_1_test.sql)

if [[ $TEST_14_1_RESULT -eq 1 ]]; then
echo "Test 14.1 passed: All feature flag tables exist"
else
echo "******** Test 14.1 failed: Missing one or more feature flag tables"
fi

0 comments on commit 1747ee2

Please sign in to comment.