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

from ppms import ppms
from ppms.migration import SQLPacket, PacketCategory, MaxVersionDependency
from ppms.constants import DBMS_MSSQL


class DeleteColumnsWithConstraints(SQLPacket):
    """Add the procedure "DropColumnWithConstraints" for MSSQL
 
    This procedure is used to delete the default constraints as well when deleting a data item
    """
    run_on_startup = True
    done_after_success = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13576

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

    def fix(self, runner):
        if ppms.db_get_dbms_name() == DBMS_MSSQL:  # only needed (and only exists) in MSSQL
            if self.procedure_exists(procedure_name='DropColumnWithConstraints'):
                self.log('Procedure already exists, nothing to do!')
            else:
                sql_file = "wi13576_delete_columns_with_constraints.sql"
                self.apply_fix_ddl_file(self.sql_planta_de_dbms, sql_file)

        return self.success()
