doctrine / dbal

Doctrine Database Abstraction Layer
https://www.doctrine-project.org/projects/dbal.html
MIT License
9.47k stars 1.34k forks source link

Schema indexes update #2913

Open tasselchof opened 6 years ago

tasselchof commented 6 years ago

Mapper is duplicating queries to create indexes on orm:schema-tool:update --dump-sql command with latest PostgreSQL.

DROP INDEX si_type_unique_idx; CREATE UNIQUE INDEX si_type_unique_idx ON storage_items (warehouse_id, sku) WHERE state IS NOT NULL AND state = 'unique';

chrisguitarguy commented 6 years ago

@tasselchof I suspect you'll need to provide the mapping you're using as well (the annotations, xml, yaml, or whatever).

tasselchof commented 6 years ago

Here it is:

<?php

namespace Orderadmin\Storage\Entity;

use Application\Entity\Model\ExtractableInterface;
use Application\Entity\Traits\Entity;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Orderadmin\Products\Entity\Order;
use Orderadmin\Products\Entity\Order\Reserve;
use Orderadmin\Products\Entity\Product;
use Orderadmin\Storage\Entity\Movement\Acceptance;

/**
 * @Gedmo\Loggable(logEntryClass="Orderadmin\Storage\Entity\Log\ItemLogEntry")
 * @ORM\Entity(repositoryClass="Orderadmin\Storage\Repository\Items")
 * @ORM\Table(name="storage_items", uniqueConstraints={
 *          @ORM\UniqueConstraint(name="si_type_unique_idx", columns={"warehouse_id", "sku"}, options={"where": "state IS NOT NULL AND state = 'unique'"})
 *     })
 * @ORM\HasLifecycleCallbacks
 *
 * @author Maxim Gasumyants <m@gasumyants.com>
 */
class Item implements ExtractableInterface
{
    use Entity;

    const TYPE_SIMPLE = 'simple';
    const TYPE_UNIQUE = 'unique';

    const STATE_NEW = 'new'; // New item, that was not accepted
    const STATE_NORMAL = 'normal'; // Normal state, item can be reserved
    const STATE_BLOCKED = 'blocked'; // Blocked state, item is reserved
    const STATE_SHIPPED = 'shipped'; // Item was shipped from the storage
    const STATE_DELIVERED = 'delivered'; // Item was delivered
    const STATE_RETURNED = 'returned'; // Item was returned to storage
    const STATE_DELETED = 'deleted';

    /**
     * @var string
     * @ORM\Column(type="string", length=255)
     * @Gedmo\Versioned
     */
    protected $type = self::TYPE_SIMPLE;

    /**
     * @ORM\Column(name="product_id", type="integer", nullable=true)
     * @Gedmo\Versioned
     */
    protected $product;
    /**
     * @ORM\Column(name="product_option_id", type="integer")
     * @Gedmo\Versioned
     */
    protected $productOffer;
    /**
     * Collection
     * @ORM\ManyToOne(targetEntity="Orderadmin\Storage\Entity\Warehouse")
     * @ORM\JoinColumn(name="warehouse_id", referencedColumnName="id")
     * @Gedmo\Versioned
     **/
    protected $warehouse;
    /**
     * @var Reserve
     * @ORM\ManyToOne(targetEntity="Orderadmin\Products\Entity\Order\Reserve", inversedBy="items")
     **/
    protected $reserve;
    /**
     * @ORM\ManyToOne(targetEntity="Orderadmin\Storage\Entity\Place", inversedBy="items")
     * @Gedmo\Versioned
     **/
    protected $place;
    /**
     * @var string
     * @ORM\Column(type="string", length=255, nullable=false)
     * @Gedmo\Versioned
     */
    protected $sku;
    /**
     * @var int
     * @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
     * @Gedmo\Versioned
     */
    protected $price;
    /**
     * @var int
     * @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
     * @Gedmo\Versioned
     */
    protected $weight;
    /**
     * @var int
     * @ORM\Column(type="array", nullable=true)
     * @Gedmo\Versioned
     */
    protected $dimensions;
    /**
     * @var int
     * @ORM\Column(type="integer", nullable=true)
     * @Gedmo\Versioned
     */
    protected $volume;
    /**
     * @ORM\ManyToOne(targetEntity="Orderadmin\Storage\Entity\Movement\Acceptance", inversedBy="items")
     * @ORM\JoinColumn(name="created_by_document_id", referencedColumnName="id")
     * @Gedmo\Versioned
     */
    protected $createdByDocument;
    /**
     * @ORM\ManyToOne(targetEntity="Orderadmin\Storage\Entity\Document", inversedBy="items")
     * @ORM\JoinColumn(name="shipped_by_document_id", referencedColumnName="id")
     * @Gedmo\Versioned
     */
    protected $shippedByDocument;

    public function __toString()
    {
        return (string)$this->id;
    }

    /**
     * Get type
     *
     * @return string
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * Set type
     *
     * @param string $type
     *
     * @return Item
     */
    public function setType($type)
    {
        if (! in_array($type, [self::TYPE_SIMPLE])) {
            throw new \InvalidArgumentException("Invalid entity");
        }

        $this->type = $type;

        return $this;
    }

