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


class DeactivateVariableObjectTables(SQLPacket):
    '''Deactivates and drops variable object tables'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13543

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

    def fix(self, runner):
        statements = ("update dt415 set enabled = 0 where DI000216 in ('DT349', 'DT395', 'DT548', 'DT954', 'DT899', 'DT005', 'DT937')",
                      "drop table dt349",
                      "drop table dt395",
                      "drop table dt548",
                      "drop table dt954",
                      "drop table dt899",
                      "drop table dt005",
                      "drop table dt937")
        statements_ora = ("purge table dt349",
                          "purge table dt395",
                          "purge table dt548",
                          "purge table dt954",
                          "purge table dt899",
                          "purge table dt005",
                          "purge table dt937")

        for statement in statements:
            self.modify_and_log(statement)

        if self.DBMS == DBMS_ORACLE:
            for ora_statement in statements_ora:
                self.modify_and_log("BEGIN p_execute_ignoring_exception('{statement}'); END;".format(statement=ora_statement))

        return self.success()