Skip to content

Commit

Permalink
Implementando migrations (Coderockr#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitornp committed Aug 8, 2014
1 parent c725ab4 commit fdde9d3
Show file tree
Hide file tree
Showing 18 changed files with 170 additions and 122 deletions.
34 changes: 24 additions & 10 deletions bin/doctrine.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
<?php

include __DIR__.'/../bootstrap.php';
use Doctrine\ORM\EntityManager;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\HelperSet;

$helpers = array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
);
include __DIR__ . '/../bootstrap.php';

$configApp = require_once __DIR__ . '/../config/config.php';

$em = EntityManager::create($configApp['db.options'], $config);

$cli = new \Symfony\Component\Console\Application('Doctrine Command Line Interface', Doctrine\Common\Version::VERSION);
$cli = new Application('Gerenciamento de Orçamentos', Doctrine\Common\Version::VERSION);
$cli->setCatchExceptions(true);
$helperSet = $cli->getHelperSet();
foreach ($helpers as $name => $helper) {
$helperSet->set($helper, $name);
}
$cli->setHelperSet(new HelperSet([
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em),
'dialog' => $cli->getHelperSet()->get('dialog')
]));

$cli->addCommands(array(
// DBAL Commands
new \Doctrine\DBAL\Tools\Console\Command\RunSqlCommand(),
Expand All @@ -34,5 +39,14 @@
new \Doctrine\ORM\Tools\Console\Command\RunDqlCommand(),
new \Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand(),

// Migrations Commands
new \Doctrine\DBAL\Migrations\Tools\Console\Command\DiffCommand(),
new \Doctrine\DBAL\Migrations\Tools\Console\Command\ExecuteCommand(),
new \Doctrine\DBAL\Migrations\Tools\Console\Command\GenerateCommand(),
new \Doctrine\DBAL\Migrations\Tools\Console\Command\LatestCommand(),
new \Doctrine\DBAL\Migrations\Tools\Console\Command\MigrateCommand(),
new \Doctrine\DBAL\Migrations\Tools\Console\Command\StatusCommand(),
new \Doctrine\DBAL\Migrations\Tools\Console\Command\VersionCommand(),

));
$cli->run();
2 changes: 1 addition & 1 deletion bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\ORM\Configuration;

$loader = require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/vendor/autoload.php';

//doctrine
$config = new Configuration();
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"symfony/console": "*",
"doctrine/dbal": "*",
"doctrine/orm": "*",
"doctrine/migrations": "1.0.*@dev",
"dflydev/doctrine-orm-service-provider": "1.0.*@dev",
"twig/twig": ">=1.8,<2.0-dev",
"symfony/twig-bridge": "2.1.*",
Expand All @@ -23,7 +24,8 @@
},
"autoload": {
"psr-4": {
"Orcamentos\\": "src/Orcamentos"
"Orcamentos\\": "src/Orcamentos",
"Orcamentos\\Migrations\\": "migrations"
}
},
"minimum-stability": "dev"
Expand Down
4 changes: 4 additions & 0 deletions migrations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: Gerenciamento de Orçamentos - Migrations
migrations_namespace: Orcamentos\Migrations
table_name: doctrine_migration_versions
migrations_directory: migrations
77 changes: 77 additions & 0 deletions migrations/Version20140807204731.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Orcamentos\Migrations;

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

