Skip to content

Instantly share code, notes, and snippets.

@alOneh
Created September 15, 2016 09:36
Show Gist options
  • Save alOneh/62f155d1b5b97f7db29df2fe92361bbf to your computer and use it in GitHub Desktop.
Save alOneh/62f155d1b5b97f7db29df2fe92361bbf to your computer and use it in GitHub Desktop.
Migration 0 exemple
<?php
namespace Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
class Version00000000000000 extends AbstractMigration
{
public function up(Schema $schema)
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$sql = <<<SQL
CREATE TABLE my_table (
id NUMBER(10) NOT NULL,
...
);
SQL;
foreach (explode(';', $sql) as $stmt) {
$stmt = trim($stmt);
if (!empty($stmt)) {
$this->addSql($stmt);
}
}
}
public function down(Schema $schema)
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$schemaName = $schema->getName();
$sql = <<<SQL
SET FOREIGN_KEY_CHECKS = 0;
SET @tables = NULL;
SELECT GROUP_CONCAT(table_schema, '.', table_name) INTO @tables
FROM information_schema.tables
WHERE table_schema = '$schemaName';
SET @tables = CONCAT('DROP TABLE ', @tables);
PREPARE stmt FROM @tables;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET FOREIGN_KEY_CHECKS = 1;
SQL;
$this->addSql($sql);
}
}
@alOneh
Copy link
Author

alOneh commented Nov 25, 2016

How to generate the up() mysql ?

$ bin/console doctrine:schema:create --dump-sql

Then copy/paste the dump into the <<<SQL...SQL block

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment