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


class CorrectPCALCPERIODS(BasePacket):
    '''Correct procedure p_calc_periods'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 9958

    def get_dependencies(self):
        return (MinVersionDependency(version='39.5.4'),
                MaxVersionDependency(version='39.5.4'))

    def fix(self, runner):
        self.log('Fixing P_CALC_PERIODS...')

        if ppms.db_get_dbms_name() == DBMS_ORACLE:
            # In Oracle, p_calc_periods is invalid at this time, but it can be compiled manually (?)
            ppms.db_modify("ALTER PROCEDURE P_CALC_PERIODS COMPILE")

        return self.success()