    /**
     * Get price
     *
     * @return string
     */
    public function getPrice()
    {
        return $this->price;
    }

    /**
     * Set price
     *
     * @param string $price
     *
     * @return Item
     */
    public function setPrice($price)
    {
        $this->price = $price;

        return $this;
    }

    /**
     * Get weight
     *
     * @return string
     */
    public function getWeight()
    {
        return $this->weight;
    }

    /**
     * Set weight
     *
     * @param string $weight
     *
     * @return Item
     */
    public function setWeight($weight)
    {
        $this->weight = $weight;

        return $this;
    }

    /**
     * Get dimensions
     *
     * @return string
     */
    public function getDimensions()
    {
        return $this->dimensions;
    }

    /**
     * Set dimensions
     *
     * @param array $dimensions
     *
     * @return Item
     */
    public function setDimensions(array $dimensions)
    {
        $this->dimensions = $dimensions;

        return $this;
    }

    /**
     * Get created
     *
     * @return \DateTime
     */
    public function getCreated()
    {
        return $this->created;
    }

    /**
     * Set created
     *
     * @param \DateTime $created
     *
     * @return Item
     */
    public function setCreated($created)
    {
        $this->created = $created;

        return $this;
    }

    /**
     * Get updated
     *
     * @return \DateTime
     */
    public function getUpdated()
    {
        return $this->updated;
    }

    /**
     * Set updated
     *
     * @param \DateTime $updated
     *
     * @return Item
     */
    public function setUpdated($updated)
    {
        $this->updated = $updated;

        return $this;
    }

    /**
     * Get product
     *
     * @return Product
     */
    public function getProduct()
    {
        return $this->product;
    }

    /**
     * Set product
     *
     * @param Product $product
     *
     * @return Item
     */
    public function setProduct(Product $product = null)
    {
        $this->product = $product;

        return $this;
    }

    /**
     * @return mixed
     */
    public function getProductOffer()
    {
        return $this->productOffer;
    }

    /**
     * @param $productOffer
     * @return $this
     */
    public function setProductOffer($productOffer)
    {
        $this->productOffer = $productOffer;

        return $this;
    }

    /**
     * Get createdByDocument
     *
     * @return Document
     */
    public function getCreatedByDocument()
    {
        return $this->createdByDocument;
    }

    /**
     * Set createdByDocument
     *
     * @param Acceptance $createdByDocument
     *
     * @return Item
     */
    public function setCreatedByDocument(Acceptance $createdByDocument = null)
    {
        $this->createdByDocument = $createdByDocument;

        return $this;
    }

    /**
     * Get shippedByDocument
     *
     * @return Document
     */
    public function getShippedByDocument()
    {
        return $this->shippedByDocument;
    }

    /**
     * Set shippedByDocument
     *
     * @param Document $shippedByDocument
     *
     * @return Item
     */
    public function setShippedByDocument(Document $shippedByDocument = null)
    {
        $this->shippedByDocument = $shippedByDocument;

        return $this;
    }

    /**
     * Get warehouse
     *
     * @return Warehouse
     */
    public function getWarehouse()
    {
        return $this->warehouse;
    }

    /**
     * Set warehouse
     *
     * @param Warehouse $warehouse
     *
     * @return Item
     */
    public function setWarehouse(Warehouse $warehouse = null)
    {
        $this->warehouse = $warehouse;

        return $this;
    }

    public function getBarcode($barcode = null)
    {
        return sprintf('S/I/%s*', is_null($barcode) ? $this->getSku() : $barcode);
    }

    /**
     * Get sku
     *
     * @return string
     */
    public function getSku()
    {
        return $this->sku;
    }

    /**
     * Set sku
     *
     * @param string $sku
     * @return Item
     */
    public function setSku($sku)
    {
        $this->sku = $sku;

        return $this;
    }

    /**
     * Get place
     *
     * @return Place
     */
    public function getPlace()
    {
        return $this->place;
    }

    /**
     * Set place
     *
     * @param Place $place
     *
     * @return Item
     */
    public function setPlace(Place $place = null)
    {
        $this->place = $place;

        return $this;
    }

    /**
     * Get volume
     *
     * @return integer
     */
    public function getVolume()
    {
        return $this->volume;
    }

    /**
     * @param $volume
     * @return $this
     */
    public function setVolume($volume)
    {
        $this->volume = $volume;

        return $this;
    }

    /**
     * @param $reserve
     * @return $this
     */
    public function setReserve($reserve)
    {
        $this->reserve = $reserve;

        return $this;
    }

    /**
     * @return mixed
     */
    public function getReserve()
    {
        return $this->reserve;
    }

    /**
     * @ORM\PrePersist()
     */
    public function preCreate()
    {
        if (is_null($this->state)) {
            $this->state = self::STATE_NEW;
        }
    }

    public function getArrayCopy()
    {
        return get_object_vars($this);
    }
}