<?php

use Phalcon\Mvc\Model\Migration;

class PAKRoleAndScopesMigration_305 extends Migration
{
    public function up()
    {
        $this->down(); 
        
        self::$connection->execute("
            INSERT INTO jaga.scopes (name, label, description) VALUES 
                ('pak.publish.isiansekolah', 'Publish isian sekolah modul PAK', 'Publish isian sekolah modul PAK')
                ON CONFLICT DO NOTHING;
                
            INSERT INTO jaga.roles_scopes (role_id, scope_id)
                SELECT r.id, s.id FROM jaga.roles r 
                JOIN jaga.scopes s ON r.name = 'admin_pak' and s.name = 'pak.publish.isiansekolah' ON CONFLICT DO NOTHING;
            INSERT INTO jaga.roles_scopes (role_id, scope_id)
                SELECT r.id, s.id FROM jaga.roles r 
                JOIN jaga.scopes s ON r.name = 'verifikator_pak' and s.name = 'pak.publish.isiansekolah' ON CONFLICT DO NOTHING;
            INSERT INTO jaga.roles_scopes (role_id, scope_id)
                SELECT r.id, s.id FROM jaga.roles r 
                JOIN jaga.scopes s ON r.name = 'user_dinas_pak' and s.name = 'pak.publish.isiansekolah' ON CONFLICT DO NOTHING;
        ");
    }

    public function down()
    {
        self::$connection->execute("
            DELETE FROM jaga.roles_scopes WHERE scope_id IN 
            (SELECT id as scope_id FROM scopes WHERE 
                name = 'pak.publish.isiansekolah'
            );
        ");

        self::$connection->execute("
            DELETE FROM jaga.scopes WHERE 
                name = 'pak.publish.isiansekolah';
        ");

    }
}
