USE Arizona

IF OBJECT_ID('upd.IA_IndexAlignementActions') IS NOT NULL 
    DROP TABLE upd.IA_IndexAlignementActions;
GO

IF OBJECT_ID('upd.IA_IndexAlignementActions') IS NOT NULL 
    DROP TABLE upd.IA_IndexAlignementActions;
GO

/* OCTPDBA-414 */
IF OBJECT_ID('upd.IA_IndexAlignementActions') IS NULL 
BEGIN
    CREATE TABLE upd.IA_IndexAlignementActions(
        IA_indexAlignementActionsID INT NOT NULL IDENTITY(1,1)
        ,IA_executionDate DATETIME2(0) NULL                     /* The date and time the action was executed, null if the action is to be executed */
        ,IA_schemaName VARCHAR(100) NOT NULL                    /* the schema the table is part of */
        ,IA_tableName VARCHAR(100) NOT NULL                     /* On which table is the index, whithout the schema */
        ,IA_columnsName VARCHAR(MAX) NOT NULL                   /* list all the columns of the index, in the correct orders */
        ,IA_isClustered BIT NOT NULL                            /* Is the indexe a clustered index ? */
            CONSTRAINT DF_IA_isClustered DEFAULT 0
        ,IA_isUnique BIT NOT NULL                               /* Is the index unique ? */
            CONSTRAINT DF_IA_isUnique DEFAULT 0
        ,IA_includedColumns VARCHAR(MAX)NULL                    /* list all the included columns the index should have. */
        ,IA_indexOptions VARCHAR(MAX) NULL                      /* Give here the index options if any. they will be applied on index creation but they are not part of the delta checks */
        ,IA_isToBeDeleted BIT NOT NULL                          /* must the script drop this index ? */
            CONSTRAINT DF_IA_isToBeDeleted DEFAULT 0
        ,IA_isToBeRecreated BIT NOT NULL                        /* must the script recreate this index ? */
            CONSTRAINT DF_IA_isToBeRecreated DEFAULT 0          /* If an existing index differs from the definition, it will be dropped and the correct one created. */
                                                                /* Use this only to force the re-creation even if the structure is identical  */
        ,IA_expectedIndexName VARCHAR(500) NOT NULL             /* the name the index should have, used to determine if a rename of the existing index should be done */
        ,IA_reference VARCHAR(500) NULL                         /* A free text that can contain a reference. Seen to simplify update scripts */

        /* everything below is a log of past changes */
        ,[IA_foundIndexName] VARCHAR(MAX) NULL                  /* computed by the script. The index name on this system, as indexes name can be different accross systems*/
        ,[IA_wasIndexFound] BIT NOT NULL                        /* computed by the script. set to 1 if the index is present on the server */
            CONSTRAINT DF_IA_wasIndexFound DEFAULT 0
        ,[IA_wasCreated] BIT NOT NULL                           /* computed by the script. does this index was created on the server ? */
            CONSTRAINT DF_IA_wasCreated DEFAULT 0
        ,[IA_wasRenamed] BIT NOT NULL                           /* computed by the script. does this index was renamed ? */
            CONSTRAINT DF_IA_wasRenamed DEFAULT 0
        ,[IA_wasDeleted] BIT NOT NULL                           /* computed by the script. does this index will be deleted ? */
            CONSTRAINT DF_IA_wasDeleted DEFAULT 0
        ,[IA_wasRecreated] BIT NOT NULL                         /* computed by the script. does this index will be re-created ? */
            CONSTRAINT DF_IA_wasRecreated DEFAULT 0
        ,[IA_includedColumnsMatch] BIT NULL                     /* computed by the script. Are the expected and actual included columns identical ? */
        ,[IA_actualColumns] VARCHAR(MAX) NULL                   /* the actual columns in the existing index */
        ,[IA_actualIncludedColumns] VARCHAR(MAX) NULL           /* the actual columns in the INCLUDE part of the index */
        ,[IA_statusMsg] VARCHAR(MAX) NULL                       /* Message, used for debug */
        ,CONSTRAINT PK_IA_IndexAlignementActions PRIMARY KEY CLUSTERED(IA_indexAlignementActionsID)
    );
END 

