# -*- coding: utf-8 -*-
from ppms import ppms
from ppms.migration import *


class CreateCriticalPathDI(SQLPacket):
    """Creates dataitems for scheduling to indicate whether a task is on the critical path or not"""
    run_on_startup = True
    done_after_success = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 21471

    filename_ddl = 'WI21471_create_critical_path_di_ddl.sql'
    filename_dml = 'WI21471_create_critical_path_di.sql'
    deletion_file = 'WI21471_create_critical_path_di_delete.sql'

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

    def apply_ddl_changes(self):
        self.log_heading("Dropping/creating necessary column(s)")
        self.drop_column('DT463', 'LONGEST_CRITICAL_PATH')
        self.drop_column('DT824', 'LONGEST_CRITICAL_PATH')
        self.drop_column('DT824', 'CRITICAL_PATH')
        self.apply_fix_ddl_file(self.sql_planta_de_dbms, self.filename_ddl, split=True, separator=';/')

    def delete_old_data(self):
        self.log_heading("Wiping data that could cause conflicts")
        self.apply_dml_file(self.sql_planta_de_ansi, self.deletion_file)

    def insert_new_data(self):
        self.log_heading("Inserting new data")
        self.apply_dml_file(self.sql_planta_de_dbms, self.filename_dml)

    def fix(self, runner):
        self.apply_ddl_changes()

        with ppms.autocommit_disabled():
            self.delete_old_data()
            self.insert_new_data()

        return self.success()
