USE Arizona

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

CREATE TABLE upd.IA_IndexesAlignementActions(
    IA_indexesAlignementActions_id 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_schema_name VARCHAR(100) NOT NULL                   /* the schema the table is part of */
    ,IA_table_name VARCHAR(100) NOT NULL                    /* On which table is the index, whithout the schema */
    ,IA_columns_name VARCHAR(MAX) NOT NULL                  /* list all the columns of the index, in the correct orders */
    ,IA_is_clustered BIT NOT NULL                           /* Is the indexe a clustered index ? */
    ,IA_is_unique BIT NOT NULL                              /* Is the index unique ? */
    ,IA_included_columns VARCHAR(MAX)NULL                   /* list all the included columns the index should have. */
    ,IA_index_options 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_is_to_be_deleted BIT NOT NULL                       /* must the script drop this index ? */
    ,IA_is_to_be_recreated BIT NOT NULL                     /* 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  */
    ,IA_expected_index_name VARCHAR(500) NOT NULL           /* the name the index should have, used to determine if a rename of the existing index should be done */


    /* 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 NULL                               /* computed by the script. does this index was created on the server ? */
        CONSTRAINT DF_IA_wasCreated DEFAULT 0
    ,[IA_wasRenamed] BIT NULL                               /* computed by the script. does this index was renamed ? */
        CONSTRAINT DF_IA_wasRenamed DEFAULT 0
    ,[IA_wasDeleted] BIT NULL                               /* computed by the script. does this index will be deleted ? */
        CONSTRAINT DF_IA_wasDeleted DEFAULT 0
    ,[IA_wasRecreated] BIT 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_IndexesAlignementActions PRIMARY KEY(IA_indexesAlignementActions_id)
);
ALTER TABLE upd.IA_IndexesAlignementActions ADD CONSTRAINT DF_IA_is_clustered DEFAULT 0 FOR IA_is_clustered;
ALTER TABLE upd.IA_IndexesAlignementActions ADD CONSTRAINT DF_IA_is_unique DEFAULT 0 FOR IA_is_unique;
ALTER TABLE upd.IA_IndexesAlignementActions ADD CONSTRAINT DF_IA_is_to_be_deleted DEFAULT 0 FOR IA_is_to_be_deleted;
ALTER TABLE upd.IA_IndexesAlignementActions ADD CONSTRAINT DF_IA_is_to_be_recreated DEFAULT 0 FOR IA_is_to_be_recreated;

CREATE UNIQUE NONCLUSTERED INDEX NCUIX_IA_IndexesAlignementActions_index_name ON upd.IA_IndexesAlignementActions(IA_expected_index_name);
GO

INSERT INTO upd.IA_IndexesAlignementActions (
    IA_columns_name,
    IA_is_to_be_deleted,
    IA_is_to_be_recreated,
    IA_expected_index_name,
    IA_included_columns,
    [IA_schema_name],
    [IA_table_name],
    [IA_is_clustered],
    [IA_is_unique]
)
/* indexe(s) we do want */
VALUES('Entry_id',0,0,'PK_Entry_id', NULL,'dbo','entry',0,0)
,('ET_batch_run',0,0,'NCIX_Entry_COL_ET_batch_run',NULL,'dbo','entry',0,0)
,('ET_accounting_period',0,0,'NCIX_Entry_COL_ET_accounting_period','Entry_ID','dbo','entry',0,0)
,('ET_document_header',0,0,'NCIX_Entry_COL_ET_document_header','ET_debit_currency_amount, ET_credit_currency_amount','dbo','entry',0,0)
,('ET_account, ET_document_header',0,0,'NCIX_Entry_COL_ET_account','Entry_ID','dbo','entry',0,0)
,('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)
,('ET_predefined_entry',0,0,'NCIX_Entry_COL_ET_predefined_entry',NULL,'dbo','entry',0,0)
,('ET_entry_address',0,0,'NCIX_Entry_COL_ET_entry_address',NULL,'dbo','entry',0,0)
,('ET_currency',0,0,'NCIX_Entry_COL_ET_currency',NULL,'dbo','entry',0,0)

/* 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)

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

;