class Version20140807204731 extends AbstractMigration
{
public function up(Schema $schema)
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql", "Migration can only be executed safely on 'mysql'.");

$this->addSql("CREATE TABLE client (id INT AUTO_INCREMENT NOT NULL, company_id INT DEFAULT NULL, name VARCHAR(255) NOT NULL, cnpj VARCHAR(14) NOT NULL, corporateName VARCHAR(255) DEFAULT NULL, email VARCHAR(255) DEFAULT NULL, logotype VARCHAR(255) DEFAULT NULL, telephone VARCHAR(255) DEFAULT NULL, responsable VARCHAR(255) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, INDEX IDX_C7440455979B1AD6 (company_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB");
$this->addSql("CREATE TABLE company (id INT AUTO_INCREMENT NOT NULL, plan_id INT DEFAULT NULL, name VARCHAR(255) NOT NULL, city VARCHAR(255) DEFAULT NULL, taxes DOUBLE PRECISION NOT NULL, email VARCHAR(255) NOT NULL, telephone VARCHAR(255) NOT NULL, site VARCHAR(255) DEFAULT NULL, logotype VARCHAR(255) DEFAULT NULL, responsable VARCHAR(255) DEFAULT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, INDEX IDX_4FBF094FE899029B (plan_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB");
$this->addSql("CREATE TABLE type (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, type VARCHAR(255) NOT NULL, contractType VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB");
$this->addSql("CREATE TABLE plan (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, price DOUBLE PRECISION DEFAULT NULL, quoteLimit INT DEFAULT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB");
$this->addSql("CREATE TABLE privatenote (id INT AUTO_INCREMENT NOT NULL, project_id INT DEFAULT NULL, user_id INT DEFAULT NULL, note LONGTEXT DEFAULT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, INDEX IDX_FFBFCCE1166D1F9C (project_id), INDEX IDX_FFBFCCE1A76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB");
$this->addSql("CREATE TABLE project (id INT AUTO_INCREMENT NOT NULL, client_id INT DEFAULT NULL, company_id INT DEFAULT NULL, name VARCHAR(255) NOT NULL, description LONGTEXT DEFAULT NULL, tags VARCHAR(255) DEFAULT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, INDEX IDX_2FB3D0EE19EB6921 (client_id), INDEX IDX_2FB3D0EE979B1AD6 (company_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB");
$this->addSql("CREATE TABLE quote (id INT AUTO_INCREMENT NOT NULL, project_id INT DEFAULT NULL, dueDate DATETIME DEFAULT NULL, taxes DOUBLE PRECISION NOT NULL, version VARCHAR(150) NOT NULL, status INT NOT NULL, profit DOUBLE PRECISION NOT NULL, commission DOUBLE PRECISION NOT NULL, privateNotes LONGTEXT DEFAULT NULL, deadline LONGTEXT DEFAULT NULL, priceDescription LONGTEXT DEFAULT NULL, paymentType LONGTEXT DEFAULT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, INDEX IDX_6B71CBF4166D1F9C (project_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB");
$this->addSql("CREATE TABLE resource (id INT AUTO_INCREMENT NOT NULL, company_id INT DEFAULT NULL, type_id INT DEFAULT NULL, name VARCHAR(150) NOT NULL, cost DOUBLE PRECISION NOT NULL, equipmentLife INT DEFAULT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, INDEX IDX_BC91F416979B1AD6 (company_id), INDEX IDX_BC91F416C54C8C93 (type_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB");
$this->addSql("CREATE TABLE resourcequote (id INT AUTO_INCREMENT NOT NULL, resource_id INT DEFAULT NULL, quote_id INT DEFAULT NULL, amount DOUBLE PRECISION NOT NULL, value DOUBLE PRECISION NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, INDEX IDX_39741B5589329D25 (resource_id), INDEX IDX_39741B55DB805178 (quote_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB");
$this->addSql("CREATE TABLE share (id INT AUTO_INCREMENT NOT NULL, quote_id INT DEFAULT NULL, email VARCHAR(150) NOT NULL, hash VARCHAR(255) NOT NULL, shortUrl VARCHAR(255) NOT NULL, sent TINYINT(1) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, INDEX IDX_EF069D5ADB805178 (quote_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB");
$this->addSql("CREATE TABLE sharenote (id INT AUTO_INCREMENT NOT NULL, share_id INT DEFAULT NULL, note LONGTEXT DEFAULT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, INDEX IDX_F829661E2AE63FDB (share_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB");
$this->addSql("CREATE TABLE user (id INT AUTO_INCREMENT NOT NULL, company_id INT DEFAULT NULL, name VARCHAR(150) NOT NULL, email VARCHAR(150) NOT NULL, password VARCHAR(100) NOT NULL, admin TINYINT(1) DEFAULT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_8D93D649E7927C74 (email), INDEX IDX_8D93D649979B1AD6 (company_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB");
$this->addSql("CREATE TABLE view (id INT AUTO_INCREMENT NOT NULL, share_id INT DEFAULT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, INDEX IDX_FEFDAB8E2AE63FDB (share_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB");
$this->addSql("ALTER TABLE client ADD CONSTRAINT FK_C7440455979B1AD6 FOREIGN KEY (company_id) REFERENCES company (id)");
$this->addSql("ALTER TABLE company ADD CONSTRAINT FK_4FBF094FE899029B FOREIGN KEY (plan_id) REFERENCES plan (id)");
$this->addSql("ALTER TABLE privatenote ADD CONSTRAINT FK_FFBFCCE1166D1F9C FOREIGN KEY (project_id) REFERENCES project (id)");
$this->addSql("ALTER TABLE privatenote ADD CONSTRAINT FK_FFBFCCE1A76ED395 FOREIGN KEY (user_id) REFERENCES user (id)");
$this->addSql("ALTER TABLE project ADD CONSTRAINT FK_2FB3D0EE19EB6921 FOREIGN KEY (client_id) REFERENCES client (id)");
$this->addSql("ALTER TABLE project ADD CONSTRAINT FK_2FB3D0EE979B1AD6 FOREIGN KEY (company_id) REFERENCES company (id)");
$this->addSql("ALTER TABLE quote ADD CONSTRAINT FK_6B71CBF4166D1F9C FOREIGN KEY (project_id) REFERENCES project (id)");
$this->addSql("ALTER TABLE resource ADD CONSTRAINT FK_BC91F416979B1AD6 FOREIGN KEY (company_id) REFERENCES company (id)");
$this->addSql("ALTER TABLE resource ADD CONSTRAINT FK_BC91F416C54C8C93 FOREIGN KEY (type_id) REFERENCES type (id)");
$this->addSql("ALTER TABLE resourcequote ADD CONSTRAINT FK_39741B5589329D25 FOREIGN KEY (resource_id) REFERENCES resource (id)");
$this->addSql("ALTER TABLE resourcequote ADD CONSTRAINT FK_39741B55DB805178 FOREIGN KEY (quote_id) REFERENCES quote (id)");
$this->addSql("ALTER TABLE share ADD CONSTRAINT FK_EF069D5ADB805178 FOREIGN KEY (quote_id) REFERENCES quote (id)");
$this->addSql("ALTER TABLE sharenote ADD CONSTRAINT FK_F829661E2AE63FDB FOREIGN KEY (share_id) REFERENCES share (id)");
$this->addSql("ALTER TABLE user ADD CONSTRAINT FK_8D93D649979B1AD6 FOREIGN KEY (company_id) REFERENCES company (id)");
$this->addSql("ALTER TABLE view ADD CONSTRAINT FK_FEFDAB8E2AE63FDB FOREIGN KEY (share_id) REFERENCES share (id)");
}

