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


class EnsureAuditDefaultValue(SQLPacket):
    '''Add audited default value if it does not already exist'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 16897

    def get_dependencies(self):
        return (MaxVersionDependency('39.5.6'), )

    def fix(self, runner):
        if self.DBMS == DBMS_ORACLE:
            self.log('Packet skipped, because it is not needed in Oracle')
            return self.success()
        else:
            try:
                self.modify_and_log('ALTER TABLE DT412 ADD CONSTRAINT AUDITED_DT412_DEFAULT DEFAULT 0 for audited')
                self.modify_and_log('ALTER TABLE DT415 ADD CONSTRAINT AUDITED_DT415_DEFAULT DEFAULT 0 for audited')
            except ppms.DatabaseError:
                self.log('Audited already has a default value - OK')
            return self.success()