From b7c6539477c02fdf9d004558ff8a3ae98c09c263 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Thu, 28 May 2015 20:36:52 +0300 Subject: [PATCH] Moved to PostgreSQL. --- .../Version20150329170323.php | 46 --------------- .../Version20150528203127.php | 59 +++++++++++++++++++ app/config/config.yml | 6 -- app/config/parameters.yml.dist | 8 +-- .../PointToolsBundle/Entity/Subscription.php | 6 +- .../Entity/SubscriptionEvent.php | 44 +++++++------- .../Entity/SubscriptionRepository.php | 15 ----- .../Bundle/PointToolsBundle/Entity/User.php | 43 ++++++++++++-- 8 files changed, 127 insertions(+), 100 deletions(-) delete mode 100644 app/DoctrineMigrations/Version20150329170323.php create mode 100644 app/DoctrineMigrations/Version20150528203127.php delete mode 100644 src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionRepository.php diff --git a/app/DoctrineMigrations/Version20150329170323.php b/app/DoctrineMigrations/Version20150329170323.php deleted file mode 100644 index bc7c777..0000000 --- a/app/DoctrineMigrations/Version20150329170323.php +++ /dev/null @@ -1,46 +0,0 @@ -abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.'); - - $this->addSql('CREATE TABLE users (id INT AUTO_INCREMENT NOT NULL, login VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, INDEX idx_name (name), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'); - $this->addSql('CREATE TABLE subscriptions_events (id INT AUTO_INCREMENT NOT NULL, subscriber_id INT NOT NULL, author_id INT NOT NULL, date DATETIME NOT NULL, action VARCHAR(12) NOT NULL, INDEX IDX_7778274B7808B1AD (subscriber_id), INDEX IDX_7778274BF675F31B (author_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'); - $this->addSql('CREATE TABLE subscriptions (author_id INT NOT NULL, subscriber_id INT NOT NULL, INDEX IDX_4778A01F675F31B (author_id), INDEX IDX_4778A017808B1AD (subscriber_id), PRIMARY KEY(author_id, subscriber_id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'); - $this->addSql('ALTER TABLE subscriptions_events ADD CONSTRAINT FK_7778274B7808B1AD FOREIGN KEY (subscriber_id) REFERENCES users (id)'); - $this->addSql('ALTER TABLE subscriptions_events ADD CONSTRAINT FK_7778274BF675F31B FOREIGN KEY (author_id) REFERENCES users (id)'); - $this->addSql('ALTER TABLE subscriptions ADD CONSTRAINT FK_4778A01F675F31B FOREIGN KEY (author_id) REFERENCES users (id)'); - $this->addSql('ALTER TABLE subscriptions ADD CONSTRAINT FK_4778A017808B1AD FOREIGN KEY (subscriber_id) REFERENCES users (id)'); - } - - /** - * @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() != 'mysql', 'Migration can only be executed safely on \'mysql\'.'); - - $this->addSql('ALTER TABLE subscriptions_events DROP FOREIGN KEY FK_7778274B7808B1AD'); - $this->addSql('ALTER TABLE subscriptions_events DROP FOREIGN KEY FK_7778274BF675F31B'); - $this->addSql('ALTER TABLE subscriptions DROP FOREIGN KEY FK_4778A01F675F31B'); - $this->addSql('ALTER TABLE subscriptions DROP FOREIGN KEY FK_4778A017808B1AD'); - $this->addSql('DROP TABLE users'); - $this->addSql('DROP TABLE subscriptions_events'); - $this->addSql('DROP TABLE subscriptions'); - } -} diff --git a/app/DoctrineMigrations/Version20150528203127.php b/app/DoctrineMigrations/Version20150528203127.php new file mode 100644 index 0000000..31b89a7 --- /dev/null +++ b/app/DoctrineMigrations/Version20150528203127.php @@ -0,0 +1,59 @@ +abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.'); + + $this->addSql('CREATE SCHEMA IF NOT EXISTS users'); + $this->addSql('CREATE SCHEMA IF NOT EXISTS subscriptions'); + + $this->addSql('CREATE SEQUENCE subscriptions.log_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); + $this->addSql('CREATE TABLE subscriptions.subscriptions (author_id INT NOT NULL, subscriber_id INT NOT NULL, PRIMARY KEY(author_id, subscriber_id))'); + $this->addSql('CREATE INDEX IDX_3B7621A2F675F31B ON subscriptions.subscriptions (author_id)'); + $this->addSql('CREATE INDEX IDX_3B7621A27808B1AD ON subscriptions.subscriptions (subscriber_id)'); + $this->addSql('CREATE UNIQUE INDEX subscription_unique ON subscriptions.subscriptions (author_id, subscriber_id)'); + $this->addSql('CREATE TABLE subscriptions.log (id INT NOT NULL, author_id INT NOT NULL, subscriber_id INT NOT NULL, date TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, action VARCHAR(12) NOT NULL, PRIMARY KEY(id))'); + $this->addSql('CREATE INDEX IDX_22DA64DDF675F31B ON subscriptions.log (author_id)'); + $this->addSql('CREATE INDEX IDX_22DA64DD7808B1AD ON subscriptions.log (subscriber_id)'); + $this->addSql('CREATE INDEX date_idx ON subscriptions.log (date)'); + $this->addSql('CREATE TABLE users.users (id INT NOT NULL, login VARCHAR(255) NOT NULL, name VARCHAR(255) DEFAULT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, PRIMARY KEY(id))'); + $this->addSql('CREATE UNIQUE INDEX UNIQ_338ADFC4AA08CB10 ON users.users (login)'); + $this->addSql('ALTER TABLE subscriptions.subscriptions ADD CONSTRAINT FK_3B7621A2F675F31B FOREIGN KEY (author_id) REFERENCES users.users (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE subscriptions.subscriptions ADD CONSTRAINT FK_3B7621A27808B1AD FOREIGN KEY (subscriber_id) REFERENCES users.users (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE subscriptions.log ADD CONSTRAINT FK_22DA64DDF675F31B FOREIGN KEY (author_id) REFERENCES users.users (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE subscriptions.log ADD CONSTRAINT FK_22DA64DD7808B1AD FOREIGN KEY (subscriber_id) REFERENCES users.users (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + } + + /** + * @param Schema $schema + */ + public function down(Schema $schema) + { + $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.'); + + $this->addSql('ALTER TABLE subscriptions.subscriptions DROP CONSTRAINT FK_3B7621A2F675F31B'); + $this->addSql('ALTER TABLE subscriptions.subscriptions DROP CONSTRAINT FK_3B7621A27808B1AD'); + $this->addSql('ALTER TABLE subscriptions.log DROP CONSTRAINT FK_22DA64DDF675F31B'); + $this->addSql('ALTER TABLE subscriptions.log DROP CONSTRAINT FK_22DA64DD7808B1AD'); + $this->addSql('DROP SEQUENCE subscriptions.log_id_seq CASCADE'); + $this->addSql('DROP TABLE subscriptions.subscriptions'); + $this->addSql('DROP TABLE subscriptions.log'); + $this->addSql('DROP TABLE users.users'); + + $this->addSql('DROP SCHEMA IF EXISTS users'); + $this->addSql('DROP SCHEMA IF EXISTS subscriptions'); + } +} diff --git a/app/config/config.yml b/app/config/config.yml index b916725..042fec5 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -53,12 +53,6 @@ doctrine: user: "%database_user%" password: "%database_password%" charset: UTF8 - # if using pdo_sqlite as your database driver: - # 1. add the path in parameters.yml - # e.g. database_path: "%kernel.root_dir%/data/data.db3" - # 2. Uncomment database_path in parameters.yml.dist - # 3. Uncomment next line: - # path: "%database_path%" orm: auto_generate_proxy_classes: "%kernel.debug%" diff --git a/app/config/parameters.yml.dist b/app/config/parameters.yml.dist index 1da778f..48232d5 100644 --- a/app/config/parameters.yml.dist +++ b/app/config/parameters.yml.dist @@ -1,13 +1,11 @@ # This file is a "template" of what your parameters.yml file should look like parameters: - database_driver: pdo_mysql + database_driver: pdo_pgsql database_host: 127.0.0.1 database_port: ~ - database_name: symfony - database_user: root + database_name: point + database_user: point database_password: ~ - # You should uncomment this if you want use pdo_sqlite - # database_path: "%kernel.root_dir%/data.db3" mailer_transport: smtp mailer_host: 127.0.0.1 diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/Subscription.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/Subscription.php index d69f5d7..03b18a3 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/Subscription.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/Subscription.php @@ -7,8 +7,10 @@ use Doctrine\ORM\Mapping as ORM; /** * Subscription * - * @ORM\Table(name="subscriptions") - * @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Entity\SubscriptionRepository") + * @ORM\Table(name="subscriptions.subscriptions", uniqueConstraints={ + * @ORM\UniqueConstraint(name="subscription_unique", columns={"author_id", "subscriber_id"})} + * ) + * @ORM\Entity */ class Subscription { diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEvent.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEvent.php index d72433a..981912e 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEvent.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionEvent.php @@ -7,7 +7,11 @@ use Doctrine\ORM\Mapping as ORM; /** * SubscriptionEvent * - * @ORM\Table(name="subscriptions_events") + * @ORM\Table(name="subscriptions.log", indexes={ + * @ORM\Index(name="author_idx", columns={"author_id"}), + * @ORM\Index(name="subscriber_idx", columns={"subscriber_id"}), + * @ORM\Index(name="date_idx", columns={"date"}) + * }) * @ORM\Entity */ class SubscriptionEvent @@ -23,29 +27,29 @@ class SubscriptionEvent * @ORM\GeneratedValue(strategy="AUTO") */ private $id; - + + /** + * @var User Blog author + * + * @ORM\ManyToOne(targetEntity="User", inversedBy="newSubscriberEvents") + * @ORM\JoinColumn(name="author_id", nullable=false) + */ + private $author; + /** * @var User Blog subscriber - * + * * @ORM\ManyToOne(targetEntity="User", inversedBy="newSubscriptionEvents") * @ORM\JoinColumn(name="subscriber_id", nullable=false) */ private $subscriber; - /** - * @var User Blog author - * - * @ORM\ManyToOne(targetEntity="User", inversedBy="newSubscriberEvents") - * @ORM\JoinColumn(name="author_id", nullable=false) - */ - private $author; - /** * @var \DateTime * * @ORM\Column(name="date", type="datetime", nullable=false) */ - private $dateTime; + private $date; /** * @var string @@ -57,7 +61,7 @@ class SubscriptionEvent /** * Get id * - * @return integer + * @return integer */ public function getId() { @@ -65,26 +69,26 @@ class SubscriptionEvent } /** - * Set dateTime + * Set date * - * @param \DateTime $dateTime + * @param \DateTime $date * @return SubscriptionEvent */ - public function setDateTime($dateTime) + public function setDate($date) { - $this->dateTime = $dateTime; + $this->date = $date; return $this; } /** - * Get dateTime + * Get date * * @return \DateTime */ - public function getDateTime() + public function getDate() { - return $this->dateTime; + return $this->date; } /** diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionRepository.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionRepository.php deleted file mode 100644 index 51c206f..0000000 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/SubscriptionRepository.php +++ /dev/null @@ -1,15 +0,0 @@ -newSubscriptionEvents = new ArrayCollection(); } + /** + * @ORM\PrePersist + */ + public function onCreate() + { + $this->createdAt = new \DateTime(); + } + /** * Get id * @@ -274,4 +287,22 @@ class User { return $this->newSubscriberEvents; } + + /** + * @return \DateTime + */ + public function getCreatedAt() + { + return $this->createdAt; + } + + /** + * @param \DateTime $createdAt + * @return User + */ + public function setCreatedAt($createdAt) + { + $this->createdAt = $createdAt; + return $this; + } }