public function down(Schema $schema)
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql", "Migration can only be executed safely on 'mysql'.");

$this->addSql("ALTER TABLE project DROP FOREIGN KEY FK_2FB3D0EE19EB6921");
$this->addSql("ALTER TABLE client DROP FOREIGN KEY FK_C7440455979B1AD6");
$this->addSql("ALTER TABLE project DROP FOREIGN KEY FK_2FB3D0EE979B1AD6");
$this->addSql("ALTER TABLE resource DROP FOREIGN KEY FK_BC91F416979B1AD6");
$this->addSql("ALTER TABLE user DROP FOREIGN KEY FK_8D93D649979B1AD6");
$this->addSql("ALTER TABLE resource DROP FOREIGN KEY FK_BC91F416C54C8C93");
$this->addSql("ALTER TABLE company DROP FOREIGN KEY FK_4FBF094FE899029B");
$this->addSql("ALTER TABLE privatenote DROP FOREIGN KEY FK_FFBFCCE1166D1F9C");
$this->addSql("ALTER TABLE quote DROP FOREIGN KEY FK_6B71CBF4166D1F9C");
$this->addSql("ALTER TABLE resourcequote DROP FOREIGN KEY FK_39741B55DB805178");
$this->addSql("ALTER TABLE share DROP FOREIGN KEY FK_EF069D5ADB805178");
$this->addSql("ALTER TABLE resourcequote DROP FOREIGN KEY FK_39741B5589329D25");
$this->addSql("ALTER TABLE sharenote DROP FOREIGN KEY FK_F829661E2AE63FDB");
$this->addSql("ALTER TABLE view DROP FOREIGN KEY FK_FEFDAB8E2AE63FDB");
$this->addSql("ALTER TABLE privatenote DROP FOREIGN KEY FK_FFBFCCE1A76ED395");
$this->addSql("DROP TABLE client");
$this->addSql("DROP TABLE company");
$this->addSql("DROP TABLE type");
$this->addSql("DROP TABLE plan");
$this->addSql("DROP TABLE privatenote");
$this->addSql("DROP TABLE project");
$this->addSql("DROP TABLE quote");
$this->addSql("DROP TABLE resource");
$this->addSql("DROP TABLE resourcequote");
$this->addSql("DROP TABLE share");
$this->addSql("DROP TABLE sharenote");
$this->addSql("DROP TABLE user");
$this->addSql("DROP TABLE view");
}
}
5 changes: 2 additions & 3 deletions src/Orcamentos/Model/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
namespace Orcamentos\Model;

use Doctrine\ORM\Mapping as ORM;
use DateTime;