IF NOT EXISTS (
    SELECT 1
    FROM [sys].all_columns c
        JOIN [sys].tables t ON c.object_id = t.object_id
        JOIN [sys].schemas s ON t.schema_id = s.schema_id
        JOIN [sys].default_constraints dc ON c.default_object_id = dc.object_id
    WHERE s.name = 'upd'
        AND t.name = 'IA_IndexAlignementActions'
        AND c.name = 'IA_isClustered'    
)
BEGIN
    ALTER TABLE upd.IA_IndexAlignementActions ADD CONSTRAINT DF_IA_isClustered DEFAULT 0 FOR [IA_isClustered];
END; 

IF NOT EXISTS (
    SELECT 1
    FROM [sys].all_columns c
        JOIN [sys].tables t ON c.object_id = t.object_id
        JOIN [sys].schemas s ON t.schema_id = s.schema_id
        JOIN [sys].default_constraints dc ON c.default_object_id = dc.object_id
    WHERE s.name = 'upd'
        AND t.name = 'IA_IndexAlignementActions'
        AND c.name = 'IA_isUnique'    
)
BEGIN
    ALTER TABLE [upd].[IA_IndexAlignementActions] ADD CONSTRAINT DF_IA_isUnique DEFAULT 0 FOR [IA_isUnique];
END
IF NOT EXISTS (
    SELECT 1
    FROM [sys].all_columns c
        JOIN [sys].tables t ON c.object_id = t.object_id
        JOIN [sys].schemas s ON t.schema_id = s.schema_id
        JOIN [sys].default_constraints dc ON c.default_object_id = dc.object_id
    WHERE s.name = 'upd'
        AND t.name = 'IA_IndexAlignementActions'
        AND c.name = 'IA_isToBeDeleted'    
)
BEGIN
    ALTER TABLE [upd].[IA_IndexAlignementActions] ADD CONSTRAINT DF_IA_isToBeDeleted DEFAULT 0 FOR [IA_isToBeDeleted];
END 

IF NOT EXISTS (
    SELECT 1
    FROM [sys].all_columns c
        JOIN [sys].tables t ON c.object_id = t.object_id
        JOIN [sys].schemas s ON t.schema_id = s.schema_id
        JOIN [sys].default_constraints dc ON c.default_object_id = dc.object_id
    WHERE s.name = 'upd'
        AND t.name = 'IA_IndexAlignementActions'
        AND c.name = 'IA_isToBeRecreated'    
)
BEGIN
    ALTER TABLE [upd].[IA_IndexAlignementActions] ADD CONSTRAINT DF_IA_isToBeRecreated DEFAULT 0 FOR [IA_isToBeRecreated];
END 

IF NOT EXISTS (   SELECT 1
                    FROM [sys].indexes i
                   WHERE i.object_id = OBJECT_ID(N'upd.IA_IndexAlignementActions')
                     AND [i].[name]      = N'NCUIX_IA_IndexAlignementActions_COL_IA_expectedIndexName')
BEGIN
    CREATE UNIQUE NONCLUSTERED INDEX NCUIX_IA_IndexAlignementActions_COL_IA_expectedIndexName ON [upd].[IA_IndexAlignementActions](IA_expectedIndexName);
END
GO


/* Create Table Comments */

EXEC sp_addextendedproperty 'MS_Description', 'The date and time the action was executed, null if the action is to be executed', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_executionDate]
GO

EXEC sp_addextendedproperty 'MS_Description', 'the schema the table is part of', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_schemaName]
GO

EXEC sp_addextendedproperty 'MS_Description', 'On which table is the index, whithout the schema', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_tableName]
GO

EXEC sp_addextendedproperty 'MS_Description', 'list all the columns of the index, in the correct orders', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_columnsName]
GO

EXEC sp_addextendedproperty 'MS_Description', 'Is the index a clustered index ?', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_isClustered]
GO

EXEC sp_addextendedproperty 'MS_Description', 'Is the index unique ?', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_isUnique]
GO

EXEC sp_addextendedproperty 'MS_Description', 'list all the included columns the index should have.', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_includedColumns]
GO

EXEC sp_addextendedproperty 'MS_Description', 'Give here the index options if any. they will be applied on index creation but they are not part of the delta checks', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_indexOptions]
GO

EXEC sp_addextendedproperty 'MS_Description', 'must the script drop this index ?', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_isToBeDeleted]
GO

EXEC sp_addextendedproperty 'MS_Description', 'must the script recreate this index ?
If an existing index differs from the definition, it will be dropped and the correct one created. 
Use this only to force the re-creation even if the structure is identical', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_isToBeRecreated]
GO

EXEC sp_addextendedproperty 'MS_Description', 'the name the index should have, used to determine if a rename of the existing index should be done', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_expectedIndexName]
GO

