# -*- coding: utf-8 -*-
from ppms import ppms
from ppms.constants import DBMS_MSSQL, DBMS_ORACLE
from ppms.migration import SQLPacket, PacketCategory, MaxVersionDependency, \
     MinVersionDependency, VersionType


class AddLogTimestampColumn(SQLPacket):
    '''This package adds the new column "log_timestamp" to DT324 "Statistik"
    which is used for the performance history evaluation.'''

    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 15793

    STATEMENTS = {DBMS_MSSQL: """IF COL_LENGTH('DT324', 'LOG_TIMESTAMP') IS NULL
                  BEGIN
                    ALTER TABLE DT324
                    ADD LOG_TIMESTAMP datetime2(7) 
                    NOT NULL CONSTRAINT DF_DT324_LOG_TIMESTAMP 
                             DEFAULT sysdatetime()
                  END""",
                  DBMS_ORACLE: """
                            begin
                            p_add_column_if_not_existing('DT324',
                                         'LOG_TIMESTAMP',
                                         'TIMESTAMP (6) DEFAULT SYSTIMESTAMP');
                            end;"""}

    def get_dependencies(self):
        return (MinVersionDependency(version='39.5.6' ,
                                     version_type=VersionType.CUSTOMIZING),
                MaxVersionDependency(version='39.5.6' ,
                                     version_type=VersionType.CUSTOMIZING))

    def fix(self, runner):           
        self.modify_and_log(self.STATEMENTS[ppms.db_get_dbms_name()])

        return self.success()