# -*- coding: latin-1 -*-
from ppms.migration.packet import *
from ppms import ppms
from . import DEFAULT_CUSTOMIZING_DEPENDENCY

        
class PortMVCreationFlag(SQLPacket):
    """Port the new mv creation flags for DT 405 and DT 511"""
    wi_number = 12369
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    
    fname = 'wi12369_mv_creation_flag.sql'

    DBMS_QUERY = {'000002': ['alter table DT405 add DI063176 number(5,0) default 0 not null',
                             'alter table DT405_HIS add DI063176 number(5,0)',
                             'alter table DT511 add DI063175 number(5,0) default 0 not null'],
                  '000004': ['alter table DT405 add DI063176 numeric(5,0) default 0 not null',
                             'alter table DT405_HIS add DI063176 numeric(5,0)',
                             'alter table DT511 add DI063175 numeric(5,0) default 0 not null']}

    UPDATE_QUERIES = ['UPDATE DT405 SET DI063176 = 1',
                      'UPDATE DT511 SET DI063175 = 1']

    def get_dependencies(self):
        return DEFAULT_CUSTOMIZING_DEPENDENCY

    def fix(self, runner):
        self.apply_dml_file(self.sql_planta_de_dbms, self.fname)

        for ddl_query in self.DBMS_QUERY[ppms.db_get_dbms_name()] + self.UPDATE_QUERIES:
            self.modify_and_log(ddl_query)

        return self.success()