EXEC sp_addextendedproperty 'MS_Description', 'A free text that can contain a reference. Seen to simplify update scripts', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_reference]
GO

EXEC sp_addextendedproperty 'MS_Description', 'computed by the proc. The index name on this system, as indexes name can be different accross systems', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_foundIndexName]
GO

EXEC sp_addextendedproperty 'MS_Description', 'computed by the proc. set to 1 if the index is present on the server ', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_wasIndexFound]
GO

EXEC sp_addextendedproperty 'MS_Description', 'computed by the proc. does this index was created on the server ?', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_wasCreated]
GO

EXEC sp_addextendedproperty 'MS_Description', 'computed by the proc. does this index was renamed ?', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_wasRenamed]
GO

EXEC sp_addextendedproperty 'MS_Description', 'computed by the proc. does this index was deleted ?', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_wasDeleted]
GO

EXEC sp_addextendedproperty 'MS_Description', 'computed by the proc. does this index was re-created ?', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_wasRecreated]
GO

EXEC sp_addextendedproperty 'MS_Description', 'computed by the proc. Are the expected and actual included columns identical ?', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_includedColumnsMatch]
GO

EXEC sp_addextendedproperty 'MS_Description', 'the actual columns in the existing index', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_actualColumns]
GO

EXEC sp_addextendedproperty 'MS_Description', 'the actual columns in the INCLUDE part of the index', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_actualIncludedColumns]
GO

EXEC sp_addextendedproperty 'MS_Description', 'Message, used for debug', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_statusMsg]
GO


INSERT INTO upd.IA_IndexAlignementActions (
    IA_columnsName,
    IA_isToBeDeleted,
    IA_isToBeRecreated,
    IA_expectedIndexName,
    IA_includedColumns,
    IA_schemaName,
    IA_tableName,
    IA_isClustered,
    IA_isUnique
    ,IA_reference
)
/* indexe(s) we do want */
VALUES('Entry_id',0,0,'PK_Entry_id', NULL,'dbo','entry',0,0,'OCTPDBA-365')
,('ET_batch_run',0,0,'NCIX_Entry_COL_ET_batch_run',NULL,'dbo','entry',0,0,'OCTPDBA-365')
,('ET_accounting_period',0,0,'NCIX_Entry_COL_ET_accounting_period','Entry_ID','dbo','entry',0,0,'OCTPDBA-365')
,('ET_document_header',0,0,'NCIX_Entry_COL_ET_document_header','ET_debit_currency_amount, ET_credit_currency_amount','dbo','entry',0,0,'OCTPDBA-365')
,('ET_account, ET_document_header',0,0,'NCIX_Entry_COL_ET_account','Entry_ID','dbo','entry',0,0,'OCTPDBA-365')
,('ET_reconciliation_status, ET_account, ET_document_header',0,0,'NCIX_Entry_COL_ET_reconciliation_status','ET_debit_base_amount, ET_credit_base_amount, ET_reconciliation_base_amount','dbo','entry',0,0,'OCTPDBA-365')
,('ET_predefined_entry',0,0,'NCIX_Entry_COL_ET_predefined_entry',NULL,'dbo','entry',0,0,'OCTPDBA-365')
,('ET_entry_address',0,0,'NCIX_Entry_COL_ET_entry_address',NULL,'dbo','entry',0,0,'OCTPDBA-365')
,('ET_currency',0,0,'NCIX_Entry_COL_ET_currency',NULL,'dbo','entry',0,0,'OCTPDBA-365')

/* indexe(s) used in central, even if not in pharmacies*/
,('ET_entry_type',0,0,'NCIX_Entry_COL_ET_entry_type',NULL,'dbo','entry',0,0,'OCTPDBA-365')

/* indexe(s) we really want to drop*/
,('ET_master_ID',1,0,'NCIX_Entry_COL_ET_master_ID',NULL,'dbo','entry',0,0,'OCTPDBA-365')
,('ET_bmc_user_profile',1,0,'NCIX_Entry_COL_ET_bmc_user_profile',NULL,'dbo','entry',0,0,'OCTPDBA-365')
,('ET_sales_tax_code',1,0,'NCIX_Entry_COL_ET_sales_tax_code',NULL,'dbo','entry',0,0,'OCTPDBA-365')
,('ET_APS_TS', 1, 0, 'NCIX_Entry_COL_ET_APS_TS',NULL,'dbo','entry',0,0,'OCTPDBA-365')

;

