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


class DropCounterHistoryTables(SQLPacket):
    """Drop DT348_HIS and DT396_HIS"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 12510

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

    def fix(self, runner):
        for dt in (348, 396):
            self.modify_and_log("UPDATE DT415 SET AUDITED = 0 WHERE DI000265 = '{dt}'".format(dt=dt))
            self.modify_and_log("DROP TABLE DT{dt}_HIS".format(dt=dt))
            if self.DBMS == DBMS_ORACLE:
                self.modify_and_log("BEGIN p_execute_ignoring_exception('PURGE TABLE DT{dt}_HIS'); END;".format(dt=dt))
        return self.success()