# -*- coding: utf-8 -*-

from ppms import ppms
from ppms.migration import *


class SaveNotesIsNotPossibleInModuleDokumente(SQLPacket, ChangeDTPPacket):
    """Drop Not-Null-Constraints of DT462.DI001092 to allow creating datasets"""
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13104
    sql_file = "wi13104_drop_constraints_dt462.sql"

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

    def fix(self, runner):
        di001092_rec = self.get_record(412, ['001092'] ,[2461])
        di001093_rec = self.get_record(412, ['001093'] ,[2461])

        if di001092_rec.sql.get_value() == 0 and di001093_rec.sql.get_value() == 2:
            self.log('Looks like this package was run already.')
            return self.success()

        try:
            self.apply_fix_ddl_file(self.sql_planta_de_dbms, self.sql_file)
        except ppms.DatabaseError as e:
            if 'ORA-02443' not in str(e) and 'ORA-06512' not in str(e):  # constraint already changed
                raise e

        if not self.change_value(di001092_rec, 'sql', 1, 0):
            return self.fail()
        if not self.change_value(di001093_rec, 'sql', 3, 2):
            return self.fail()
        return self.success()
