Skip to content

Commit 4cf6252

Browse files
CopilotpylippCopilotHaGuesto
authored
Clean up sizes/sizegroup: deduplicate sizes + cross-reference table (#821)
* chore: plan size deduplication and cross-reference table migrations Agent-Logs-Url: https://github.com/boxwise/dropapp/sessions/870cef39-acd9-49fe-8c16-4dfd54f3b425 Co-authored-by: pylipp <10617122+pylipp@users.noreply.github.com> * feat: deduplicate sizes and create sizes_sizegroup cross-reference table Migration 1 (20260421120000): removes 75 duplicate size rows, updating references in stock, shipment_detail, and history tables. Migration 2 (20260421130000): creates sizes_sizegroup cross-reference table with all size-sizegroup relationships (including extra sizegroup memberships from merged duplicates), then drops sizes.sizegroup_id and sizes.seq. Updates include/container-stock.php to JOIN sizes_sizegroup instead of filtering on sizes.sizegroup_id. Agent-Logs-Url: https://github.com/boxwise/dropapp/sessions/870cef39-acd9-49fe-8c16-4dfd54f3b425 Co-authored-by: pylipp <10617122+pylipp@users.noreply.github.com> * revert: undo cs-fixer formatting changes outside db/migrations and container-stock.php Agent-Logs-Url: https://github.com/boxwise/dropapp/sessions/363f3d93-798a-48aa-b118-9a9a41ecb889 Co-authored-by: pylipp <10617122+pylipp@users.noreply.github.com> * Avoid implicit cross join Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Philipp Metzner <10617122+pylipp@users.noreply.github.com> * progress: planning remaining fixes from review Agent-Logs-Url: https://github.com/boxwise/dropapp/sessions/ce308e1f-588a-4185-aa41-13ca1c14601d Co-authored-by: pylipp <10617122+pylipp@users.noreply.github.com> * fix: remap itemsout+distro_events_* before DELETE; update sizes.php, getsizes.php, stock_edit.php, sizes_edit.php to use sizes_sizegroup Agent-Logs-Url: https://github.com/boxwise/dropapp/sessions/ce308e1f-588a-4185-aa41-13ca1c14601d Co-authored-by: pylipp <10617122+pylipp@users.noreply.github.com> * refactor: use explicit distro_events table names instead of INFORMATION_SCHEMA Agent-Logs-Url: https://github.com/boxwise/dropapp/sessions/2c1f475d-e3cc-4a30-83d0-3a45e2ba207c Co-authored-by: pylipp <10617122+pylipp@users.noreply.github.com> * revert-css * feat: add down() to re-insert deleted size rows from init.sql Agent-Logs-Url: https://github.com/boxwise/dropapp/sessions/ca2517b0-c9c2-497a-9e87-c76fca107d62 Co-authored-by: pylipp <10617122+pylipp@users.noreply.github.com> * refactor: swap migration order — CreateSizesSizegroupTable first, DeduplicateSizes second Agent-Logs-Url: https://github.com/boxwise/dropapp/sessions/ca6077f8-c7fa-4a6f-bf02-d8793b688a47 Co-authored-by: pylipp <10617122+pylipp@users.noreply.github.com> * wip: before fixing sizes_sizegroup remap in DeduplicateSizes migration Agent-Logs-Url: https://github.com/boxwise/dropapp/sessions/f5db50ea-15bf-4438-9c1e-b0d0bf82d2d9 Co-authored-by: pylipp <10617122+pylipp@users.noreply.github.com> * fix: remap sizes_sizegroup rows in DeduplicateSizes instead of deleting them Agent-Logs-Url: https://github.com/boxwise/dropapp/sessions/f5db50ea-15bf-4438-9c1e-b0d0bf82d2d9 Co-authored-by: pylipp <10617122+pylipp@users.noreply.github.com> * fix: restore minified.css to master version Co-authored-by: pylipp <10617122+pylipp@users.noreply.github.com> * Update comment Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Philipp Metzner <10617122+pylipp@users.noreply.github.com> * Use phinx methods * feat: add migration to drop audit columns (created, created_by, modified, modified_by) from sizes table Agent-Logs-Url: https://github.com/boxwise/dropapp/sessions/5a267922-fb0f-4a79-9c7d-04cc5362d322 Co-authored-by: HaGuesto <29065125+HaGuesto@users.noreply.github.com> * plan: add 4th migration to fix sizes data inconsistencies Agent-Logs-Url: https://github.com/boxwise/dropapp/sessions/388acca9-0bf3-4f83-ba03-25ffdb08105d Co-authored-by: pylipp <10617122+pylipp@users.noreply.github.com> * feat: add migration 4 to fix seq values and add All ages to sizegroups 17/18/22/25 Agent-Logs-Url: https://github.com/boxwise/dropapp/sessions/388acca9-0bf3-4f83-ba03-25ffdb08105d Co-authored-by: pylipp <10617122+pylipp@users.noreply.github.com> * add-logging * Remove Mixed in OneSize sizegroup * Correct row numbers For production (and staging in parens) * feat: add migration 5 to remove sizegroup 20 (Children by month 2-5) Agent-Logs-Url: https://github.com/boxwise/dropapp/sessions/17d8a353-5722-415f-8a3c-38ca124a5203 Co-authored-by: pylipp <10617122+pylipp@users.noreply.github.com> * add-logging * insert-seq * Add on-delete FK handling Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Signed-off-by: Philipp Metzner <10617122+pylipp@users.noreply.github.com> --------- Signed-off-by: Philipp Metzner <10617122+pylipp@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: pylipp <10617122+pylipp@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Philipp Metzner <pmetzner@freenet.de> Co-authored-by: HaGuesto <29065125+HaGuesto@users.noreply.github.com>
1 parent 4287803 commit 4cf6252

8 files changed

Lines changed: 627 additions & 5 deletions
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Phinx\Migration\AbstractMigration;
6+
7+
/**
8+
* Creates the sizes_sizegroup cross-reference table that stores the many-to-many
9+
* relationship between sizes and sizegroups, then drops the now-redundant
10+
* sizegroup_id and seq columns from the sizes table.
11+
*
12+
* This migration runs BEFORE DeduplicateSizes so that every size – including
13+
* the duplicates that will later be removed – already has a row in
14+
* sizes_sizegroup before deduplication happens. When DeduplicateSizes removes
15+
* duplicate rows from sizes, the corresponding sizes_sizegroup rows are removed
16+
* automatically via the size_id foreign key's ON DELETE CASCADE behavior.
17+
*/
18+
final class CreateSizesSizegroupTable extends AbstractMigration
19+
{
20+
public function up(): void
21+
{
22+
// ----------------------------------------------------------------
23+
// 1. Create cross-reference table
24+
// ----------------------------------------------------------------
25+
$this->execute('CREATE TABLE `sizes_sizegroup` (
26+
`size_id` INT UNSIGNED NOT NULL,
27+
`sizegroup_id` INT UNSIGNED NOT NULL,
28+
`seq` INT DEFAULT NULL,
29+
PRIMARY KEY (`size_id`, `sizegroup_id`),
30+
KEY `idx_sizegroup_id` (`sizegroup_id`),
31+
CONSTRAINT `fk_ss_size_id`
32+
FOREIGN KEY (`size_id`) REFERENCES `sizes` (`id`)
33+
ON DELETE CASCADE ON UPDATE CASCADE,
34+
CONSTRAINT `fk_ss_sizegroup_id`
35+
FOREIGN KEY (`sizegroup_id`) REFERENCES `sizegroup` (`id`)
36+
ON DELETE CASCADE ON UPDATE CASCADE
37+
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC');
38+
39+
// ----------------------------------------------------------------
40+
// 2. Populate from existing sizes.sizegroup_id (one row per size,
41+
// including rows for the duplicates that DeduplicateSizes will
42+
// later remove along with their sizes_sizegroup entries)
43+
// ----------------------------------------------------------------
44+
$this->execute('INSERT INTO `sizes_sizegroup` (`size_id`, `sizegroup_id`, `seq`)
45+
SELECT `id`, `sizegroup_id`, `seq`
46+
FROM `sizes`
47+
WHERE `sizegroup_id` IS NOT NULL');
48+
49+
// ----------------------------------------------------------------
50+
// 3. Drop the now-redundant columns from sizes
51+
// ----------------------------------------------------------------
52+
$this->table('sizes')->dropForeignKey('sizegroup_id')->save();
53+
$this->table('sizes')->removeColumn('sizegroup_id')->save();
54+
$this->table('sizes')->removeColumn('seq')->save();
55+
}
56+
57+
public function down(): void
58+
{
59+
// Restore sizegroup_id and seq columns on sizes
60+
$this->execute('ALTER TABLE `sizes`
61+
ADD COLUMN `sizegroup_id` INT UNSIGNED DEFAULT NULL AFTER `label`,
62+
ADD COLUMN `seq` INT DEFAULT NULL AFTER `sizegroup_id`,
63+
ADD KEY `sizegroup_id` (`sizegroup_id`),
64+
ADD CONSTRAINT `sizes_ibfk_4`
65+
FOREIGN KEY (`sizegroup_id`) REFERENCES `sizegroup` (`id`)
66+
ON UPDATE CASCADE');
67+
68+
// Re-populate from the cross-reference table.
69+
// Where a size belongs to multiple sizegroups, pick the row with the
70+
// smallest sizegroup_id as the "primary" one (mirrors the original data).
71+
$this->execute('UPDATE `sizes` s
72+
JOIN (
73+
SELECT size_id, sizegroup_id, seq
74+
FROM sizes_sizegroup ss1
75+
WHERE sizegroup_id = (
76+
SELECT MIN(sizegroup_id)
77+
FROM sizes_sizegroup ss2
78+
WHERE ss2.size_id = ss1.size_id
79+
)
80+
) best ON best.size_id = s.id
81+
SET s.sizegroup_id = best.sizegroup_id,
82+
s.seq = best.seq');
83+
84+
$this->execute('DROP TABLE `sizes_sizegroup`');
85+
}
86+
}

0 commit comments

Comments
 (0)