# -*- coding: utf-8 -*-
import os

from ppms import ppms
from ppms.migration.packet import *

class PortSQLStatementsNew(SQLPacket):
    '''Port the necessary sql statements for the new period aggregation'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 11319

    fname = 'wi9958_period_creation_and_aggregation.sql'

    def fix(self, runner):
        previous_packet = BasePacket.from_packet_path('migration.hotfix.S_39_5_0.wi9958_period_creation_and_aggregation.PortSQLStatements')
        if previous_packet and previous_packet.done:
            self.log('Period packet already executed successfully - nothing to do!')
            return self.success()
        elif previous_packet and not previous_packet.done:
            previous_packet.log('Setting this packet to done since it is superseded by PortSQLStatementsNew')
            previous_packet.done = True
            previous_packet.success()
            previous_packet.save_history()

        sqlpath = self.get_sql_file_path(self.sql_planta_de_dbms, self.fname)
        queries = self.get_queries_from_file(sqlpath, encoding='utf-8')

        for query in queries:
            self.modify_and_log(query)

        return self.success()

class PortSchemaNew(SQLPacket):
    '''Port the necessary db schema changes for the new period aggregation'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 11319

    def rollback(self):
        self.log_heading('Rolling back')
        fname = 'wi9958_period_creation_and_aggregation_rollback.sql'
        sqlpath = self.get_sql_file_path(self.sql_planta_de_dbms, fname)

        queries = self.get_queries_from_file(sqlpath)

        for query in queries:
            try:
                self.modify_and_log(query)
            except ppms.DatabaseError:
                pass

    def fix(self, runner):
        previous_packet = BasePacket.from_packet_path('migration.hotfix.S_39_5_0.wi9958_period_creation_and_aggregation.PortSchema')
        if previous_packet and previous_packet.done:
            self.log('Period packet already executed successfully - nothing to do!')
            return self.success()
        elif previous_packet and not previous_packet.done:
            previous_packet.log('Setting this packet to done since it is superseded by PortSchemaNew')
            previous_packet.done = True
            previous_packet.success()
            previous_packet.save_history()

        self.rollback() # fix up the mess left over by the original package
        self.port_tables(runner)
        self.port_procedure(runner)
        self.port_function(runner)

        return self.success()

    def port_tables(self, runner):
        self.log_heading('Porting the tables')
        fname = 'wi9958_period_creation_and_aggregation_tables.sql'
        sqlpath = self.get_sql_file_path(self.sql_planta_de_dbms, fname)

        queries = self.get_queries_from_file(sqlpath, encoding='utf-8')

        for query in queries:
            self.modify_and_log(query)

    def port_procedure(self, runner):
        self.log_heading('Porting the procedure')
        fname = 'wi9958_period_creation_and_aggregation_procedure.sql'
        sqlpath = self.get_sql_file_path(self.sql_planta_de_dbms, fname)

        with open(sqlpath, encoding='utf-8') as f:
            query = f.read()

        self.modify_and_log(query)

    def port_function(self, runner):
        self.log_heading('Porting the function')
        fname = 'wi9958_period_creation_and_aggregation_function.sql'
        sqlpath = self.get_sql_file_path(self.sql_planta_de_dbms, fname)

        with open(sqlpath, encoding='utf-8') as f:
            query = f.read()

        self.modify_and_log(query)
