# -*- coding: utf-8 -*-
from ppms.constants import *
from ppms.migration import SQLPacket, PacketCategory, VersionType, MinVersionDependency, MaxVersionDependency
from ppms.ppms_ import DatabaseError


class UpdateColumns(SQLPacket):
    """Update DI059758 and NATIVE_MEM_USAGE to NUMBER/NUMERIC(10, 0) NOT NULL"""

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

    STATEMENTS = {DBMS_ORACLE: ("""ALTER TABLE DT324 MODIFY DI059758 NUMBER(10, 0) NOT NULL""",
                                """ALTER TABLE DT324 MODIFY NATIVE_MEM_USAGE NUMBER(10, 0) NOT NULL"""),
                  DBMS_MSSQL: ("""ALTER TABLE DT324 ALTER COLUMN DI059758 NUMERIC(10, 0) NOT NULL""",
                                """ALTER TABLE DT324 ALTER COLUMN NATIVE_MEM_USAGE NUMERIC(10, 0) NOT NULL""")}

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

    def fix(self, runner):
        for statement in self.STATEMENTS[self.DBMS]:
            try:
                self.modify_and_log(statement)
            except DatabaseError:
                pass  # probably already NOT NULL

        return self.success()