/**
* @ORM\Entity
* @ORM\Table(name="Client")
* @ORM\Table(name="client")
*/
class Client extends Entity
{
Expand Down Expand Up @@ -70,7 +69,7 @@ class Client extends Entity
/**
* @ORM\OneToMany(targetEntity="Project", mappedBy="client", cascade={"all"}, orphanRemoval=true, fetch="LAZY")
*
* @var Doctrine\Common\Collections\Collection
* @var \Doctrine\Common\Collections\Collection
*/
protected $projectCollection;

Expand Down
10 changes: 5 additions & 5 deletions src/Orcamentos/Model/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/**
* @ORM\Entity
* @ORM\Table(name="Company")
* @ORM\Table(name="company")
*/
class Company extends Entity
{
Expand Down Expand Up @@ -69,30 +69,30 @@ class Company extends Entity
* @ORM\OneToMany(targetEntity="Client", mappedBy="company", cascade={"all"}, orphanRemoval=true, fetch="LAZY")
*
* @ORM\OrderBy({"name" = "ASC"})
* @var Doctrine\Common\Collections\Collection
* @var \Doctrine\Common\Collections\Collection
*/
protected $clientCollection;

/**
* @ORM\OneToMany(targetEntity="Project", mappedBy="company", cascade={"all"}, orphanRemoval=true, fetch="LAZY")
* @ORM\OrderBy({"name" = "ASC"})
*
* @var Doctrine\Common\Collections\Collection
* @var \Doctrine\Common\Collections\Collection
*/
protected $projectCollection;

/**
* @ORM\OneToMany(targetEntity="Resource", mappedBy="company", cascade={"all"}, orphanRemoval=true, fetch="LAZY")
*
* @var Doctrine\Common\Collections\Collection
* @var \Doctrine\Common\Collections\Collection
*/
protected $resourceCollection;

/**
* @ORM\OneToMany(targetEntity="User", mappedBy="company", cascade={"all"}, orphanRemoval=true, fetch="LAZY")
* @ORM\OrderBy({"name" = "ASC"})
*
* @var Doctrine\Common\Collections\Collection
* @var \Doctrine\Common\Collections\Collection
*/
protected $userCollection;

Expand Down
17 changes: 14 additions & 3 deletions src/Orcamentos/Model/Plan.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/**
* @ORM\Entity
* @ORM\Table(name="Plan")
* @ORM\Table(name="plan")
*/

class Plan extends Entity
Expand Down Expand Up @@ -33,14 +33,14 @@ class Plan extends Entity

/* @ORM\Column(type="text", nullable=false)
*
* @var text
* @var string
*/
private $description;

/**
* @ORM\OneToMany(targetEntity="Company", mappedBy="plan", cascade={"all"}, orphanRemoval=true, fetch="LAZY")
*
* @var Doctrine\Common\Collections\Collection
* @var \Doctrine\Common\Collections\Collection
*/
protected $companyCollection;

Expand Down Expand Up @@ -83,4 +83,15 @@ public function setPrice($price)
{
return $this->price = $price;
}

public function setDescription($description)
{
$this->description = $description;
}

public function getDescription()
{
return $this->description;
}

}
5 changes: 2 additions & 3 deletions src/Orcamentos/Model/PrivateNote.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
namespace Orcamentos\Model;

use Doctrine\ORM\Mapping as ORM;
use DateTime;

/**
* @ORM\Entity
* @ORM\Table(name="PrivateNote")
* @ORM\Table(name="privatenote")
*/
class PrivateNote extends Entity
{
Expand All @@ -28,7 +27,7 @@ class PrivateNote extends Entity
/**
* @ORM\Column(type="text", nullable=true)
*
* @var text
* @var string
*/
private $note;

Expand Down
18 changes: 4 additions & 14 deletions src/Orcamentos/Model/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/**
* @ORM\Entity
* @ORM\Table(name="Project")
* @ORM\Table(name="project")
*/
class Project extends Entity
{
Expand All @@ -20,7 +20,7 @@ class Project extends Entity
/**
* @ORM\Column(type="text", nullable=true)
*
* @var text
* @var string
*/
private $description;

Expand Down Expand Up @@ -48,14 +48,14 @@ class Project extends Entity
/**
* @ORM\OneToMany(targetEntity="Quote", mappedBy="project", cascade={"all"}, orphanRemoval=true, fetch="LAZY")
*
* @var Doctrine\Common\Collections\Collection
* @var \Doctrine\Common\Collections\Collection
*/
protected $quoteCollection;

/**
* @ORM\OneToMany(targetEntity="PrivateNote", mappedBy="project", cascade={"all"}, orphanRemoval=true, fetch="LAZY")
* @ORM\OrderBy({"created" = "DESC"})
* @var Doctrine\Common\Collections\Collection
* @var \Doctrine\Common\Collections\Collection
*/
protected $privateNotesCollection;

Expand Down Expand Up @@ -93,16 +93,6 @@ public function setDescription($description)
{
return $this->description = $description;
}

public function getClientNotes()
{
return $this->clientNotes;
}

public function setClientNotes($clientNotes)
{
return $this->clientNotes = $clientNotes;
}

public function getTags()
{
Expand Down
Loading

0 comments on commit fdde9d3

Please sign in to comment.