<?php

/*
 * This file is part of the Claroline Connect package.
 *
 * (c) Claroline Consortium <consortium@claroline.net>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Claroline\CoreBundle\Migrations\ibm_db2;

use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;

/**
 * Auto-generated migration based on mapping information: modify it with caution
 *
 * Generation date: 2013/10/10 03:10:56
 */
class Version20131010151055 extends AbstractMigration
{
    public function up(Schema $schema)
    {
        $this->addSql("
            CREATE TABLE claro_log_widget_config (
                id INTEGER GENERATED BY DEFAULT AS IDENTITY NOT NULL,
                amount INTEGER NOT NULL,
                restrictions CLOB(1M) DEFAULT NULL,
                widgetInstance_id INTEGER DEFAULT NULL,
                PRIMARY KEY(id)
            )
        ");
        $this->addSql("
            CREATE INDEX IDX_C16334B2AB7B5A55 ON claro_log_widget_config (widgetInstance_id)
        ");
        $this->addSql("
            CREATE TABLE claro_badge_rule (
                id INTEGER GENERATED BY DEFAULT AS IDENTITY NOT NULL,
                badge_id INTEGER NOT NULL,
                occurrence SMALLINT NOT NULL,
                action VARCHAR(255) NOT NULL,
                PRIMARY KEY(id)
            )
        ");
        $this->addSql("
            CREATE INDEX IDX_805FCB8FF7A2C2FC ON claro_badge_rule (badge_id)
        ");
        $this->addSql("
            CREATE TABLE claro_widget_instance (
                id INTEGER GENERATED BY DEFAULT AS IDENTITY NOT NULL,
                workspace_id INTEGER DEFAULT NULL,
                user_id INTEGER DEFAULT NULL,
                widget_id INTEGER NOT NULL,
                is_admin SMALLINT NOT NULL,
                is_desktop SMALLINT NOT NULL,
                name VARCHAR(255) NOT NULL,
                PRIMARY KEY(id)
            )
        ");
        $this->addSql("
            CREATE INDEX IDX_5F89A38582D40A1F ON claro_widget_instance (workspace_id)
        ");
        $this->addSql("
            CREATE INDEX IDX_5F89A385A76ED395 ON claro_widget_instance (user_id)
        ");
        $this->addSql("
            CREATE INDEX IDX_5F89A385FBE885E2 ON claro_widget_instance (widget_id)
        ");
        $this->addSql("
            CREATE TABLE claro_simple_text_widget_config (
                id INTEGER GENERATED BY DEFAULT AS IDENTITY NOT NULL,
                content CLOB(1M) NOT NULL,
                widgetInstance_id INTEGER DEFAULT NULL,
                PRIMARY KEY(id)
            )
        ");
        $this->addSql("
            CREATE INDEX IDX_C389EBCCAB7B5A55 ON claro_simple_text_widget_config (widgetInstance_id)
        ");
        $this->addSql("
            ALTER TABLE claro_log_widget_config
            ADD CONSTRAINT FK_C16334B2AB7B5A55 FOREIGN KEY (widgetInstance_id)
            REFERENCES claro_widget_instance (id)
            ON DELETE CASCADE
        ");
        $this->addSql("
            ALTER TABLE claro_badge_rule
            ADD CONSTRAINT FK_805FCB8FF7A2C2FC FOREIGN KEY (badge_id)
            REFERENCES claro_badge (id)
            ON DELETE CASCADE
        ");
        $this->addSql("
            ALTER TABLE claro_widget_instance
            ADD CONSTRAINT FK_5F89A38582D40A1F FOREIGN KEY (workspace_id)
            REFERENCES claro_workspace (id)
            ON DELETE CASCADE
        ");
        $this->addSql("
            ALTER TABLE claro_widget_instance
            ADD CONSTRAINT FK_5F89A385A76ED395 FOREIGN KEY (user_id)
            REFERENCES claro_user (id)
            ON DELETE CASCADE
        ");
        $this->addSql("
            ALTER TABLE claro_widget_instance
            ADD CONSTRAINT FK_5F89A385FBE885E2 FOREIGN KEY (widget_id)
            REFERENCES claro_widget (id)
            ON DELETE CASCADE
        ");
        $this->addSql("
            ALTER TABLE claro_simple_text_widget_config
            ADD CONSTRAINT FK_C389EBCCAB7B5A55 FOREIGN KEY (widgetInstance_id)
            REFERENCES claro_widget_instance (id)
            ON DELETE CASCADE
        ");
        $this->addSql("
            ALTER TABLE claro_user
            ADD COLUMN picture VARCHAR(255) DEFAULT NULL
            ADD COLUMN description CLOB(1M) DEFAULT NULL
        ");
        $this->addSql("
            ALTER TABLE claro_badge
            ADD COLUMN workspace_id INTEGER DEFAULT NULL
            ADD COLUMN automatic_award SMALLINT DEFAULT NULL
        ");
        $this->addSql("
            ALTER TABLE claro_badge
            ADD CONSTRAINT FK_74F39F0F82D40A1F FOREIGN KEY (workspace_id)
            REFERENCES claro_workspace (id)
        ");
        $this->addSql("
            CREATE INDEX IDX_74F39F0F82D40A1F ON claro_badge (workspace_id)
        ");
        $this->addSql("
            ALTER TABLE claro_widget
            ADD COLUMN is_displayable_in_workspace SMALLINT NOT NULL
            ADD COLUMN is_displayable_in_desktop SMALLINT NOT NULL
        ");
        $this->addSql("
            ALTER TABLE claro_widget_home_tab_config
            ADD COLUMN widget_instance_id INTEGER DEFAULT NULL
            DROP COLUMN widget_id
        ");
        $this->addSql("
            ALTER TABLE claro_widget_home_tab_config
            DROP FOREIGN KEY FK_D48CC23EFBE885E2
        ");
        $this->addSql("
            DROP INDEX IDX_D48CC23EFBE885E2
        ");
        $this->addSql("
            ALTER TABLE claro_widget_home_tab_config
            ADD CONSTRAINT FK_D48CC23E44BF891 FOREIGN KEY (widget_instance_id)
            REFERENCES claro_widget_instance (id)
            ON DELETE CASCADE
        ");
        $this->addSql("
            CREATE INDEX IDX_D48CC23E44BF891 ON claro_widget_home_tab_config (widget_instance_id)
        ");
    }

    public function down(Schema $schema)
    {
        $this->addSql("
            ALTER TABLE claro_log_widget_config
            DROP FOREIGN KEY FK_C16334B2AB7B5A55
        ");
        $this->addSql("
            ALTER TABLE claro_widget_home_tab_config
            DROP FOREIGN KEY FK_D48CC23E44BF891
        ");
        $this->addSql("
            ALTER TABLE claro_simple_text_widget_config
            DROP FOREIGN KEY FK_C389EBCCAB7B5A55
        ");
        $this->addSql("
            DROP TABLE claro_log_widget_config
        ");
        $this->addSql("
            DROP TABLE claro_badge_rule
        ");
        $this->addSql("
            DROP TABLE claro_widget_instance
        ");
        $this->addSql("
            DROP TABLE claro_simple_text_widget_config
        ");
        $this->addSql("
            ALTER TABLE claro_badge
            DROP COLUMN workspace_id
            DROP COLUMN automatic_award
        ");
        $this->addSql("
            ALTER TABLE claro_badge
            DROP FOREIGN KEY FK_74F39F0F82D40A1F
        ");
        $this->addSql("
            DROP INDEX IDX_74F39F0F82D40A1F
        ");
        $this->addSql("
            ALTER TABLE claro_user
            DROP COLUMN picture
            DROP COLUMN description
        ");
        $this->addSql("
            ALTER TABLE claro_widget
            DROP COLUMN is_displayable_in_workspace
            DROP COLUMN is_displayable_in_desktop
        ");
        $this->addSql("
            ALTER TABLE claro_widget_home_tab_config
            ADD COLUMN widget_id INTEGER NOT NULL
            DROP COLUMN widget_instance_id
        ");
        $this->addSql("
            DROP INDEX IDX_D48CC23E44BF891
        ");
        $this->addSql("
            ALTER TABLE claro_widget_home_tab_config
            ADD CONSTRAINT FK_D48CC23EFBE885E2 FOREIGN KEY (widget_id)
            REFERENCES claro_widget (id)
            ON DELETE CASCADE
        ");
        $this->addSql("
            CREATE INDEX IDX_D48CC23EFBE885E2 ON claro_widget_home_tab_config (widget_id)
        ");
    }
}
