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


class SetLanguageDT462(SQLPacket):
    """Sets language in DT462 to DE for all records where it was null before"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 17532

    UPDATE_STATEMENT = """update DT462 set DI001092= 'DE' where DI001092 is null"""

    SET_NOT_NULL = """ALTER TABLE DT462 ALTER COLUMN DI001092 varchar(6) NOT NULL"""

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

    def fix(self, packet_runner):
        self.modify_and_log(self.UPDATE_STATEMENT)
        if self.DBMS == DBMS_MSSQL:
            self.modify_and_log(self.SET_NOT_NULL)
        return self.success()