From f02753ac6206bdbad7cc1c9151bba6ddc440090b Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Thu, 6 Feb 2020 12:43:16 +0100 Subject: [PATCH] Migration to database (MariaDB) + Change 'Group' to 'TargetGroup' --- .env | 4 - .idea/.gitignore | 5 +- .idea/dataSources.xml | 11 +++ src/Entity/Member.php | 14 +-- src/Entity/{Group.php => TargetGroup.php} | 10 +-- src/Entity/User.php | 28 ++++++ src/Entity/Wish.php | 6 +- src/Migrations/Version20200206113340.php | 85 +++++++++++++++++++ ...pository.php => TargetGroupRepository.php} | 14 +-- 9 files changed, 150 insertions(+), 27 deletions(-) create mode 100644 .idea/dataSources.xml rename src/Entity/{Group.php => TargetGroup.php} (90%) create mode 100644 src/Migrations/Version20200206113340.php rename src/Repository/{GroupRepository.php => TargetGroupRepository.php} (67%) diff --git a/.env b/.env index 3d62b8d..85a5165 100644 --- a/.env +++ b/.env @@ -4,7 +4,3 @@ APP_ENV=dev APP_SECRET=02d2777e0242595653c8a47180aeba9e ###< symfony/framework-bundle ### - -###> doctrine/doctrine-bundle ### -DATABASE_URL=pdo-mysql://wishlist:wishlist@localhost:3306/wishlist?serverVersion=mariadb-10.4.7&charset=UTF-8 -###< doctrine/doctrine-bundle ### diff --git a/.idea/.gitignore b/.idea/.gitignore index e51cd25..e6bb9f3 100644 --- a/.idea/.gitignore +++ b/.idea/.gitignore @@ -1,3 +1,6 @@ # Default ignored files /workspace.xml -/deployment.xml \ No newline at end of file +/deployment.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..ebb2232 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,11 @@ + + + + + mariadb + true + org.mariadb.jdbc.Driver + jdbc:mariadb://localhost:3306/wishlist + + + \ No newline at end of file diff --git a/src/Entity/Member.php b/src/Entity/Member.php index 5d41284..e19f679 100644 --- a/src/Entity/Member.php +++ b/src/Entity/Member.php @@ -16,10 +16,10 @@ class Member { private $id; /** - * @ORM\ManyToOne(targetEntity="App\Entity\Group", inversedBy="members") - * @ORM\JoinColumn(name="group_id", nullable=false) + * @ORM\ManyToOne(targetEntity="App\Entity\TargetGroup", inversedBy="members") + * @ORM\JoinColumn(nullable=false) */ - private $group; + private $targetGroup; /** * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="memberOfGroups") @@ -35,12 +35,12 @@ class Member { return $this->id; } - public function getGroup (): ?Group { - return $this->group; + public function getTargetGroup (): ?TargetGroup { + return $this->targetGroup; } - public function setGroup (?Group $group): self { - $this->group = $group; + public function setTargetGroup (?TargetGroup $targetGroup): self { + $this->targetGroup = $targetGroup; return $this; } diff --git a/src/Entity/Group.php b/src/Entity/TargetGroup.php similarity index 90% rename from src/Entity/Group.php rename to src/Entity/TargetGroup.php index 3f025fd..4a25e23 100644 --- a/src/Entity/Group.php +++ b/src/Entity/TargetGroup.php @@ -7,9 +7,9 @@ use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; /** - * @ORM\Entity(repositoryClass="App\Repository\GroupRepository") + * @ORM\Entity(repositoryClass="App\Repository\TargetGroupRepository") */ -class Group +class TargetGroup { /** * @ORM\Id() @@ -68,7 +68,7 @@ class Group { if (!$this->members->contains($member)) { $this->members[] = $member; - $member->setGroup($this); + $member->setTargetGroup($this); } return $this; @@ -79,8 +79,8 @@ class Group if ($this->members->contains($member)) { $this->members->removeElement($member); // set the owning side to null (unless already changed) - if ($member->getGroup() === $this) { - $member->setGroup(null); + if ($member->getTargetGroup() === $this) { + $member->setTargetGroup(null); } } diff --git a/src/Entity/User.php b/src/Entity/User.php index 3f9b3c7..c47da02 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -85,6 +85,15 @@ class User implements UserInterface * @var ArrayCollection The user {@see User friends} * * @ORM\ManyToMany(targetEntity="App\Entity\User") + * @ORM\JoinTable( + * name="friend", + * joinColumns={ + * @ORM\JoinColumn(name="user_id", referencedColumnName="id") + * }, + * inverseJoinColumns={ + * @ORM\JoinColumn(name="friend_id", referencedColumnName="id") + * } + * ) */ private ArrayCollection $friends; @@ -123,6 +132,13 @@ class User implements UserInterface */ private ArrayCollection $comments; + /** + * @var bool Is the user inactive ? + * + * @ORM\Column(type="boolean") + */ + private $inactive; + public function __construct () { $this->friends = new ArrayCollection(); $this->configNotifications = new ArrayCollection(); @@ -431,4 +447,16 @@ class User implements UserInterface return $this; } + + public function getInactive(): ?bool + { + return $this->inactive; + } + + public function setInactive(bool $inactive): self + { + $this->inactive = $inactive; + + return $this; + } } diff --git a/src/Entity/Wish.php b/src/Entity/Wish.php index 259995d..9fbdab6 100644 --- a/src/Entity/Wish.php +++ b/src/Entity/Wish.php @@ -19,7 +19,7 @@ class Wish { private $id; /** - * @ORM\ManyToOne(targetEntity="App\Entity\Group", inversedBy="wishes") + * @ORM\ManyToOne(targetEntity="App\Entity\TargetGroup", inversedBy="wishes") * @ORM\JoinColumn(nullable=false) */ private $target; @@ -89,11 +89,11 @@ class Wish { return $this->id; } - public function getTarget (): ?Group { + public function getTarget (): ?TargetGroup { return $this->target; } - public function setTarget (?Group $target): self { + public function setTarget (?TargetGroup $target): self { $this->target = $target; return $this; diff --git a/src/Migrations/Version20200206113340.php b/src/Migrations/Version20200206113340.php new file mode 100644 index 0000000..2f56643 --- /dev/null +++ b/src/Migrations/Version20200206113340.php @@ -0,0 +1,85 @@ +abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + $this->addSql('CREATE TABLE comment (wish_id INT NOT NULL, user_id INT NOT NULL, date DATETIME NOT NULL, anonymous TINYINT(1) NOT NULL, content LONGTEXT NOT NULL, INDEX IDX_9474526C42B83698 (wish_id), INDEX IDX_9474526CA76ED395 (user_id), PRIMARY KEY(wish_id, user_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE config_notification (user_id INT NOT NULL, notif_id INT NOT NULL, site TINYINT(1) NOT NULL, email TINYINT(1) NOT NULL, INDEX IDX_6C50D7BDA76ED395 (user_id), INDEX IDX_6C50D7BD5E61BFFA (notif_id), PRIMARY KEY(user_id, notif_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE language (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(50) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE member (id INT AUTO_INCREMENT NOT NULL, target_group_id INT NOT NULL, user_id INT DEFAULT NULL, fakeuser_name VARCHAR(255) DEFAULT NULL, INDEX IDX_70E4FA7824FF092E (target_group_id), INDEX IDX_70E4FA78A76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE notification (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(50) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE participant (wish_id INT NOT NULL, user_id INT NOT NULL, anonymous TINYINT(1) NOT NULL, price NUMERIC(11, 2) DEFAULT NULL, paid TINYINT(1) NOT NULL, INDEX IDX_D79F6B1142B83698 (wish_id), INDEX IDX_D79F6B11A76ED395 (user_id), PRIMARY KEY(wish_id, user_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE target_group (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE user (id INT AUTO_INCREMENT NOT NULL, language_id INT DEFAULT NULL, email VARCHAR(180) NOT NULL, roles LONGTEXT NOT NULL COMMENT \'(DC2Type:json)\', password VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, fist_name VARCHAR(255) DEFAULT NULL, gender SMALLINT NOT NULL, avatar VARCHAR(255) DEFAULT NULL, inactive TINYINT(1) NOT NULL, UNIQUE INDEX UNIQ_8D93D649E7927C74 (email), INDEX IDX_8D93D64982F1BAF4 (language_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE friend (user_id INT NOT NULL, friend_id INT NOT NULL, INDEX IDX_55EEAC61A76ED395 (user_id), INDEX IDX_55EEAC616A5458E8 (friend_id), PRIMARY KEY(user_id, friend_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE user_notification (user_id INT NOT NULL, notif_id INT NOT NULL, date DATETIME NOT NULL, description VARCHAR(255) NOT NULL, seen TINYINT(1) NOT NULL, INDEX IDX_3F980AC8A76ED395 (user_id), INDEX IDX_3F980AC85E61BFFA (notif_id), PRIMARY KEY(user_id, notif_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE wish (id INT AUTO_INCREMENT NOT NULL, target_id INT NOT NULL, owner_id INT NOT NULL, date DATETIME NOT NULL, name VARCHAR(255) NOT NULL, description LONGTEXT DEFAULT NULL, visible TINYINT(1) NOT NULL, price_estimated NUMERIC(11, 2) DEFAULT NULL, price_real NUMERIC(11, 2) DEFAULT NULL, crowdfunding TINYINT(1) NOT NULL, finished DATETIME DEFAULT NULL, INDEX IDX_D7D174C9158E0B66 (target_id), INDEX IDX_D7D174C97E3C61F9 (owner_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('ALTER TABLE comment ADD CONSTRAINT FK_9474526C42B83698 FOREIGN KEY (wish_id) REFERENCES wish (id)'); + $this->addSql('ALTER TABLE comment ADD CONSTRAINT FK_9474526CA76ED395 FOREIGN KEY (user_id) REFERENCES user (id)'); + $this->addSql('ALTER TABLE config_notification ADD CONSTRAINT FK_6C50D7BDA76ED395 FOREIGN KEY (user_id) REFERENCES user (id)'); + $this->addSql('ALTER TABLE config_notification ADD CONSTRAINT FK_6C50D7BD5E61BFFA FOREIGN KEY (notif_id) REFERENCES notification (id)'); + $this->addSql('ALTER TABLE member ADD CONSTRAINT FK_70E4FA7824FF092E FOREIGN KEY (target_group_id) REFERENCES target_group (id)'); + $this->addSql('ALTER TABLE member ADD CONSTRAINT FK_70E4FA78A76ED395 FOREIGN KEY (user_id) REFERENCES user (id)'); + $this->addSql('ALTER TABLE participant ADD CONSTRAINT FK_D79F6B1142B83698 FOREIGN KEY (wish_id) REFERENCES wish (id)'); + $this->addSql('ALTER TABLE participant ADD CONSTRAINT FK_D79F6B11A76ED395 FOREIGN KEY (user_id) REFERENCES user (id)'); + $this->addSql('ALTER TABLE user ADD CONSTRAINT FK_8D93D64982F1BAF4 FOREIGN KEY (language_id) REFERENCES language (id)'); + $this->addSql('ALTER TABLE friend ADD CONSTRAINT FK_55EEAC61A76ED395 FOREIGN KEY (user_id) REFERENCES user (id)'); + $this->addSql('ALTER TABLE friend ADD CONSTRAINT FK_55EEAC616A5458E8 FOREIGN KEY (friend_id) REFERENCES user (id)'); + $this->addSql('ALTER TABLE user_notification ADD CONSTRAINT FK_3F980AC8A76ED395 FOREIGN KEY (user_id) REFERENCES user (id)'); + $this->addSql('ALTER TABLE user_notification ADD CONSTRAINT FK_3F980AC85E61BFFA FOREIGN KEY (notif_id) REFERENCES notification (id)'); + $this->addSql('ALTER TABLE wish ADD CONSTRAINT FK_D7D174C9158E0B66 FOREIGN KEY (target_id) REFERENCES target_group (id)'); + $this->addSql('ALTER TABLE wish ADD CONSTRAINT FK_D7D174C97E3C61F9 FOREIGN KEY (owner_id) REFERENCES user (id)'); + } + + public function down(Schema $schema) : void + { + // 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 user DROP FOREIGN KEY FK_8D93D64982F1BAF4'); + $this->addSql('ALTER TABLE config_notification DROP FOREIGN KEY FK_6C50D7BD5E61BFFA'); + $this->addSql('ALTER TABLE user_notification DROP FOREIGN KEY FK_3F980AC85E61BFFA'); + $this->addSql('ALTER TABLE member DROP FOREIGN KEY FK_70E4FA7824FF092E'); + $this->addSql('ALTER TABLE wish DROP FOREIGN KEY FK_D7D174C9158E0B66'); + $this->addSql('ALTER TABLE comment DROP FOREIGN KEY FK_9474526CA76ED395'); + $this->addSql('ALTER TABLE config_notification DROP FOREIGN KEY FK_6C50D7BDA76ED395'); + $this->addSql('ALTER TABLE member DROP FOREIGN KEY FK_70E4FA78A76ED395'); + $this->addSql('ALTER TABLE participant DROP FOREIGN KEY FK_D79F6B11A76ED395'); + $this->addSql('ALTER TABLE friend DROP FOREIGN KEY FK_55EEAC61A76ED395'); + $this->addSql('ALTER TABLE friend DROP FOREIGN KEY FK_55EEAC616A5458E8'); + $this->addSql('ALTER TABLE user_notification DROP FOREIGN KEY FK_3F980AC8A76ED395'); + $this->addSql('ALTER TABLE wish DROP FOREIGN KEY FK_D7D174C97E3C61F9'); + $this->addSql('ALTER TABLE comment DROP FOREIGN KEY FK_9474526C42B83698'); + $this->addSql('ALTER TABLE participant DROP FOREIGN KEY FK_D79F6B1142B83698'); + $this->addSql('DROP TABLE comment'); + $this->addSql('DROP TABLE config_notification'); + $this->addSql('DROP TABLE language'); + $this->addSql('DROP TABLE member'); + $this->addSql('DROP TABLE notification'); + $this->addSql('DROP TABLE participant'); + $this->addSql('DROP TABLE target_group'); + $this->addSql('DROP TABLE user'); + $this->addSql('DROP TABLE friend'); + $this->addSql('DROP TABLE user_notification'); + $this->addSql('DROP TABLE wish'); + } +} diff --git a/src/Repository/GroupRepository.php b/src/Repository/TargetGroupRepository.php similarity index 67% rename from src/Repository/GroupRepository.php rename to src/Repository/TargetGroupRepository.php index 6908d15..316a407 100644 --- a/src/Repository/GroupRepository.php +++ b/src/Repository/TargetGroupRepository.php @@ -2,21 +2,21 @@ namespace App\Repository; -use App\Entity\Group; +use App\Entity\TargetGroup; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Common\Persistence\ManagerRegistry; /** - * @method Group|null find($id, $lockMode = null, $lockVersion = null) - * @method Group|null findOneBy(array $criteria, array $orderBy = null) - * @method Group[] findAll() - * @method Group[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) + * @method TargetGroup|null find($id, $lockMode = null, $lockVersion = null) + * @method TargetGroup|null findOneBy(array $criteria, array $orderBy = null) + * @method TargetGroup[] findAll() + * @method TargetGroup[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) */ -class GroupRepository extends ServiceEntityRepository +class TargetGroupRepository extends ServiceEntityRepository { public function __construct(ManagerRegistry $registry) { - parent::__construct($registry, Group::class); + parent::__construct($registry, TargetGroup::class); } // /**