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


class DeactivateVariableObjectTablesMSSQL(SQLPacket):
    '''Deactivates and drops variable object tables'''
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 14313

    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):
        if self.DBMS != DBMS_MSSQL:
            self.log('Only necessary in MSSQL, Oracle should be done with DB_39_5_5.WI13543_deactivate_variable_object_tables.DeactivateVariableObjectTables')
            return self.success()

        statements = ("drop table dt349",
                      "drop table dt395",
                      "drop table dt548",
                      "drop table dt954",
                      "drop table dt899",
                      "drop table dt005",
                      "drop table dt937")

        for statement in statements:
            try:
                self.modify_and_log(statement)
            except DatabaseError:
                pass  # probably dropped already

        return self.success()