Doctrine Migrations: Difference between revisions
Line 301: | Line 301: | ||
===Doctrine Migrate=== | ===Doctrine Migrate=== | ||
Execution of <code>bin/console doctrine:migrations:migrate</code> command we can migrate to version of database from 0 to up to date. It's like an wizard and reduce the complexity of application migration. | Execution of <code>bin/console doctrine:migrations:migrate</code> command we can migrate to version of database from 0 to up to date. It's like an wizard and reduce the complexity of application migration. | ||
WARNING! You are about to execute a database migration that could result in schema changes and data lost. Are you sure you wish to continue? (y/n)y | |||
Migrating up to 20180113140230 from 0 | |||
++ migrating 20180113140052 | |||
-> CREATE TABLE t00e00 (id INTEGER NOT NULL, name_native VARCHAR(255) DEFAULT NULL, name_english VARCHAR(255) NOT NULL, host VARCHAR(255) NOT NULL, site VARCHAR(255) NOT NULL, PRIMARY KEY(id)) | |||
-> CREATE UNIQUE INDEX UNIQ_T00E00_HOST ON t00e00 (host) | |||
-> CREATE TABLE t00i00 (id INTEGER NOT NULL, name_english VARCHAR(255) NOT NULL, name_native VARCHAR(255) NOT NULL, site_code VARCHAR(4) NOT NULL, PRIMARY KEY(id)) | |||
-> CREATE UNIQUE INDEX UNIQ_T00I00_SITE_CODE ON t00i00 (site_code) | |||
++ migrated (0.12s) | |||
++ migrating 20180113140130 | |||
-> INSERT INTO t00e00(name_native, name_english, host, site) VALUES('Shahed', 'Shahed','shahed.biz', 'site') | |||
-> INSERT INTO t00e00(name_native, name_english, host, site) VALUES('Chorke', 'Chorke','chorke.org', 'site') | |||
-> INSERT INTO t00e00(name_native, name_english, host, site) VALUES('Chorke', 'Chorke','chorke.com', 'site') | |||
++ migrated (0s) | |||
++ migrating 20180113140230 | |||
-> INSERT INTO t00i00(name_native, name_english, site_code) VALUES('Chorke', 'Chorke', 'init') | |||
++ migrated (0s) | |||
------------------------ | |||
++ finished in 0.12s | |||
++ 3 migrations executed | |||
++ 8 sql queries |
Revision as of 09:09, 13 January 2018
Parameters
# app/config/parameters.yml
parameters:
database_host: ~
database_port: ~
database_name: ~
database_user: ~
database_password: ~
database_path: '%kernel.root_dir%/../var/rdbms/chorke.sqlite '
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: ~
mailer_password: ~
secret: ff1a949ebf4d50f1f51a1fb3864f52e9e5c9e136
Doctrine Config
# app/config/config.yml
doctrine:
dbal:
driver: pdo_sqlite
path: '%database_path%'
charset: UTF8
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
Database Creation
# var/sqlite/.gitkeep
mkdir -p ./var/rdbms/;\
touch ./var/rdbms/.gitkeep
# sqlite database create by doctrine
bin/console doctrine:database:create
# doctrine migrations dependency require
composer require doctrine/doctrine-migrations-bundle "^1.0"
Migrations Config
Finally, be sure to enable the bundle in AppKernel.php
by including the following:
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
//...
new Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle(),
);
}
# app/config/config.yml
doctrine_migrations:
dir_name: "%kernel.root_dir%/DoctrineMigrations"
namespace: Application\Migrations
table_name: T00000
name: Application Migrations
organize_migrations: false
Doctrine Entities
// src/WebappBundle/Entity/T00E00.php
namespace Chorke\Bundle\WebappBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* T00E00
*
* @ORM\Table(name="t00e00", uniqueConstraints={
* @ORM\UniqueConstraint(name="UNIQ_T00E00_HOST", columns={"host"})
* })
* @ORM\Entity(repositoryClass="Chorke\Bundle\WebappBundle\Repository\T00E00Repository")
*/
class T00E00
{
/**
* @var int
*
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(name="id", type="integer")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name_native", type="string", length=255, nullable=true)
*/
private $nameNative;
/**
* @var string
*
* @ORM\Column(name="name_english", type="string", length=255)
*/
private $nameEnglish;
/**
* @var string
*
* @ORM\Column(name="host", type="string", length=255, unique=true)
*/
private $host;
/**
* @var string
*
* @ORM\Column(name="site", type="string", length=255)
*/
private $site;
// getter and setter omitted
}
// src/WebappBundle/Entity/T00I00.php
namespace Chorke\Bundle\WebappBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* T00I00
*
* @ORM\Table(name="t00i00", uniqueConstraints={
* @ORM\UniqueConstraint(name="UNIQ_T00I00_SITE_CODE", columns={"site_code"})
* })
* @ORM\Entity(repositoryClass="Chorke\Bundle\WebappBundle\Repository\T00I00Repository")
*/
class T00I00
{
/**
* @var int
*
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(name="id", type="integer")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name_english", type="string", length=255)
*/
private $nameEnglish;
/**
* @var string
*
* @ORM\Column(name="name_native", type="string", length=255)
*/
private $nameNative;
/**
* @var string
*
* @ORM\Column(name="site_code", type="string", length=4, unique=true)
*/
private $siteCode;
// getter and setter omitted
}
Migration Differ
After creation of entity/entities, need to generate Doctrine Migrations using bin/console doctrine:migrations:diff
command. Will generate the out put as below.
// src/DoctrineMigrations/Version20180113140052.php
namespace Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20180113140052 extends AbstractMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'sqlite', 'Migration can only be executed safely on \'sqlite\'.');
$this->addSql('CREATE TABLE t00e00 (id INTEGER NOT NULL, name_native VARCHAR(255) DEFAULT NULL, name_english VARCHAR(255) NOT NULL, host VARCHAR(255) NOT NULL, site VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE UNIQUE INDEX UNIQ_T00E00_HOST ON t00e00 (host)');
$this->addSql('CREATE TABLE t00i00 (id INTEGER NOT NULL, name_english VARCHAR(255) NOT NULL, name_native VARCHAR(255) NOT NULL, site_code VARCHAR(4) NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE UNIQUE INDEX UNIQ_T00I00_SITE_CODE ON t00i00 (site_code)');
}
/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'sqlite', 'Migration can only be executed safely on \'sqlite\'.');
$this->addSql('DROP TABLE t00e00');
$this->addSql('DROP TABLE t00i00');
}
}
Migration Generate
We need add some seeds/initialization data for this reason we need to execute bin/console doctrine:migrations:generate
command to create and empty Doctrine Migration class.
// src/DoctrineMigrations/Version20180113140130.php
namespace Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20180113140130 extends AbstractMigration{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'sqlite', 'Migration can only be executed safely on \'sqlite\'.');
$this->addSql("INSERT INTO t00e00(name_native, name_english, host, site) VALUES('Shahed', 'Shahed','shahed.biz', 'site')");
$this->addSql("INSERT INTO t00e00(name_native, name_english, host, site) VALUES('Chorke', 'Chorke','chorke.org', 'site')");
$this->addSql("INSERT INTO t00e00(name_native, name_english, host, site) VALUES('Chorke', 'Chorke','chorke.com', 'site')");
}
/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'sqlite', 'Migration can only be executed safely on \'sqlite\'.');
$this->addSql("DELETE FROM t00e00 WHERE host = 'shahed.biz'");
$this->addSql("DELETE FROM t00e00 WHERE host = 'chorke.org'");
$this->addSql("DELETE FROM t00e00 WHERE host = 'chorke.com'");
}
}
You can execute as many as your needs of Doctrine Migration classes. You can also delete unnecessary generates Doctrine Migration classes.
// src/DoctrineMigrations/Version20180113140230.php
namespace Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20180113140230 extends AbstractMigration{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'sqlite', 'Migration can only be executed safely on \'sqlite\'.');
$this->addSql("INSERT INTO t00i00(name_native, name_english, site_code) VALUES('Chorke', 'Chorke', 'init')");
}
/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'sqlite', 'Migration can only be executed safely on \'sqlite\'.');
$this->addSql("DELETE FROM t00i00 WHERE site_code = 'init'");
}
}
Doctrine Migrate
Execution of bin/console doctrine:migrations:migrate
command we can migrate to version of database from 0 to up to date. It's like an wizard and reduce the complexity of application migration.
WARNING! You are about to execute a database migration that could result in schema changes and data lost. Are you sure you wish to continue? (y/n)y Migrating up to 20180113140230 from 0 ++ migrating 20180113140052 -> CREATE TABLE t00e00 (id INTEGER NOT NULL, name_native VARCHAR(255) DEFAULT NULL, name_english VARCHAR(255) NOT NULL, host VARCHAR(255) NOT NULL, site VARCHAR(255) NOT NULL, PRIMARY KEY(id)) -> CREATE UNIQUE INDEX UNIQ_T00E00_HOST ON t00e00 (host) -> CREATE TABLE t00i00 (id INTEGER NOT NULL, name_english VARCHAR(255) NOT NULL, name_native VARCHAR(255) NOT NULL, site_code VARCHAR(4) NOT NULL, PRIMARY KEY(id)) -> CREATE UNIQUE INDEX UNIQ_T00I00_SITE_CODE ON t00i00 (site_code) ++ migrated (0.12s) ++ migrating 20180113140130 -> INSERT INTO t00e00(name_native, name_english, host, site) VALUES('Shahed', 'Shahed','shahed.biz', 'site') -> INSERT INTO t00e00(name_native, name_english, host, site) VALUES('Chorke', 'Chorke','chorke.org', 'site') -> INSERT INTO t00e00(name_native, name_english, host, site) VALUES('Chorke', 'Chorke','chorke.com', 'site') ++ migrated (0s) ++ migrating 20180113140230 -> INSERT INTO t00i00(name_native, name_english, site_code) VALUES('Chorke', 'Chorke', 'init') ++ migrated (0s) ------------------------ ++ finished in 0.12s ++ 3 migrations executed ++ 8 sql queries