From 87255eaf1329eed16e46ae10156c75497c77af61 Mon Sep 17 00:00:00 2001
From: Kacper Donat <kadet1090@gmail.com>
Date: Sat, 14 Mar 2020 12:36:24 +0100
Subject: [PATCH] Fix situation when there are multiple first stops on same
 trip

---
 src/Entity/TripStopEntity.php            | 17 +++++++---
 src/Migrations/Version20200314112552.php | 43 ++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 5 deletions(-)
 create mode 100644 src/Migrations/Version20200314112552.php

diff --git a/src/Entity/TripStopEntity.php b/src/Entity/TripStopEntity.php
index f90df10..f80b012 100644
--- a/src/Entity/TripStopEntity.php
+++ b/src/Entity/TripStopEntity.php
@@ -4,6 +4,7 @@ namespace App\Entity;
 
 use App\Model\Fillable;
 use App\Model\FillTrait;
+use App\Model\Referable;
 use App\Model\Trip;
 use App\Service\IdUtils;
 use Carbon\Carbon;
@@ -14,21 +15,28 @@ use JMS\Serializer\Tests\Fixtures\Discriminator\Car;
  * @ORM\Entity
  * @ORM\Table("trip_stop")
  */
-class TripStopEntity implements Fillable
+class TripStopEntity implements Fillable, Referable
 {
-    use FillTrait;
+    use FillTrait, ReferableEntityTrait;
+
+    /**
+     * Identifier for stop coming from provider
+     *
+     * @ORM\Column(type="integer")
+     * @ORM\Id
+     * @ORM\GeneratedValue
+     */
+    private $id;
 
     /**
      * @var StopEntity
      * @ORM\ManyToOne(targetEntity=StopEntity::class, fetch="EAGER")
-     * @ORM\Id
      */
     private $stop;
 
     /**
      * @var TripEntity
      * @ORM\ManyToOne(targetEntity=TripEntity::class, fetch="EAGER", inversedBy="stops")
-     * @ORM\Id
      */
     private $trip;
 
@@ -37,7 +45,6 @@ class TripStopEntity implements Fillable
      * @var int
      *
      * @ORM\Column(name="sequence", type="integer")
-     * @ORM\Id
      */
     private $order;
 
diff --git a/src/Migrations/Version20200314112552.php b/src/Migrations/Version20200314112552.php
new file mode 100644
index 0000000..a16a743
--- /dev/null
+++ b/src/Migrations/Version20200314112552.php
@@ -0,0 +1,43 @@
+<?php
+
+declare(strict_types=1);
+
+namespace DoctrineMigrations;
+
+use Doctrine\DBAL\Schema\Schema;
+use Doctrine\Migrations\AbstractMigration;
+
+/**
+ * Auto-generated Migration: Please modify to your needs!
+ */
+final class Version20200314112552 extends AbstractMigration
+{
+    public function getDescription() : string
+    {
+        return '';
+    }
+
+    public function up(Schema $schema) : void
+    {
+        // 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 TEMPORARY TABLE __temp__trip_stop AS SELECT stop_id, trip_id, sequence, arrival, departure FROM trip_stop');
+        $this->addSql('DROP TABLE trip_stop');
+        $this->addSql('CREATE TABLE trip_stop (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, sequence INTEGER NOT NULL, arrival DATETIME NOT NULL, departure DATETIME NOT NULL, stop_id VARCHAR(255) DEFAULT NULL, trip_id VARCHAR(255) DEFAULT NULL)');
+        $this->addSql('INSERT INTO trip_stop (stop_id, trip_id, sequence, arrival, departure) SELECT stop_id, trip_id, sequence, arrival, departure FROM __temp__trip_stop');
+        $this->addSql('DROP TABLE __temp__trip_stop');
+    }
+
+    public function down(Schema $schema) : void
+    {
+        // 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('CREATE TEMPORARY TABLE __temp__trip_stop AS SELECT sequence, arrival, departure, stop_id, trip_id FROM trip_stop');
+        $this->addSql('DROP TABLE trip_stop');
+        $this->addSql('CREATE TABLE trip_stop (sequence INTEGER NOT NULL, stop_id VARCHAR(255) NOT NULL COLLATE BINARY, trip_id VARCHAR(255) NOT NULL COLLATE BINARY, arrival DATETIME NOT NULL, departure DATETIME NOT NULL, PRIMARY KEY(stop_id, trip_id, sequence))');
+        $this->addSql('INSERT INTO trip_stop (sequence, arrival, departure, stop_id, trip_id) SELECT sequence, arrival, departure, stop_id, trip_id FROM __temp__trip_stop');
+        $this->addSql('DROP TABLE __temp__trip_stop');
+    }
+}