<?php
namespace Neos\Flow\Persistence\Doctrine\Migrations;

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

/**
 * Auto-generated Migration: Please modify to your needs! This block will be used as the migration description if getDescription() is not used.
 */
class Version20190611073141 extends AbstractMigration
{

    /**
     * @return string
     */
    public function getDescription()
    {
        return '';
    }

    /**
     * @param Schema $schema
     * @return void
     */
    public function up(Schema $schema)
    {
        // this up() migration is autogenerated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on "mysql".');

        if (!$this->columnExists('newland_toubiz_sync_neos_domain_model_offer', 'language')) {
            $this->addSql('ALTER TABLE newland_toubiz_sync_neos_domain_model_offer ADD language CHAR(2) DEFAULT NULL');
        }
        if (!$this->columnExists('newland_toubiz_sync_neos_domain_model_package', 'language')) {
            $this->addSql('ALTER TABLE newland_toubiz_sync_neos_domain_model_package ADD language CHAR(2) DEFAULT NULL');
        }
    }

    /**
     * @param Schema $schema
     * @return void
     */
    public function down(Schema $schema)
    {
        // this down() migration is autogenerated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on "mysql".');
        
        $this->addSql('ALTER TABLE newland_toubiz_sync_neos_domain_model_offer DROP language');
        $this->addSql('ALTER TABLE newland_toubiz_sync_neos_domain_model_package DROP language');
    }


    private function columnExists(string $table, string $columnName): bool
    {
        foreach ($this->sm->listTableColumns($table) as $column) {
            if ($column->getName() === $columnName) {
                return true;
            }
        }
        return false;
    }
}