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

class FixCostTrendsAndEVA(SQLPacket):
    '''Fixes the view V_PLANTA_CUMU_DT827 which is the data source for cost
    trends and the Earned Value Analyis. This will fix the issue that charts
    show planned costs in baselines/reports for the wrong report.
    '''
    
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 16217

    sql_file = 'WI16217_fix_cost_trends_db7.sql'
    
    def get_dependencies(self):
        return (MinVersionDependency(version='39.5.6', 
                                     version_type=VersionType.CUSTOMIZING),
                MaxVersionDependency(version='39.5.6', 
                                     version_type=VersionType.CUSTOMIZING))

    def fix(self, runner):
        # Apply new version of V_PLANTA_CUMU_DT827 
        if self.DBMS == DBMS_ORACLE:
            self.apply_fix_ddl_file(self.sql_planta_de_dbms, self.sql_file, split=True)
        else:
            self.apply_fix_ddl_file(self.sql_planta_de_dbms, self.sql_file, split=True, separator='GO')
        
        return self.success()