From b0a6fbfb7ffb0388d25cda0a1c2eb5f7178d6bf4 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Thu, 28 Feb 2019 02:44:51 +0300 Subject: [PATCH] Comment DB unique constraint (post_id, number). New migration. --- .../Version20190227233244.php | 28 +++++++++++++++++++ .../PointToolsBundle/Entity/Blogs/Comment.php | 2 ++ 2 files changed, 30 insertions(+) create mode 100644 app/DoctrineMigrations/Version20190227233244.php diff --git a/app/DoctrineMigrations/Version20190227233244.php b/app/DoctrineMigrations/Version20190227233244.php new file mode 100644 index 0000000..f253901 --- /dev/null +++ b/app/DoctrineMigrations/Version20190227233244.php @@ -0,0 +1,28 @@ +abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.'); + + $this->addSql('CREATE UNIQUE INDEX unique_post_id_comment_number ON posts.comments (post_id, number)'); + } + + public function down(Schema $schema) : void + { + // this down() migration is auto-generated, please modify it to your needs + $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.'); + + $this->addSql('DROP INDEX posts.unique_post_id_comment_number'); + } +} diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/Blogs/Comment.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/Blogs/Comment.php index 6c114f2..2d1937a 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/Blogs/Comment.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/Blogs/Comment.php @@ -9,6 +9,8 @@ use Skobkin\Bundle\PointToolsBundle\Entity\User; /** * @ORM\Table(name="comments", schema="posts", indexes={ * @ORM\Index(name="idx_comment_created_at", columns={"created_at"}) + * }, uniqueConstraints={ + * @ORM\UniqueConstraint(name="unique_post_id_comment_number", columns={"post_id", "number"}) * }) * @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\Blogs\CommentRepository") */