<?php

use Phalcon\Mvc\Model\Migration;

class FunctionGetMaxIdTriwulanOfTargetMigration_387 extends Migration {

    public function up()
    {
        $createFunctionQuery = <<<QUERY
            CREATE OR REPLACE FUNCTION jaga.get_max_id_triwulan_of_target(id_target INTEGER)
            RETURNS INTEGER AS $$
            DECLARE max_id_triwulan INTEGER;
            BEGIN
            select max(id_triwulan) INTO max_id_triwulan 
                from jaga.str_target_log where 
                id_master in ( 
                    select id from jaga.str_target_master as B where 
                    B.id_target = $1);

                RETURN max_id_triwulan;
            END;
            $$  LANGUAGE plpgsql
QUERY;
        self::$connection->execute($createFunctionQuery);
    }

    public function down()
    {
        $dropFunctionQuery = <<<QUERY
            DROP FUNCTION IF EXISTS jaga.get_max_id_triwulan_of_target;
QUERY;
        self::$connection->execute($dropFunctionQuery);
    }
}

?>