Skip to content

Commit

Permalink
重複在庫の情報を削除するsqlの追加
Browse files Browse the repository at this point in the history
  • Loading branch information
dotani1111 committed Sep 28, 2023
1 parent 72c5d45 commit 5dbaaa0
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions app/DoctrineMigrations/Version20230928014611.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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 Version20230928014611 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// 重複した在庫がdtb_product_stockにあるのか確認する。
$exists = $this->connection->fetchAllKeyValue("
SELECT id,product_class_id, product_class_id_count
FROM (
SELECT
id,
product_class_id,
COUNT(product_class_id) AS product_class_id_count
FROM
dtb_product_stock
GROUP BY
product_class_id
) AS subquery
WHERE product_class_id_count > 1;
");

// 重複在庫がある場合、dtb_product_class.stockを正として、それ以外の在庫情報は削除する
if (count($exists) != 0) {
foreach ($exists as $id => $pc_id) {
$stock = $this->connection->fetchOne("SELECT stock FROM dtb_product_class WHERE id = :id", ["id" => $pc_id]);
$this->addSql("DELETE FROM dtb_product_stock WHERE product_class_id = :pc_id", ["pc_id" => $pc_id]);
$this->addSql("insert into dtb_product_stock (product_class_id, creator_id, stock, create_date, update_date, discriminator_type) VALUES (:pc_id, null, $stock, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'productstock')", ["pc_id" => $pc_id]);
}
}
}

public function down(Schema $schema): void
{
}
}

0 comments on commit 5dbaaa0

Please sign in to comment.