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


class UpdateColumnType(SQLPacket):
    """Update column type of DT060.DI029547 to VARCHAR2(30 CHAR)
    """

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

    STATEMENTS = {DBMS_ORACLE: ("CREATE TABLE DT060_TEMP AS (SELECT * FROM DT060)",
                                "DELETE FROM DT060",
                                "ALTER TABLE DT060 MODIFY DI029547 VARCHAR2(30 CHAR)",
                                "INSERT INTO DT060 (SELECT * FROM DT060_TEMP)",
                                "DROP TABLE DT060_TEMP"),
                  DBMS_MSSQL: ("SELECT * INTO DT060_TEMP FROM DT060",
                            "DELETE FROM DT060",
                            "ALTER TABLE DT060 DROP CONSTRAINT DI029547_DEFAULT",
                            "ALTER TABLE DT060 DROP CONSTRAINT DI029547_NOT_NULL",
                            "ALTER TABLE DT060 ALTER COLUMN DI029547 VARCHAR(30) NOT NULL",
                            "INSERT INTO DT060 SELECT * FROM DT060_TEMP",
                            "DROP TABLE DT060_TEMP")}

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

    def fix(self, runner):
        for statement in self.STATEMENTS[self.DBMS]:
            self.modify_and_log(statement)
            
        return self.success()